28 $this->_authManager = Yii::app()->getAuthManager();
39 public function getRoles($includeSuperuser =
true, $sort =
true)
41 $exclude = $includeSuperuser ===
false ? array($this->superuserName) : array();
42 $roles = $this->
getAuthItems(CAuthItem::TYPE_ROLE, null, null, $sort, $exclude);
62 $bizRule = $bizRule !==
'' ? $bizRule : null;
68 return $this->_authManager->createAuthItem($name, $type, $description, $bizRule,
$data);
83 $authItem = $this->_authManager->getAuthItem($oldName);
84 $authItem->name = $name;
85 $authItem->description = $description !==
'' ? $description : null;
86 $authItem->bizRule = $bizRule !==
'' ? $bizRule : null;
89 if (@unserialize(
$data) ===
false) {
93 $this->_authManager->saveAuthItem($authItem, $oldName);
109 public function getAuthItems($types = null, $userId = null, CAuthItem $parent = null, $sort =
true, $exclude = array())
112 if ($types !== (array) $types) {
113 $items = $this->_authManager->getAuthItems($types, $userId, $sort);
117 $typeItemList = array();
118 foreach ($types as $type) {
119 $typeItemList[ $type ] = $this->_authManager->getAuthItems($type, $userId, $sort);
124 foreach ($typeItemList as $typeItems) {
145 foreach ($array2 as $itemName => $item) {
146 if (isset($array1[ $itemName ]) ===
false) {
147 $array1[ $itemName ] = $item;
168 if ($parent !== null) {
169 $exclude[] = $parent->name;
170 foreach ($parent->getChildren() as $childName => $child) {
171 $exclude[] = $childName;
176 $exclude = array_merge($parentNames, $exclude);
180 foreach ($exclude as $itemName) {
181 if (isset($items[ $itemName ])) {
182 unset($items[ $itemName ]);
202 if (($item instanceof CAuthItem) ===
false) {
203 $item = $this->_authManager->getAuthItem($item);
208 $parents = $this->_authManager->getAuthItemsByNames($parentNames);
211 if ($type !== null) {
212 foreach ($parents as $parentName => $parent) {
213 if ((
int) $parent->type !== $type) {
214 unset($parents[ $parentName ]);
234 foreach ($items as $childName => $children) {
235 if ($children !== array()) {
236 if (isset($children[ $itemName ])) {
237 if (isset($parents[ $childName ]) ===
false) {
238 $parents[ $childName ] = $childName;
242 if ($direct ===
false && isset($parents[ $childName ]) ===
false) {
243 $parents[ $childName ] = $childName;
246 $parents = array_merge($parents, $p);
266 if (($item instanceof CAuthItem) ===
false) {
267 $item = $this->_authManager->getAuthItem($item);
270 $childrenNames = array();
271 foreach ($item->getChildren() as $childName => $child) {
272 if ($type === null || (
int) $child->type === $type) {
273 $childrenNames[] = $childName;
277 $children = $this->_authManager->getAuthItemsByNames($childrenNames);
295 if ($items instanceof CAuthItem) {
299 elseif ($items === (array) $items) {
300 foreach ($items as $item) {
315 $assignments = $this->_authManager->getAssignmentsByItemName(
Rights::module()->superuserName);
317 $userIdList = array();
318 foreach ($assignments as $userId => $assignment) {
319 $userIdList[] = $userId;
322 $criteria =
new CDbCriteria();
323 $criteria->addInCondition(
Rights::module()->userIdColumn, $userIdList);
326 $users = CActiveRecord::model($userClass)->findAll($criteria);
329 $superusers = array();
330 foreach ($users as $user) {
331 $superusers[] = $user->name;
336 if ($superusers === array()) {
337 throw new CHttpException(403,
Rights::t(
'core',
'There must be at least one superuser!'));
355 if ($users instanceof $userClass) {
359 elseif ($users === (array) $users) {
360 foreach ($users as $user) {
377 $assignments = $this->_authManager->getAuthAssignments($userId);
379 return isset($assignments[ $this->superuserName ]);
392 $permissions = array();
394 if ($itemName !== null) {
395 $item = $this->_authManager->getAuthItem($itemName);
398 foreach ($this->
getRoles() as $roleName => $role) {
415 $permissions = array();
416 foreach ($item->getChildren() as $childName => $child) {
417 $permissions[ $childName ] = array();
419 $permissions[ $childName ] = $grandChildren;
435 public function hasPermission($itemName, $parentName = null, $permissions = array())
437 if ($parentName !== null) {
438 if ($parentName === $this->superuserName) {
445 if (isset($permissions[ $itemName ])) {
449 foreach ($permissions as $children) {
450 if ($children !== array()) {
470 $languageConstructs = array(
484 foreach ($languageConstructs as $lc) {
485 if (preg_match(
'/'.$lc.
'\ *\(?\ *[\"\']+/', $code)>0) {
491 $definedFunctions = get_defined_functions();
492 $functions = array_merge($definedFunctions[
'internal'], $definedFunctions[
'user']);
496 foreach ($functions as $f) {
497 if (preg_match(
'/'.$f.
'\ *\({1}/', $code)>0) {
503 $result = @eval($code);
506 return $result !==
false ? $result : null;