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