# Role based restriction for fields (not views or actions)

**URL:** <https://discuss.rubyonrails.org/t/role-based-restriction-for-fields-not-views-or-actions/27169>\
**Category:** rubyonrails-talk\
**Created:** [March 29, 2008, 7:43pm UTC](https://discuss.rubyonrails.org/t/role-based-restriction-for-fields-not-views-or-actions/27169 "2008-03-29T19:43:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_Schofield](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Sean\_Schofield](https://discuss.rubyonrails.org/u/Sean_Schofield)\
**Post date:** [March 29, 2008, 7:43pm UTC](https://discuss.rubyonrails.org/t/role-based-restriction-for-fields-not-views-or-actions/27169/1 "2008-03-29T19:43:23Z")

</div>

I have an application that requires some complex access rules. Certain fields can only be edited by certain roles. I am using restful\_authentication and role\_requirement plugins so I am fine with restricting an entire controller or specific action based on a role.

I am trying to come up with a solution that would allow me to specify something as a text field but make it just plain text if the user does not have edit permissions. Anyone have any ideas on fine grain access control like this? Can you point me to a project that uses something like this?

TIA,

Sean

---

<div class="post-metadata">

**Author:** ![Dan\_Sharp](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Dan\_Sharp](https://discuss.rubyonrails.org/u/Dan_Sharp)\
**Post date:** [March 30, 2008, 2:33am UTC](https://discuss.rubyonrails.org/t/role-based-restriction-for-fields-not-views-or-actions/27169/2 "2008-03-30T02:33:10Z")

</div>

Sean,

I don't have any leads for you, but I'm very interested in this myself. I do have one thought: I imagine you could roll your own text field helper method (and then probably helper methods for the other form elements). Something like:

require 'active\_support/inflector.rb' module ActionView &nbsp;&nbsp;module Helpers &nbsp;&nbsp;&nbsp;&nbsp;module FormHelper &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def secured\_text\_field(object\_name, method, options = {}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current\_user.has\_role?(options[:required\_role]) ? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\<div id=\"#{object\_name}\_#{method} \"\>#{object\_name.instance\_eval(method.to\_s)}\</div\>" : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text\_field(object\_name, method, options) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end end

# (I added the above to the ApplicationHelper.rb file but not within the Module namespace... i.e. after it.)

And you'd call it with something like:

\<% form\_for(@foo) do |f| %\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :name %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= secured\_text\_field @foo, :name, :required\_role =\> "admin" %\> &nbsp;&nbsp;\</p\> \<% end %\>

If the current\_user has the "admin" role, then you'd get:

\<form tag...\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;\<label for="foo\_name"\>Name\</label\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<input id="foo\_name" name="foo[name]" size="30" type="text" value="" /\> &nbsp;&nbsp;\</p\> \</form\>

But if the current\_user has any other role, you'd get:

\<form tag...\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;\<label for="foo\_name"\>Name\</label\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<div id="foo\_name"\>Some Name\</div\> &nbsp;&nbsp;\</p\> \</form\>

I'm sure there's a cleaner way, but the above works. I tried doing it within the FormBuilder constrains (i.e. "f.secured\_text\_field...") but I don't understand that code... lots of meta-programming there.

Anyway, I tested the above and it works. Hopefully that gets you started.

-Danimal

---

<div class="post-metadata">

**Author:** ![Dan\_Sharp](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Dan\_Sharp](https://discuss.rubyonrails.org/u/Dan_Sharp)\
**Post date:** [March 30, 2008, 2:45am UTC](https://discuss.rubyonrails.org/t/role-based-restriction-for-fields-not-views-or-actions/27169/3 "2008-03-30T02:45:31Z")

</div>

Heh... I'm still learning.

The namespacing was unnecessary. Slap it right in the middle of the ApplicationHelper module, I.e.:

module ApplicationHelper &nbsp;&nbsp;def secured\_text\_field(object\_name, method, options = {}) &nbsp;&nbsp;&nbsp;&nbsp;options[:required] == "yes" ? content\_tag(:div, "#{object\_name.instance\_eval(method.to\_s)}", :id =\> "#{object\_name.class.to\_s.underscore}\_#{method}") : text\_field(object\_name, method, options) &nbsp;&nbsp;end end

There's probably a cleaner way to get "foo\_name" for the ID but the above works... and is terse and to the point.

-Danimal

---

<div class="post-metadata">

**Author:** ![Alberto\_Perdomo](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@Alberto\_Perdomo](https://discuss.rubyonrails.org/u/Alberto_Perdomo)\
**Post date:** [March 30, 2008, 1:26pm UTC](https://discuss.rubyonrails.org/t/role-based-restriction-for-fields-not-views-or-actions/27169/4 "2008-03-30T13:26:40Z")

</div>

Hi,

i think there is a ruby gem called model\_security which provides such fine grained access control from within the model. I don't know though if it's mantained and up to date and/or runs nicely on rails 2.

Hops that helps.

Cheers! Alberto.

---

<div class="post-metadata">

**Author:** ![Serge\_Chevarie-Pelle](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Serge\_Chevarie-Pelle](https://discuss.rubyonrails.org/u/Serge_Chevarie-Pelle)\
**Post date:** [March 31, 2008, 12:44pm UTC](https://discuss.rubyonrails.org/t/role-based-restriction-for-fields-not-views-or-actions/27169/5 "2008-03-31T12:44:09Z")

</div>

There's also the acl2 plugin from Ezra. [http://opensvn.csie.org/ezra/rails/plugins/dev/acl\_system2/](http://opensvn.csie.org/ezra/rails/plugins/dev/acl_system2/)

It has a view helper, \<% restrict\_for "role" do %\> which will let you only show what is part of the code block in the view.

Hope this helps.

Serge
