# A richer role model

**URL:** <https://www.ruby-forum.com/t/a-richer-role-model/112587>\
**Category:** Radiant CMS\
**Created:** [August 21, 2007, 5:44am UTC](https://www.ruby-forum.com/t/a-richer-role-model/112587 "2007-08-21T05:44:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [August 21, 2007, 5:44am UTC](https://www.ruby-forum.com/t/a-richer-role-model/112587/1 "2007-08-21T05:44:06Z")

</div>

A question of philosophy

Currently, we’re looking at extending radiant for a client whose  
primary need is content. However, they also need some other tools  
specific to not-for-profits. Ideally it would be great to deliver  
everything in one interface. (Content management and applications).  
The applications would live inside the admin interface for radiant,  
utilizing Radiant’s login and user-management system. Where we’ve  
started is with an extension to manage roles and users assigned to  
those roles. What we’re trying to extend now is the process of  
displaying tabs for users. For example, only people in the “finance”  
role should see the tab in the admin interface for finance related  
reports, receipts from web donations, etc.

For example, the following code from admin\_ui.rb appears to be  
responsible for making the decision to display tabs for users. It  
would be nice to extend the user to read all the available roles in  
our roles table to provide “#{role}?” methods for the available roles.

def shown\_for?(user)  
visibility.include?(:all) or visibility.any? { |role| user.send  
("#{role}?") }  
end

As we look through the code it might not be possible to do everything  
we would like from extensions. Would there be interest in the  
community in re-visiting the user roles to provide for a more  
flexible roles system?

Paul

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [August 21, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/a-richer-role-model/112587/2 "2007-08-21T15:52:32Z")

</div>

Paul,

It should be easy to add a new role(s) for this if you have only  
role-specific permissions and nothing more granular than that. Just  
create an extension that modifies the User model (in the database and in  
the code), providing methods in the manner of “finance?” or  
“public\_relations?” that test whether a given user has that role. Then  
your other interfaces/extensions can add restrictions on those tabs  
easily.

Sean

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [August 21, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/a-richer-role-model/112587/3 "2007-08-21T20:30:38Z")

</div>

Thanks, Sean:

It looks like this works. I’ll push the rest of the extension into  
our public SVN when I get it ironed out.

def activate  
admin.tabs.add “Rbac Base”, “/admin/rbac”, :after =\>  
“Layouts”, :visibility =\> [:admin]  
User.send :has\_and\_belongs\_to\_many, :roles  
User.send :include, RbacSupport  
end

module RbacSupport

all\_roles = Role.find(:all)

all\_roles.each do | possible\_role |  
define\_method("#{possible\_role.role\_name.underscore}?") do  
if @my\_roles == nil  
@my\_roles = Hash.new  
roles.each do | role |  
@my\_roles["#{role.role\_name.underscore}"] = true  
end  
end  
@my\_roles["#{possible\_role.role\_name.underscore}"] || admin?  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [August 21, 2007, 10:07pm UTC](https://www.ruby-forum.com/t/a-richer-role-model/112587/4 "2007-08-21T22:07:48Z")

</div>

VERY rough code to play with. Drag and drop users into roles. Role  
names can be anything, they are .underscore 'd in the extension.  
Inside another extension that depends on these roles set :visibility  
appropriately →

admin.tabs.add “Membership”, “/admin/membership”, :after =\>  
“Layouts”, :visibility =\> [:membership]

Let me know if you can’t check out the code from:

[http://www.saturnflyer.com/svn/radiantP/rbac\_base/trunk/](http://www.saturnflyer.com/svn/radiantP/rbac_base/trunk/)

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [August 21, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/a-richer-role-model/112587/5 "2007-08-21T21:41:45Z")

</div>

Very interesting! Looking forward to seeing the final product.

Sean
