123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace app\admin\controller\base\user;
- use think\Db;
- use app\admin\controller\Base;
- use app\common\model\base\user\UserRole as UserRoleModel;
- use app\common\model\base\org\OrgRole;
- class UserRole extends Base
- {
- protected $model = null;
- public function initialize()
- {
- parent::initialize();
- $this->model = new UserRoleModel;
- }
- private function createWhere()
- {
- $data = $this->request->param();
- $where = [];
- if(!empty($data['user_id'])){
- $where[] = ['user_id','=',$data['user_id']];
- }
- if(!empty($data['org_id'])){
- $where[] = ['org_id','=',$data['org_id']];
- }
- if(!empty($data['role_id'])){
- $where[] = ['role_id','=',$data['role_id']];
- }
- if(!empty($data['role_code'])){
- $where[] = ['role_code','=',$data['role_code']];
- }
- return $where;
- }
- public function getList()
- {
- $where = $this->createWhere();
- $list = $this->model->where($where)->select();
- return res(1,"获取成功",$list);
- }
-
- private function operateAuth($user_id,$role_id)
- {
-
- return true;
- }
-
- public function bind(int $user_id,int $role_id)
- {
- $OrgRole = OrgRole::find($role_id);
- if(empty($OrgRole) || $OrgRole->isEmpty()){
- return res(2,"未找到此角色");
- }
-
- $has = $this->model->where('user_id',$user_id)->where('role_id',$role_id)->find();
- if(!empty($has) && !$has->isEmpty()){
- return res(2,"角色绑定失败,该用户已绑定此角色");
- }
-
- if(!$this->operateAuth($user_id,$role_id)){
- return res(2,"角色绑定失败,没有权限");
- }
- $data = [
- 'user_id' => $user_id,
- 'role_id' => $role_id,
- 'org_id' => $OrgRole->org_id,
- 'role_code' => $OrgRole->code,
- ];
- $this->model->replace()->save($data);
- return res(1,"绑定成功");
- }
-
- public function unbind(int $id)
- {
- $userRole = $this->model->find($id);
- if(empty($userRole)){
- return res(2,"未找到可解绑角色");
- }
-
- if(!$this->operateAuth($userRole->user_id,$userRole->role_id)){
- return res(2,"角色解除绑定失败,没有权限");
- }
- $userRole->force()->delete();
- return res(1,"解绑成功");
- }
- }
|