AuthItem.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. namespace common\models\rbac;
  3. use common\enums\AppEnum;
  4. use common\enums\StatusEnum;
  5. use common\enums\WhetherEnum;
  6. use common\traits\Tree;
  7. use PHPUnit\Util\Exception;
  8. use yii\db\ActiveRecord;
  9. /**
  10. * This is the model class for table "{{%rbac_auth_item}}".
  11. *
  12. *
  13. * @property int $id
  14. * @property string $name 别名
  15. * @property string $title 标题
  16. * @property string $app_id 应用
  17. * @property string $addons_name 插件名称
  18. * @property int $pid 父级id
  19. * @property int $level 级别
  20. * @property int $is_addon 是否插件
  21. * @property int $sort 排序
  22. * @property string $tree 树
  23. * @property int $status 状态[-1:删除;0:禁用;1启用]
  24. * @property int $created_at
  25. * @property int $updated_at
  26. * @property int $cate_id
  27. */
  28. class AuthItem extends \common\models\base\BaseActiveRecord
  29. {
  30. use Tree;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%rbac_auth_item}}';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['title', 'name'], 'trim'],
  45. [['title', 'name'], 'required'],
  46. [['name'], 'uniquName', 'on' => 'default'],
  47. [['pid', 'level', 'is_addon', 'cate_id', 'sort', 'status', 'created_at', 'updated_at'], 'integer'],
  48. [['name'], 'string', 'max' => 64],
  49. [['title', 'addons_name'], 'string', 'max' => 200],
  50. [['app_id'], 'string', 'max' => 20],
  51. [['tree'], 'string', 'max' => 500],
  52. ];
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'id' => 'ID',
  61. 'name' => '别名',
  62. 'title' => '标题',
  63. 'app_id' => '应用',
  64. 'addons_name' => '插件名称',
  65. 'pid' => '父级id',
  66. 'level' => '级别',
  67. 'is_addon' => '是否插件',
  68. 'sort' => '排序',
  69. 'tree' => '树',
  70. 'status' => '状态',
  71. 'created_at' => 'Created At',
  72. 'updated_at' => 'Updated At',
  73. 'cate_id' => 'Cate Id',
  74. ];
  75. }
  76. /**
  77. * 场景
  78. *
  79. * @return array
  80. */
  81. public function scenarios()
  82. {
  83. $scenarios = parent::scenarios();
  84. $scenarios['addonsBatchCreate'] = array_keys($this->attributeLabels());
  85. return $scenarios;
  86. }
  87. /**
  88. * @param $attribute
  89. */
  90. public function uniquName($attribute)
  91. {
  92. $model = self::find()
  93. ->where(['name' => $this->name, 'app_id' => $this->app_id, 'cate_id' => $this->cate_id])
  94. ->andFilterWhere(['addons_name' => $this->addons_name])
  95. ->one();
  96. if ($model && $model->id != $this->id) {
  97. // $this->addError($attribute, '别名已存在');
  98. }
  99. }
  100. /**
  101. * @param AuthItem $parent
  102. */
  103. public function setParent(AuthItem $parent)
  104. {
  105. $this->parent = $parent;
  106. }
  107. /**
  108. * @param bool $insert
  109. * @param array $changedAttributes
  110. */
  111. public function afterSave($insert, $changedAttributes)
  112. {
  113. if (!$this->isNewRecord) {
  114. AuthItemChild::updateAll(['name' => $this->name], ['item_id' => $this->id]);
  115. }
  116. parent::afterSave($insert, $changedAttributes);
  117. }
  118. /**
  119. * 关联模型
  120. * @return \yii\db\ActiveQuery
  121. */
  122. public function getChild()
  123. {
  124. return $this->hasMany(self::class, ['pid' => 'id'])->where(['<>', 'status', StatusEnum::DELETE])->orderBy('sort asc,id asc');
  125. }
  126. /**
  127. * @param array $cond
  128. * @param array $with
  129. * @param int $is_array
  130. * @return array|AuthRole[]|ActiveRecord[]\
  131. */
  132. public static function getItems(array $cond = [], array $with = [], bool $is_array = true)
  133. {
  134. $query = self::find();
  135. self::paramPack(['where' => $cond, 'with' => $with], $query);
  136. return $query->asArray($is_array)->orderBy('sort asc,id asc')->limit(1000000)->all();
  137. }
  138. public static function getRbacItemsByAppId($app_id, $cate_id = [])
  139. {
  140. $query = AuthItem::find()->orderBy('sort asc, id asc');
  141. $query->andWhere(['status' => StatusEnum::ENABLED]);
  142. if (!empty($app_id)) $query->Where(['app_id' => $app_id]);
  143. if (!empty($cate_id)) $query->Where(['cate_id' => $cate_id]);
  144. return $query->asArray()->all();
  145. }
  146. public static function getRabcItemsList($params)
  147. {
  148. $query = AuthItem::find()->where(['app_id' => $params['app_id'], ['or',
  149. ['is_addon' => 0, 'cate_id' => AppEnum::MALL],
  150. ['is_addon' => 1]
  151. ]]);
  152. $item_list = $query->asArray()->all();
  153. $item_list = AuthItem::getTree($item_list, 0, $params['route']);
  154. $data['item_list'] = $item_list;
  155. }
  156. /**
  157. * 获取树状菜单
  158. * @Author bing
  159. * @DateTime 2021-08-10 18:22:16
  160. * @param array $menu 菜单
  161. * @param integer $parentId
  162. * @param string $app_id APP_ID
  163. * @return array
  164. * @copyright: Copyright (c) 2020 广东七件事集团
  165. */
  166. public static function getTree(array $menu, int $pid = 0, string $route = '')
  167. {
  168. $tree = [];
  169. foreach ($menu as $k => $v) {
  170. $v['value'] = $v['id'];
  171. $v['label'] = $v['title'];
  172. if ($v['pid'] == $pid) {
  173. $tmp = $v;
  174. // 当前路由的菜单被选中
  175. if ($route == $v['name']) $tmp['is_active'] = true;
  176. $children = self::getTree($menu, $v['id'], $route);
  177. $children && $tmp['children'] = $children;
  178. //判断父级菜单是否被选中
  179. if (isset($tmp['children']) && !empty($tmp['children'])) {
  180. foreach ($tmp['children'] as $child) {
  181. if (isset($child['is_active']) && $child['is_active'] == true) {
  182. $tmp['is_active'] = true;
  183. }
  184. }
  185. }
  186. $tree[] = $tmp;
  187. }
  188. }
  189. return $tree;
  190. }
  191. /**
  192. * 获取编辑菜单数据
  193. * @param $id
  194. * @param $type
  195. * @param $app_id
  196. * @return array
  197. */
  198. public static function getEditData($id, $type, $app_id)
  199. {
  200. $where = ['and',
  201. ['id' => $id],
  202. ['<>', 'status', StatusEnum::DELETE]
  203. ];
  204. $items = AuthItem::find()->where($where)->asArray()->one();
  205. if (!empty($id) && $type == 'add') {
  206. $parent_title = $items['title'];
  207. } else {
  208. $parent_title = '顶级权限';
  209. if (!empty($items['pid'])) {
  210. $parent_item = AuthItem::find()->where(['id' => $items['pid']])->andWhere(['<>', 'status', StatusEnum::DELETE])->asArray()->one();
  211. $parent_title = $parent_item['title'];
  212. }
  213. }
  214. $list = \Yii::$app->services->rbacAuthItem->getDropDownForEdit($app_id, $id);
  215. return [
  216. 'items' => !empty($items) ? $items : [],
  217. 'parent_title' => $parent_title,
  218. 'list' => $list];
  219. }
  220. /**
  221. * 保存数据
  222. * @param array $params
  223. * @return bool|AuthItem
  224. */
  225. public static function setData(array $params)
  226. {
  227. try {
  228. if (!empty($params['id'])) {
  229. $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  230. if (empty($model)) throw new Exception('服务不存在或已删除');
  231. } else {
  232. $model = new self();
  233. $model->loadDefaultValues();
  234. }
  235. if (!$model->load($params, '')) throw new Exception($model->getErrorMessage());
  236. if(empty($params['id'])){
  237. if (in_array($params['cate_id'], [AppEnum::PLATFORM, AppEnum::MALL])) $model->is_addon = WhetherEnum::DISABLED;
  238. if (in_array($params['cate_id'], [AppEnum::ADDON])) $model->is_addon = WhetherEnum::ENABLED;
  239. }
  240. $model->level = 1;
  241. $model->tree = 'tr_0';
  242. if (!empty($model->pid)) {
  243. $rbac_item = AuthItem::findOne($model->pid);
  244. $model->level = $rbac_item->level + 1;
  245. $model->tree = $rbac_item->tree . ' tr_' . $rbac_item['id'];
  246. }
  247. $res = $model->save();
  248. if ($res === false) throw new Exception($model->getErrorMessage());
  249. return $model;
  250. } catch (Exception $e) {
  251. self::$static_error = $e->getMessage();
  252. return false;
  253. }
  254. }
  255. public static function getItemListTree($where,$route){
  256. $item_list = self::getItems($where,[],1);
  257. return self::getTree($item_list, 0, $route);
  258. }
  259. /**
  260. * 获取商城插件的权限id
  261. * @Author: lun
  262. * @DateTime: 2022/9/6 0006 17:55
  263. * @Copyright: copyright (c) 2021 广东七件事集团
  264. * @param $addons_names
  265. * @return array
  266. */
  267. public static function getAddonsItemIds($addons_names)
  268. {
  269. return self::find()->select('id')->where(['addons_name' => $addons_names ,'is_addon' => 1, 'pid' => 0, 'status' => StatusEnum::ENABLED])->column();
  270. }
  271. /**
  272. * 迁移创建权限
  273. * @Author: lun
  274. * @DateTime: 2023/7/13 0013 16:31
  275. * @Copyright: copyright (c) 2021 广东七件事集团
  276. * @param array $params
  277. * @return bool|AuthItem
  278. */
  279. public static function createAuthItem(array $params)
  280. {
  281. try {
  282. $model = self::findOne(['name' => $params['name'], 'pid' => $params['pid'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  283. if (empty($model)) {
  284. $model = new self();
  285. $model->loadDefaultValues();
  286. }
  287. if (!$model->load($params, '')) throw new Exception($model->getErrorMessage());
  288. $model->level = 1;
  289. $model->tree = 'tr_0';
  290. if (!empty($model->pid)) {
  291. $rbac_item = AuthItem::findOne($model->pid);
  292. $model->level = $rbac_item->level + 1;
  293. $model->tree = $rbac_item->tree . ' tr_' . $rbac_item['id'];
  294. }
  295. $res = $model->save();
  296. if ($res === false) throw new Exception($model->getErrorMessage());
  297. return $model;
  298. } catch (Exception $e) {
  299. self::$static_error = $e->getMessage();
  300. return false;
  301. }
  302. }
  303. }