-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add workflows api to create, get and list
- Loading branch information
Showing
15 changed files
with
445 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Lightning.Policies.Workflows do | ||
@moduledoc """ | ||
The Bodyguard Policy module for Workflows. | ||
Access to collections is controlled by the project the collection belongs to. | ||
The `access_collection` action is allowed if the user has access to the | ||
project, or if a run belongs to the project (via it's workflow). | ||
""" | ||
@behaviour Bodyguard.Policy | ||
|
||
alias Lightning.Accounts.User | ||
alias Lightning.Projects.Project | ||
alias Lightning.Run | ||
|
||
@type actions :: :access_write | :access_read | ||
@spec authorize(actions(), User.t() | Runt.t(), Project.t()) :: | ||
:ok | {:error, :unauthorized} | ||
def authorize(access, %User{} = user, project) | ||
when access in [:access_write, :access_read] do | ||
Lightning.Policies.Permissions.can( | ||
Lightning.Policies.ProjectUsers, | ||
:access_project, | ||
user, | ||
project | ||
) | ||
end | ||
|
||
def authorize(access, %Run{} = run, project) | ||
when access in [:access_write, :access_read] do | ||
Lightning.Runs.get_project_id_for_run(run) == project.id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
defmodule LightningWeb.WorkflowsController do | ||
use LightningWeb, :controller | ||
|
||
alias Lightning.Projects.Project | ||
alias Lightning.Repo | ||
alias Lightning.Workflows | ||
alias Lightning.Workflows.Presence | ||
alias Lightning.Workflows.Workflow | ||
alias Lightning.Policies.Permissions | ||
|
||
action_fallback LightningWeb.FallbackController | ||
|
||
require Logger | ||
|
||
def post(conn, %{"project_id" => project_id} = params) do | ||
with :ok <- authorize_write(conn, project_id), | ||
{:ok, %{id: workflow_id}} <- save_workflow(params, conn.assigns.subject) do | ||
json(conn, %{id: workflow_id, error: nil}) | ||
end | ||
end | ||
|
||
def get(conn, %{"project_id" => project_id, "id" => workflow_id}) do | ||
with :ok <- authorize_read(conn, project_id), | ||
{:ok, workflow} <- get_workflow(workflow_id, project_id) do | ||
json(conn, %{workflow: workflow, error: nil}) | ||
end | ||
end | ||
|
||
def get(conn, %{"project_id" => project_id}) do | ||
with :ok <- authorize_read(conn, project_id) do | ||
list = Workflows.list_project_workflows(project_id, include: [:edges, :jobs, :triggers]) | ||
json(conn, %{workflows: list, error: nil}) | ||
end | ||
end | ||
|
||
def put(conn, %{"project_id" => project_id, "id" => workflow_id} = params) do | ||
with :ok <- authorize_write(conn, project_id), | ||
{:ok, workflow} <- get_workflow(workflow_id, project_id), | ||
:ok <- authorize_write(conn, workflow), | ||
{:ok, %{id: workflow_id}} <- save_workflow(params, conn.assigns.subject) do | ||
json(conn, %{id: workflow_id}) | ||
end | ||
end | ||
|
||
defp save_workflow(params, user), do: Workflows.save_workflow(params, user) | ||
|
||
defp get_workflow(workflow_id, project_id) do | ||
case Workflows.get_workflow(workflow_id, include: [:edges, :jobs, :triggers]) do | ||
nil -> {:error, :not_found} | ||
%{project_id: ^project_id} = workflow -> {:ok, workflow} | ||
_project_mismatch -> {:error, :bad_request} | ||
end | ||
end | ||
|
||
defp authorize_write(_conn, %Workflow{} = workflow) do | ||
if Presence.has_any_presence?(workflow) do | ||
{:error, :conflict} | ||
else | ||
:ok | ||
end | ||
end | ||
|
||
defp authorize_write(conn, project_id) do | ||
authorize_for_project(conn, project_id, :access_write) | ||
end | ||
|
||
defp authorize_read(conn, project_id) do | ||
authorize_for_project(conn, project_id, :access_read) | ||
end | ||
|
||
defp authorize_for_project(conn, project_id, access) do | ||
project = Repo.get(Project, project_id) | ||
|
||
Permissions.can( | ||
Lightning.Policies.Workflows, | ||
access, | ||
conn.assigns.subject, | ||
project | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.