UserRole.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\admin\controller\base\user;
  3. /**
  4. * @title:用户角色控制器
  5. * @Description:
  6. * @Author: goldenrock
  7. * @Date: 2024-12-03 14:53:47
  8. * @LastEditTime: 2024-12-03 16:32:12
  9. * @LastEditors: goldenrock
  10. * @FilePath: \OA_hbdrwhe:\HBDRWHCODE\JindDongFang_platform\app\admin\controller\base\user\UserRole.php
  11. */
  12. use think\Db;
  13. use app\admin\controller\Base;
  14. use app\common\model\base\user\UserRole as UserRoleModel;
  15. use app\common\model\base\org\OrgRole;
  16. class UserRole extends Base
  17. {
  18. protected $model = null;
  19. public function initialize()
  20. {
  21. parent::initialize();
  22. $this->model = new UserRoleModel;
  23. }
  24. private function createWhere()
  25. {
  26. $data = $this->request->param();
  27. $where = [];
  28. if(!empty($data['user_id'])){
  29. $where[] = ['user_id','=',$data['user_id']];
  30. }
  31. if(!empty($data['org_id'])){
  32. $where[] = ['org_id','=',$data['org_id']];
  33. }
  34. if(!empty($data['role_id'])){
  35. $where[] = ['role_id','=',$data['role_id']];
  36. }
  37. if(!empty($data['role_code'])){
  38. $where[] = ['role_code','=',$data['role_code']];
  39. }
  40. return $where;
  41. }
  42. public function getList()
  43. {
  44. $where = $this->createWhere();
  45. $list = $this->model->where($where)->select();
  46. return res(1,"获取成功",$list);
  47. }
  48. /**
  49. * @title 验证当前登录用户是否有权限绑定/解除绑定用户角色
  50. */
  51. private function operateAuth($user_id,$role_id)
  52. {
  53. // 判断对所选角色是否有管理权限
  54. return true;
  55. }
  56. /**
  57. * @title 绑定角色
  58. * @param {int} {user_id} {必填} {用户ID}
  59. * @param {int} {role_id} {必填} {角色ID}
  60. */
  61. public function bind(int $user_id,int $role_id)
  62. {
  63. $OrgRole = OrgRole::find($role_id);
  64. if(empty($OrgRole) || $OrgRole->isEmpty()){
  65. return res(2,"未找到此角色");
  66. }
  67. // 判断是否已经绑定过此角色
  68. $has = $this->model->where('user_id',$user_id)->where('role_id',$role_id)->find();
  69. if(!empty($has) && !$has->isEmpty()){
  70. return res(2,"角色绑定失败,该用户已绑定此角色");
  71. }
  72. // 判断管理权限
  73. if(!$this->operateAuth($user_id,$role_id)){
  74. return res(2,"角色绑定失败,没有权限");
  75. }
  76. $data = [
  77. 'user_id' => $user_id,
  78. 'role_id' => $role_id,
  79. 'org_id' => $OrgRole->org_id,
  80. 'role_code' => $OrgRole->code,
  81. ];
  82. $this->model->replace()->save($data);
  83. return res(1,"绑定成功");
  84. }
  85. /**
  86. * @解除绑定角色
  87. * @param {int} {id} {必填} {用户与角色关系ID}
  88. */
  89. public function unbind(int $id)
  90. {
  91. $userRole = $this->model->find($id);
  92. if(empty($userRole)){
  93. return res(2,"未找到可解绑角色");
  94. }
  95. // 判断管理权限
  96. if(!$this->operateAuth($userRole->user_id,$userRole->role_id)){
  97. return res(2,"角色解除绑定失败,没有权限");
  98. }
  99. $userRole->force()->delete();
  100. return res(1,"解绑成功");
  101. }
  102. }