Skip to content

Commit 13de440

Browse files
treble37begedin
authored andcommitted
Switch GithubAppInstallationController away from ja_resource and canary (#1055)
Helps support #864 Closes #890
1 parent a75aacb commit 13de440

File tree

4 files changed

+25
-43
lines changed

4 files changed

+25
-43
lines changed

lib/code_corps/policy/github_app_installation.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ defmodule CodeCorps.Policy.GithubAppInstallation do
66

77
alias CodeCorps.{GithubAppInstallation, User}
88

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

12-
def update?(%User{} = user, %GithubAppInstallation{} = github_app_installation),
13-
do: github_app_installation |> get_project |> owned_by?(user)
1411
end

lib/code_corps/policy/policy.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ defmodule CodeCorps.Policy do
8686
do: Policy.StripePlatformCustomer.create?(current_user, params)
8787
defp can?(%User{} = current_user, :show, %StripePlatformCustomer{} = stripe_platform_customer, %{}),
8888
do: Policy.StripePlatformCustomer.show?(current_user, stripe_platform_customer)
89-
89+
defp can?(%User{} = user, :create, %GithubAppInstallation{}, %{} = params), do: Policy.GithubAppInstallation.create?(user, params)
9090

9191
defimpl Canada.Can, for: User do
9292
# NOTE: Canary sets an :unauthorized and a :not_found handler on a config level
@@ -104,7 +104,6 @@ defmodule CodeCorps.Policy do
104104
def can?(%User{} = user, :update, %DonationGoal{} = comment), do: Policy.DonationGoal.update?(user, comment)
105105
def can?(%User{} = user, :delete, %DonationGoal{} = comment), do: Policy.DonationGoal.delete?(user, comment)
106106

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

109108
def can?(%User{} = user, :create, Role), do: Policy.Role.create?(user)
110109
end
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
defmodule CodeCorpsWeb.GithubAppInstallationController do
22
use CodeCorpsWeb, :controller
3-
use JaResource
43

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

7-
alias CodeCorps.{GithubAppInstallation}
6+
alias CodeCorps.{GithubAppInstallation, User}
87

9-
@preloads [:project, :user]
8+
action_fallback CodeCorpsWeb.FallbackController
9+
plug CodeCorpsWeb.Plug.DataToAttributes
10+
plug CodeCorpsWeb.Plug.IdsToIntegers
1011

11-
plug :load_resource, model: GithubAppInstallation, only: [:show], preload: @preloads
12-
plug :load_and_authorize_changeset, model: GithubAppInstallation, only: [:create], preload: @preloads
13-
14-
plug JaResource
15-
16-
@spec model :: module
17-
def model, do: CodeCorps.GithubAppInstallation
12+
@spec index(Conn.t, map) :: Conn.t
13+
def index(%Conn{} = conn, %{} = params) do
14+
with installations <- GithubAppInstallation |> id_filter(params) |> Repo.all do
15+
conn |> render("index.json-api", data: installations)
16+
end
17+
end
1818

19-
@spec filter(Plug.Conn.t, Ecto.Query.t, String.t, String.t) :: Ecto.Query.t
20-
def filter(_conn, query, "id", id_list) do
21-
query |> id_filter(id_list)
19+
@spec show(Conn.t, map) :: Conn.t
20+
def show(%Conn{} = conn, %{"id" => id}) do
21+
with %GithubAppInstallation{} = installation <- GithubAppInstallation |> Repo.get(id) do
22+
conn |> render("show.json-api", data: installation)
23+
end
2224
end
2325

24-
@spec handle_create(Plug.Conn.t, map) :: Ecto.Changeset.t
25-
def handle_create(_conn, attributes) do
26-
%GithubAppInstallation{}
27-
|> GithubAppInstallation.create_changeset(attributes)
26+
@spec create(Plug.Conn.t, map) :: Conn.t
27+
def create(%Conn{} = conn, %{} = params) do
28+
with %User{} = current_user <- conn |> Guardian.Plug.current_resource,
29+
{:ok, :authorized} <- current_user |> Policy.authorize(:create, %GithubAppInstallation{}, params),
30+
{:ok, %GithubAppInstallation{} = installation} <- %GithubAppInstallation{} |> GithubAppInstallation.create_changeset(params) |> Repo.insert do
31+
conn |> put_status(:created) |> render("show.json-api", data: installation)
32+
end
2833
end
2934
end
Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule CodeCorps.Policy.GithubAppInstallationTest do
22
use CodeCorps.PolicyCase
33

4-
import CodeCorps.Policy.GithubAppInstallation, only: [create?: 2, update?: 2]
4+
import CodeCorps.Policy.GithubAppInstallation, only: [create?: 2]
55
import CodeCorps.GithubAppInstallation, only: [create_changeset: 2]
66

77
alias CodeCorps.GithubAppInstallation
@@ -24,23 +24,4 @@ defmodule CodeCorps.Policy.GithubAppInstallationTest do
2424
refute create?(user, changeset)
2525
end
2626
end
27-
28-
describe "update?/2" do
29-
test "returns true when user is owner of the project" do
30-
project = insert(:project)
31-
user = insert(:user)
32-
insert(:project_user, project: project, user: user, role: "owner")
33-
github_app_installation = insert(:github_app_installation, project: project, user: user)
34-
35-
assert update?(user, github_app_installation)
36-
end
37-
38-
test "returns false for normal user" do
39-
project = insert(:project)
40-
user = insert(:user)
41-
github_app_installation = insert(:github_app_installation, project: project, user: user)
42-
43-
refute update?(user, github_app_installation)
44-
end
45-
end
4627
end

0 commit comments

Comments
 (0)