Created
April 9, 2012 22:39
-
-
Save interlock/2347115 to your computer and use it in GitHub Desktop.
ACL Group Permissions, kills parent_id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this is a snip from a Behavior I wrote that overrides afterSave(...) from the AclBehavior. | |
// This solves having multiple instances of your model in the ACO/ARO trees. I'm not really sure "what" | |
// the point of updating an ACO/ARO is in the $created == true case is unless you have defined a single "Group" | |
// inheritance as they show in the docs. Clearly that isn't ideal for some setups, but changing the current | |
// implementation could break a lot of sites (I actually would want to see a complex case that proves that, I'm only | |
// "told" that is the case). At any rate, in my case I'll take care of updating all the other ACO/ARO's that are not | |
// at the default parent node for the model. | |
// This appears to be a purposeful limitation to basically allow "grouping" but not multiple grouping. In theory | |
// (and what I think they want you to do is...) you could put groups in groups and users in grouped groups and | |
// things would work. That is the kind of line of thought makes ACL really hard to follow IMHO. | |
// The better solution here would be implement detection of "hasAndBelongsToMany" relationship within ACL. It could | |
// then iterate over the relationships and update (again, looks pointless to me) all of them with the appropriate | |
// parent_id. | |
<?php | |
public function afterSave(Model &$Model, $created) { | |
$types = $this->_typeMaps[$this->settings[$Model->name]['type']]; | |
if (!is_array($types)) { | |
$types = array($types); | |
} | |
foreach ($types as $type) { | |
$parent = $Model->parentNode(); | |
if (!empty($parent)) { | |
$parent = $this->node($Model, $parent, $type); | |
} | |
$data = array( | |
'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null, | |
'model' => $Model->name, | |
'foreign_key' => $Model->id | |
); | |
if (!$created) { | |
// added this to be very specific about which parent node we wanted | |
$find = array( $Model->name => array( | |
'id' => $Model->id, | |
'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null | |
)); | |
// lame loop | |
$node = $this->node($Model, $find, $type); | |
foreach($node as $_node) { | |
if ( $_node[$type]['parent_id'] == $parent[0][$type]['id'] ) { | |
$node = array($_node); | |
break; | |
} | |
} | |
$data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null; | |
} | |
$Model->{$type}->create(); | |
$Model->{$type}->save($data); | |
} | |
// remove the debugs of course |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment