[ Index ]

PHP Cross Reference of ACL Module

title

Body

[close]

/models/behaviors/ -> RequestingActiveRecordBehavior.php (source)

   1  <?php
   2  
   3  /**

   4   * RequestingActiveRecordBehavior Class File

   5   *

   6   * This class serves as the behavior to be used for all "actors" in the program who have 

   7   * to per form actions on other objects 

   8   * 

   9   * @author dispy <dispyfree@googlemail.com>

  10   * @license LGPLv2

  11   * @package acl.base

  12   */
  13  class RequestingActiveRecordBehavior extends CActiveRecordBehavior{
  14      
  15      /**

  16       * Serves as a temporary space for the associated Aro-Object

  17       * @var AclObject 

  18       */
  19      protected $aro = NULL;
  20         
  21      /**

  22       * Loads the associated Aro_Object

  23       * @throws RuntimeException 

  24       */
  25      protected function loadAro(){
  26          $class = Strategy::getClass('Aro');
  27          
  28          $owner = $this->getOwner();
  29          
  30          if($this->aro === NULL){
  31              $this->aro = $class::model()->find('model = :model AND foreign_key = :foreign_key', 
  32                      array(':model' => get_class($owner), 'foreign_key' => $owner->id));
  33              
  34              //If there's no such Aro-Collection... use Guest ^^

  35              $guest = Strategy::get('guestGroup');
  36              if(!$this->aro && $guest){
  37                  $this->aro = $class::model()->find('alias = :alias', array(':alias' => $guest));
  38                  
  39                  //If there's no guest...

  40                  if(!$this->aro)
  41                      throw new RuntimeException('There is no associated Aro nor a guest-group');
  42              }
  43          }
  44      }
  45      
  46      /**

  47       * Looks up if the user is granted a specific action to the given object

  48       * @param   string|array    $obj    The object to be checked   

  49       * @param   string          $action the action to be performed

  50       * @return bool true if access is granted, false otherwise

  51       */
  52      public function may($obj, $action){
  53          $this->loadAro();
  54          return $this->aro->may($obj, $action);
  55      }
  56      
  57      /**

  58       * Grants the object denoted by the $obj-identifier the given actions

  59       * @param type $obj the object identifier

  60       * @param array $actions        the actions to grant

  61       * @param bool  $byPassCheck    Whether to bypass the additional grant-check

  62       * @return bool 

  63       */
  64      public function grant($obj, $actions, $byPassCheck = false){
  65          $this->loadAro();
  66          return $this->aro->grant($obj, $actions, $byPassCheck);
  67      }
  68      
  69      /**

  70       * Denies the object denoted by the $obj-identifier the given actions

  71       * @param type $obj the object identifier

  72       * @param array $actions the actions to deny

  73       * @return bool 

  74       */
  75      public function deny($obj, $actions){
  76          $this->loadAro();
  77          return $this->aro->deny($obj, $actions);
  78      }
  79      
  80      /**

  81       * This method takes care to associate an ARO-collection with this one

  82       * 

  83       * @param CEvent $evt 

  84       */
  85      public function afterSave($event){
  86          $owner = $this->getOwner();
  87          if($owner->isNewRecord){
  88              $class = Strategy::getClass('Aro');
  89              $aro = new $class();
  90              $aro->model = get_class($owner);
  91              $aro->foreign_key = $owner->getPrimaryKey();
  92              if(!$aro->save())
  93                  throw new RuntimeError("Unable to save Aro-Collection");
  94          }
  95      }
  96      
  97      /**

  98       * This method takes care that every associated ACL-objects are properly removed

  99       */
 100      public function beforeDelete($event){
 101          $owner = $this->getOwner();
 102          //Ok he has the right to do that - remove all the ACL-objects associated with this object

 103          $class = Strategy::getClass('Aro');
 104          $aro = $class::model()->find('model = :model AND foreign_key = :key', array(':model' => get_class($owner), ':key' => $owner->id));
 105          
 106          if(!$aro)
 107              throw new RuntimeException('No associated Aro-Collection!');
 108          
 109          $transaction = Yii::app()->db->beginTransaction();
 110          try{
 111              $suc =$aro->delete()&& parent::beforeDelete();
 112              $transaction->commit();
 113              return $suc;
 114          }
 115          catch(Exception $e){
 116              $transaction->rollback();
 117              throw $e;
 118          }
 119      }
 120      
 121  }
 122  ?>


Generated: Sun Jul 1 19:24:45 2012 Cross-referenced by PHPXref 0.7.1