Add rules for authorising properties
This commit is contained in:
parent
068be8d583
commit
41d852b06a
1 changed files with 21 additions and 0 deletions
21
lib/real_estate_web/roles.ex
Normal file
21
lib/real_estate_web/roles.ex
Normal file
|
@ -0,0 +1,21 @@
|
|||
defmodule RealEstateWeb.Roles do
|
||||
@moduledoc """
|
||||
Defines roles related functions.
|
||||
"""
|
||||
|
||||
alias RealEstate.Accounts.User
|
||||
alias RealEstate.Properties.Property
|
||||
|
||||
@type entity :: struct()
|
||||
@type action :: :new | :index | :edit | :show | :delete
|
||||
@spec can?(%User{}, entity(), action()) :: boolean()
|
||||
|
||||
def can?(user, entity, action)
|
||||
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
|
||||
def can?(_, _, _), do: false
|
||||
end
|
Loading…
Reference in a new issue