all(), [ "ownerType" => "required|max:32", "ownerId" => "required|integer|min:1", ]); if ($validator->fails()) { return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors()); } // 这里的uid从中间件那里获得 $currentUid = $request->input("currentUid"); $ownerId = (int)$request->input("ownerId"); $ownerType = $request->input("ownerType"); $barIdsStr = trim($request->input("barIds")); $barIds = $barIdsStr == "" ? [] : explode(",", $barIdsStr); // 判断当前用户是否有权分配权限 $userRole = new Models\UserRole(); $role = $userRole->LoadRoleByUid($currentUid); if (!$role || $role["status"] != "normal" || $role["role"] != "admin") { // return $this->fail(PERMISSION_DENIED, $this->error[PERMISSION_DENIED]); } // 有权分配 $userActionBar = new Models\UserActionBar(); $result = $userActionBar->AssignUserActionBar($ownerType, $ownerId, $barIds); if ($result["code"] == 0) { return $this->success(SUCCESS); } return $this->fail($result["code"], $this->error[$result["code"]]); } /** * 分配某个用户所拥有的角色 * * @param Request $request * @return string */ public function AssignUserRole(Request $request) { $validator = Validator::make($request->all(), [ "userId" => "required|min:1", // "roleIds" => "array", // "roleIds.*" => "integer", ]); if ($validator->fails()) { return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors()); } $uid = $request->input("userId"); $roleIdsStr = $request->input("roleIds"); $roleIds = $this->GetArrayInt($roleIdsStr); if (0 == $roleIds) { return $this->fail(PARAM_ARRAY_INT, $this->error[PARAM_ARRAY_INT]); } // 判断当前用户是否有权分配权限 $currentUid = 1; $userRole = new Models\UserRole(); $role = $userRole->LoadRoleByUid($currentUid); if (!$role || $role["status"] != "normal" || $role["role"] != "admin") { // return $this->fail(PERMISSION_DENIED, $this->error[PERMISSION_DENIED]); } // 有权分配 $result = $userRole->AssignUserRoles($uid, $roleIds); if ($result["code"] == 0) { return $this->success($result["data"]); } return $this->fail($result["code"], $this->error[$result["code"]]); } /** * 指定角色下有哪些用户,注意和AssignUserRoles区别 * * @param Request $request * @return \Illuminate\Http\JsonResponse */ public function AssignRoleUser(Request $request) { $validator = Validator::make($request->all(), [ "roleId" => "required|min:1", // "userIds" => "array", // "userIds.*" => "integer", ]); if ($validator->fails()) { return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors()); } $roleId = $request->input("roleId"); $userIdsStr = $request->input("userIds"); $userIds = $this->GetArrayInt($userIdsStr); if (0 == $userIds) { return $this->fail(PARAM_ARRAY_INT, $this->error[PARAM_ARRAY_INT]); } // 判断当前用户是否有权分配权限 $currentUid = 1; $userRole = new Models\UserRole(); $role = $userRole->LoadRoleByUid($currentUid); if (!$role || $role["status"] != "normal" || $role["role"] != "admin") { // return $this->fail(PERMISSION_DENIED, $this->error[PERMISSION_DENIED]); } // 有权分配 $result = $userRole->AssignRoleUsers($roleId, $userIds); if ($result["code"] == 0) { return $this->success($result["data"]); } return $this->fail($result["code"], $this->error[$result["code"]]); } /** * 把传入的参数检查是不是正确的int数组,并且每个数字必须大于0。 * * @param $str * @return array|int */ private function GetArrayInt($str) { $str = trim($str); if ($str == "") { return []; } $intArray = []; $multi = explode(",", $str); foreach ($multi as $m) { $mInt = (int)$m; if ($mInt < 1) { return 0; } array_push($intArray, $mInt); } return $intArray; } // 管理员列出角色下的actionBars /** * 列出角色下的菜单数据 * * @param Request $request * @return array|\Illuminate\Http\JsonResponse */ public function LoadRoleActionBars(Request $request) { $validator = Validator::make($request->all(), [ "page" => "integer|min:1", "pageCount" => "integer|min:1", "keyword" => "max:255", ]); if ($validator->fails()) { return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors()); } $params = []; $params["page"] = (int)$request->input("page"); $params["pageCount"] = (int)$request->input("pageCount"); $params["keyword"] = $request->input("keyword"); // 先加载所有的角色信息 $role = new Models\Role(); $roleData = $role->ListRole($params); $roles = $roleData["roles"]; $roleIds = []; if (count($roles) < 1) { return $this->success(SUCCESS, ["roles"=>[]]); } // 加载角色下的菜单数据 $userBars = new Models\UserActionBar(); $bars = $userBars->ListActionBarIds_Format(["role"], $roleIds); $result = ["roles" => $roles, "roleActionBar" => $bars]; return $this->success($result); } }