Skip to content

Commit bf313db

Browse files
authored
Merge pull request #726 from ignu/716-organization-owner
Check Organization owner for update permissions
2 parents e03f6e6 + 673440f commit bf313db

File tree

3 files changed

+17
-30
lines changed

3 files changed

+17
-30
lines changed

lib/code_corps/helpers/policy.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ defmodule CodeCorps.Helpers.Policy do
3131
|> Repo.one
3232
end
3333

34+
@doc """
35+
Determines if the provided organization is owned by the provided user
36+
"""
37+
@spec organization_owned_by?(Organization.t, User.t) :: boolean
38+
def organization_owned_by?(%Organization{owner_id: owner_id}, %User{id: user_id}) do
39+
owner_id == user_id
40+
end
41+
3442
@doc """
3543
Retrieves a project record, from a model struct, or an `Ecto.Changeset` containing a `project_id` field
3644

test/policies/organization_policy_test.exs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ defmodule CodeCorps.OrganizationPolicyTest do
33

44
import CodeCorps.OrganizationPolicy, only: [create?: 1, update?: 2]
55

6-
defp setup_user_organization_by_role(role) do
7-
user = insert(:user)
8-
organization = insert(:organization)
9-
insert(:organization_membership, role: role, member: user, organization: organization)
10-
[user, organization]
11-
end
12-
136
describe "create" do
147
test "returns true when user is an admin" do
158
user = build(:user, admin: true)
@@ -29,30 +22,16 @@ defmodule CodeCorps.OrganizationPolicyTest do
2922
assert update?(user, organization)
3023
end
3124

32-
test "returns false when user is not member of organization" do
33-
user = insert(:user)
34-
organization = insert(:organization)
35-
refute update?(user, organization)
36-
end
37-
38-
test "returns false when user is pending member of organization" do
39-
[user, organization] = setup_user_organization_by_role("pending")
40-
refute update?(user, organization)
41-
end
42-
43-
test "returns false when user is contributor of organization" do
44-
[user, organization] = setup_user_organization_by_role("contributor")
45-
refute update?(user, organization)
46-
end
47-
48-
test "returns true when user is admin of organization" do
49-
[user, organization] = setup_user_organization_by_role("admin")
25+
test "returns true when user is the organization owner" do
26+
user = insert(:user, admin: true)
27+
organization = build(:organization, owner_id: user.id)
5028
assert update?(user, organization)
5129
end
5230

53-
test "returns true when user is owner of organization" do
54-
[user, organization] = setup_user_organization_by_role("owner")
55-
assert update?(user, organization)
31+
test "returns false when user is not the organization owner" do
32+
user = insert(:user)
33+
organization = build(:organization)
34+
refute update?(user, organization)
5635
end
5736
end
5837
end
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule CodeCorps.OrganizationPolicy do
22
import CodeCorps.Helpers.Policy,
3-
only: [get_membership: 2, get_role: 1, admin_or_higher?: 1]
3+
only: [organization_owned_by?: 2]
44

55
alias CodeCorps.User
66
alias CodeCorps.Organization
@@ -9,5 +9,5 @@ defmodule CodeCorps.OrganizationPolicy do
99
def create?(%User{admin: false}), do: false
1010

1111
def update?(%User{admin: true}, %Organization{}), do: true
12-
def update?(%User{} = user, %Organization{} = organization), do: organization |> get_membership(user) |> get_role |> admin_or_higher?
12+
def update?(%User{} = user, %Organization{} = organization), do: organization |> organization_owned_by?(user)
1313
end

0 commit comments

Comments
 (0)