/protected/modules/rbac/models/AuthItem.php

https://github.com/allinside/Yii-CMS · PHP · 208 lines · 162 code · 46 blank · 0 comment · 8 complexity · 091900c9e011e4e279b219420496d5de MD5 · raw file

  1. <?php
  2. class AuthItem extends ActiveRecordModel
  3. {
  4. const PAGE_SIZE = 10;
  5. const PHOTOS_DIR = 'upload/news';
  6. const ROLE_DEFAULT = 'user';
  7. const ROLE_GUEST = 'guest';
  8. const ROLE_ROOT = 'admin';
  9. const TYPE_OPERATION = 0;
  10. const TYPE_TASK = 1;
  11. const TYPE_ROLE = 2;
  12. public $module;
  13. public $parent;
  14. public static $system_roles = array(
  15. self::ROLE_DEFAULT,
  16. self::ROLE_GUEST,
  17. self::ROLE_ROOT
  18. );
  19. public static function model($className=__CLASS__)
  20. {
  21. return parent::model($className);
  22. }
  23. public function tableName()
  24. {
  25. return 'AuthItem';
  26. }
  27. public function rules()
  28. {
  29. return array(
  30. array('name, description', 'required'),
  31. array(
  32. 'name',
  33. 'match',
  34. 'pattern' => '/^[A-Za-z_]+$/ui',
  35. 'message' => 'только латинский алфавит и нижнее подчеркивание'
  36. ),
  37. array('type, allow_for_all', 'numerical', 'integerOnly' => true),
  38. array('name', 'length', 'max' => 64),
  39. array('description, bizrule, data', 'safe'),
  40. array('name', 'TypeUnique'),
  41. array('name, type, description, bizrule, data', 'safe', 'on' => 'search'),
  42. );
  43. }
  44. public function typeUnique($attr)
  45. {
  46. $exist = $this->findByAttributes(array('type' => $this->type, 'name' => $this->$attr));
  47. if ($exist)
  48. {
  49. if ($exist->primaryKey != $this->primaryKey)
  50. {
  51. $this->addError($attr, 'Данное имя уже занято!');
  52. }
  53. }
  54. }
  55. public function relations()
  56. {
  57. return array(
  58. 'operations' => array(
  59. self::MANY_MANY,
  60. 'AuthItem',
  61. 'AuthItemChild(parent, child)',
  62. 'condition' => 'type = "' . self::TYPE_OPERATION . '"'
  63. ),
  64. 'tasks' => array(
  65. self::MANY_MANY,
  66. 'AuthItem',
  67. 'AuthItemChild(parent, child)',
  68. 'condition' => 'type = "' . self::TYPE_TASK . '"'
  69. ),
  70. 'assignments' => array(self::HAS_MANY, 'AuthAssignment', 'itemname'),
  71. 'users' => array(self::HAS_MANY, 'User', 'userid', 'through' => 'assignments')
  72. );
  73. }
  74. public function attributeLabels()
  75. {
  76. $labels = parent::attributeLabels();
  77. $labels['operations'] = 'Операции';
  78. $labels['tasks'] = 'Задачи';
  79. return $labels;
  80. }
  81. public function getTask()
  82. {
  83. $sql = "SELECT * FROM AuthItem
  84. WHERE name = (SELECT parent FROM AuthItemChild WHERE child = '" . $this->name . "')";
  85. return $this->findBySql($sql);
  86. }
  87. public function search($type)
  88. {
  89. $criteria = new CDbCriteria;
  90. $criteria->compare('name', $this->name, true);
  91. $criteria->compare('type', $this->type);
  92. $criteria->compare('description', $this->description, true);
  93. $criteria->compare('bizrule', $this->bizrule, true);
  94. $criteria->compare('data', $this->data, true);
  95. $criteria->addCondition('type = ' . $type);
  96. return new ActiveDataProvider(get_class($this), array(
  97. 'criteria' => $criteria
  98. ));
  99. }
  100. public function getModulesWithActions()
  101. {
  102. $result = array();
  103. $items = AuthItem::model()->findAllByAttributes(array("type" => AuthItem::TYPE_OPERATION));
  104. $modules = AppManager::getModulesData(true);
  105. foreach ($modules as $class => $data)
  106. {
  107. $actions = AppManager::getModuleActions($class);
  108. foreach ($items as $item)
  109. {
  110. if (isset($actions[$item->name]))
  111. {
  112. unset($actions[$item->name]);
  113. }
  114. }
  115. if ($actions)
  116. {
  117. $result[$class] = $data;
  118. }
  119. }
  120. return $result;
  121. }
  122. public static function constructName($controller, $action)
  123. {
  124. return ucfirst($controller) . '_' . ucfirst($action);
  125. }
  126. public function getRoles()
  127. {
  128. return $this->findAllByAttributes(array(
  129. 'type' => self::TYPE_ROLE
  130. ));
  131. }
  132. public function actionExists()
  133. {
  134. list($controller, $action) = explode('_', $this->name);
  135. $controller_class = $controller . 'Controller';
  136. $controller_file = $controller_class . '.php';
  137. $modules = Yii::app()->getModules();
  138. foreach ($modules as $module)
  139. {
  140. $module_dir = array_shift(explode('.', $module['class']));
  141. $module_path = Yii::getPathOfAlias("application.modules.{$module_dir}");
  142. $controllers_path = $module_path . DIRECTORY_SEPARATOR . 'controllers' .DIRECTORY_SEPARATOR;
  143. if (!is_dir($controllers_path))
  144. {
  145. continue;
  146. }
  147. $controllers_files = scandir($controllers_path);
  148. if (in_array($controller_file, $controllers_files))
  149. {
  150. require_once $controllers_path . $controller_file;
  151. if (method_exists($controller_class, "action{$action}"))
  152. {
  153. return true;
  154. }
  155. else
  156. {
  157. return false;
  158. }
  159. }
  160. }
  161. }
  162. }