123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace App\Http\Controllers;
- use App\Models;
- use foo\bar;
- use function GuzzleHttp\Psr7\str;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Validator;
- // todo 返回值为 数字代码还是json还是其他格式待定
- class PermissionController extends Controller
- {
- /**
- * 分配用户可以查看的菜单的内容
- *
- * @param Request $request
- * @return string
- */
- public function AssignActionBars(Request $request)
- {
- $validator = Validator::make($request->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);
- }
- }
|