Skip to content

Commit

Permalink
add grant_role/3 function + test to grant roles to people #90 |> #27 #31
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 20, 2020
1 parent 259e239 commit 506d365
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/auth/people_roles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Auth.PeopleRoles do
use Ecto.Schema
import Ecto.Changeset
alias Auth.Repo
# https://stackoverflow.com/a/47501059/1148249
alias __MODULE__

schema "people_roles" do
Expand Down
9 changes: 8 additions & 1 deletion lib/auth/person.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ defmodule Auth.Person do
field :status, :id
field :tag, :id
field :key_id, :integer
many_to_many :roles, Auth.Role, join_through: "people_roles"
# many_to_many :roles, Auth.Role, join_through: "people_roles"
# has_many :roles, through: [:people_roles, :role]
many_to_many :roles, Auth.Role, join_through: Auth.PeopleRoles

has_many :statuses, Auth.Status
# has_many :sessions, Auth.Session, on_delete: :delete_all
Expand All @@ -31,6 +33,9 @@ defmodule Auth.Person do
Default attributes validation for Person
"""
def changeset(person, attrs, roles \\ []) do
# IO.inspect(person, label: "changeset > person")
# IO.inspect(attrs, label: "changeset > attrs")
# IO.inspect(roles, label: "changeset > roles")
person
|> cast(attrs, [
:id,
Expand Down Expand Up @@ -199,6 +204,7 @@ defmodule Auth.Person do
__MODULE__
|> Repo.get_by(id: id)
|> Repo.preload(:roles)
|> Repo.preload(:statuses)
end

defp put_pass_hash(changeset) do
Expand All @@ -218,6 +224,7 @@ defmodule Auth.Person do
__MODULE__
|> Repo.get_by(email_hash: email)
|> Repo.preload(:roles)
|> Repo.preload(:statuses)
end

@doc """
Expand Down
15 changes: 4 additions & 11 deletions lib/auth/role.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Auth.Role do
field :desc, :string
field :name, :string
field :person_id, :id
many_to_many :people, Auth.Person, join_through: "people_roles"
# many_to_many :roles, Auth.Role, join_through: Auth.PeopleRoles

timestamps()
end
Expand Down Expand Up @@ -118,18 +118,11 @@ defmodule Auth.Role do
end


@doc """
grants the default "subscriber" (6) role to the person
"""
# @doc """
# grants the default "subscriber" (6) role to the person
# """
# def set_default_role(person) do

# end

@doc """
grants a role to the given person
"""
# def grant_role(person, role, granter) do

# end

end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Auth.Repo.Migrations.CreateRolePermissions do
create table(:role_permissions) do
add :role_id, references(:roles, on_delete: :nothing)
add :permission_id, references(:permissions, on_delete: :nothing)
add :granter, references(:people, on_delete: :nothing)
add :granter_id, references(:people, on_delete: :nothing)

timestamps()
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Auth.Repo.Migrations.CreatePeopleRoles do
create table(:people_roles) do
add :person_id, references(:people, on_delete: :nothing)
add :role_id, references(:roles, on_delete: :nothing)
add :granter, references(:people, on_delete: :nothing)
add :granter_id, references(:people, on_delete: :nothing)

timestamps()
end
Expand Down
13 changes: 12 additions & 1 deletion priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule Auth.Seeds do
IO.inspect(person.id, label: "seeds.exs person.id")
IO.puts("- - - - - - - - - - - - - - - - - - - - - - ")
end

person
end

Expand All @@ -47,7 +48,12 @@ defmodule Auth.Seeds do

api_key = key.client_id <> "/" <> key.client_secret
# set the AUTH_API_KEY environment variable during test run:
write_env("AUTH_API_KEY", api_key)
if(Mix.env() == :test) do
System.put_env("AUTH_API_KEY", api_key)
else
# update the AUTH_API_KEY in the .env file:
write_env("AUTH_API_KEY", api_key)
end
end

# write the key:value pair to project .env file
Expand Down Expand Up @@ -105,9 +111,14 @@ defmodule SetupRoles do
json = get_json("/priv/repo/default_roles.json")
Enum.each(json, fn role ->
Role.create_role(role)
# |> IO.inspect()
end)
end

def assign_superadmin_role() do

end


end

Expand Down
17 changes: 17 additions & 0 deletions test/auth/role_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Auth.RoleTest do
use Auth.DataCase
# use AuthWeb.ConnCase

describe "roles" do
alias Auth.Role
Expand Down Expand Up @@ -129,4 +130,20 @@ defmodule Auth.RoleTest do
assert %Ecto.Changeset{} = Permission.change_permission(permission)
end
end


# create a new person and confirm they were asigned a default role of "subscriber"



# describe "grant role" do




# # test "change_permission/1 returns a permission changeset" do
# # permission = permission_fixture()
# # assert %Ecto.Changeset{} = Permission.change_permission(permission)
# # end
# end
end
14 changes: 14 additions & 0 deletions test/auth_web/controllers/role_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,18 @@ defmodule AuthWeb.RoleControllerTest do
role = fixture(:role)
%{role: role}
end


test "grant_role/3 happy path", %{conn: conn} do
# login as superadmin
conn = AuthTest.admin_login(conn)
# create a new person
alex = %{email: "[email protected]", auth_provider: "email"}
grantee = Auth.Person.create_person(alex)
role_id = 4
Auth.PeopleRoles.grant_role(conn, grantee.id, role_id)
person_with_role = Auth.Person.get_person_by_id(grantee.id)
role = List.first(person_with_role.roles)
assert role_id == role.id
end
end

0 comments on commit 506d365

Please sign in to comment.