From b27a21abca63499173636e2528feacf7ee979b02 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Fri, 14 Aug 2020 09:20:16 +0100 Subject: [PATCH] fix failing tests by adding ecto associations to Auth.Person functions #89 --- lib/auth/person.ex | 6 +++++- lib/auth/role.ex | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/auth/person.ex b/lib/auth/person.ex index 04878b54..4db327f8 100644 --- a/lib/auth/person.ex +++ b/lib/auth/person.ex @@ -20,6 +20,7 @@ defmodule Auth.Person do field :status, :id field :tag, :id field :key_id, :integer + many_to_many :roles, Auth.Role, join_through: "people_roles" has_many :statuses, Auth.Status # has_many :sessions, Auth.Session, on_delete: :delete_all @@ -29,7 +30,7 @@ defmodule Auth.Person do @doc """ Default attributes validation for Person """ - def changeset(person, attrs) do + def changeset(person, attrs, roles \\ []) do person |> cast(attrs, [ :id, @@ -49,6 +50,7 @@ defmodule Auth.Person do |> validate_required([:email]) |> put_email_hash() |> put_pass_hash() + |> put_assoc(:roles, roles) end def create_person(person) do @@ -196,6 +198,7 @@ defmodule Auth.Person do def get_person_by_id(id) do __MODULE__ |> Repo.get_by(id: id) + |> Repo.preload(:roles) end defp put_pass_hash(changeset) do @@ -214,6 +217,7 @@ defmodule Auth.Person do def get_person_by_email(email) do __MODULE__ |> Repo.get_by(email_hash: email) + |> Repo.preload(:roles) end @doc """ diff --git a/lib/auth/role.ex b/lib/auth/role.ex index 8508c657..6c564c99 100644 --- a/lib/auth/role.ex +++ b/lib/auth/role.ex @@ -10,6 +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" timestamps() end @@ -116,4 +117,19 @@ defmodule Auth.Role do Role.changeset(role, attrs) end + + @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