Role.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 int
  47. */
  48. public function ModifyRole(array $params)
  49. {
  50. $update = [];
  51. $id = $params["id"];
  52. if ($id == "") {
  53. return 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 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. return SUCCESS;
  71. }
  72. /**
  73. * 删除一个用户角色
  74. *
  75. * @param array $params
  76. * @return int
  77. */
  78. public function DeleteRole(array $params)
  79. {
  80. $id = $params["id"];
  81. if ($id == "") {
  82. return EMPTY_ROLE_ID;
  83. }
  84. $role = $this->where("id", $id)->where("is_del", false)->first();
  85. if (!$role) {
  86. return INVALID_ROLE_ID;
  87. }
  88. $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
  89. return SUCCESS;
  90. }
  91. /**
  92. * 列出用户角色列表
  93. *
  94. * @param array $params
  95. * @return mixed
  96. */
  97. public function ListRole(array $params)
  98. {
  99. $page = $params["page"];
  100. $pageCount = $params["pageCount"];
  101. if ($page < 1) {
  102. $page = 1;
  103. }
  104. if ($pageCount > 15 || $pageCount < 1) {
  105. $pageCount = 15;
  106. }
  107. $data = $this->select("name", "description")
  108. ->where("is_del", false)->orderBy("created_at", "asc")
  109. ->paginate($pageCount, ["*"], "page", $page);
  110. return $data;
  111. }
  112. public function LoadRoleByIds($rids)
  113. {
  114. $data = $this->select("id", "name", "description", "status")
  115. ->whereIn("id", $rids)->where("is_del", false)->get();
  116. return $data;
  117. }
  118. public function LoadRoleByIds_KV($rids)
  119. {
  120. $roles = $this->LoadRoleByIds($rids);
  121. $data = [];
  122. foreach ($roles as $role){
  123. $data[$role["id"]] = $role;
  124. }
  125. return $data;
  126. }
  127. public function LoadRole($uid)
  128. {
  129. $roles = $this->where("user_id", $uid)->where("is_del", false)->get();
  130. if (count($roles) == 0) {
  131. return ["code" => 0];
  132. }
  133. $roleIds = [];
  134. foreach ($roles as $role) {
  135. array_push($roleIds, $role["role_id"]);
  136. }
  137. return [];
  138. }
  139. /**
  140. * 列出角色下的用户,如果列出指定角色下的用户,可以keyword指定为角色名即可.
  141. *
  142. * @param $keyword 根据role的名称来列出,支持模糊搜索
  143. * @param $page 暂时没用
  144. * @param $pageCount 暂时没用
  145. * @return array
  146. */
  147. public function LoadRoleUsers($keyword, $page, $pageCount)
  148. {
  149. // print $keyword;
  150. // print $page;
  151. // print $pageCount;
  152. // load all roles
  153. $roleQuery = $this->select("id", "name", "description");
  154. if ($keyword != "") {
  155. $roleQuery = $roleQuery->where("name", "like", "%" . $keyword . "%")->where("is_del", false);
  156. }
  157. $roles = $roleQuery->get();
  158. if (count($roles) < 1) {
  159. return [];
  160. }
  161. // load users by role ids.
  162. $rids = [];
  163. foreach ($roles as $role) {
  164. array_push($rids, $role["id"]);
  165. }
  166. // load users with roles.
  167. $roleUsers = new UserRole();
  168. $users = $roleUsers->LoadUsersWithRoles($rids);
  169. // return result
  170. $users["roles"] = $roles;
  171. return $users;
  172. }
  173. }