Skip to content

Commit

Permalink
Fixed Update AROs
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanamat committed Apr 11, 2017
1 parent 8762864 commit 9013d2c
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/Controller/Component/AclManagerComponent.php
Expand Up @@ -32,6 +32,7 @@
use Cake\Controller\ComponentRegistry;
use Cake\Core\Configure;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;

class AclManagerComponent extends Component {

Expand Down Expand Up @@ -71,13 +72,22 @@ public function arosBuilder() {
$parent = null;

$models = Configure::read('AclManager.aros');
foreach ($models as $model) {
// foreach ($models as $model) {
for($i = 0; $i < count($models); $i++) {
$model = $models[$i];
$this->{$model} = TableRegistry::get($model);

// Build the roles.
$items = $this->{$model}->find('all');
foreach($items as $item) {
$arrayItem = $item->toArray();
if($i > 0 && isset($models[$i-1])) {
$pk = strtolower(Inflector::singularize($models[$i-1])).'_id';
$parent = $this->Aro->find('all',
['conditions' => [
'model' => $models[$i-1],
'foreign_key' => $item->{$pk}
]])->first();
}

// Prepare alias
$alias = null;
Expand All @@ -91,21 +101,13 @@ public function arosBuilder() {

// Create aro
$aro = new Aro([
'alias'=>$alias,
'alias' => $alias,
'foreign_key' => $item->id,
'model'=>$this->{$model}->alias(),
'parent_id' => (isset($parent->id)) ? $parent->id : null
'model' => $model,
'parent_id' => (isset($parent->id)) ? $parent->id : Null
]);

// If aro not exist and aro saved store the parent
if($this->__findAro($aro) == 0 && $this->Acl->Aro->save($aro)) {
if($counter < (count($models)-1)) {
$parent = $this->Aro->find('all',
['conditions' => [
'model' => $model,
'foreign_key' => $arrayItem["id"]
]])->first();
}
$counter++;
}
}
Expand Down Expand Up @@ -145,11 +147,20 @@ public function checkNodeOrSave($path, $alias, $parentId = null) {
**/
private function __findAro($aro) {

$conditions = [
'alias' => $aro->alias,
'foreign_key' => $aro->foreign_key,
'model' => $aro->model
];

if(isset($aro->parent_id)) {
$conditions = [
'parent_id' => $aro->parent_id,
'foreign_key' => $aro->foreign_key,
'model' => $aro->model
];
} else {
$conditions = [
'parent_id IS NULL',
'foreign_key' => $aro->foreign_key,
'model' => $aro->model
];
}

return $this->Acl->Aro->find('all', [
'conditions' => $conditions,
Expand Down

0 comments on commit 9013d2c

Please sign in to comment.