# New Plugin: RESTful\_Acl

**URL:** <https://www.ruby-forum.com/t/new-plugin-restful-acl/128545>\
**Category:** Rails\
**Created:** [February 8, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545 "2008-02-08T17:18:37Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [February 8, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/1 "2008-02-08T17:18:37Z")

</div>

I’d like to announce my very first plugin: RESTful\_Acl

=What it does=  
RESTful\_Acl is a simple access control layer, it allows you to restrict  
access on a fine-grained level to any RESTful MVC stack. While the ACL  
structure and engine are provided by this plugin, the implementation is  
fully up to the user. Every application is different and everyone likes  
to setup their User / Account / Role resources differently; this plugin  
will allow you to do your thing and keep that thing locked down.

The full write up and source can be found at:  
[http://code.google.com/p/restful-acl/](http://code.google.com/p/restful-acl/)

Thoughts and suggestions are welcome!

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 2, 2008, 12:43pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/2 "2008-03-02T12:43:48Z")

</div>

Thanks Matt!

It would be great if the instructions would cover a bit more how to use  
it. I’m just fighting with it.

undefined local variable or method `error\_url’ is the error I have now.  
And I only added before\_filter :has\_permission? to one controller. I’m  
not so experienced with RoR but anyway…

Regards

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 2, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/3 "2008-03-02T15:54:19Z")

</div>

> undefined local variable or method `error\_url’ is the error I have now.  
> And I only added before\_filter :has\_permission? to one controller. I’m  
> not so experienced with RoR but anyway…

It seems as though you’re using the first revision of RESTful\_ACL that  
expected a few named routes to exist. I’ve taken these requirements out  
in the latest versions, so if you update the plugin you should be ready  
to roll right away.

./script/plugin remove  
[http://restful-acl.googlecode.com/svn/tags/restful\_acl](http://restful-acl.googlecode.com/svn/tags/restful_acl)  
./script/plugin install  
[http://restful-acl.googlecode.com/svn/tags/restful\_acl](http://restful-acl.googlecode.com/svn/tags/restful_acl)

Hope this helps and thanks for checking out RESTful\_ACL!

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 5, 2008, 10:40am UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/4 "2008-03-05T10:40:11Z")

</div>

Hi,

As part of other topic you where asking for suggestions/comments about  
the plugin documentation.

This post: [Using roles vs namespaces for admin - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/144456#new)

I started looking at it but decided to put it away since I’m not so  
experienced.  
I hope you could make some example about how to use it and integrate it  
to a simple app.

The contents of the readme is pasted below (not much help there)

* * *

# RestfulAcl

Introduction goes here.

# Example

Example goes here.

* * *

Then the other piece of info is in google code:

> **[Google Code Archive - Long-term storage for Google Code Project Hosting.](https://code.google.com/archive/p/restful-acl)**

But again that’s for experienced people. What kind of user model is  
needed, role model? Again a simple example would be much more helpful.

> > undefined local variable or method `error\_url’  
> > Thas was the first problem I had. Besides not knowing how to get the  
> > plugin in the first place since you assume that people know how to add  
> > the repository before intalling. I didn’t so I had to ask again.

Still the error\_url was not fixed. Now based on some other info from the  
last days it might be that I just need to create a “error” route in the  
routes file and that’s what you are calling. But still it requires some  
further additions.

Part of your documentation is “and a smattering of mapped URLs in order  
to work.” Well, that’s something I would really ellaborate a “bit” more.

Of course you can target your plugin to people with lot of experience  
but I’m sure many people will look at the info available before deciding  
to try it out.

I guess this is some feedback already 🕶

Cheers!

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 5, 2008, 3:04pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/5 "2008-03-05T15:04:19Z")

</div>

Hi, excellent! I’ll give it a try and see how it goes after I finish  
some stuff. I’m learning every minute 🕶

Thanks.

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 5, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/6 "2008-03-05T15:00:00Z")

</div>

comopasta Gr wrote:

> Hi,
> 
> As part of other topic you where asking for suggestions/comments about  
> the plugin documentation.
> 
> This post: [Using roles vs namespaces for admin - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/144456#new)
> 
> I started looking at it but decided to put it away since I’m not so  
> experienced.  
> I hope you could make some example about how to use it and integrate it  
> to a simple app.
> 
> The contents of the readme is pasted below (not much help there)
> 
> * * *
> 
> # RestfulAcl
> 
> Introduction goes here.
> 
> # Example
> 
> Example goes here.
> 
> * * *
> 
> Then the other piece of info is in google code:  
> [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/restful-acl/)
> 
> But again that’s for experienced people. What kind of user model is  
> needed, role model? Again a simple example would be much more helpful.
> 
> > > undefined local variable or method `error\_url’  
> > > Thas was the first problem I had. Besides not knowing how to get the  
> > > plugin in the first place since you assume that people know how to add  
> > > the repository before intalling. I didn’t so I had to ask again.
> 
> Still the error\_url was not fixed. Now based on some other info from the  
> last days it might be that I just need to create a “error” route in the  
> routes file and that’s what you are calling. But still it requires some  
> further additions.
> 
> Part of your documentation is “and a smattering of mapped URLs in order  
> to work.” Well, that’s something I would really ellaborate a “bit” more.
> 
> Of course you can target your plugin to people with lot of experience  
> but I’m sure many people will look at the info available before deciding  
> to try it out.
> 
> I guess this is some feedback already 🕶
> 
> Cheers!

Good point about the readme file; I will update that with the contents  
from the Google repository.  
The write up says that this plugin expects that you are using the  
RESTful\_Authentication plugin; it creates the User model for you and  
also supplies other needed methods. All you need to do is add the  
before\_filter to the User controller and the four CRUD functions to the  
User model and RESTful\_ACL is ready to work. Just follow the steps in  
the Google write up and you’ll be up and running in seconds.

The write up now includes directions on how to add the required named  
routes to your routes.rb file.  
Experience is all relative; I’d only used RoR for a few months at the  
time of writing RESTful\_ACL 🙂

Thank you for feedback and I hope this will help!

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 29, 2008, 9:59am UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/7 "2008-03-29T09:59:46Z")

</div>

Hello,

Sorry if my question looks stupid but I am quite new with RoR, I am  
looking at an ACL plugin for my projects (I have already decided for  
Restful\_Authentication) and I have a question about restful\_acl.  
Let’s say that I have 2 roles : teachers and students and a note model  
teacher can create, update, view, delete his notes (note the one from  
an other teacher) and a student can only see his notes.

If I understood correctly in the note.rb (my model), I need to define  
the 4 methods described in the plugin documentation (is\_updatable, is  
deletable, …). But I am not sure how to write these method to  
restrict the access depending on the note I am "evaluating and not  
only the role?

def is\_updatable\_by(user) # note should be updatable  
only by the teacher who created it  
user.is\_teacher? and ???  
end

def self.is\_readable\_by(user, object = nil) # note should be  
readable only by all the teachers or by the student who has this note  
user.is\_student? or user.eql?(object.author)  
end

Thanks very much for your help.  
Additional question : why are some of the method “self.” and not the  
others?

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 29, 2008, 1:17pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/8 "2008-03-29T13:17:39Z")

</div>

On 29 Mar 2008, at 09:59, Tranquiliste wrote:

> def is\_updatable\_by(user) # note should be updatable  
> only by the teacher who created it  
> user.is\_teacher? and ???  
> end

Given you have a relationship  
belongs\_to :owner, :class\_name =\> “Teacher”, :foreign\_key =\> “owner\_id”

def is\_updatable\_by(user) # note should be updatable  
user.is\_teacher? and self.owner==user  
end

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 29, 2008, 7:02pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/9 "2008-03-29T19:02:56Z")

</div>

Thanks both of you for the answers. I am going to look at them.

Nciolas

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 29, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/10 "2008-03-29T13:41:17Z")

</div>

Tranquiliste wrote:

> def is\_updatable\_by(user) # note should be updatable  
> only by the teacher who created it  
> user.is\_teacher? and ???  
> end
> 
> def self.is\_readable\_by(user, object = nil) # note should be  
> readable only by all the teachers or by the student who has this note  
> user.is\_student? or user.eql?(object.author)  
> end
> 
> Thanks very much for your help.  
> Additional question : why are some of the method “self.” and not the  
> others?

Hi Tranquiliste, thanks for using RESTful\_ACL! To answer your questions:

To restrict editing of a Note to only the Teacher who created it I would  
do (in Note)

belongs\_to :author, :foreign\_key =\> ‘created\_by\_id’, :class\_name =\>  
‘Teacher’

def is\_updatable\_by(user)  
user.eql?(author)  
end

def self.is\_readable\_by(user, object = nil)  
user.is\_teacher? || object.student\_id == user.id  
end

When you say ‘note should be readable by the student who has this note’  
I’ve assumed that you’re linking each Note with a Student in some  
fashion.

As for your last question: the is\_readable\_by and is\_creatable\_by  
methods are class methods, meaning that they operate at the class level  
and not with just a single object. (Note instead of note, if you get my  
drift). That’s where the ‘self’ comes into play.

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 29, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/11 "2008-03-29T22:43:26Z")

</div>

Tranquiliste wrote:

> Hello,
> 
> Sorry to bother you again but what if several teachers can modify the  
> note and a teacher can modify several notes? (which means we have a  
> model note-owner?)
> 
> Tranquiliste a ï¿½crit :

In Note.rb:  
def is\_updatable\_by(user)  
user.eql?(author) || user.is\_a?(Teacher)  
end

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 29, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/12 "2008-03-29T22:39:34Z")

</div>

Hello,

Sorry to bother you again but what if several teachers can modify the  
note and a teacher can modify several notes? (which means we have a  
model note-owner?)

Tranquiliste a écrit :

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 30, 2008, 9:22am UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/13 "2008-03-30T09:22:15Z")

</div>

> ## In Note.rb: def is\_updatable\_by(user) user.eql?(author) || user.is\_a?(Teacher) end
> 
> Posted viahttp://www.ruby-forum.com/.

Thanks again, but in my case not all the teachers can modify the note,  
only those who have been authorized.

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 30, 2008, 11:26am UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/14 "2008-03-30T11:26:50Z")

</div>

On 30 Mar 2008, at 09:21, Tranquiliste wrote:

> > ## In Note.rb: def is\_updatable\_by(user) user.eql?(author) || user.is\_a?(Teacher) end
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> Thanks again, but in my case not all the teachers can modify the note,  
> only those who have been authorized.

A bit of common sense could take you a long way:

def is\_updatable\_by(user)  
user.eql?(author) || ( user.is\_a?(Teacher) && user.is\_authorized? )  
end

Replace the is\_authorized? method with something else if you want,  
maybe something that has a bit more logic behind it…

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [April 6, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/15 "2008-04-06T17:18:39Z")

</div>

Hello,

Me again, because I don’t understand why the is\_readable\_by method is  
defined as a class method. If I have an class Invoice and that someone  
ask to view an invoice it should call  
@invoice.is\_readable\_by(current\_user) and not with a generic  
Invoice.is\_readable\_by(current\_user) .

Would you mind explaining this choice?

Thanks very much  
Nicolas

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [March 30, 2008, 11:50am UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/16 "2008-03-30T11:50:02Z")

</div>

Sorry I am stupid, I have just understood something (I won’t tell you  
what because I will look even more stupid), and I think I am ready now  
to do what I want.

Thanks again for your help

Nicolas

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [April 6, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/new-plugin-restful-acl/128545/17 "2008-04-06T17:29:25Z")

</div>

Tranquiliste wrote:

> Hello,
> 
> Me again, because I don’t understand why the is\_readable\_by method is  
> defined as a class method. If I have an class Invoice and that someone  
> ask to view an invoice it should call  
> @invoice.is\_readable\_by(current\_user) and not with a generic  
> Invoice.is\_readable\_by(current\_user) .
> 
> Would you mind explaining this choice?
> 
> Thanks very much  
> Nicolas

Sure thing.

The is\_readable\_by() method pulls double duty. It is a class method as  
it supports the Object::index action, along with the Object::show  
action.

Since the index action usually deals with a collection of objects, it  
must be a class method. The method includes a parameter variable,  
object, that can represent a singular object when checking access on  
that singular object. This takes care of the Object::show action (that  
normally deals with just one object).

In your case, if you want to check access on a singular invoice object,  
you can do:  
Invoice.is\_readable\_by(current\_user, @invoice).

Hope this helps!
