1 <?php
2 3 4 5 6 7 8
9 class RDbAuthManager extends CDbAuthManager
10 {
11 12 13
14 public $rightsTable = 'Rights';
15
16 private $_items = array();
17 private $_itemChildren = array();
18
19 20 21 22 23 24 25 26
27 public function addItemChild($itemName, $childName)
28 {
29
30 if( $this->hasItemChild($itemName, $childName)===false )
31 return parent::addItemChild($itemName, $childName);
32 }
33
34 35 36 37 38 39 40 41 42 43 44 45 46
47 public function assign($itemName, $userId, $bizRule=null, $data=null)
48 {
49
50 if( $this->getAuthAssignment($itemName, $userId)===null )
51 return parent::assign($itemName, $userId, $bizRule, $data);
52 }
53
54 55 56 57 58 59 60
61 public function getAuthItem($name, $allowCaching=true)
62 {
63
64 if( $allowCaching && $this->_items===array() )
65 $this->_items = $this->getAuthItems();
66
67
68 if( $allowCaching && isset($this->_items[ $name ]) )
69 {
70 return $this->_items[ $name ];
71 }
72
73 else if( ($item = parent::getAuthItem($name))!==null )
74 {
75 return $item;
76 }
77
78
79 return null;
80 }
81
82
83 84 85 86 87 88
89 public function getAuthItemsByNames($names, $nested=false)
90 {
91
92 if( $this->_items===array() )
93 $this->_items = $this->getAuthItems();
94
95
96 $items = array();
97 foreach( $this->_items as $name=>$item )
98 {
99 if( in_array($name, $names) )
100 {
101 if( $nested===true )
102 $items[ $item->getType() ][ $name ] = $item;
103 else
104 $items[ $name ] = $item;
105 }
106 }
107
108 return $items;
109 }
110
111 112 113 114 115 116 117 118 119 120
121 public function getAuthItems($type=null, $userId=null, $sort=true)
122 {
123
124 if( $sort===true )
125 {
126 if( $type===null && $userId===null )
127 {
128 $sql = "SELECT name,t1.type,description,t1.bizrule,t1.data,weight
129 FROM {$this->itemTable} t1
130 LEFT JOIN {$this->rightsTable} t2 ON name=itemname
131 ORDER BY t1.type DESC, weight ASC";
132 $command=$this->db->createCommand($sql);
133 }
134 else if( $userId===null )
135 {
136 $sql = "SELECT name,t1.type,description,t1.bizrule,t1.data,weight
137 FROM {$this->itemTable} t1
138 LEFT JOIN {$this->rightsTable} t2 ON name=itemname
139 WHERE t1.type=:type
140 ORDER BY t1.type DESC, weight ASC";
141 $command=$this->db->createCommand($sql);
142 $command->bindValue(':type', $type);
143 }
144 else if( $type===null )
145 {
146 $sql = "SELECT name,t1.type,description,t1.bizrule,t1.data,weight
147 FROM {$this->itemTable} t1
148 LEFT JOIN {$this->assignmentTable} t2 ON name=t2.itemname
149 LEFT JOIN {$this->rightsTable} t3 ON name=t3.itemname
150 WHERE userid=:userid
151 ORDER BY t1.type DESC, weight ASC";
152 $command=$this->db->createCommand($sql);
153 $command->bindValue(':userid', $userId);
154 }
155 else
156 {
157 $sql = "SELECT name,t1.type,description,t1.bizrule,t1.data,weight
158 FROM {$this->itemTable} t1
159 LEFT JOIN {$this->assignmentTable} t2 ON name=t2.itemname
160 LEFT JOIN {$this->rightsTable} t3 ON name=t3.itemname
161 WHERE t1.type=:type AND userid=:userid
162 ORDER BY t1.type DESC, weight ASC";
163 $command=$this->db->createCommand($sql);
164 $command->bindValue(':type', $type);
165 $command->bindValue(':userid', $userId);
166 }
167
168 $items = array();
169 foreach($command->queryAll() as $row)
170 $items[ $row['name'] ] = new CAuthItem($this, $row['name'], $row['type'], $row['description'], $row['bizrule'], unserialize($row['data']));
171 }
172
173 else
174 {
175 $items = parent::getAuthItems($type, $userId);
176 }
177
178 return $items;
179 }
180
181 182 183 184 185 186 187 188
189 public function getItemChildren($names, $allowCaching=true)
190 {
191
192 $key = $names===(array)$names ? implode('|', $names) : $names;
193
194
195 if( $allowCaching && isset($this->_itemChildren[ $key ])===true )
196 {
197 return $this->_itemChildren[ $key ];
198 }
199
200 else
201 {
202
203 if( is_string($names) )
204 {
205 $condition = 'parent='.$this->db->quoteValue($names);
206 }
207
208 else if( $names===(array)$names && $names!==array() )
209 {
210 foreach($names as &$name)
211 $name=$this->db->quoteValue($name);
212
213 $condition = 'parent IN ('.implode(', ', $names).')';
214 }
215 else
216 {
217 $condition = '1';
218 }
219
220 $sql = "SELECT name, type, description, bizrule, data
221 FROM {$this->itemTable}, {$this->itemChildTable}
222 WHERE {$condition} AND name=child";
223 $children = array();
224 foreach( $this->db->createCommand($sql)->queryAll() as $row )
225 {
226 if( ($data = @unserialize($row['data']))===false )
227 $data = null;
228
229 $children[ $row['name'] ] = new CAuthItem($this, $row['name'], $row['type'], $row['description'], $row['bizrule'], $data);
230 }
231
232
233 $children = Rights::getAuthorizer()->attachAuthItemBehavior($children);
234
235
236 return $this->_itemChildren[ $key ] = $children;
237 }
238 }
239
240 public function getAssignmentsByItemName($name)
241 {
242 $sql = "SELECT * FROM {$this->assignmentTable} WHERE itemname=:itemname";
243 $command = $this->db->createCommand($sql);
244 $command->bindValue(':itemname', $name);
245
246 $assignments=array();
247 foreach($command->queryAll($sql) as $row)
248 {
249 if(($data=@unserialize($row['data']))===false)
250 $data=null;
251
252 $assignments[ $row['userid'] ] = new CAuthAssignment($this, $row['itemname'], $row['userid'], $row['bizrule'], $data);
253 }
254
255 return $assignments;
256 }
257
258 259 260 261
262 public function updateItemWeight($result)
263 {
264 foreach( $result as $weight=>$itemname )
265 {
266 $sql = "SELECT COUNT(*) FROM {$this->rightsTable}
267 WHERE itemname=:itemname";
268 $command = $this->db->createCommand($sql);
269 $command->bindValue(':itemname', $itemname);
270
271
272 if( $command->queryScalar()>0 )
273 {
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
283 else
284 {
285 if( ($item = $this->getAuthItem($itemname))!==null )
286 {
287 $sql = "INSERT INTO {$this->rightsTable} (itemname, type, weight)
288 VALUES (:itemname, :type, :weight)";
289 $command = $this->db->createCommand($sql);
290 $command->bindValue(':itemname', $itemname);
291 $command->bindValue(':type', $item->getType());
292 $command->bindValue(':weight', $weight);
293 $command->execute();
294 }
295 }
296 }
297 }
298 }
299