Skip to content

Commit

Permalink
fix failing tests by adding ecto associations to Auth.Person functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 14, 2020
1 parent c1f9d3b commit b27a21a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/auth/person.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 """
Expand Down
16 changes: 16 additions & 0 deletions lib/auth/role.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit b27a21a

Please sign in to comment.