defmodule VigilWeb.Api.V0.ProjectController do
@moduledoc """
Project management in Sentry's shapes and scopes (2019 Sentry's
`team_projects.py` and `project_details.py`, BSD-3; GlitchTip's
`apps/projects/api.py`, MIT):
* `POST /teams/{org}/{team}/projects/` (`project:write`): `name`,
optional `slug` and `platform`; 201 with the project, which joins
the team and gets its first DSN key. A member or admin must be on
the team (or the organization have open membership).
* `GET /projects/{org}/{project}/` (`project:read`, `:write` or `:admin`).
* `PUT /projects/{org}/{project}/` (`project:write`): `name`, `slug`,
`platform`.
* `DELETE /projects/{org}/{project}/` (`project:admin`): soft delete, 204.
A project the token can't see (not on its teams) is 404, as one that
doesn't exist.
"""
use VigilWeb, :controller
alias Vigil.Accounts.Scope
alias Vigil.{Organizations, Projects}
alias VigilWeb.Api.Auth
alias VigilWeb.Api.V0.{ProjectJSON, ReleaseController}
@read ~w(project:read project:write project:admin)
def create(conn, %{"org" => org, "team" => team_slug}) do
with {:ok, conn} <- Auth.put_org(conn, org),
{:ok, conn} <- Auth.require_scope(conn, "project:write"),
{:ok, team} <- fetch_team(conn, team_slug) do
scope = conn.assigns.current_scope
attrs =
conn.body_params
|> Map.take(~w(name slug platform))
|> Map.put("team_id", team.id)
case Projects.create_project(scope, attrs) do
{:ok, project} ->
conn |> put_status(201) |> render_project(project)
{:error, reason} ->
ReleaseController.error(conn, reason)
end
end
|> ReleaseController.done()
end
def show(conn, params) do
with {:ok, conn, project} <- resolve(conn, params, @read) do
render_project(conn, project)
end
|> ReleaseController.done()
end
def update(conn, params) do
with {:ok, conn, project} <- resolve(conn, params, ["project:write"]) do
attrs = Map.take(conn.body_params, ~w(name slug platform))
case Projects.update_project(conn.assigns.current_scope, project, attrs) do
{:ok, project} -> render_project(conn, project)
{:error, reason} -> ReleaseController.error(conn, reason)
end
end
|> ReleaseController.done()
end
def delete(conn, params) do
with {:ok, conn, project} <- resolve(conn, params, ["project:admin"]) do
case Projects.delete_project(conn.assigns.current_scope, project) do
{:ok, _} -> send_resp(conn, 204, "")
{:error, reason} -> ReleaseController.error(conn, reason)
end
end
|> ReleaseController.done()
end
@doc false
# The organization and project of a `/projects/{org}/{project}/` path:
# 404 for what the token can't see, 403 without one of `scopes`.
def resolve(conn, %{"org" => org, "project" => slug}, scopes) do
with {:ok, conn} <- Auth.put_org(conn, org),
{:ok, conn} <- Auth.require_any_scope(conn, scopes) do
case Projects.get_visible(conn.assigns.current_scope, slug) do
nil -> {:error, Auth.not_found(conn)}
project -> {:ok, conn, project}
end
end
end
defp fetch_team(conn, slug) do
scope = conn.assigns.current_scope
case Organizations.get_team_by_slug(scope.organization, slug) do
nil ->
{:error, Auth.not_found(conn)}
team ->
if Scope.global?(scope) or scope.organization.open_membership or
Enum.any?(team.members, &(&1.id == scope.member.id)),
do: {:ok, team},
else: {:error, Auth.forbidden(conn)}
end
end
defp render_project(conn, project) do
[project] = Projects.preload_teams([project])
json(conn, ProjectJSON.project(project, conn.assigns.current_scope.organization))
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
3.8 KiB · text
History
573fef1