How do I prevent someone from going to a route/url unless user is admin?

41 views
Skip to first unread message

nko...@gmail.com

unread,
Sep 2, 2015, 2:59:24 AM9/2/15
to Devise
Hello

with rails admin, I can insert the following code into initialisers and therefore prevent someone from going to mysite.com/admin unless
that user is admin (admin is boolean in users table) 

# prevent from going to /admin unless user is admin
  config.authorize_with do
    redirect_to main_app.root_path unless warden.user.admin?
 end

My question is how do I protect a route/url with devise? Can I have the same functionality that rails admin gives me?
I know that I can define actions that require authentication (this article: https://github.com/plataformatec/devise/wiki/How-To:-Define-resource-actions-that-require-authentication-using-routes.rb), but what about going a step further and authenticating an action with a certain user?

Thank you in advance!

nko...@gmail.com

unread,
Sep 2, 2015, 8:00:33 AM9/2/15
to Devise

nko...@gmail.com

unread,
Sep 2, 2015, 8:01:23 AM9/2/15
to Devise

Carlos Antonio da Silva

unread,
Sep 3, 2015, 6:46:30 AM9/3/15
to Devise
You should use something like current_user.admin? to check that, and redirect to any route you need.

--

---
You received this message because you are subscribed to the Google Groups "Devise" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio
Reply all
Reply to author
Forward
0 new messages