acl icon indicating copy to clipboard operation
acl copied to clipboard

Feature: AclExtras Import AROs from another table

Open sarrala opened this issue 10 years ago • 3 comments

I wanted to import data from my users table into aros table and here's what I ended up with.

My idea is to generate ARO nodes based on another table, like Users or Roles or whatever. However, this probably requires some reviews and discussion about usefulness and possible problems with different requester designs.

For example, console command cake acl.aclExtras aro_update Users would generate something like:

$> cake acl.aclExtras aro_update Users
$> cake acl.acl view aro
Aro tree:
---------------------------------------------------------------
[1] Users
    [2] Users.1
    [3] Users.2
    [3] Users.3

Quick mockup that generates ARO nodes using supplied model name as data source:

Class Shell\AclExtrasShell

/**
 * Updates the Aco Tree with new controller actions.
 *
 * @return void
 **/
    public function aroUpdate()
    {
        $this->loadModel($this->args[0]);
        $this->AclExtras->aro_update( $this->{$this->args[0]} );
        return true;
    }

Class AclExtras

/**
 * Updates the Aro Tree with new requesters.
 *
 * @return void
 **/
    public function aro_update( $aro_import_model ) {
        // Get model information
        $import_aros = $aro_import_model->find('all');
        $pk = $aro_import_model->primaryKey();
        $alias = $aro_import_model->alias();
        // Get parent node
        $root_node = $this->Acl->Aro->find()->where(['alias' => $alias, 'model' => $alias, 'parent_id IS NULL'])->first();
        if (!$root_node) {
            $root_node = $this->Acl->Aro->newEntity([
                    'alias' => $alias,
                    'model' => $alias
            ]);
            $this->Acl->Aro->save( $root_node );
        }
        $parentId = $root_node->id;
        // Add AROs
        $aros = $this->Acl->Aro->find()->where(['model' => $alias, 'parent_id' => $parentId]);
        foreach ($import_aros as $import_aro) {
            foreach ($aros as $aro) {
                if ($aro->foreign_key == $import_aro->{$pk}) {
                    continue 2;
                }
            }
            $entity = $this->Acl->Aro->newEntity([
                    'model' => $alias,
                    'foreign_key' => $import_aro->{$pk},
                    'parent_id' => $parentId
            ]);
            $this->Acl->Aro->save( $entity );
        }
    }

sarrala avatar Jan 29 '15 11:01 sarrala

Would you be able to create a PR for this?

dakota avatar Mar 08 '16 11:03 dakota

Currently I don't have any Cake projects going on but I could still make a PR for this one. However, I don't have any tests or even plans on how this behavior should be tested and what the possible situations are that we should take into account.

Any feedback would be greatly appreciated and helpful if anyone have already used code snippets found from original post. I'll stress the word ANY, good or bad.

Also do not hold you breath while waiting for updates on this feature, it could be bad for your health.

sarrala avatar Aug 20 '16 13:08 sarrala

👍 Found this Code Snippet and it helped a lot.

makakken avatar Aug 07 '17 18:08 makakken