Skip to content

Commit

Permalink
Switch GithubAppInstallationController away from ja_resource and canary
Browse files Browse the repository at this point in the history
Helps support code-corps#864

Closes code-corps#890
  • Loading branch information
treble37 committed Oct 11, 2017
1 parent 6b8e48d commit 0170b79
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/code_corps/policy/github_app_installation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ defmodule CodeCorps.Policy.GithubAppInstallation do

alias CodeCorps.{GithubAppInstallation, User}

def create?(%User{} = user, %Ecto.Changeset{} = changeset),
do: changeset |> get_project |> owned_by?(user)
def create?(%User{} = user, params),
do: params |> get_project |> owned_by?(user)

def update?(%User{} = user, %GithubAppInstallation{} = github_app_installation),
do: github_app_installation |> get_project |> owned_by?(user)
Expand Down
3 changes: 1 addition & 2 deletions lib/code_corps/policy/policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ defmodule CodeCorps.Policy do
do: Policy.StripePlatformCustomer.create?(current_user, params)
defp can?(%User{} = current_user, :show, %StripePlatformCustomer{} = stripe_platform_customer, %{}),
do: Policy.StripePlatformCustomer.show?(current_user, stripe_platform_customer)

defp can?(%User{} = user, :create, %GithubAppInstallation{}, %{} = params), do: Policy.GithubAppInstallation.create?(user, params)

defimpl Canada.Can, for: User do
# NOTE: Canary sets an :unauthorized and a :not_found handler on a config level
Expand All @@ -99,7 +99,6 @@ defmodule CodeCorps.Policy do
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)
def can?(%User{} = user, :update, %OrganizationInvite{}), do: Policy.OrganizationInvite.update?(user)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
defmodule CodeCorpsWeb.GithubAppInstallationController do
use CodeCorpsWeb, :controller
use JaResource

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

alias CodeCorps.{GithubAppInstallation}
alias CodeCorps.{GithubAppInstallation, User}

@preloads [:project, :user]

plug :load_resource, model: GithubAppInstallation, only: [:show], preload: @preloads
plug :load_and_authorize_changeset, model: GithubAppInstallation, only: [:create], preload: @preloads

plug JaResource
action_fallback CodeCorpsWeb.FallbackController
plug CodeCorpsWeb.Plug.DataToAttributes
plug CodeCorpsWeb.Plug.IdsToIntegers

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

@spec filter(Plug.Conn.t, Ecto.Query.t, String.t, String.t) :: Ecto.Query.t
def filter(_conn, query, "id", id_list) do
query |> id_filter(id_list)
@spec index(Conn.t, map) :: Conn.t
def index(%Conn{} = conn, %{} = params) do
with installations <- GithubAppInstallation |> id_filter(params) |> Repo.all do
conn |> render("index.json-api", data: installations)
end
end

@spec show(Conn.t, map) :: Conn.t
def show(%Conn{} = conn, %{"id" => id}) do
with %GithubAppInstallation{} = installation <- GithubAppInstallation |> Repo.get(id) do
conn |> render("show.json-api", data: installation)
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, %GithubAppInstallation{}, params),
{:ok, %GithubAppInstallation{} = installation} <- %GithubAppInstallation{} |> GithubAppInstallation.create_changeset(params) |> Repo.insert do
conn |> put_status(:created) |> render("show.json-api", data: installation)
end
end

@spec handle_create(Plug.Conn.t, map) :: Ecto.Changeset.t
def handle_create(_conn, attributes) do
%GithubAppInstallation{}
|> GithubAppInstallation.create_changeset(attributes)
@spec delete(Plug.Conn.t, map) :: Conn.t
def delete(%Conn{} = conn, %{"id" => id} = params) do
with %GithubAppInstallation{} = installation <- GithubAppInstallation |> Repo.get(id),
%User{} = current_user <- conn |> Guardian.Plug.current_resource,
{:ok, :authorized} <- current_user |> Policy.authorize(:delete, installation, params),
{:ok, _installation} <-
installation
|> Repo.delete do
conn |> send_resp(:no_content, "")
end
end
end

0 comments on commit 0170b79

Please sign in to comment.