SystemRole.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/13
  5. */
  6. namespace app\models\system;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use think\facade\Cache;
  10. use think\facade\Db;
  11. /**
  12. * 身份管理 model
  13. * Class SystemRole
  14. * @package app\models\system
  15. */
  16. class SystemRole extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'system_role';
  28. /**
  29. * 所有权限缓存前缀
  30. */
  31. const ADMIN_RULES_ALL = 'Admin_rules_all_';
  32. /**
  33. * 当前管理员权限缓存前缀
  34. */
  35. const ADMIN_RULES_LEVEL = 'Admin_rules_level_';
  36. /**
  37. * 验证规则缓存
  38. * @var bool
  39. */
  40. protected static $isCache = true;
  41. use ModelTrait;
  42. public static function setRulesAttr($value)
  43. {
  44. return is_array($value) ? implode(',', $value) : $value;
  45. }
  46. /**
  47. * 选择管理员身份
  48. * @param int $level
  49. * @return array
  50. */
  51. public static function getRole($level = 0)
  52. {
  53. return self::where('status', 1)->where('level', $level)->column('role_name', 'id');
  54. }
  55. public static function getAllRule(string $module, $cachePrefix = self::ADMIN_RULES_ALL): array
  56. {
  57. static $auth = null;
  58. if (Cache::has($cachePrefix) && ($auth = Cache::get($cachePrefix)) && !self::$isCache) return $auth;
  59. $auth === null && ($auth = SystemMenus::where('module', $module)->where('rule', '<>', '')->column('rule', 'rule'));
  60. if (is_null($auth))
  61. $auth = [];
  62. else
  63. Cache::set($cachePrefix, $auth);
  64. return $auth;
  65. }
  66. /**
  67. * 获取当前管理员可访问的权限
  68. * @param $rules
  69. * @return array
  70. */
  71. public static function getRolesByAuthRule($module, $rules, $cachePrefix = self::ADMIN_RULES_LEVEL)
  72. {
  73. $cacheName = $cachePrefix . $rules;
  74. if (empty($rules)) return [];
  75. if (Cache::has($cacheName) && ($_auth = Cache::get($cacheName)) && !self::$isCache) return $_auth;
  76. $rules = self::where('id', 'IN', $rules)->where('status', '1')->column('rules', 'id');
  77. $rules = array_unique(explode(',', implode(',', $rules)));
  78. $_auth = SystemMenus::where('module', $module)->whereIn('id', $rules)->column('rule', 'rule');
  79. if (is_null($_auth))
  80. $_auth = [];
  81. else
  82. Cache::set($cacheName, $_auth);
  83. return $_auth;
  84. }
  85. public static function rolesByAuth($rules, $type = 1, $cachePrefix = self::ADMIN_RULES_LEVEL)
  86. {
  87. $cacheName = $cachePrefix . $rules . '_' . $type;
  88. if (empty($rules)) return [];
  89. if (Cache::has($cacheName) && ($_auth = Cache::get($cacheName)) && !self::$isCache) return $_auth;
  90. $rules = self::where('id', 'IN', $rules)->where('status', '1')->column('rules', 'id');
  91. $rules = array_unique(explode(',', implode(',', $rules)));
  92. $_auth = SystemMenus::all(function ($query) use ($rules, $type) {
  93. $query->where('id', 'IN', $rules)->where('type', $type)
  94. ->where('controller|action', '<>', '')
  95. ->field('module,controller,action,params');
  96. });
  97. $_auth = self::tidyAuth($_auth ?: []);
  98. if (count($_auth)) Cache::set($cacheName, $_auth);
  99. return $_auth;
  100. }
  101. public static function getAllAuth($type = 1, $cachePrefix = self::ADMIN_RULES_ALL)
  102. {
  103. $cachePrefix = $cachePrefix . $type;
  104. static $auth = null;
  105. $auth === null && ($auth = SystemMenus::where('api_url', '<>', '')->where('auth_type', $type)->column('api_url', 'methods'));
  106. return $auth;
  107. }
  108. /**
  109. * 获取所有权限
  110. * @param int $type
  111. * @param string $cachePrefix
  112. * @return mixed
  113. */
  114. public function getAllRuleAuth(int $type = 1, string $cachePrefix = self::ADMIN_RULES_ALL)
  115. {
  116. $cachePrefix = $cachePrefix . $type;
  117. return Cache::remember($cachePrefix, function () use ($type) {
  118. return SystemMenus::where('api_url', '<>', '')->where('auth_type', $type)->field(['api_url', 'methods'])->select()->toArray();
  119. });
  120. }
  121. /**
  122. * 获取指定权限
  123. * @param array $rules
  124. * @param int $type
  125. * @param string $cachePrefix
  126. * @return mixed
  127. */
  128. public function getRolesByAuth(array $rules, int $type = 1, string $cachePrefix = self::ADMIN_RULES_LEVEL)
  129. {
  130. if (empty($rules)) return [];
  131. $cacheName = $cachePrefix . '_' . $type;
  132. return Cache::remember($cacheName, function () use ($rules, $type) {
  133. return SystemMenus::whereIn('id', $this->getRoleIds($rules))->where('auth_type', $type)->field(['api_url', 'methods'])->select()->toArray();
  134. });
  135. }
  136. /**
  137. * 获取权限id
  138. * @param array $rules
  139. * @return array
  140. */
  141. public function getRoleIds(array $rules)
  142. {
  143. $rules = self::where('id', 'IN', $rules)->where('status', '1')->column('rules', 'id');
  144. return array_unique(explode(',', implode(',', $rules)));
  145. }
  146. protected static function tidyAuth($_auth)
  147. {
  148. $auth = [];
  149. foreach ($_auth as $k => $val) {
  150. $auth[] = SystemMenus::getAuthName($val['action'], $val['controller'], $val['module'], $val['params']);
  151. }
  152. return $auth;
  153. }
  154. public static function systemPage($where)
  155. {
  156. $model = new self;
  157. if (strlen(trim($where['role_name']))) $model = $model->where('role_name', 'LIKE', "%$where[role_name]%");
  158. if (strlen(trim($where['status']))) $model = $model->where('status', $where['status']);
  159. $model = $model->where('level', bcadd($where['level'], 1, 0));
  160. $model = $model->order('id desc');
  161. $count = $model->count();
  162. $list = $model->page((int)$where['page'], (int)$where['limit'])
  163. ->select()
  164. ->each((function ($item) {
  165. $item->rules = SystemMenus::where('id', 'IN', $item->rules)->column('menu_name', 'id');
  166. }));
  167. return compact('count', 'list');
  168. }
  169. }