| CakePHP Official Site · Rules and Regulations |
Help
Search
Members
Calendar
Shoutbox
|
| Welcome Guest ( Log In | Register ) | Resend Validation Email |
| Pages: (3) [1] 2 3 ( Go to first unread post ) | ![]() ![]() ![]() |
| francky06l |
Posted: Jul 19 2008, 03:13 PM
|
||||||||||||||||||||
|
Advanced Member ![]() ![]() ![]() Group: Super Moderator Posts: 457 Member No.: 29 Joined: 4-November 07 |
Hi All,
I am back on Auth/ACL after a while. I have seen many posts on google groups and many questions about how to manage permissions for user belonging to multiple roles/groups. Actually it's quite simple using ACL and Auth. There is just a small extension to do on Auth and understand how ACL works. Goal
I will not be as "slow" (I have seen comment on this) as in the previous tutorial, so I suggest, for the beginners to start with tutorial one. Note for people wanted users belonging to multiple groups, they should just replace the word "Role" by "Group" anywhere in the project. This sample, takes most of the previous code as starting point. We start with the models, we will get 3
User model
Notes
Nothing else to add on models, for people preferring "Group" the can adjust the model names and fields accordingly. For the controllers, the special methods to mention are : UsersController
The original Auth can check our FirstRole because it's the parent Aro of our User Aro. Now since an Aro can have only one parent, we need to find a solution for checking the others. I has the idea of all this when looking at the node function of the db_acl, there is a way to retrieve node(s), by using this syntax :
Auth is doing this check for the User with it's FirstRole
This will check that the FirstRole of the user have the right to access the controller/action. Now for the others role, we just need to check :
Very simple isn't ? However Auth does not allow to make this so easily, no problem I created a component derive from Auth that I call AuthExt (Auth extended). Here is the code :
Notes The AuhtExt uses 2 member variables call parentModel and fieldKey. In our case, for this sample, this is "Role" and "role_id". This can be turned in anything you fancy. The login method is overriden, and does the following:
This is the sample appController
I have attached the complete project. To run it : - create your database using the aclrole.sql. Adjust you database name, user, password in /config/database.php accordingly. - the default database comes with only one user "admin" (password: admin). - the admin user, has also the role "ItManager". The ItManager role has only the right to "view users", and the admin does not. - The first test would be to log as an admin, and try to view a user (that should succeed). Then go to ACL menu and "deny" the user view on the ItManager role. Try to view a user, you should be redirected. Well that's about it, not so complex in fact. Comments and remarks are welcome. Franck Attached File ( Number of downloads: 1315 )
aclrole.zip |
||||||||||||||||||||
| polakiran |
Posted: Jul 20 2008, 05:46 PM
|
|
Newbie ![]() Group: Members Posts: 7 Member No.: 554 Joined: 18-July 08 |
Thank you for your support....
Actually my proj requirement is Users will be assigned to roles roles will have set of perms Users will be added to groups. ex: kiran added to sales group or finance group. But groups will not have permissions. It is just to group logically. It is possible. If possible can u pls extend your support. Thank you in advance |
| francky06l |
Posted: Jul 20 2008, 05:53 PM
|
||
|
Advanced Member ![]() ![]() ![]() Group: Super Moderator Posts: 457 Member No.: 29 Joined: 4-November 07 |
This is exactly what the tutorial does. Now if you user belongs to a group, logical group, it's just a User belongsTo Group property, nothing to do with permissions... You would have a group_id in the User model (unless user can belongs to many groups), but this a nothing to do with permission, it's more an organization/display views story.. |
||
| penfold_99 |
Posted: Aug 11 2008, 09:08 PM
|
|
Newbie ![]() Group: Members Posts: 3 Member No.: 531 Joined: 9-July 08 |
Hi Franck
Great tutorial and I have been able to get up and running straight away. I have a couple of questions. I have Users, Editors, Admins In this scenario users can view and create posts. Editors have view, create and edit. Admins have view, create, edit, delete. I would like to be able to give users edit rights over their own records only not all records. Does your demo support this already? Would it be possible to integrate groups of users, so an editor role can only edit records belonging to users within the group? Thanks |
| francky06l |
Posted: Aug 14 2008, 04:52 PM
|
|
Advanced Member ![]() ![]() ![]() Group: Super Moderator Posts: 457 Member No.: 29 Joined: 4-November 07 |
The demo does not support this, however it's easy to implement. I suppose you already have the user_id in the post (the creator), so you could check in case of user if the user_id is matching the current user.
For group of users, if you mean having "groups" it's a bit has "roles". But in such case you have to manage the groups also (no action permission at this level). I suppose then the easiest would to check the current group of the user against the group of the user that did create the post. There is many way to implement this, some easier and more open as other one. Of course you could also use the ACL in "model" / "crud" mode to manage all the rights on records (create an ACO for each record) and make a call to ACL->check in the actions. |
| penfold_99 |
Posted: Sep 19 2008, 03:42 PM
|
|
Newbie ![]() Group: Members Posts: 3 Member No.: 531 Joined: 9-July 08 |
Hi Francky06l
Is there any real need to have first role and additional roles? couldn't first role and additional roles be group together as roles? |
| francky06l |
Posted: Sep 23 2008, 09:28 AM
|
|
Advanced Member ![]() ![]() ![]() Group: Super Moderator Posts: 457 Member No.: 29 Joined: 4-November 07 |
Hi penfold,
No actually you could avoid the primary role. I did in fact for several reasons: - you would set primary role as the most used - that would let Auth doing most of the work - the search for the ACL is more costive when having multiple roles But again, it's easy to implement. Feel free to post your solution .. cheers |
| fly2279 |
Posted: Mar 2 2009, 01:18 PM
|
||
|
Newbie ![]() Group: Members Posts: 1 Member No.: 1092 Joined: 2-March 09 |
Great tutorial. ACL is hard for me to wrap my head around and it's taking a few days to get it. I downloaded the code and ran with a test database to try it out. It all works great except when I try to add a new role I get a few errors. The role is getting added to the aros table and I can assign the new role to acos using adjustperms. Why is there an error here?
|
||
| karsten_l |
Posted: Mar 18 2009, 02:29 PM
|
|
Newbie ![]() Group: Members Posts: 1 Member No.: 1134 Joined: 18-March 09 |
Hi francky06l,
I like this solution, but I had a little problem... There are some functions in my app_controller, for example some filter-functions. I use them from all my controllers. This works fine without your roles-handling. If I use your solution, I can "allow" the controller-funtions, but not the functions in the app_controller. I think your component should look (when valid==false) if the function in the app_controller is "allowed". Thanks for any tips to solve this. best regards Karsten |
| francky06l |
Posted: Mar 24 2009, 07:34 AM
|
|
Advanced Member ![]() ![]() ![]() Group: Super Moderator Posts: 457 Member No.: 29 Joined: 4-November 07 |
I did something to fix this problem, I really have to update the code (but really busy).
Another easy solution would be to set a function in controller and call the parent function. Thus you can "allow/deny" the controller function and authorize the app_controller one. cheers |
| francky06l |
Posted: Mar 24 2009, 05:02 PM
|
|
Advanced Member ![]() ![]() ![]() Group: Super Moderator Posts: 457 Member No.: 29 Joined: 4-November 07 |
Sorry for the late answer on this:
Notice (8): Undefined index: Aro [CORE/cake/libs/model/behaviors/tree.php, line 167] Warning (2): array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object [CORE/cake/libs/model/behaviors/tree.php, line 167] Warning (2): Cannot modify header information - headers already sent by (output started at /mydir/cake/basics.php:111) [CORE/cake/libs/controller/controller.php, line 615] You have to run the cleanAcl method, I think that should fix. I can add roles without problems |
| double07 |
Posted: Apr 15 2009, 03:24 AM
|
|
Member ![]() ![]() Group: Members Posts: 15 Member No.: 228 Joined: 19-February 08 |
[QUOTE=francky06l,Mar 24 2009, 05:02 PM] Sorry for the late answer on this:
Notice (8): Undefined index: Aro [CORE/cake/libs/model/behaviors/tree.php, line 167] Warning (2): array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object [CORE/cake/libs/model/behaviors/tree.php, line 167] Warning (2): Cannot modify header information - headers already sent by (output started at /mydir/cake/basics.php:111) [CORE/cake/libs/controller/controller.php, line 615] I get this error too but only in debug mode and it doesn't seem to affect anything. When debug mode is set to zero, there's no problem. |
| francky06l |
Posted: Apr 17 2009, 09:20 AM
|
|
Advanced Member ![]() ![]() ![]() Group: Super Moderator Posts: 457 Member No.: 29 Joined: 4-November 07 |
Having debug to 0 suppresses all the output messages, but the problem is there. It seems to be in the core tree behavior.
What version are you running ? Maybe give a try with the nightly build .. Cheers |
| 3pling |
Posted: Apr 23 2009, 02:40 PM
|
||||||||
|
Newbie ![]() Group: Members Posts: 4 Member No.: 1208 Joined: 17-April 09 |
Change in the add function of the roles_controller.php this rule:
to this:
And the error is gone.. |
||||||||
| senseBOP |
|
||||||
|
Newbie ![]() Group: Members Posts: 5 Member No.: 1267 Joined: 11-May 09 |
Hey everyone, I'm trying to customize this code to work with UUID's, as my app is already set up with UUID's instead of auto-incremented ID's, but am having a hard time. The user gets authenticated just fine (because the AuthExt doesn't care what data you give it about the user), but then, the authorization step fails as it tries to compare user_id to a column name instead of a string. I.e. Say your UUID is X1X2X3X4X5X... You'd think that Cake will do this:
But instead it does this:
Which of course fails with this error:
I tried setting up the associations for the User and Role models myself (instead of just counting on Cake to do everything) but the results remain the same. Any ideas anyone? Thanks! |
||||||
Pages:
(3) [1] 2 3 |
![]() ![]() ![]() |