2021-02-24 07:49:39 -05:00
|
|
|
defmodule Bones73kWeb.Roles do
|
2020-09-14 21:06:39 -04:00
|
|
|
@moduledoc """
|
|
|
|
Defines roles related functions.
|
|
|
|
"""
|
|
|
|
|
2021-02-24 07:49:39 -05:00
|
|
|
alias Bones73k.Accounts.User
|
|
|
|
alias Bones73k.Properties.Property
|
2020-09-14 21:06:39 -04:00
|
|
|
|
|
|
|
@type entity :: struct()
|
2021-03-04 22:03:27 -05:00
|
|
|
@type action :: :new | :index | :edit | :show | :delete | :edit_role
|
2020-09-14 21:06:39 -04:00
|
|
|
@spec can?(%User{}, entity(), action()) :: boolean()
|
|
|
|
|
|
|
|
def can?(user, entity, action)
|
2021-03-04 22:03:27 -05:00
|
|
|
|
|
|
|
# Properties / Property
|
2020-09-14 21:06:39 -04:00
|
|
|
def can?(%User{role: :admin}, %Property{}, _any), do: true
|
|
|
|
def can?(%User{}, %Property{}, :index), do: true
|
|
|
|
def can?(%User{}, %Property{}, :new), do: true
|
|
|
|
def can?(%User{}, %Property{}, :show), do: true
|
|
|
|
def can?(%User{id: id}, %Property{user_id: id}, :edit), do: true
|
|
|
|
def can?(%User{id: id}, %Property{user_id: id}, :delete), do: true
|
2021-03-04 22:03:27 -05:00
|
|
|
|
|
|
|
# Accounts / User
|
|
|
|
def can?(%User{role: :admin}, %User{}, _any), do: true
|
|
|
|
def can?(%User{role: :manager}, %User{}, :index), do: true
|
|
|
|
def can?(%User{role: :manager}, %User{}, :new), do: true
|
|
|
|
def can?(%User{role: :manager}, %User{}, :edit), do: true
|
|
|
|
def can?(%User{role: :manager}, %User{}, :show), do: true
|
|
|
|
|
|
|
|
# Final response
|
2020-09-14 21:06:39 -04:00
|
|
|
def can?(_, _, _), do: false
|
|
|
|
end
|