AdminAuthCache.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\cache;
  20. use app\adminapi\logic\auth\AuthLogic;
  21. use app\common\{model\Admin, model\Role, model\RoleAuthIndex};
  22. use think\facade\Config;
  23. class AdminAuthCache extends BaseCache
  24. {
  25. private $prefix = 'admin_auth_';
  26. private $authConfigList = [];
  27. private $cacheMd5Key = ''; //权限文件MD5的key
  28. private $cacheAllKey = ''; //全部权限的key
  29. private $caheUrlKey = ''; //管理员的url缓存key
  30. private $cahePageKey = ''; //管理员的page缓存key
  31. private $authMd5 = ''; //权限文件MD5的值
  32. private $adminId = '';
  33. public function __construct($adminId = '')
  34. {
  35. parent::__construct();
  36. $this->adminId = $adminId;
  37. $this->authConfigList = Config::get('auth');
  38. // //当前权限配置文件的md5
  39. $this->authMd5 = md5(json_encode($this->authConfigList));
  40. $this->cacheMd5Key = $this->prefix.'md5';
  41. $this->cacheAllKey = $this->prefix.'all';
  42. $this->caheUrlKey = $this->prefix .'url_'.$this->adminId;
  43. $this->cahePageKey = $this->prefix .'page_'.$this->adminId;
  44. $cacheAuthMd5 = $this->get($this->cacheMd5Key);
  45. $cacheAuth = $this->get($this->cacheAllKey);
  46. //权限配置文件和缓存的配置文件对比,不一样说明权限配置文件已修改,清理缓存
  47. if($this->authMd5 !== $cacheAuthMd5 || empty($cacheAuth)){
  48. $this->deleteTag();
  49. }
  50. }
  51. /**
  52. * @notes 获取管理权限uri
  53. * @param $adminId
  54. * @return array|mixed
  55. * @author 令狐冲
  56. * @date 2021/8/19 15:27
  57. */
  58. public function getAdminUri()
  59. {
  60. //从缓存获取,直接返回
  61. $urisAuth = $this->get($this->caheUrlKey);
  62. if ($urisAuth) {
  63. return $urisAuth;
  64. }
  65. //获取角色所有权限id
  66. $roleAuthKeys = $this->getKeyAuth($this->adminId);
  67. if (empty($roleAuthKeys)) {
  68. return [];
  69. }
  70. //获取角色权限uri
  71. $urisAuth = AuthLogic::getAuth($roleAuthKeys);
  72. $this->set($this->caheUrlKey, $urisAuth, 3600);
  73. //保存到缓存并读取返回
  74. return $urisAuth;
  75. }
  76. /**
  77. * @notes 获取全部权限uri
  78. * @return array|mixed
  79. * @author cjhao
  80. * @date 2021/9/13 11:41
  81. */
  82. public function getAllUri()
  83. {
  84. $cacheAuth = $this->get($this->cacheAllKey);
  85. if($cacheAuth){
  86. return $cacheAuth;
  87. }
  88. $authList = [];
  89. foreach ($this->authConfigList as $authKey => $authConfig){
  90. foreach ($authConfig as $authValList){
  91. array_shift($authValList);
  92. foreach ($authValList as $authVal){
  93. $buttonAuth = $authVal['button_auth'] ?? [];
  94. $actionAuth = $authVal['action_auth'] ?? [];
  95. $authList = [
  96. 'button_auth' => array_merge($authList['button_auth'] ?? [],$buttonAuth),
  97. 'action_auth' => array_merge($authList['action_auth'] ?? [],$actionAuth),
  98. ];
  99. }
  100. }
  101. }
  102. //保存到缓存并读取返回
  103. $this->set($this->cacheMd5Key, $this->authMd5, null);
  104. $this->set($this->cacheAllKey, $authList, null);
  105. return $authList;
  106. }
  107. /**
  108. * @notes 获取管理员页面权限
  109. * @param int $adminId
  110. * @return array
  111. * @author cjhao
  112. * @date 2021/10/13 17:43
  113. */
  114. public function getAdminPageAuth():array
  115. {
  116. //从缓存获取,直接返回
  117. $pageAuth = $this->get($this->cahePageKey);
  118. if($pageAuth){
  119. return $pageAuth;
  120. }
  121. $roleAuthKeys = $this->getKeyAuth($this->adminId);
  122. if (empty($roleAuthKeys)) {
  123. return [];
  124. }
  125. $authConfigList = [];
  126. //处理权限数据结构
  127. foreach ($this->authConfigList as $configKey => $configList){
  128. foreach ($configList as $authKey => $authVal){
  129. $pagePathList = array_shift($authVal);
  130. if(is_string($pagePathList)){
  131. $pagePathList = [$pagePathList];
  132. }
  133. $pagePath = current($pagePathList);
  134. $buttonAuth = [];
  135. foreach ($authVal as $key => $auth ){
  136. $authKeys = $configKey.'/'.$authKey.'.'.$key;
  137. if(in_array($authKeys,$roleAuthKeys)){
  138. $buttonAuth = array_merge($buttonAuth,$auth['button_auth']);
  139. }
  140. }
  141. //相同的接口分开。
  142. $authConfigList[$pagePath] = $buttonAuth;
  143. foreach ($pagePathList as $pagePath){
  144. $authConfigList[$pagePath] = $buttonAuth;
  145. }
  146. }
  147. }
  148. //设置缓存
  149. $this->set($this->cahePageKey, $authConfigList, 3600);
  150. //保存到缓存并读取返回
  151. return $authConfigList;
  152. }
  153. /**
  154. * @notes 获取管理员权限
  155. * @param $adminId
  156. * @return array
  157. * @author cjhao
  158. * @date 2021/10/13 17:42
  159. */
  160. public function getKeyAuth(int $adminId):array
  161. {
  162. $role = Admin::with(['role_auth_index'])
  163. ->where(['id'=>$adminId])
  164. ->findOrEmpty()->toArray();
  165. if(empty($role)){
  166. return [];
  167. }
  168. $roleAuthKeys = array_column($role['role_auth_index'],'auth_key');
  169. return $roleAuthKeys;
  170. }
  171. /**
  172. * @notes 清理管理员缓存
  173. * @return bool
  174. * @author cjhao
  175. * @date 2021/10/13 18:47
  176. */
  177. public function clearAuthCache()
  178. {
  179. $this->clear($this->cahePageKey);
  180. $this->clear($this->caheUrlKey);
  181. return true;
  182. }
  183. }