CakePHP Rebuild and Generate the AROS table

I was in a situation where I needed to rebuild the CakePHP ACL AROS table. There are scripts available to rebuild the ACOS table and the AROS_ACOS table, but I could not find anything to rebuild the AROS table. So, I wrote one. It's not anything spectacular but it works.

 

First, make sure you have the Aor and Group models available and make sure the new action is available. Like this:

  1. var $name = 'Users';
  2. var $uses = array('User','Aro','Group');
  3.  
  4. function beforeFilter() {
  5. parent::beforeFilter();
  6. // $this->Auth->allow(array('login','logout'));
  7. $this->Auth->allow(array('login','logout','rebuildARO'));
  8. }

Now, add in the function to your users controller. This function will rebuild AROS for the groups and then the users.

  1. function rebuildARO() {
  2. // Build the groups.
  3. $groups = $this->Group->find('all');
  4. $aro = new Aro();
  5. foreach($groups as $group) {
  6. $aro->create();
  7. $aro->save(array(
  8. // 'alias'=>$group['Group']['name'],
  9. 'foreign_key' => $group['Group']['id'],
  10. 'model'=>'Group',
  11. 'parent_id' => null
  12. ));
  13. }
  14.  
  15. // Build the users.
  16. $users = $this->User->find('all');
  17. $i=0;
  18. foreach($users as $user) {
  19. $aroList[$i++]= array(
  20. // 'alias' => $user['User']['email'],
  21. 'foreign_key' => $user['User']['id'],
  22. 'model' => 'User',
  23. 'parent_id' => $user['User']['group_id']
  24. );
  25. }
  26. foreach($aroList as $data) {
  27. $aro->create();
  28. $aro->save($data);
  29. }
  30.  
  31. echo "AROs rebuilt!";
  32. }

Now, load the /users/rebuildaro/ page and that should do it!

Special thanks go our to this site: http://www.edwardawebb.com/web-development/cakephp/started-acl-cakephp

Please let me know if this helped you or if you have a better solution.

Comments

Posted by Dave on
Perfect...
Posted by Tim on
Thanks. This was just what I was looking for!
Posted by William on
Thank for sharing. This is just what I need.
My approach is different, I am not put it in the controller but instead in a shell.
Posted by Peter on
Looks great, but nothing happens on the Aros table or on the Aros tree (cake acl view aro) and if I empty Aros table and run this, the table remains empty ...

What am I doing wrong?

Thanks!!

captcha.... uff
Posted by Peter on
Im sorry.. works fine!!
Excellent!!!
Thanks a lot!
Posted by Arvind on
Had been finding it for ages!! You helped me a lot!!
Posted by pex on
why the parent_id of users is group_id?
Posted by admin on
To be honest I don't remember exactly how it works. I think, in the group based permissions system, the group is the parent of the users. These links might have better info:

http://stackoverflow.com/questions/6180199/please-explain-cakephp-acl-parent-id-and-foreign-key
http://code.tutsplus.com/tutorials/how-to-use-cakephps-access-control-lists--net-13457
Posted by Andrew on
I think the parent_id should be set to the id of the parent in the aro table, not to the id of the group from the group table, as is done in this line:
'parent_id' => $user['User']['group_id']

The example works only as long as the group ids are numbered sequentially from 1 (so that the group id and aro id are matching)...
Posted by admin on
Good point! Thank you for posting!
Leave a Reply



(Your email will not be publicly displayed.)


Captcha Code

Click the image to see another captcha.