Update ARO alias on save

by carbontwelve

After much searching through google on how to set the alias of my ARO's using parentNode only to eventually figure out that it was a stupid question I began to wonder why the default AclBehavior does not set the ARO's alias on save, nor update on edit.

This is my solution.

If you look into the default AclBehavior here you can see around line 98 to 100 that it saves all other fields relating to the ARO except for its alias. While there are countless ways around this, I quite like the fact that the AclBehavior nicely ties in with Auth without too much extra code. Therefore to fix this I decided to write my own extension to the AclBehavior that could be simply inserted into a Model Class to fix the issue.

What I have written could quite easily be converted into its own behavior but I will leave that as a task for the reader. Below is my solution (it must be put in the model, I use it in both my Users and Groups models:




    /* ACL afterSave Fix, as the default acl-behavior does not save an aro's alias
     * I have created this function which can be dropped into ANY model. It will 
     * hook on after the acl-behavior has finished and set the correct alias 
     * depending on model used. No modification required.
     * 
     * Requires: displayField and name be set in model!
     *
     * Created: 26th May 2011
     * Author : Simon Dann
     * Version: 1.0.0
     */
    function afterSave(){
        
        $saveAro = false;
    
        if ($this->getLastInsertID()){
            $saveAro = true;
            $insertId = $this->getLastInsertID();
        }else{
            if ($this->data[$this->name]['id']){
                $saveAro = true;
                $insertId = $this->data[$this->name]['id'];
            }
        }
        if ($saveAro == true){
                $aroRecord = $this->Aro->find('first', array('conditions' => array('foreign_key' => $insertId, 'model' => $this->name)));
                $aroRecord['Aro']['alias'] = $this->name . '::' . $this->data[$this->name][$this->displayField];
                $this->Aro->save($aroRecord);
        }
    }

Report

More on Snippets

Advertising

Comments

login to post a comment.