Role.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 该类调用时应该在http控制层限制管理员才能调用
  6. *
  7. * Class Role
  8. * @package App\Models
  9. */
  10. class Role extends Model
  11. {
  12. protected $table = "roles";
  13. public $timestamps = false;
  14. /**
  15. * 创建一个用户的角色
  16. *
  17. * @param array $params
  18. * @return array
  19. */
  20. public function CreateRole(array $params)
  21. {
  22. if ($params["description"] == null) {
  23. $params["description"] = "";
  24. }
  25. $this->name = $params["name"];
  26. $this->description = $params["description"];
  27. $this->status = "normal";
  28. if ($this->name == "") {
  29. return ["code" => EMPTY_ROLE_NAME];
  30. }
  31. // check if has the same role name in system.
  32. $role = $this->where("name", $this->name)->where("is_del", false)->first();
  33. if ($role) {
  34. return ["code" => ALREADY_EXIST_ROLE];
  35. }
  36. //
  37. $this->save();
  38. $params["id"] = $this->getQueueableId();
  39. return ["code" => SUCCESS, "data" => $params];
  40. }
  41. /**
  42. * 更新用户角色的名字,描述等信息
  43. *
  44. * @param array $params
  45. * @return int
  46. */
  47. public function ModifyRole(array $params)
  48. {
  49. $update = [];
  50. $id = $params["id"];
  51. if ($id == "") {
  52. return EMPTY_ROLE_ID;
  53. }
  54. // check if role exist
  55. $role = $this->where("id", $id)->where("is_del", false)->first();
  56. if (!$role) {
  57. return INVALID_ROLE_ID;
  58. }
  59. if ($params["name"] != "") {
  60. $update["name"] = $params["name"];
  61. }
  62. if ($params["description"] != "") {
  63. $update["description"] = $params["description"];
  64. }
  65. if (count($update) == 0) {
  66. return NOTHING_UPDATE;
  67. }
  68. $this->where("id", $id)->where("is_del", false)->update($update);
  69. return SUCCESS;
  70. }
  71. /**
  72. * 删除一个用户角色
  73. *
  74. * @param array $params
  75. * @return int
  76. */
  77. public function DeleteRole(array $params)
  78. {
  79. $id = $params["id"];
  80. if ($id == "") {
  81. return EMPTY_ROLE_ID;
  82. }
  83. $role = $this->where("id", $id)->where("is_del", false)->first();
  84. if (!$role) {
  85. return INVALID_ROLE_ID;
  86. }
  87. $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
  88. return SUCCESS;
  89. }
  90. /**
  91. * 列出用户角色列表
  92. *
  93. * @param array $params
  94. * @return mixed
  95. */
  96. public function ListRole(array $params)
  97. {
  98. $page = $params["page"];
  99. $pageCount = $params["pageCount"];
  100. if ($page < 1) {
  101. $page = 1;
  102. }
  103. if ($pageCount > 15 || $pageCount < 1) {
  104. $pageCount = 15;
  105. }
  106. $data = $this->select("name", "description")
  107. ->where("is_del", false)->orderBy("created_at", "asc")
  108. ->paginate($pageCount, ["*"], "page", $page);
  109. return $data;
  110. }
  111. }