enkr1

enkr1

RBAC in elixir phoenix (managing routes & permissions, etc)

Hi Elixir community,

I am implementing RBAC into my project and have declared the routes that each role can access such as:

# User Management
scope "/users", MyWeb do
  pipe_through([:browser, :require_authenticated_user, :require_admin_role])

  live_session :crud_users, on_mount: MyWeb.UserAuthLive do
    live("/", UserLive.Index, :index)
    live("/new", UserLive.Index, :new)
    live("/:id/edit", UserLive.Index, :edit)
    live("/:id/show", UserLive.Index, :show)
    # ...
  end
end

# Product Management
scope "/products", MyWeb do
  pipe_through([:browser, :require_authenticated_user, :require_role_product_manager])

  live_session :crud_products, on_mount: MyWeb.UserAuthLive do
    live("/", ProductLive.Index, :index)
    # ...
  end
end

Now, the accessible routes of each role of the users have been declared and it works perfectly.


The problem I am facing now is that I am trying to display certain elements on the frontend which requires checking the user’s role(s).

My current solution is to use a custom function like has_role?/2 to validate by passing in the user and required_roles. The problem here is that whenever there is a change in the user’s permission, we have to find the functions (has_role?/2) that need to be modified everywhere in the app.


To make it clearer, for instance:
There is this edit user button that can be accessed by admin & hr. One day, the app owner decided to not allow hr to edit users anymore. We have to change has_role?(user, ["Admin", "HR"]) to has_role?(user, ["Admin"]).


I was thinking if there is any method to check if this user has permission to access a route by validating from the declared routes in the router.ex instead of using a function like has_role?/2?

Thank you so much in advance,

Best wishes,
Jing Hui P.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement