| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
- namespace common\models\rbac;
- use common\enums\AppEnum;
- use common\enums\StatusEnum;
- use common\helpers\ArrayHelper;
- use PHPUnit\Util\Exception;
- use Yii;
- use common\traits\Tree;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "rf_rbac_auth_role".
- *
- *
- * @property int $id 主键
- * @property int $merchant_id 商户id
- * @property string $title 标题
- * @property string $app_id 应用
- * @property int $pid 上级id
- * @property int $level 级别
- * @property int $sort 排序
- * @property string $tree 树
- * @property int $is_default 是否默认角色
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 添加时间
- * @property int $updated_at 修改时间
- * @property int $mall_id 商城id
- */
- class AuthRole extends \common\models\base\BaseActiveRecord
- {
- const STATUS_ARR = ['-1' => '删除', '0' => '禁用', '1' => '启用'];
- use Tree;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%rbac_auth_role}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['title'], 'trim'],
- [['title', 'pid'], 'required'],
- [['title'], 'isUniquTitle'],
- [['merchant_id', 'pid', 'mall_id', 'is_default', 'level', 'sort', 'status', 'created_at', 'updated_at'], 'integer'],
- [['title'], 'string', 'max' => 50],
- [['app_id'], 'string', 'max' => 20],
- [['tree'], 'string', 'max' => 300],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'merchant_id' => '商户id',
- 'title' => '标题',
- 'app_id' => '应用',
- 'pid' => '上级角色',
- 'level' => '级别',
- 'sort' => '排序',
- 'tree' => '树',
- 'is_default' => '默认',
- 'status' => '状态',
- 'created_at' => '添加时间',
- 'updated_at' => '修改时间',
- 'mall_id' => '商城id',
- ];
- }
- /**
- * @param $attribute
- */
- public function isUniquTitle($attribute)
- {
- $merchant_id = $this->merchant_id;
- $mall_id = $this->mall_id;
- $model = self::find()->where([
- 'merchant_id' => $merchant_id,
- 'mall_id' => $mall_id,
- 'title' => $this->title,
- 'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]
- ])->one();
- if ($model && $model->id != $this->id) {
- $this->addError($attribute, '角色名称已存在');
- }
- }
- /**
- * 关联模型
- * @return \yii\db\ActiveQuery
- */
- public function getChild()
- {
- return $this->hasMany(self::class, ['pid' => 'id'])->where(['<>', 'status', StatusEnum::DELETE])->orderBy('sort asc,id asc');
- }
- /**
- * @param array $cond
- * @param array $with
- * @param int $is_array
- * @return array|AuthRole[]|ActiveRecord[]\
- */
- public static function getRole(array $cond = [], array $with = [], bool $is_array = true)
- {
- $query = self::find();
- self::paramPack(['where' => $cond, 'with' => $with], $query);
- return $query->asArray($is_array)->orderBy('sort asc,id desc')->all();
- }
- public static function getDropDownForEdit($app_id,$mall_id='', $id = '')
- {
- $list = AuthRole::find()->where(['app_id' => $app_id,'mall_id'=>$mall_id,'status'=>StatusEnum::ENABLED])->asArray()->all();
- $list = ArrayHelper::removeByValue($list, $id);
- $models = ArrayHelper::itemsMerge($list);
- $data = ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
- // if (\Yii::$app->services->rbacAuthRole->isSuperAdmin()) {
- return ArrayHelper::merge([0 => '顶级角色'], $data);
- // }
- return $data;
- }
- public static function getEditData($params)
- {
- $where = ['and',
- ['id' => $params['id']],
- ['mall_id' => $params['mall_id']],
- ['status'=> [StatusEnum::ENABLED,StatusEnum::DISABLED]],
- ];
- $query = AuthRole::find()->where($where);
- $role = $query->asArray()->one();
- $data['role'] = !empty($role) ? $role : [];
- $data['checked_key'] = AuthItemChild::getCheckedKey(['role_id' => $params['id']]);
- $list = AuthRole::getDropDownForEdit($params['app_id'],$params['mall_id']);
- $data['list'] = $list;
- $parent_title = '顶级角色';
- if (!empty($items['pid'])) {
- $parent_item = AuthRole::getOne([['id' => $items['pid']],['<>', 'status', StatusEnum::DELETE]],true);
- $parent_title = $parent_item['title'];
- }
- $data['parent_title'] = $parent_title;
- return $data;
- }
- /**
- * 更新
- * @param $params
- * @return bool
- */
- public static function updateRole($params)
- {
- try {
- $query = AuthRole::find()->where(['and',
- ['id' => $params['id']],
- ['mall_id' => empty($params['mall_id']) ? 0 : $params['mall_id']],
- ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ]);
- $model = $query->one();
- isset($params['sort']) && $model->sort = $params['sort'];
- isset($params['status']) && $model->status = $params['status'];
- return $model->save();
- } catch (\Exception $e) {
- return false;
- }
- }
- /**
- * 保存数据
- * @param array $params
- * @return bool|AuthRole
- * @throws \yii\db\Exception
- */
- public static function setData(array $params)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($id = $params['id']) && !empty($pid = $params['pid'])) {
- if (static::isChildIdOrSelf($id, $pid)) throw new Exception('上级角色无法选择自己或者其下级角色');
- }
- if (!empty($params['id'])) {
- $where = ['id' => $params['id'], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
- if (isset($params['mall_id'])) $where[] = ['mall_id' => $params['mall_id']];
- $model = self::findOne($where);
- if (empty($model)) {
- $t->rollBack();
- throw new Exception('服务不存在或已删除');
- }
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- isset($params['merchant_id']) && $model->merchant_id = $params['merchant_id'];
- isset($params['title']) && $model->title = $params['title'];
- isset($params['app_id']) && $model->app_id = $params['app_id'];
- isset($params['pid']) && $model->pid = $params['pid'];
- isset($params['sort']) && $model->sort = $params['sort'];
- isset($params['is_default']) && $model->is_default = $params['is_default'];
- isset($params['status']) && $model->status = $params['status'];
- isset($params['mall_id']) && $model->mall_id = $params['mall_id'];
- $model->level = 1;
- $model->tree = 'tr_0';
- if (!empty($model->pid)) {
- $rbac_item = AuthItem::findOne($model->pid);
- $model->level = ($rbac_item->level ?? 0) + 1;
- $model->tree = ($rbac_item->tree ?? '') . ' tr_' . ($rbac_item['id'] ?? '');
- }
- $res = $model->save();
- if ($res === false) {
- $t->rollBack();
- throw new Exception($model->getErrorMessage());
- }
- if (!empty($res) && !empty($params['item_ids'])) {
- AuthItemChild::deleteAll(['role_id' => $params['id']]);
- $auth_item_list = AuthItem::find()->where(['and', ['in', 'id', $params['item_ids']], ['<>', 'status', StatusEnum::DELETE]])->indexBy('id')->all();
- foreach ($params['item_ids'] as $v) {
- $batch_insert_data[] = [
- 'role_id' => $model->id,
- 'item_id' => $v,
- 'app_id' => $params['app_id'],
- 'name' => empty($auth_item_list[$v]['name']) ? '' : $auth_item_list[$v]['name'],
- 'is_addon' => empty($auth_item_list[$v]['is_addon']) ? 0 : $auth_item_list[$v]['is_addon'],
- 'addons_name' => empty($auth_item_list[$v]['addons_name']) ? '' : $auth_item_list[$v]['addons_name'],
- ];
- }
- $res = AuthItemChild::batchInsertAuth($batch_insert_data);
- if ($res === false) {
- $t->rollBack();
- throw new Exception($model->getErrorMessage());
- }
- }
- if (empty($params['item_ids'])) {
- AuthItemChild::deleteAll(['role_id' => $params['id']]);
- }
- $t->commit();
- return $model;
- } catch (Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * pid是否是id的子节点,或者自己
- *
- * @param integer $id
- * @param integer $pid
- * @return boolean
- */
- public static function isChildIdOrSelf(int $id, int $pid)
- {
- $ids = [$id];
- $tmp_id = $id;
- while ($child_ids = static::find()->select('id')->where(['pid' => $tmp_id])->column()) {
- $ids = array_merge($child_ids, $ids);
- $tmp_id = $child_ids;
- }
- return in_array($pid, $ids);
- }
- }
|