PermissionController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models;
  4. use foo\bar;
  5. use function GuzzleHttp\Psr7\str;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Validator;
  9. // todo 返回值为 数字代码还是json还是其他格式待定
  10. class PermissionController extends Controller
  11. {
  12. /**
  13. * 分配用户可以查看的菜单的内容
  14. *
  15. * @param Request $request
  16. * @return string
  17. */
  18. public function AssignActionBars(Request $request)
  19. {
  20. $validator = Validator::make($request->all(), [
  21. "ownerType" => "required|max:32",
  22. "ownerId" => "required|integer|min:1",
  23. ]);
  24. if ($validator->fails()) {
  25. return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
  26. }
  27. // 这里的uid从中间件那里获得
  28. $currentUid = $request->input("currentUid");
  29. $ownerId = (int)$request->input("ownerId");
  30. $ownerType = $request->input("ownerType");
  31. $barIdsStr = trim($request->input("barIds"));
  32. $barIds = $barIdsStr == "" ? [] : explode(",", $barIdsStr);
  33. // 判断当前用户是否有权分配权限
  34. $userRole = new Models\UserRole();
  35. $role = $userRole->LoadRoleByUid($currentUid);
  36. if (!$role || $role["status"] != "normal" || $role["role"] != "admin") {
  37. // return $this->fail(PERMISSION_DENIED, $this->error[PERMISSION_DENIED]);
  38. }
  39. // 有权分配
  40. $userActionBar = new Models\UserActionBar();
  41. $result = $userActionBar->AssignUserActionBar($ownerType, $ownerId, $barIds);
  42. if ($result["code"] == 0) {
  43. return $this->success(SUCCESS);
  44. }
  45. return $this->fail($result["code"], $this->error[$result["code"]]);
  46. }
  47. /**
  48. * 分配某个用户所拥有的角色
  49. *
  50. * @param Request $request
  51. * @return string
  52. */
  53. public function AssignUserRole(Request $request)
  54. {
  55. $validator = Validator::make($request->all(), [
  56. "userId" => "required|min:1",
  57. // "roleIds" => "array",
  58. // "roleIds.*" => "integer",
  59. ]);
  60. if ($validator->fails()) {
  61. return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
  62. }
  63. $uid = $request->input("userId");
  64. $roleIdsStr = $request->input("roleIds");
  65. $roleIds = $this->GetArrayInt($roleIdsStr);
  66. if (0 == $roleIds) {
  67. return $this->fail(PARAM_ARRAY_INT, $this->error[PARAM_ARRAY_INT]);
  68. }
  69. // 判断当前用户是否有权分配权限
  70. $currentUid = 1;
  71. $userRole = new Models\UserRole();
  72. $role = $userRole->LoadRoleByUid($currentUid);
  73. if (!$role || $role["status"] != "normal" || $role["role"] != "admin") {
  74. // return $this->fail(PERMISSION_DENIED, $this->error[PERMISSION_DENIED]);
  75. }
  76. // 有权分配
  77. $result = $userRole->AssignUserRoles($uid, $roleIds);
  78. if ($result["code"] == 0) {
  79. return $this->success($result["data"]);
  80. }
  81. return $this->fail($result["code"], $this->error[$result["code"]]);
  82. }
  83. /**
  84. * 指定角色下有哪些用户,注意和AssignUserRoles区别
  85. *
  86. * @param Request $request
  87. * @return \Illuminate\Http\JsonResponse
  88. */
  89. public function AssignRoleUser(Request $request)
  90. {
  91. $validator = Validator::make($request->all(), [
  92. "roleId" => "required|min:1",
  93. // "userIds" => "array",
  94. // "userIds.*" => "integer",
  95. ]);
  96. if ($validator->fails()) {
  97. return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
  98. }
  99. $roleId = $request->input("roleId");
  100. $userIdsStr = $request->input("userIds");
  101. $userIds = $this->GetArrayInt($userIdsStr);
  102. if (0 == $userIds) {
  103. return $this->fail(PARAM_ARRAY_INT, $this->error[PARAM_ARRAY_INT]);
  104. }
  105. // 判断当前用户是否有权分配权限
  106. $currentUid = 1;
  107. $userRole = new Models\UserRole();
  108. $role = $userRole->LoadRoleByUid($currentUid);
  109. if (!$role || $role["status"] != "normal" || $role["role"] != "admin") {
  110. // return $this->fail(PERMISSION_DENIED, $this->error[PERMISSION_DENIED]);
  111. }
  112. // 有权分配
  113. $result = $userRole->AssignRoleUsers($roleId, $userIds);
  114. if ($result["code"] == 0) {
  115. return $this->success($result["data"]);
  116. }
  117. return $this->fail($result["code"], $this->error[$result["code"]]);
  118. }
  119. /**
  120. * 把传入的参数检查是不是正确的int数组,并且每个数字必须大于0。
  121. *
  122. * @param $str
  123. * @return array|int
  124. */
  125. private function GetArrayInt($str)
  126. {
  127. $str = trim($str);
  128. if ($str == "") {
  129. return [];
  130. }
  131. $intArray = [];
  132. $multi = explode(",", $str);
  133. foreach ($multi as $m) {
  134. $mInt = (int)$m;
  135. if ($mInt < 1) {
  136. return 0;
  137. }
  138. array_push($intArray, $mInt);
  139. }
  140. return $intArray;
  141. }
  142. // 管理员列出角色下的actionBars
  143. /**
  144. * 列出角色下的菜单数据
  145. *
  146. * @param Request $request
  147. * @return array|\Illuminate\Http\JsonResponse
  148. */
  149. public function LoadRoleActionBars(Request $request)
  150. {
  151. $validator = Validator::make($request->all(), [
  152. "page" => "integer|min:1",
  153. "pageCount" => "integer|min:1",
  154. "keyword" => "max:255",
  155. ]);
  156. if ($validator->fails()) {
  157. return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
  158. }
  159. $params = [];
  160. $params["page"] = (int)$request->input("page");
  161. $params["pageCount"] = (int)$request->input("pageCount");
  162. $params["keyword"] = $request->input("keyword");
  163. // 先加载所有的角色信息
  164. $role = new Models\Role();
  165. $roleData = $role->ListRole($params);
  166. $roles = $roleData["roles"];
  167. $roleIds = [];
  168. if (count($roles) < 1) {
  169. return $this->success(SUCCESS, ["roles"=>[]]);
  170. }
  171. // 加载角色下的菜单数据
  172. $userBars = new Models\UserActionBar();
  173. $bars = $userBars->ListActionBarIds_Format(["role"], $roleIds);
  174. $result = ["roles" => $roles, "roleActionBar" => $bars];
  175. return $this->success($result);
  176. }
  177. }