AuthItemChild.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace common\models\rbac;
  3. use common\enums\StatusEnum;
  4. use common\models\user\UserLevel;
  5. use PHPUnit\Util\Exception;
  6. /**
  7. * This is the model class for table "{{%rbac_auth_item_child}}".
  8. *
  9. *
  10. * @property int $role_id 角色id
  11. * @property int $item_id 权限id
  12. * @property string $name 别名
  13. * @property string $app_id 类别
  14. * @property int $is_addon 是否插件
  15. * @property string $addons_name 插件名称
  16. */
  17. class AuthItemChild extends \common\models\base\BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%rbac_auth_item_child}}';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['role_id', 'item_id', 'is_addon'], 'integer'],
  33. [['name'], 'string', 'max' => 64],
  34. [['app_id'], 'string', 'max' => 20],
  35. [['addons_name'], 'string', 'max' => 100],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'role_id' => '角色id',
  45. 'item_id' => '权限id',
  46. 'name' => '别名',
  47. 'app_id' => '类别',
  48. 'is_addon' => '是否插件',
  49. 'addons_name' => '插件名称',
  50. ];
  51. }
  52. public static function setData(array $params){
  53. try{
  54. $model = new self();
  55. $model->loadDefaultValues();
  56. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  57. return $model;
  58. }catch(Exception $e){
  59. self::$static_error = $e->getMessage();
  60. return false;
  61. }
  62. }
  63. /**
  64. * @param array $cond
  65. * @param array $with
  66. * @param int $is_array
  67. * @param string $select
  68. * @return array|AuthItemChild[]|\yii\db\ActiveRecord[]
  69. */
  70. public static function getList(array $cond, array $with = [], int $is_array = 0, string $select = '*')
  71. {
  72. $query = self::find()->select($select);
  73. self::paramPack(['where' => $cond, 'with' => $with], $query);
  74. $i = 1;
  75. $page_size = 1000;
  76. $data = [];
  77. while ($list =$query->offset(($i - 1) * $page_size)->limit($page_size)->asArray($is_array)->all()) {
  78. $data = array_merge($data, $list);
  79. $i++;
  80. }
  81. return $data;
  82. }
  83. /**
  84. * @desc:更新应用配置,异步更新用户已选择的权限
  85. * @Author: hua
  86. * @Date: 2021/12/23
  87. * @Time: 9:16
  88. * @Copyright: copyright (c) 2021 广东七件事集团
  89. * @param $data // ['addons_name'=>'xxxx']
  90. */
  91. public static function updateAuthItemChildSync($data){
  92. $where = [
  93. ['is_addon'=>1],
  94. ['addons_name'=>$data['addons_name']],
  95. ['status'=>StatusEnum::ENABLED],
  96. ];
  97. $list = AuthItem::lists(['where'=>$where]);
  98. if($list){
  99. foreach ($list as $item){
  100. $attributes = [
  101. 'item_id'=>$item['id'],
  102. ];
  103. $condition = [
  104. 'name'=>$item['name'],
  105. 'app_id'=>$item['app_id'],
  106. 'is_addon'=>$item['is_addon'],
  107. 'addons_name'=>$item['addons_name'],
  108. ];
  109. AuthItemChild::updateAll($attributes,$condition);
  110. }
  111. }
  112. }
  113. /**
  114. * 获取全部选中的itemid
  115. *
  116. * @param array $condition
  117. * @return array
  118. */
  119. public static function getCheckedKey(array $condition)
  120. {
  121. return static::find()
  122. ->where($condition)
  123. ->select('item_id')
  124. ->column();
  125. }
  126. /**
  127. * 批量插入权限
  128. *
  129. * @param array $data
  130. * @return int
  131. */
  132. public static function batchInsertAuth(array $data)
  133. {
  134. return static::find()
  135. ->createCommand()
  136. ->batchInsert(static::tableName(), array_keys($data[0]), $data)->execute();
  137. }
  138. }