name = $params["name"]; $this->description = $params["description"]; $this->status = "normal"; if ($this->name == "") { return ["code" => EMPTY_ROLE_NAME]; } // check if has the same role name in system. $role = $this->where("name", $this->name)->where("is_del", false)->first(); if ($role) { return ["code" => ALREADY_EXIST_ROLE]; } // $this->save(); $params["id"] = $this->getQueueableId(); return ["code" => SUCCESS, "data" => $params]; } /** * 更新用户角色的名字,描述等信息 * * @param array $params * @return int */ public function ModifyRole(array $params) { $update = []; $id = $params["id"]; if ($id == "") { return EMPTY_ROLE_ID; } // check if role exist $role = $this->where("id", $id)->where("is_del", false)->first(); if (!$role) { return INVALID_ROLE_ID; } if ($params["name"] != "") { $update["name"] = $params["name"]; } if ($params["description"] != "") { $update["description"] = $params["description"]; } if (count($update) == 0) { return NOTHING_UPDATE; } $this->where("id", $id)->where("is_del", false)->update($update); return SUCCESS; } /** * 删除一个用户角色 * * @param array $params * @return int */ public function DeleteRole(array $params) { $id = $params["id"]; if ($id == "") { return EMPTY_ROLE_ID; } $role = $this->where("id", $id)->where("is_del", false)->first(); if (!$role) { return INVALID_ROLE_ID; } $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]); return SUCCESS; } /** * 列出用户角色列表 * * @param array $params * @return mixed */ public function ListRole(array $params) { $page = $params["page"]; $pageCount = $params["pageCount"]; if ($page < 1) { $page = 1; } if ($pageCount > 15 || $pageCount < 1) { $pageCount = 15; } $data = $this->select("name", "description") ->where("is_del", false)->orderBy("created_at", "asc") ->paginate($pageCount, ["*"], "page", $page); return $data; } public function LoadRoleByIds($rids) { $data = $this->select("id", "name", "description", "status") ->whereIn("id", $rids)->where("is_del", false)->get(); return $data; } public function LoadRoleByIds_KV($rids) { $roles = $this->LoadRoleByIds($rids); $data = []; foreach ($roles as $role){ $data[$role["id"]] = $role; } return $data; } public function LoadRole($uid) { $roles = $this->where("user_id", $uid)->where("is_del", false)->get(); if (count($roles) == 0) { return ["code" => 0]; } $roleIds = []; foreach ($roles as $role) { array_push($roleIds, $role["role_id"]); } return []; } /** * 列出角色下的用户,如果列出指定角色下的用户,可以keyword指定为角色名即可. * * @param $keyword 根据role的名称来列出,支持模糊搜索 * @param $page 暂时没用 * @param $pageCount 暂时没用 * @return array */ public function LoadRoleUsers($keyword, $page, $pageCount) { // print $keyword; // print $page; // print $pageCount; // load all roles $roleQuery = $this->select("id", "name", "description"); if ($keyword != "") { $roleQuery = $roleQuery->where("name", "like", "%" . $keyword . "%")->where("is_del", false); } $roles = $roleQuery->get(); if (count($roles) < 1) { return []; } // load users by role ids. $rids = []; foreach ($roles as $role) { array_push($rids, $role["id"]); } // load users with roles. $roleUsers = new UserRole(); $users = $roleUsers->LoadUsersWithRoles($rids); // return result $users["roles"] = $roles; return $users; } }