david234
I have the following routes
get "/locations", LocationsController, :show
get "/locations/add", LocationsController, :add
post "/locations/add", LocationsController, :create
get "/units", LocationsController, :show_units
get "/unit/add", LocationsController, :add_units
post "/unit/add", LocationsController, :create_units
get "/products", ProductsController, :show
get "/product/add", ProductsController, :add
post "/products/add", ProductsController, :create_product
get "/barcodes", ProductsController, :show_barcodes
get "/barcode/add", ProductsController, :add_barcode
post "/barcode/add", ProductsController, :create_barcode
I have three permission groups(admin can add more permission group)
can_admin_location
can_admin_products
can_admin_barcode
For example, You can see the below image to understand more what i am trying to achieve
I want to restrict route access based on permission given
For example if the below permission
can_admin_barcode (can use only below routes)
get "/barcodes", ProductsController, :show_barcodes
get "/barcode/add", ProductsController, :add_barcode
post "/barcode/add", ProductsController, :create_barcode
Then admin can change the user access level for this permission group
can_admin_barcode (can only view barcodes and only the below route will be accessible by this user)
get "/barcodes", ProductsController, :show_barcodes
Routes blocking must happen dynamically
I am storing current logged in user permission in session. I believe i can achieve this Route restriction using plugs.
Can someone share some insight on how to achieve this or any example. Your help is greatly appreciated
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex












Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mathieuprog
I created a similar Plug which protects routes against authorization rules. It’s on GitHub and the source is trivial:
https://github.com/mathieuprog/auth_z/blob/master/README.md#protecting-routes-against-unauthorized-users
Let me know if it doesn’t answer your case.
david234
Thanks. I will check now.
david234
I checked your code but I could able to figure out how I can use it to my situation.
For example, A user with this permission “can_admin_barcode” Can be allowed to view only below routes.
This value “can_admin_barcode” will be stored in session.
Can you give me insight on how to achieve this using your library.
kokolegorille
The plug does not know about the routes… but a simple plug like this might do
mathieuprog
Assuming that the user session is stored in
conn.assignsunder the:current_userkey (if not, just adapt) :It’s just one way to achieve authorization over routes.
david234
Thank you very much for your time and very neat explanation.
One small dout
My current router is like this
scope “/admin”, LogisticsWeb.Admin, as: :admin do
end
I am changing like this. Is this a proper way to achieve this?
mathieuprog
Yeah, that’s how I do it. You can see that I in fact attach an atom to each set of routes. Like all these barcode routes and the
:barcode_routesatom.Then in the Plug I retrieve that atom (representing thus that set of routes), and I check if the user has the right permissions for those routes.
Maybe there’s an easier way for you, but I just show you how I implemented it. This way, if you want to check something more than the
permissionsproperty in the future, there are no restrictions and you can check other user attributes.NduatiK
Continuing the discussion from Phoenix user role based access control:
You can also check out changelog.com’s approach. They define an authorize plug that allows them to define a policy for each route on a controller.
For example in the admin postcontroller these two plugs are defined.
For actions where an existing post is being manipulated, that post is first preloaded.
Then, permissions are checked on a per action basis using the Policies.Admin.Post policy which looks like:
Taken all together, when a user tries to update a post,