shift73k/lib/shift73k_web/roles.ex

43 lines
1.6 KiB
Elixir
Raw Normal View History

2021-03-05 19:23:32 -05:00
defmodule Shift73kWeb.Roles do
2020-09-14 21:06:39 -04:00
@moduledoc """
Defines roles related functions.
"""
2021-03-05 19:23:32 -05:00
alias Shift73k.Accounts.User
2021-03-11 16:57:20 -05:00
alias Shift73k.Shifts.Templates.ShiftTemplate
2021-03-20 12:45:50 -04:00
alias Shift73k.Shifts.Shift
2020-09-14 21:06:39 -04:00
@type entity :: struct()
@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-11 16:57:20 -05:00
# Shifts.Templates / ShiftTemplate
def can?(%User{role: :admin}, %ShiftTemplate{}, _any), do: true
def can?(%User{}, %ShiftTemplate{}, :index), do: true
def can?(%User{}, %ShiftTemplate{}, :new), do: true
2021-03-20 12:45:50 -04:00
# def can?(%User{id: id}, %ShiftTemplate{user_id: id}, :show), do: true
def can?(%User{id: id}, %ShiftTemplate{user_id: id}, :edit), do: true
2021-03-11 14:05:22 -05:00
def can?(%User{id: id}, %ShiftTemplate{user_id: id}, :clone), do: true
def can?(%User{id: id}, %ShiftTemplate{user_id: id}, :delete), do: true
2021-03-20 12:45:50 -04:00
# Shifts / Shift
def can?(%User{role: :admin}, %Shift{}, _any), do: true
def can?(%User{}, %Shift{}, :index), do: true
def can?(%User{}, %Shift{}, :new), do: true
def can?(%User{id: id}, %Shift{user_id: id}, :show), do: true
def can?(%User{id: id}, %Shift{user_id: id}, :edit), do: true
def can?(%User{id: id}, %Shift{user_id: id}, :delete), do: true
# 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
2021-03-08 07:39:41 -05:00
# def can?(%User{role: :manager}, %User{}, :show), do: true
# Final response
2020-09-14 21:06:39 -04:00
def can?(_, _, _), do: false
end