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 array */ public function ModifyRole(array $params) { $update = []; $id = $params["id"]; if ($id == "") { return ["code" => EMPTY_ROLE_ID]; } // check if role exist $role = $this->where("id", $id)->where("is_del", false)->first(); if (!$role) { return ["code" => 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); $role = $this->select("id", "name", "description", "status") ->where("id", $id)->where("is_del", false)->first(); return ["code" => SUCCESS, "data" => $role]; } /** * 删除一个用户角色 * * @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; } $opera = $this->select("id", "name", "description"); if ($params["keyword"] != "") { $opera = $opera->where("name", "like", "%" . $params["keyword"] . "%"); } $data = $opera->where("is_del", false)->orderBy("created_at", "asc") ->paginate($pageCount, ["*"], "page", $page); $roleDataArr = json_decode(json_encode($data), true); $roleDataArr["roles"] = $roleDataArr["data"]; unset($roleDataArr["first_page_url"]); unset($roleDataArr["from"]); unset($roleDataArr["last_page"]); unset($roleDataArr["last_page_url"]); unset($roleDataArr["next_page_url"]); unset($roleDataArr["path"]); unset($roleDataArr["prev_page_url"]); unset($roleDataArr["to"]); unset($roleDataArr["data"]); return $roleDataArr; } 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; } /** * 列出角色下的用户,如果列出指定角色下的用户,可以keyword指定为角色名即可. * * @param string $keyword 根据role的名称来列出,支持模糊搜索 * @param int $page 用户角色分页 * @param int $pageCount 用户角色分页 * @param string $ukeyword 根据角色下用户的名称来列出,支持模糊搜索 * @param int $upage 用户角色下的用户分页 * @param int $upageCount 用户角色下的用户分页 * @return array */ public function LoadRoleUsers($keyword, $page, $pageCount, $ukeyword, $upage, $upageCount) { $page = $page < 1 ? 1 : $page; $pageCount = $pageCount < 1 || $pageCount > 15 ? 15 : $pageCount; // $upage = $upage < 1 ? 1 : $upage; // $upageCount = $upageCount < 1 || $upageCount > 15 ? 15 : $upageCount; // load all roles $roleQuery = $this->select("id", "name", "description"); if ($keyword != "") { $roleQuery = $roleQuery->where("name", "like", "%" . $keyword . "%")->where("is_del", false); } $total = $roleQuery->count(); $roles = $roleQuery->offset(($page - 1) * $pageCount)->limit($pageCount)->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 $result = ["roles" => $roles, "roleUser" => $users, "total" => $total]; return $result; } }