Role.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Log;
  5. /**
  6. * 该类调用时应该在http控制层限制管理员才能调用
  7. *
  8. * Class Role
  9. * @package App\Models
  10. */
  11. class Role extends Model
  12. {
  13. protected $table = "roles";
  14. public $timestamps = false;
  15. /**
  16. * 创建一个用户的角色
  17. *
  18. * @param array $params
  19. * @return array
  20. */
  21. public function CreateRole(array $params)
  22. {
  23. if ($params["description"] == null) {
  24. $params["description"] = "";
  25. }
  26. $this->name = $params["name"];
  27. $this->description = $params["description"];
  28. $this->status = "normal";
  29. if ($this->name == "") {
  30. return ["code" => EMPTY_ROLE_NAME];
  31. }
  32. // check if has the same role name in system.
  33. $role = $this->where("name", $this->name)->where("is_del", false)->first();
  34. if ($role) {
  35. return ["code" => ALREADY_EXIST_ROLE];
  36. }
  37. //
  38. $this->save();
  39. $params["id"] = $this->getQueueableId();
  40. return ["code" => SUCCESS, "data" => $params];
  41. }
  42. /**
  43. * 更新用户角色的名字,描述等信息
  44. *
  45. * @param array $params
  46. * @return array
  47. */
  48. public function ModifyRole(array $params)
  49. {
  50. $update = [];
  51. $id = $params["id"];
  52. if ($id == "") {
  53. return ["code" => EMPTY_ROLE_ID];
  54. }
  55. // check if role exist
  56. $role = $this->where("id", $id)->where("is_del", false)->first();
  57. if (!$role) {
  58. return ["code" => INVALID_ROLE_ID];
  59. }
  60. if ($params["name"] != "") {
  61. $update["name"] = $params["name"];
  62. }
  63. if ($params["description"] != "") {
  64. $update["description"] = $params["description"];
  65. }
  66. if (count($update) == 0) {
  67. return NOTHING_UPDATE;
  68. }
  69. $this->where("id", $id)->where("is_del", false)->update($update);
  70. $role = $this->select("id", "name", "description", "status")
  71. ->where("id", $id)->where("is_del", false)->first();
  72. return ["code" => SUCCESS, "data" => $role];
  73. }
  74. /**
  75. * 删除一个用户角色
  76. *
  77. * @param array $params
  78. * @return int
  79. */
  80. public function DeleteRole(array $params)
  81. {
  82. $id = $params["id"];
  83. if ($id == "") {
  84. return EMPTY_ROLE_ID;
  85. }
  86. $role = $this->where("id", $id)->where("is_del", false)->first();
  87. if (!$role) {
  88. return INVALID_ROLE_ID;
  89. }
  90. $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
  91. return SUCCESS;
  92. }
  93. /**
  94. * 列出用户角色列表
  95. *
  96. * @param array $params
  97. * @return mixed
  98. */
  99. public function ListRole(array $params)
  100. {
  101. $page = $params["page"];
  102. $pageCount = $params["pageCount"];
  103. if ($page < 1) {
  104. $page = 1;
  105. }
  106. if ($pageCount > 15 || $pageCount < 1) {
  107. $pageCount = 15;
  108. }
  109. $opera = $this->select("id", "name", "description");
  110. if ($params["keyword"] != "") {
  111. $opera = $opera->where("name", "like", "%" . $params["keyword"] . "%");
  112. }
  113. $data = $opera->where("is_del", false)->orderBy("created_at", "asc")
  114. ->paginate($pageCount, ["*"], "page", $page);
  115. $roleDataArr = json_decode(json_encode($data), true);
  116. $roleDataArr["roles"] = $roleDataArr["data"];
  117. unset($roleDataArr["first_page_url"]);
  118. unset($roleDataArr["from"]);
  119. unset($roleDataArr["last_page"]);
  120. unset($roleDataArr["last_page_url"]);
  121. unset($roleDataArr["next_page_url"]);
  122. unset($roleDataArr["path"]);
  123. unset($roleDataArr["prev_page_url"]);
  124. unset($roleDataArr["to"]);
  125. unset($roleDataArr["data"]);
  126. return $roleDataArr;
  127. }
  128. public function LoadRoleByIds($rids)
  129. {
  130. $data = $this->select("id", "name", "description", "status")
  131. ->whereIn("id", $rids)->where("is_del", false)->get();
  132. return $data;
  133. }
  134. public function LoadRoleByIds_KV($rids)
  135. {
  136. $roles = $this->LoadRoleByIds($rids);
  137. $data = [];
  138. foreach ($roles as $role){
  139. $data[$role["id"]] = $role;
  140. }
  141. return $data;
  142. }
  143. /**
  144. * 列出角色下的用户,如果列出指定角色下的用户,可以keyword指定为角色名即可.
  145. *
  146. * @param string $keyword 根据role的名称来列出,支持模糊搜索
  147. * @param int $page 用户角色分页
  148. * @param int $pageCount 用户角色分页
  149. * @param string $ukeyword 根据角色下用户的名称来列出,支持模糊搜索
  150. * @param int $upage 用户角色下的用户分页
  151. * @param int $upageCount 用户角色下的用户分页
  152. * @return array
  153. */
  154. public function LoadRoleUsers($keyword, $page, $pageCount, $ukeyword, $upage, $upageCount)
  155. {
  156. $page = $page < 1 ? 1 : $page;
  157. $pageCount = $pageCount < 1 || $pageCount > 15 ? 15 : $pageCount;
  158. // $upage = $upage < 1 ? 1 : $upage;
  159. // $upageCount = $upageCount < 1 || $upageCount > 15 ? 15 : $upageCount;
  160. // load all roles
  161. $roleQuery = $this->select("id", "name", "description");
  162. if ($keyword != "") {
  163. $roleQuery = $roleQuery->where("name", "like", "%" . $keyword . "%")->where("is_del", false);
  164. }
  165. $total = $roleQuery->count();
  166. $roles = $roleQuery->offset(($page - 1) * $pageCount)->limit($pageCount)->get();
  167. if (count($roles) < 1) {
  168. return [];
  169. }
  170. // load users by role ids.
  171. $rids = [];
  172. foreach ($roles as $role) {
  173. array_push($rids, $role["id"]);
  174. }
  175. // load users with roles.
  176. $roleUsers = new UserRole();
  177. $users = $roleUsers->LoadUsersWithRoles($rids);
  178. // return result
  179. $result = ["roles" => $roles, "roleUser" => $users, "total" => $total];
  180. return $result;
  181. }
  182. }