123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Log;
- /**
- * 该类调用时应该在http控制层限制管理员才能调用
- *
- * Class Role
- * @package App\Models
- */
- class Role extends Model
- {
- protected $table = "roles";
- public $timestamps = false;
- /**
- * 创建一个用户的角色
- *
- * @param array $params
- * @return array
- */
- public function CreateRole(array $params)
- {
- if ($params["description"] == null) {
- $params["description"] = "";
- }
- $this->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;
- }
- }
|