diff --git a/lib/auth/people_roles.ex b/lib/auth/people_roles.ex index 49ee05cc..bffd49d4 100644 --- a/lib/auth/people_roles.ex +++ b/lib/auth/people_roles.ex @@ -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 diff --git a/lib/auth/person.ex b/lib/auth/person.ex index 4db327f8..b4dedcdd 100644 --- a/lib/auth/person.ex +++ b/lib/auth/person.ex @@ -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 @@ -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, @@ -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 @@ -218,6 +224,7 @@ defmodule Auth.Person do __MODULE__ |> Repo.get_by(email_hash: email) |> Repo.preload(:roles) + |> Repo.preload(:statuses) end @doc """ diff --git a/lib/auth/role.ex b/lib/auth/role.ex index 6c564c99..fbac1f8c 100644 --- a/lib/auth/role.ex +++ b/lib/auth/role.ex @@ -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 @@ -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 diff --git a/priv/repo/migrations/20200723143204_create_role_permissions.exs b/priv/repo/migrations/20200723143204_create_role_permissions.exs index 2651ee17..e607b969 100644 --- a/priv/repo/migrations/20200723143204_create_role_permissions.exs +++ b/priv/repo/migrations/20200723143204_create_role_permissions.exs @@ -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 diff --git a/priv/repo/migrations/20200723154847_create_people_roles.exs b/priv/repo/migrations/20200723154847_create_people_roles.exs index 8d69b222..f5938f41 100644 --- a/priv/repo/migrations/20200723154847_create_people_roles.exs +++ b/priv/repo/migrations/20200723154847_create_people_roles.exs @@ -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 diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 72259d8d..fd72ccf0 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -32,6 +32,7 @@ defmodule Auth.Seeds do IO.inspect(person.id, label: "seeds.exs person.id") IO.puts("- - - - - - - - - - - - - - - - - - - - - - ") end + person end @@ -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 @@ -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 diff --git a/test/auth/role_test.exs b/test/auth/role_test.exs index 6764f9db..9a425a3d 100644 --- a/test/auth/role_test.exs +++ b/test/auth/role_test.exs @@ -1,5 +1,6 @@ defmodule Auth.RoleTest do use Auth.DataCase + # use AuthWeb.ConnCase describe "roles" do alias Auth.Role @@ -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 diff --git a/test/auth_web/controllers/role_controller_test.exs b/test/auth_web/controllers/role_controller_test.exs index f7562279..35fe8147 100644 --- a/test/auth_web/controllers/role_controller_test.exs +++ b/test/auth_web/controllers/role_controller_test.exs @@ -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: "alex_grant_role@gmail.com", 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