# Role Based Authentication

**URL:** <https://discuss.rubyonrails.org/t/role-based-authentication/10224>\
**Category:** rubyonrails-talk\
**Created:** [March 14, 2007, 8:25am UTC](https://discuss.rubyonrails.org/t/role-based-authentication/10224 "2007-03-14T08:25:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://discuss.rubyonrails.org/u/gaurav_bagga)\
**Post date:** [March 14, 2007, 8:25am UTC](https://discuss.rubyonrails.org/t/role-based-authentication/10224/1 "2007-03-14T08:25:45Z")

</div>

Hi All,

I want to authorize user according to role he has. I found some rails plugins,

ActiveRBAC ActiveACL

anybody has used them (how was it), or can provide little info (hints) how to go about role based authentication. I'll appreciate if anyone helps me with this.

Regards Gaurav

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://discuss.rubyonrails.org/u/gaurav_bagga)\
**Post date:** [March 14, 2007, 11:18am UTC](https://discuss.rubyonrails.org/t/role-based-authentication/10224/2 "2007-03-14T11:18:38Z")

</div>

> What I do?
> 
> I add new column in the Users table, and call it "status" which has different enum('user', 'moderator', 'admin')
> 
> Then in my controller, I would use:
> 
> before\_filer :is\_admin, :only =\> %w(this\_method)
> 
> 🙂
> 
> --

Thanks Jamal that was nice input.

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://discuss.rubyonrails.org/u/gaurav_bagga)\
**Post date:** [March 14, 2007, 11:22am UTC](https://discuss.rubyonrails.org/t/role-based-authentication/10224/3 "2007-03-14T11:22:42Z")

</div>

Hi,

&nbsp;&nbsp;I am facing another problem, I am not able to install above mentioned plugins, as I have to evaluate them and finally conclude how to go about this.

&nbsp;&nbsp;Has anyone successfully implemented any thing recently using those plugins? i.e..

&nbsp;&nbsp;ActiveRBAC &nbsp;&nbsp;ActiveACL

Gaurav

---

<div class="post-metadata">

**Author:** ![K\_Rajesh](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@K\_Rajesh](https://discuss.rubyonrails.org/u/K_Rajesh)\
**Post date:** [March 14, 2007, 12:37pm UTC](https://discuss.rubyonrails.org/t/role-based-authentication/10224/4 "2007-03-14T12:37:23Z")

</div>

You could do something like

def check\_authentication &nbsp;&nbsp;unless session[:user] &nbsp;&nbsp;redirect\_to :controller =\> "login", :action =\> "signin\_form" &nbsp;&nbsp;return &nbsp;&nbsp;end end

# The authorization check uses the ruby detect function to great effect. Assumes each user has multiple roles and each of these roles can be assigned to multiple rights. Rights are defined as a controller- action combination and stored in the database in tables roles and rights.

def check\_authorization &nbsp;&nbsp;user = User.find(session[:user]) &nbsp;&nbsp;unless user.roles.detect{|role| &nbsp;&nbsp;&nbsp;&nbsp;role.rights.detect{|right| &nbsp;&nbsp;&nbsp;&nbsp;right.action == action\_name && right.controller == controller\_name &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "You are not authorized to access Controller: " + controller\_name + " Action: " + action\_name &nbsp;&nbsp;&nbsp;&nbsp;session[:prev\_controller]="error" unless session[:prev\_controller] &nbsp;&nbsp;&nbsp;&nbsp;session[:prev\_action] ="no\_access" unless session[:prev\_action] &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :controller =\> session[:prev\_controller], :action =\> session[:prev\_action] &nbsp;&nbsp;&nbsp;&nbsp;return &nbsp;&nbsp;end &nbsp;&nbsp;session[:prev\_controller] = controller\_name &nbsp;&nbsp;session[:prev\_action] = action\_name end

This works out for me. Wherever you want this checked add before\_filter

before\_filter :check\_authentication, :check\_authorization

Regards, Rajesh

---

<div class="post-metadata">

**Author:** ![Fredrik1](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@Fredrik1](https://discuss.rubyonrails.org/u/Fredrik1)\
**Post date:** [March 14, 2007, 1:56pm UTC](https://discuss.rubyonrails.org/t/role-based-authentication/10224/5 "2007-03-14T13:56:53Z")

</div>

I use activeRBAC for a large project I'm currently working on. After having had it in place for 6 months, it looks nothing like it did. Great start and I like the Group/Role management interfaces and how it reports on how many users are in each. If you are looking for an rbac, it works great.

There is also a nice PDF doc for it that would give you some more insight into it. I like it, but it is a pretty large plugin, so you will likely modify it which some of it is easily done through mixins and overriding the views and controller functions.

Someone correct me if I'm wrong, but I don't think it is actively developed anymore (I could not really update anyway.)

In short. I would recommend it.

Fredrik

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://discuss.rubyonrails.org/u/gaurav_bagga)\
**Post date:** [March 14, 2007, 5:12pm UTC](https://discuss.rubyonrails.org/t/role-based-authentication/10224/6 "2007-03-14T17:12:57Z")

</div>

Thanks Rajesh for help,

Well Fredrik

> but I don't think it is actively developed anymore (I could not really update anyway.)

I tried to install it but in vane, then I tried to access the repository given on its site through svn (radrails) could get it but how to make it work. The controllers,views had files and I ran the migration script and models were also present. But when I tried access [http://localhost:3000/active\_arbac/login](http://localhost:3000/active_arbac/login) or [http://localhost:3000/active\_arbac/registration](http://localhost:3000/active_arbac/registration) it dint work.It complains of something not being initialized.

I have not used any plugins before how to get it running any idea.

Gaurav

---

<div class="post-metadata">

**Author:** ![Jonathan\_Linowes](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jonathan_linowes/32/2572_2.png) [@Jonathan\_Linowes](https://discuss.rubyonrails.org/u/Jonathan_Linowes)\
**Post date:** [March 14, 2007, 8:10pm UTC](https://discuss.rubyonrails.org/t/role-based-authentication/10224/7 "2007-03-14T20:10:18Z")

</div>

I've been looking into Goldberg for this. [http://goldberg.240gl.org](http://goldberg.240gl.org)

It looks like a powerful, flexible, and de-coupled solution for role based authentication.

"Goldberg is essentially just a before\_filter that checks to see whether the user has the permissions to perform the incoming action. This includes AJAX requests etc. "

I'm curious if anyone reading this has any experience or comments about that project.

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://discuss.rubyonrails.org/u/gaurav_bagga)\
**Post date:** [March 16, 2007, 5:18am UTC](https://discuss.rubyonrails.org/t/role-based-authentication/10224/8 "2007-03-16T05:18:22Z")

</div>

Hi,

Thanks all for replies. Yesterday I tried goldberg and it fits my need so got over the dilemma :).

Regards, Guarav
