| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- namespace app\modules\admin\models;
- use app\models\Admin;
- use app\models\AuthRole;
- use app\models\SaasAdminRole;
- use app\models\SaasAuthRole;
- use Yii;
- use yii\base\Model;
- class SaasStaffForm extends Model
- {
- public $id;
- public $data;
- public $edit_data;
- public $name;
- public $status;
- public $describe;
- public function rules()
- {
- return [
- [['id'], 'number'],
- [['data', 'name', 'describe', 'edit_data'], 'string'],
- [['status'], 'integer']
- ];
- }
- /**
- * 创建角色
- * @return array
- */
- public function create()
- {
- if ($this->validate()) {
- if (SaasAuthRole::findOne(['name' => $this->name])) {
- return [
- 'code' => 1,
- 'msg' => '角色名称已经存在.',
- ];
- }
- $data = json_decode($this->data, true);
- array_unshift($data, 'dashboard');
- $role = new SaasAuthRole();
- $role->name = $this->name;
- $role->data = json_encode($data);
- $role->edit_data = $this->edit_data;
- $role->status = $this->status;
- if ($this->describe) {
- $role->describe = $this->describe;
- }
- if ($role->save()) {
- return [
- 'code' => 0,
- 'msg' => '创建成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '创建失败',
- ];
- }
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- /**
- * 编辑角色
- * @return array
- */
- public function edit()
- {
- if ($this->validate()) {
- $role = SaasAuthRole::findOne($this->id);
- if (! $role) {
- return [
- 'code' => 1,
- 'msg' => '角色不存在',
- ];
- }
- $data = json_decode($this->data, true);
- array_unshift($data, 'dashboard');
- $role->name = $this->name;
- $role->data = json_encode($data);
- $role->edit_data = $this->edit_data;
- $role->describe = $this->describe;
- $role->status = $this->status;
- if ($role->save()) {
- return [
- 'code' => 0,
- 'msg' => '保存成功',
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '保存失败',
- ];
- }
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- //员工账户列表
- /**
- * 获取员工账号列表
- * @return array
- * @throws \yii\base\InvalidConfigException
- */
- public function getAdminList()
- {
- try {
- $query = Admin::find()
- ->where([
- 'is_delete' => Admin::ADMIN_NORMAL,
- 'type' => ADMIN::ADMIN_TYPE_SAAS_STAFF
- ])
- ->select('id, created_at, username, mobile, remark, email, name, avatar');
- $pagination = pagination_make($query);
- $admins = $pagination['list'];
- foreach ($admins as $key => $admin) {
- $admins[$key]['created_at'] = date('Y-m-d H:i:s', $admin['created_at']);
- $admins[$key]['roles'] = Admin::findOne($admin['id'])->getSaasRoles()->select('id, name')->all();
- }
- $roles = SaasAuthRole::find()
- ->select('id, name')
- ->where([
- 'status' => SaasAuthRole::STATUS_NORMAL
- ])
- ->asArray()
- ->all();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $admins,
- 'roles' => $roles,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //角色删除
- public function deteleRole()
- {
- try {
- if (!$this->id) {
- return [
- 'code' => 1,
- 'msg' => '请提供角色ID',
- ];
- }
- $role = SaasAuthRole::findOne($this->id);
- if (!$role) {
- throw new \Exception('角色不存在');
- }
- $adminCount = $role->getAdmins()->count();
- if ($adminCount > 0) {
- throw new \Exception('该角色还有员工使用,不能删除');
- }
- if ($role->delete()) {
- return [
- 'code' => 0,
- 'msg' => '删除成功',
- ];
- }
- throw new \Exception('删除失败');
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 获取角色列表
- * @return array
- */
- public function getRoles()
- {
- $query = SaasAuthRole::find();
- $pagination = pagination_make($query);
- $roles = $pagination['list'];
- foreach ($roles as $key => $role) {
- $roles[$key]['data'] = json_decode($role['data']);
- $roles[$key]['edit_data'] = [];
- if (!empty($role['edit_data'])) {
- $roles[$key]['edit_data'] = json_decode($role['edit_data'], true);
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $roles,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ];
- }
- /**
- * 格式化权限
- * @param array $permission
- * @param $key
- */
- public static function formatPermission(array &$permission, $key = null)
- {
- foreach ($permission as &$value) {
- if ($key) {
- if (isset($value['type']) && $value['type'] == 'action') {
- $action = explode('_', $key);
- $action = array_pop($action);
- $value['key'] = $key . '_' . $action . '@' . $value['key'];
- } else {
- $value['key'] = $key . '_' . $value['key'];
- }
- }
- if (isset($value['children']) && count($value['children']) > 0) {
- static::formatPermission($value['children'], $value['key']);
- }
- }
- }
- /**
- * 获取账号拥有的权限
- * @param null $params
- * @return array
- */
- public static function getAdminPermission($params = null)
- {
- $adminPermission = $params;
- if (! $params) {
- $admin = Yii::$app->jwt->getAdmin();
- $adminPermission = SaasAdminRole::find()->alias('ar')->where(['ar.admin_id' => $admin->id])
- ->leftJoin(['ad' => SaasAuthRole::tableName()], 'ad.id=ar.role_id')->select('ad.data')->column();
- if (count($adminPermission) > 0) {
- foreach ($adminPermission as &$v) {
- $v = json_decode($v);
- }
- $adminPermission = array_unique(array_merge(...$adminPermission));
- }
- }
- $result = [];
- foreach ($adminPermission as $value) {
- if (\Yii::$app->prod_is_dandianpu()) { // 供应链版本不显示联盟菜单
- if (strpos($value, 'saasAlliance') !== false) {
- continue;
- }
- }
- $permission = explode('_', $value);
- foreach ($permission as $v) {
- if (! isset($result[$v])) {
- if (strpos($v, '@') !== false) {
- $ex = explode('@', $v);
- $result[$ex[0]]['actionEntitySet'][] = [
- 'action' => $ex[1],
- ];
- } else {
- $result[$v] = [
- 'permissionId' => $v,
- ];
- }
- }
- }
- }
- return array_values($result);
- }
- /**
- * 获取所有权限key
- * @param $params
- * @param $result
- */
- public static function getAllPermission($params, &$result)
- {
- foreach ($params as $value) {
- $result[] = $value['key'];
- if (isset($value['children'])) {
- static::getAllPermission($value['children'], $result);
- }
- }
- }
- }
|