From 41d852b06afd037c467e663044133850456f8d41 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Balsini Moura Date: Mon, 14 Sep 2020 22:06:39 -0300 Subject: [PATCH] Add rules for authorising properties --- lib/real_estate_web/roles.ex | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lib/real_estate_web/roles.ex diff --git a/lib/real_estate_web/roles.ex b/lib/real_estate_web/roles.ex new file mode 100644 index 0000000..e1b7e2c --- /dev/null +++ b/lib/real_estate_web/roles.ex @@ -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