HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
RDbAuthManager.php
Go to the documentation of this file.
1 <?php
10 class RDbAuthManager extends CDbAuthManager
11 {
15  public $rightsTable = 'Rights';
16 
17  private $_items = array();
18  private $_itemChildren = array();
19 
30  public function addItemChild($itemName, $childName)
31  {
32  // Make sure that the item doesn't already have this child.
33  if ($this->hasItemChild($itemName, $childName) === false) {
34  return parent::addItemChild($itemName, $childName);
35  }
36  }
37 
54  public function assign($itemName, $userId, $bizRule = null, $data = null)
55  {
56  // Make sure that this user doesn't already have this assignment.
57  if ($this->getAuthAssignment($itemName, $userId) === null) {
58  return parent::assign($itemName, $userId, $bizRule, $data);
59  }
60  }
61 
71  public function getAuthItem($name, $allowCaching = true)
72  {
73  // Get all items if necessary and cache them.
74  if ($allowCaching && $this->_items === array()) {
75  $this->_items = $this->getAuthItems();
76  }
77 
78  // Get the items from cache if possible.
79  if ($allowCaching && isset($this->_items[ $name ])) {
80  return $this->_items[ $name ];
81  }
82  // Attempt to get the item.
83  elseif (($item = parent::getAuthItem($name)) !== null) {
84  return $item;
85  }
86 
87  // Item does not exist.
88  return;
89  }
90 
99  public function getAuthItemsByNames($names, $nested = false)
100  {
101  // Get all items if necessary and cache them.
102  if ($this->_items === array()) {
103  $this->_items = $this->getAuthItems();
104  }
105 
106  // Collect the items we want.
107  $items = array();
108  foreach ($this->_items as $name => $item) {
109  if (in_array($name, $names)) {
110  if ($nested === true) {
111  $items[ $item->getType() ][ $name ] = $item;
112  } else {
113  $items[ $name ] = $item;
114  }
115  }
116  }
117 
118  return $items;
119  }
120 
133  public function getAuthItems($type = null, $userId = null, $sort = true)
134  {
135  // We need to sort the items.
136  if ($sort === true) {
137  if ($type === null && $userId === null) {
138  $sql = "SELECT name,t1.type,description,t1.bizrule,t1.data,weight
139  FROM {$this->itemTable} t1
140  LEFT JOIN {$this->rightsTable} t2 ON name=itemname
141  ORDER BY t1.type DESC, weight ASC";
142  $command = $this->db->createCommand($sql);
143  } elseif ($userId === null) {
144  $sql = "SELECT name,t1.type,description,t1.bizrule,t1.data,weight
145  FROM {$this->itemTable} t1
146  LEFT JOIN {$this->rightsTable} t2 ON name=itemname
147  WHERE t1.type=:type
148  ORDER BY t1.type DESC, weight ASC";
149  $command = $this->db->createCommand($sql);
150  $command->bindValue(':type', $type);
151  } elseif ($type === null) {
152  $sql = "SELECT name,t1.type,description,t1.bizrule,t1.data,weight
153  FROM {$this->itemTable} t1
154  LEFT JOIN {$this->assignmentTable} t2 ON name=t2.itemname
155  LEFT JOIN {$this->rightsTable} t3 ON name=t3.itemname
156  WHERE userid=:userid
157  ORDER BY t1.type DESC, weight ASC";
158  $command = $this->db->createCommand($sql);
159  $command->bindValue(':userid', $userId);
160  } else {
161  $sql = "SELECT name,t1.type,description,t1.bizrule,t1.data,weight
162  FROM {$this->itemTable} t1
163  LEFT JOIN {$this->assignmentTable} t2 ON name=t2.itemname
164  LEFT JOIN {$this->rightsTable} t3 ON name=t3.itemname
165  WHERE t1.type=:type AND userid=:userid
166  ORDER BY t1.type DESC, weight ASC";
167  $command = $this->db->createCommand($sql);
168  $command->bindValue(':type', $type);
169  $command->bindValue(':userid', $userId);
170  }
171 
172  $items = array();
173  foreach ($command->queryAll() as $row) {
174  $items[ $row['name'] ] = new CAuthItem($this, $row['name'], $row['type'], $row['description'], $row['bizrule'], unserialize($row['data']));
175  }
176  }
177  // No sorting required.
178  else {
179  $items = parent::getAuthItems($type, $userId);
180  }
181 
182  return $items;
183  }
184 
195  public function getItemChildren($names, $allowCaching = true)
196  {
197  // Resolve the key for runtime caching.
198  $key = $names === (array) $names ? implode('|', $names) : $names;
199 
200  // Get the children from cache if possible.
201  if ($allowCaching && isset($this->_itemChildren[ $key ]) === true) {
202  return $this->_itemChildren[ $key ];
203  }
204  // Children not cached or cached data is not accepted.
205  else {
206  // We only have one name.
207  if (is_string($names)) {
208  $condition = 'parent='.$this->db->quoteValue($names);
209  }
210  // We have multiple names.
211  elseif ($names === (array) $names && $names !== array()) {
212  foreach ($names as &$name) {
213  $name = $this->db->quoteValue($name);
214  }
215 
216  $condition = 'parent IN ('.implode(', ', $names).')';
217  } else {
218  $condition = '1';
219  }
220 
221  $sql = "SELECT name, type, description, bizrule, data
222  FROM {$this->itemTable}, {$this->itemChildTable}
223  WHERE {$condition} AND name=child";
224  $children = array();
225  foreach ($this->db->createCommand($sql)->queryAll() as $row) {
226  if (($data = @unserialize($row['data'])) === false) {
227  $data = null;
228  }
229 
230  $children[ $row['name'] ] = new CAuthItem($this, $row['name'], $row['type'], $row['description'], $row['bizrule'], $data);
231  }
232 
233  // Attach the authorization item behavior.
234  $children = Rights::getAuthorizer()->attachAuthItemBehavior($children);
235 
236  // Cache the result.
237  return $this->_itemChildren[ $key ] = $children;
238  }
239  }
240 
241  public function getAssignmentsByItemName($name)
242  {
243  $sql = "SELECT * FROM {$this->assignmentTable} WHERE itemname=:itemname";
244  $command = $this->db->createCommand($sql);
245  $command->bindValue(':itemname', $name);
246 
247  $assignments = array();
248  foreach ($command->queryAll($sql) as $row) {
249  if (($data = @unserialize($row['data'])) === false) {
250  $data = null;
251  }
252 
253  $assignments[ $row['userid'] ] = new CAuthAssignment($this, $row['itemname'], $row['userid'], $row['bizrule'], $data);
254  }
255 
256  return $assignments;
257  }
258 
264  public function updateItemWeight($result)
265  {
266  foreach ($result as $weight => $itemname) {
267  $sql = "SELECT COUNT(*) FROM {$this->rightsTable}
268  WHERE itemname=:itemname";
269  $command = $this->db->createCommand($sql);
270  $command->bindValue(':itemname', $itemname);
271 
272  // Check if the item already has a weight.
273  if ($command->queryScalar()>0) {
274  $sql = "UPDATE {$this->rightsTable}
275  SET weight=:weight
276  WHERE itemname=:itemname";
277  $command = $this->db->createCommand($sql);
278  $command->bindValue(':weight', $weight);
279  $command->bindValue(':itemname', $itemname);
280  $command->execute();
281  }
282  // Item does not have a weight, insert it.
283  else {
284  if (($item = $this->getAuthItem($itemname)) !== null) {
285  $sql = "INSERT INTO {$this->rightsTable} (itemname, type, weight)
286  VALUES (:itemname, :type, :weight)";
287  $command = $this->db->createCommand($sql);
288  $command->bindValue(':itemname', $itemname);
289  $command->bindValue(':type', $item->getType());
290  $command->bindValue(':weight', $weight);
291  $command->execute();
292  }
293  }
294  }
295  }
296 }