123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?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 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;
- }
- /**
- * 列出角色下的用户,如果列出指定角色下的用户,可以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;
- }
- }
|