-
Notifications
You must be signed in to change notification settings - Fork 782
Authorizing access to specific fields on a model #154
Comments
I'm very interested in this feature too. |
Me too. |
One solution is to use a custom action, see this wiki page for details. Another solution is to use the trusted-params plugin, but that is not yet Rails 3 compatible. It also does some ugly overriding of internal Rails methods and all hashes, which is not something I want built into CanCan. I agree neither of these are ideal and don't flow with CanCan's handling of permissions. Does anyone have some other ideas on better ways to do this through CanCan? |
Yes, overriding ActiveRecord methods is not the best way. Now I use solution like this: Controller:
Ability:
Where User#toucheble_parameters is a list of the subjects parameters that can be touched by the user. May be better to have just methods like can/can? but without actions? Example:
I think we don't need actions for checking attributes. But if somebody needs it, he can use the first solution. |
dapi, Excellent comments here. I like the technique you're using in I'm still thinking about situations like this:
In these examples, I need to control who can
I think you're on to something with your |
dapi, I'm trying to come up with a good combination of cancan Custom Actions and the techniques you described above... Keeping everything defined in Ability.rb is very attractive to me. Let's keep poking at this and see if can come up with something :) |
What if can :manage, Article, :content, :user_id => user.id And similar for can? :update, @article, :content In both cases the last symbol is optional, and if not present it will represent all attributes which is the current behavior. The check for each attribute would happen automatically in This conflicts a little with the existing API but let's not worry about that when brainstorming. |
Yes. Something like this. ViewsTo check parameters that are not permitted to update. Don't show it in the form.
or
AbilitiesAdmin can update any attributes:
Owner can update any attributes except
or
or just
ControllerRaise an exception if the user has no the ability or if he try to update banned attributes.
Raise an exception if the user has no any declared abilities to update the
And the hit of brainstorming: If he has some abilities then remove banned attributes silently.
|
Perhaps the can :update, Article cannot :update, Article, :banned Also silently removing attributes with Everything else looks good though. I'll definitely consider this in a major update to CanCan. |
dapi, this looks great. A couple comments. AbilitiesYou use the hash key If we use with
Seems unlikely, but it shouldn't be overlooked entirely. ControllerFirst, a little error in your code. You're authorizing I think we'd want to use
Moving on to your |
Ryan, Ah, I was likely writing my previous comment the same time as you. I didn't see your comment until after I posted. Handling dapi, to be more explicit, I think this is what Ryan is going for.
This brings up another idea. What if the attributes array contains associations or nested attributes? Would something like this work?
I also agree that raising an exception is good for the default behavior. If desired, perhaps a flag could be passed to |
@macek, nested attributes partially work since they are considered an attribute on the model. However, if you want to control access to specific, deeply nested attributes then that will be a problem. This happens outside of the controller, so there's no way to know that Actually nested attributes are a problem with CanCan's current behavior as well, but I haven't heard any complaints. I'm guessing they just work around it with before filters. Regarding |
Just some more brainstorming ideas. It will be tedious to perform the |
Ryan, I agree with you, it's better to raise an authorization error when user tries to update secret fields. About Formtastic. It's very good idea.
or
In this case |
dapi, I'm pretty sure most people won't be using the |
@ryanb I guess you already know but rails 3(activerecord) has a mass assignment security module, cancan should take advantage of this and i think trusted params could be reworked to take advantage of this. |
@rbritom, wow that's awesome, I did not know that. Time to investigate and maybe I'll do a Railscasts episode on it. |
@ryanb =D I said is part of activerecord but is really part of active model so it can be applied to classes that dont have a DB backing them up. |
Will be nice to have something like |
I'll be moving forward with this soon in a new 2.0 branch. Please see issue #191 for a good overview of the different changes planned for 2.0 and provide your feedback there. |
Awesome. I'm super excited for this :) |
Closing because this is now in the 2.0 branch. It may not be completely polished and fully working yet, but the functionality is there. |
Hi, what happened with this? it seems to be abandoned, any new? |
@ssoulless use CanCanCan |
reference new PR: CanCanCommunity/cancancan#474 |
Cancan is my favorite authorization plugin for Rails but I seem to find difficulty when getting down to the nitty gritty. How would we go about role-based permissions for specific fields on any given model?
For example, say sales and customer service staff can edit the address fields on a user, but only admin or sales can update a user's customer number. Loading the user resource with cancan is no problem, but the field-based permission logic in the views get very ugly very quickly.
This seems to be a very common issue I see people running into (myself included), but I'm never quite sure how to address it in the best way. If possible, I'd like to get a discussion going on this topic.
Note: If it's not in cancan's scope, I completely understand closing this issue.
The text was updated successfully, but these errors were encountered: