Role.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 LoadRole($uid)
  113. {
  114. $roles = $this->where("user_id", $uid)->where("is_del", false)->get();
  115. if (count($roles) == 0) {
  116. return ["code" => 0];
  117. }
  118. $roleIds = [];
  119. foreach ($roles as $role) {
  120. array_push($roleIds, $role["role_id"]);
  121. }
  122. return [];
  123. }
  124. /**
  125. * 列出角色下的用户,如果列出指定角色下的用户,可以keyword指定为角色名即可.
  126. *
  127. * @param $keyword 根据role的名称来列出,支持模糊搜索
  128. * @param $page 暂时没用
  129. * @param $pageCount 暂时没用
  130. * @return array
  131. */
  132. public function LoadRoleUsers($keyword, $page, $pageCount)
  133. {
  134. // print $keyword;
  135. // print $page;
  136. // print $pageCount;
  137. // load all roles
  138. $roleQuery = $this->select("id", "name", "description");
  139. if ($keyword != "") {
  140. $roleQuery = $roleQuery->where("name", "like", "%" . $keyword . "%")->where("is_del", false);
  141. }
  142. $roles = $roleQuery->get();
  143. if (count($roles) < 1) {
  144. return [];
  145. }
  146. // load users by role ids.
  147. $rids = [];
  148. foreach ($roles as $role) {
  149. array_push($rids, $role["id"]);
  150. }
  151. // load users with roles.
  152. $roleUsers = new UserRole();
  153. $users = $roleUsers->LoadUsersWithRoles($rids);
  154. // return result
  155. $users["roles"] = $roles;
  156. return $users;
  157. }
  158. }