ACL Extension  0.3
 All Data Structures Namespaces Files Functions Variables
RequestingActiveRecord.php
Go to the documentation of this file.
1 <?php
2 
13 class RequestingActiveRecord extends CActiveRecord{
14 
19  protected $aro = NULL;
20 
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 
50  public function may($obj, $action){
51  $this->loadAro();
52  return $this->aro->may($obj, $action);
53  }
54 
61  public function grant($obj, $actions){
62  $this->loadAro();
63  return $this->aro->grant($obj, $actions);
64  }
65 
72  public function deny($obj, $actions){
73  $this->loadAro();
74  return $this->aro->deny($obj, $actions);
75  }
76 
82  public function afterSave(){
84  if($this->isNewRecord){
85  $class = Strategy::getClass('Aro');
86  $aro = new $class();
87  $aro->model = get_class($this);
88  $aro->foreign_key = $this->getPrimaryKey();
89  if(!$aro->save())
90  throw new RuntimeError("Unable to save Aro-Collection");
91  }
92  }
93 
97  public function beforeDelete(){
98  //Ok he has the right to do that - remove all the ACL-objects associated with this object
99  $class = Strategy::getClass('Aro');
100  $aro = $class::model()->find('model = :model AND foreign_key = :key', array(':model' => get_class( $this), ':key' => $this->id));
101 
102  if(!$aro)
103  throw new RuntimeException('No associated Aro-Collection!');
104 
105  $transaction = Yii::app()->db->beginTransaction();
106  try{
107  $suc =$aro->delete()&& parent::beforeDelete();
108  $transaction->commit();
109  return $suc;
110  }
111  catch(Exception $e){
112  $transaction->rollback();
113  throw $e;
114  }
115  }
116 
117 }
118 ?>