123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace app\admin\controller\base\user;
- /**
- * @title:用户角色控制器
- * @Description:
- * @Author: goldenrock
- * @Date: 2024-12-03 14:53:47
- * @LastEditTime: 2024-12-03 16:32:12
- * @LastEditors: goldenrock
- * @FilePath: \OA_hbdrwhe:\HBDRWHCODE\JindDongFang_platform\app\admin\controller\base\user\UserRole.php
- */
- 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);
- }
- /**
- * @title 验证当前登录用户是否有权限绑定/解除绑定用户角色
- */
- private function operateAuth($user_id,$role_id)
- {
- // 判断对所选角色是否有管理权限
- return true;
- }
- /**
- * @title 绑定角色
- * @param {int} {user_id} {必填} {用户ID}
- * @param {int} {role_id} {必填} {角色ID}
- */
- 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,"绑定成功");
- }
- /**
- * @解除绑定角色
- * @param {int} {id} {必填} {用户与角色关系ID}
- */
- 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,"解绑成功");
- }
- }
|