AdminAuthCache.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\cache;
  15. use app\adminapi\logic\auth\AuthLogic;
  16. /**
  17. * 管理员权限缓存
  18. * Class AdminAuthCache
  19. * @package app\common\cache
  20. */
  21. class AdminAuthCache extends BaseCache
  22. {
  23. private $prefix = 'admin_auth_';
  24. private $authConfigList = [];
  25. private $cacheMd5Key = ''; //权限文件MD5的key
  26. private $cacheAllKey = ''; //全部权限的key
  27. private $cacheUrlKey = ''; //管理员的url缓存key
  28. private $authMd5 = ''; //权限文件MD5的值
  29. private $adminId = '';
  30. public function __construct($adminId = '')
  31. {
  32. parent::__construct();
  33. $this->adminId = $adminId;
  34. // 全部权限
  35. $this->authConfigList = AuthLogic::getAllAuth();
  36. // 当前权限配置文件的md5
  37. $this->authMd5 = md5(json_encode($this->authConfigList));
  38. $this->cacheMd5Key = $this->prefix . 'md5';
  39. $this->cacheAllKey = $this->prefix . 'all';
  40. $this->cacheUrlKey = $this->prefix . 'url_' . $this->adminId;
  41. $cacheAuthMd5 = $this->get($this->cacheMd5Key);
  42. $cacheAuth = $this->get($this->cacheAllKey);
  43. //权限配置和缓存权限对比,不一样说明权限配置文件已修改,清理缓存
  44. if ($this->authMd5 !== $cacheAuthMd5 || empty($cacheAuth)) {
  45. $this->deleteTag();
  46. }
  47. }
  48. /**
  49. * @notes 获取管理权限uri
  50. * @param $adminId
  51. * @return array|mixed
  52. * @author 令狐冲
  53. * @date 2021/8/19 15:27
  54. */
  55. public function getAdminUri()
  56. {
  57. //从缓存获取,直接返回
  58. $urisAuth = $this->get($this->cacheUrlKey);
  59. if ($urisAuth) {
  60. return $urisAuth;
  61. }
  62. //获取角色关联的菜单id(菜单或权限)
  63. $urisAuth = AuthLogic::getAuthByAdminId($this->adminId);
  64. if (empty($urisAuth)) {
  65. return [];
  66. }
  67. $this->set($this->cacheUrlKey, $urisAuth, 3600);
  68. //保存到缓存并读取返回
  69. return $urisAuth;
  70. }
  71. /**
  72. * @notes 获取全部权限uri
  73. * @return array|mixed
  74. * @author cjhao
  75. * @date 2021/9/13 11:41
  76. */
  77. public function getAllUri()
  78. {
  79. $cacheAuth = $this->get($this->cacheAllKey);
  80. if ($cacheAuth) {
  81. return $cacheAuth;
  82. }
  83. // 获取全部权限
  84. $authList = AuthLogic::getAllAuth();
  85. //保存到缓存并读取返回
  86. $this->set($this->cacheMd5Key, $this->authMd5);
  87. $this->set($this->cacheAllKey, $authList);
  88. return $authList;
  89. }
  90. /**
  91. * @notes 清理管理员缓存
  92. * @return bool
  93. * @author cjhao
  94. * @date 2021/10/13 18:47
  95. */
  96. public function clearAuthCache()
  97. {
  98. $this->clear($this->cacheUrlKey);
  99. return true;
  100. }
  101. }