[ Index ]

PHP Cross Reference of ACL Module

title

Body

[close]

/models/ -> RequestingActiveRecord.php (source)

   1  <?php
   2  
   3  /**

   4   * RequestingActiveRecord Class File

   5   *

   6   * This class serves as the base class 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 RequestingActiveRecord extends CActiveRecord{
  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          if($this->aro === NULL){
  29              $this->aro = $class::model()->find('model = :model AND foreign_key = :foreign_key', 
  30                      array(':model' => get_class($this), 'foreign_key' => $this->id));
  31              
  32              //If there's no such Aro-Collection... use Guest ^^

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

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

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

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

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

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

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

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

  57       * @param type $obj the object identifier

  58       * @param array $actions the actions to grant

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

  60       * @return bool 

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

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

  69       * @param type $obj the object identifier

  70       * @param array $actions the actions to deny

  71       * @return bool 

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

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

  80       * 

  81       * @param CEvent $evt 

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

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

  97       */
  98      public function beforeDelete(){
  99          //Ok he has the right to do that - remove all the ACL-objects associated with this object

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


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