Skip to content

Commit

Permalink
Merge pull request #726 from ignu/716-organization-owner
Browse files Browse the repository at this point in the history
Check Organization owner for update permissions
  • Loading branch information
joshsmith committed Mar 7, 2017
2 parents e03f6e6 + 673440f commit bf313db
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
8 changes: 8 additions & 0 deletions lib/code_corps/helpers/policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ defmodule CodeCorps.Helpers.Policy do
|> Repo.one
end

@doc """
Determines if the provided organization is owned by the provided user
"""
@spec organization_owned_by?(Organization.t, User.t) :: boolean
def organization_owned_by?(%Organization{owner_id: owner_id}, %User{id: user_id}) do
owner_id == user_id
end

@doc """
Retrieves a project record, from a model struct, or an `Ecto.Changeset` containing a `project_id` field
Expand Down
35 changes: 7 additions & 28 deletions test/policies/organization_policy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ defmodule CodeCorps.OrganizationPolicyTest do

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

defp setup_user_organization_by_role(role) do
user = insert(:user)
organization = insert(:organization)
insert(:organization_membership, role: role, member: user, organization: organization)
[user, organization]
end

describe "create" do
test "returns true when user is an admin" do
user = build(:user, admin: true)
Expand All @@ -29,30 +22,16 @@ defmodule CodeCorps.OrganizationPolicyTest do
assert update?(user, organization)
end

test "returns false when user is not member of organization" do
user = insert(:user)
organization = insert(:organization)
refute update?(user, organization)
end

test "returns false when user is pending member of organization" do
[user, organization] = setup_user_organization_by_role("pending")
refute update?(user, organization)
end

test "returns false when user is contributor of organization" do
[user, organization] = setup_user_organization_by_role("contributor")
refute update?(user, organization)
end

test "returns true when user is admin of organization" do
[user, organization] = setup_user_organization_by_role("admin")
test "returns true when user is the organization owner" do
user = insert(:user, admin: true)
organization = build(:organization, owner_id: user.id)
assert update?(user, organization)
end

test "returns true when user is owner of organization" do
[user, organization] = setup_user_organization_by_role("owner")
assert update?(user, organization)
test "returns false when user is not the organization owner" do
user = insert(:user)
organization = build(:organization)
refute update?(user, organization)
end
end
end
4 changes: 2 additions & 2 deletions web/policies/organization_policy.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule CodeCorps.OrganizationPolicy do
import CodeCorps.Helpers.Policy,
only: [get_membership: 2, get_role: 1, admin_or_higher?: 1]
only: [organization_owned_by?: 2]

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

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

0 comments on commit bf313db

Please sign in to comment.