07.04 php saved
josoroma
Tags add more
josoroma acl  
Note
user model
  1. <?php
  2. class User extends AppModel {
  3.  
  4.  
  5.     var $name            = 'User';
  6.  
  7.     var $actsAs    = array('Acl'=>'requester');
  8.  
  9.  
  10.     var $belongsTo = array(
  11.  
  12.             'Role' => array('className' => 'Role',
  13.                                 'foreignKey' => 'role_id',
  14.                                 'conditions' => '',
  15.                                 'fields' => '',
  16.                                 'order' => ''
  17.             )
  18.  
  19.     );
  20.  
  21.     var $validate = array(
  22.  
  23.         'name' => array(
  24.        
  25.             'minlength' => array(
  26.                 'rule' => array('minlength', 3),
  27.                 'required' => true,
  28.                 'message' => 'Usted debe ingresar al menos tres caracteres.'
  29.             )
  30.        
  31.         ),
  32.    
  33.         'email' => array(
  34.        
  35.             'email' => array(
  36.                 'rule' => 'email',
  37.                 'required' => true,
  38.                 'message' => 'Por favor, ingrese una dirección de correo electrónico válida.'
  39.             ),
  40.            
  41.             'isUnique' => array(
  42.                 'rule' => 'isUnique',
  43.                 'message' => 'Debe ser única, parece que la dirección de correo electrónico ya existe.'
  44.             )
  45.        
  46.         ),
  47.  
  48.         'username' => array(
  49.        
  50.             'alphaNumeric' => array(
  51.                 'rule' => 'alphaNumeric',
  52.                 'required' => true,
  53.                 'message' => 'Solo letras y números.'
  54.             ),
  55.  
  56.             'between' => array(
  57.                 'rule' => array('between', 5, 25),
  58.                 'message' => 'Debe tener entre 5 y 25 caracteres.'
  59.             ),
  60.            
  61.             'isUnique' => array(
  62.                 'rule' => 'isUnique',
  63.                 'message' => 'Debe ser único, nombre de persona usuaria ya existe.'
  64.             )
  65.        
  66.         ),
  67.  
  68.         'passwd' => array(
  69.  
  70.             'betweenCreate' => array(
  71.                 'rule' => array('between', 5, 20),
  72.                 'on' => 'create',
  73.                 'message' => 'Debe tener entre 5 y 20 caracteres de largo.'
  74.             ),
  75.  
  76.             'equalTo' => array(
  77.                 'rule' => array('equalTo', 'confirm_passwd' ),
  78.                 'on' => 'create',
  79.                 'message' => 'Por favor, ingrese nuevamente la misma contraseña en el campo de confirmar contraseña.'
  80.             ),
  81.        
  82.         ),
  83.  
  84.         'new_passwd' => array(
  85.  
  86.             'betweenCreate' => array(
  87.                 'rule' => array('between', 5, 20),
  88.                 'on' => 'update',
  89.                 'allowEmpty' => true,
  90.                 'message' => 'Debe tener entre 5 y 20 caracteres de largo.'
  91.             )
  92.        
  93.         )
  94.    
  95.     );
  96.  
  97.  
  98.     /**
  99.      * Allows the AclBehavior to determine parental ownership of
  100.      * currently active record.
  101.      *
  102.      * @access public
  103.      * @returns array data array to be used by AclBehavior for node lookup
  104.      */
  105.     function parentNode(){
  106.  
  107.         if (!$this->id) {
  108.             return null;
  109.         }
  110.  
  111.         $data = $this->read();
  112.  
  113.         if (!$data['User']['parent_id']){
  114.  
  115.             return null;
  116.  
  117.         } else {
  118.  
  119.             return array('model' => 'Role', 'foreign_key' => $data['User']['role_id']);
  120.  
  121.         }
  122.  
  123.     }
  124.  
  125.  
  126.  
  127.     function beforeSave(){
  128.  
  129.         $this->setNewPassword();
  130.  
  131.         return true;
  132.  
  133.     }
  134.  
  135.  
  136.  
  137.     function afterSave($created = null){
  138.  
  139.         if( $created ){
  140.  
  141.             $this->id = $this->getLastInsertId();
  142.  
  143.             $userAliasAndRole   = array();
  144.             $userAliasAndRole   = $this->find('first', array( 'conditions' => array('User.id' => $this->id), 'fields' => array('username','role_id')));
  145.  
  146.             // Primero se crea el alias para el nuevo Aro.
  147.             // AclBehavior no crea un alias de manera automática.
  148.  
  149.             $this->__createAroAlias($userAliasAndRole);
  150.  
  151.         } else {
  152.  
  153.             $this->__updateAclRole();
  154.  
  155.         }
  156.  
  157.         return TRUE;
  158.  
  159.     }
  160.  
  161.  
  162.  
  163.     function __createAroAlias($userAliasAndRole=null) {
  164.  
  165.         $aroId = $this->Aro->getLastInsertId();
  166.         $this->Aro->create();
  167.         $this->Aro->id = $aroId;
  168.  
  169.         $aroAliasAndRole                        = array();
  170.         $aroAliasAndRole['Aro']['alias']       = $userAliasAndRole['User']['username'];
  171.         $aroAliasAndRole['Aro']['parent_id']    = $userAliasAndRole['User']['role_id'];
  172.  
  173.         if($this->Aro->save($aroAliasAndRole)){
  174.  
  175.             return TRUE;
  176.  
  177.         } else {
  178.  
  179.             return FALSE;
  180.  
  181.         }
  182.     }
  183.  
  184.    
  185.    
  186.     /**
  187.      * When the parent_id has changed, then need to also change the parent_id field in the
  188.      * matching Aro row.
  189.      *
  190.      * @access private
  191.      */
  192.     function __updateAclRole(){
  193.  
  194.  
  195.         if ($this->data['User']['role_id'] != $this->data['User']['old_role_id']){
  196.  
  197.             $roleInfo   = $this->Aro->find('first', array( 'conditions' => array('foreign_key' => $this->data['User']['role_id'], 'model' => 'Role'), 'fields' => array('id')));
  198.             $userAro    = $this->Aro->find('first', array( 'conditions' => array('foreign_key' => $this->id, 'model' => 'User'), 'fields' => array('id')));
  199.  
  200.             $updatedAro = array(
  201.                 'Aro' => array(
  202.                     'id'        => $userAro['Aro']['id'],
  203.                     'parent_id' => $roleInfo['Aro']['id'],
  204.                     'alias'  => $this->data['User']['username']
  205.                     )
  206.                 );
  207.  
  208.             $this->Aro->save($updatedAro);
  209.  
  210.         } else {
  211.  
  212.             $updatedAro = array(
  213.                 'Aro' => array(
  214.                     'id'        => $this->id,
  215.                     'parent_id' => $this->data['User']['role_id'],
  216.                     'alias'  => $this->data['User']['username']
  217.                     )
  218.                 );
  219.  
  220.             $this->Aro->save($updatedAro);
  221.  
  222.         }
  223.  
  224.     }
  225.  
  226.  
  227.  
  228.     /**
  229.      * sets the password to be equal to the verified value from the temporary password field
  230.      *
  231.      * Under AuthComponent, any time a form is submitted with a field name that matches the
  232.      * expected password field, it is hashed before any other operation can be done.  This
  233.      * prevents the equalTo() rule check from working, so we take the password in a form input
  234.      * named something else.  Then after verification, but before saving the record, we pass
  235.      * the hashed value to the correct password field.
  236.      *
  237.      * @return boolean TRUE
  238.      */
  239.     function setNewPassword(){
  240.  
  241.         if (!empty( $this->data['User']['new_passwd_hash'])){
  242.  
  243.             $this->data['User']['passwd'] = $this->data['User']['new_passwd_hash'];
  244.  
  245.         }
  246.  
  247.         return TRUE;
  248.  
  249.     }
  250.  
  251.  
  252.  
  253.     /**
  254.      * Overrides core equalTo() to verify that two form fields are equal
  255.      *
  256.      * @param array $field contains the name of the primary field and the value of that field
  257.      * @param string $compare_field contains the name of the field to compare the primary field to
  258.      * @access public
  259.      * @return boolean FALSE if the fields do not match TRUE if they do
  260.      */
  261.     function equalTo($field=array(), $compare_field=null){
  262.  
  263.         foreach ($field as $key => $value){
  264.  
  265.             $v1 = $value;
  266.  
  267.             $v2 = $this->data[$this->name][$compare_field];
  268.  
  269.             if ($v1 !== $v2) {
  270.  
  271.                 return FALSE;
  272.  
  273.             } else {
  274.  
  275.                continue;
  276.  
  277.             }
  278.         }
  279.  
  280.         return TRUE;
  281.  
  282.     }
  283.  
  284.  
  285.  
  286. }
  287. ?>
Parsed in 0.394 seconds, using GeSHi 1.0.7.14

Modify this Paste