Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/code_corps/policy/policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule CodeCorps.Policy do
defp can?(%User{} = current_user, :update, %Project{} = project, %{}), do: Policy.Project.update?(current_user, project)
defp can?(%User{} = current_user, :create, %ProjectUser{}, %{} = params), do: Policy.ProjectUser.create?(current_user, params)
defp can?(%User{} = current_user, :update, %ProjectUser{} = project_user, %{} = params), do: Policy.ProjectUser.update?(current_user, project_user, params)
defp can?(%User{} = current_user, :delete, %ProjectUser{} = project_user, %{}), do: Policy.ProjectUser.delete?(current_user, project_user)
defp can?(%User{} = current_user, :delete, %ProjectUser{} = project_user, %{}), do: Policy.ProjectUser.delete?(current_user, project_user)
defp can?(%User{} = user, :delete,
%OrganizationGithubAppInstallation{} = organization_github_app_installation, %{}),
do: Policy.OrganizationGithubAppInstallation.delete?(user, organization_github_app_installation)
Expand All @@ -53,6 +53,10 @@ defmodule CodeCorps.Policy do
defp can?(%User{} = current_user, :delete, %UserSkill{} = user_skill, %{}), do: Policy.UserSkill.delete?(current_user, user_skill)
defp can?(%User{} = current_user, :create, %UserRole{} = user_role, %{}), do: Policy.UserRole.create?(current_user, user_role)
defp can?(%User{} = current_user, :delete, %UserRole{} = user_role, %{}), do: Policy.UserRole.delete?(current_user, user_role)
defp can?(%User{} = user, :create, %DonationGoal{}, %{} = params), do: Policy.DonationGoal.create?(user, params)
defp can?(%User{} = user, :update, %DonationGoal{} = donation_goal), do: Policy.DonationGoal.update?(user, donation_goal)
defp can?(%User{} = user, :delete, %DonationGoal{} = donation_goal), do: Policy.DonationGoal.delete?(user, donation_goal)


defimpl Canada.Can, for: User do
# NOTE: Canary sets an :unauthorized and a :not_found handler on a config level
Expand All @@ -66,10 +70,6 @@ defmodule CodeCorps.Policy do
# NOTE: other tests are using the User policy for the time being.
def can?(%User{}, _action, nil), do: true

def can?(%User{} = user, :create, %Changeset{data: %DonationGoal{}} = changeset), do: Policy.DonationGoal.create?(user, changeset)
def can?(%User{} = user, :update, %DonationGoal{} = comment), do: Policy.DonationGoal.update?(user, comment)
def can?(%User{} = user, :delete, %DonationGoal{} = comment), do: Policy.DonationGoal.delete?(user, comment)

def can?(%User{} = user, :create, %Changeset{data: %GithubAppInstallation{}} = changeset), do: Policy.GithubAppInstallation.create?(user, changeset)

def can?(%User{} = user, :create, OrganizationInvite), do: Policy.OrganizationInvite.create?(user)
Expand Down
52 changes: 45 additions & 7 deletions lib/code_corps_web/controllers/donation_goal_controller.ex
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
defmodule CodeCorpsWeb.DonationGoalController do
use CodeCorpsWeb, :controller
use JaResource

import CodeCorps.Helpers.Query, only: [id_filter: 2]

alias CodeCorps.DonationGoal
alias CodeCorps.{DonationGoal, User, Helpers.Query}
alias CodeCorps.Services.DonationGoalsService

action_fallback CodeCorpsWeb.FallbackController
plug :load_and_authorize_changeset, model: DonationGoal, only: [:create]
plug :load_and_authorize_resource, model: DonationGoal, only: [:update, :delete]
plug JaResource
plug CodeCorpsWeb.Plug.DataToAttributes
plug CodeCorpsWeb.Plug.IdsToIntegers

@spec model :: module
def model, do: CodeCorps.DonationGoal

def filter(_conn, query, "id", id_list), do: id_filter(query, id_list)

def handle_create(_conn, attributes) do
attributes |> DonationGoalsService.create
@spec index(Conn.t, map) :: Conn.t
def index(%Conn{} = conn, %{} = params) do
with donation_goals <- DonationGoal |> Query.id_filter(params) |> Repo.all do
conn |> render("index.json-api", data: donation_goals)
end
end

def handle_update(_conn, record, attributes) do
record |> DonationGoalsService.update(attributes)
@spec show(Conn.t, map) :: Conn.t
def show(%Conn{} = conn, %{"id" => id}) do
with %DonationGoal{} = donation_goal <- DonationGoal |> Repo.get(id) do
conn |> render("show.json-api", data: donation_goal)
end
end

@spec create(Plug.Conn.t, map) :: Conn.t
def create(%Conn{} = conn, %{} = params) do
with %User{} = current_user <- conn |> Guardian.Plug.current_resource,
{:ok, :authorized} <- current_user |> Policy.authorize(:create, %DonationGoal{}, params),
{:ok, %DonationGoal{} = donation_goal} <- %DonationGoal{} |> DonationGoal.create_changeset(params) |> Repo.insert do
conn |> put_status(:created) |> render("show.json-api", data: donation_goal)
end
end

@spec update(Conn.t, map) :: Conn.t
def update(%Conn{} = conn, %{"id" => id} = params) do
with %DonationGoal{} = donation_goal <- DonationGoal |> Repo.get(id),
%User{} = current_user <- conn |> Guardian.Plug.current_resource,
{:ok, :authorized} <- current_user |> Policy.authorize(:update, donation_goal),
{:ok, %DonationGoal{} = donation_goal} <- donation_goal |> DonationGoal.changeset(params) |> Repo.update do
conn |> render("show.json-api", data: donation_goal)
end
end

@spec delete(Plug.Conn.t, map) :: Conn.t
def delete(%Conn{} = conn, %{"id" => id} = params) do
with %DonationGoal{} = donation_goal <- DonationGoal |> Repo.get(id),
%User{} = current_user <- conn |> Guardian.Plug.current_resource,
{:ok, :authorized} <- current_user |> Policy.authorize(:delete, donation_goal, params),
{:ok, _donation_goal} <-
donation_goal
|> Repo.delete do
conn |> send_resp(:no_content, "")
end
end
end