cakephp / acl

Plugin for managing ACL in CakePHP applications.
Other
110 stars 91 forks source link

Feature: AclExtras Import AROs from another table #42

Open sarrala opened 9 years ago

sarrala commented 9 years ago

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 );
        }
    }
dakota commented 8 years ago

Would you be able to create a PR for this?

sarrala commented 8 years ago

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.

makakken commented 7 years ago

👍 Found this Code Snippet and it helped a lot.