Auth.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace app\store\service;
  3. use app\store\model\store\Access;
  4. use think\Session;
  5. use app\store\model\store\User;
  6. use app\store\model\store\UserRole;
  7. use app\store\model\store\RoleAccess;
  8. /**
  9. * 商家后台权限业务
  10. * Class Auth
  11. * @package app\admin\service
  12. */
  13. class Auth
  14. {
  15. /** @var self $instance 存放实例 */
  16. static public $instance;
  17. /** @var array $store 商家登录信息 */
  18. private $store;
  19. /** @var User $user 商家用户信息 */
  20. private $user;
  21. /** @var array $allowAllAction 权限验证白名单 */
  22. protected $allowAllAction = [
  23. // 测试入口
  24. 'index/test',
  25. // 用户登录
  26. 'passport/login',
  27. // 退出登录
  28. 'passport/logout',
  29. // 修改当前用户信息
  30. 'store.user/renew',
  31. // 文件库
  32. 'upload.library/*',
  33. // 图片上传
  34. 'upload/image',
  35. // 数据选择
  36. 'data/*',
  37. // 添加商品规格
  38. 'goods.spec/*',
  39. // 订单批量发货模板
  40. 'order.operate/deliverytpl',
  41. // 物流公司编码表
  42. 'setting.express/company',
  43. // 帮助信息
  44. 'setting.help/*',
  45. // 腾讯地图坐标选取器
  46. 'shop/getpoint',
  47. ];
  48. /** @var array $accessUrls 商家用户权限url */
  49. private $accessUrls = [];
  50. /**
  51. * 公有化获取实例方法
  52. * @return Auth
  53. * @throws \think\exception\DbException
  54. */
  55. public static function getInstance()
  56. {
  57. if (!(self::$instance instanceof Auth)) {
  58. self::$instance = new self;
  59. }
  60. return self::$instance;
  61. }
  62. /**
  63. * 私有化构造方法
  64. * Auth constructor.
  65. * @throws \think\exception\DbException
  66. */
  67. private function __construct()
  68. {
  69. // 商家登录信息
  70. $this->store = Session::get('yoshop_store');
  71. // 当前用户信息
  72. $this->user = User::detail($this->store['user']['store_user_id']);
  73. }
  74. /**
  75. * 私有化克隆方法
  76. */
  77. private function __clone()
  78. {
  79. }
  80. /**
  81. * 验证指定url是否有访问权限
  82. * @param string|array $url
  83. * @param bool $strict 严格模式(必须全部通过才返回true)
  84. * @return bool
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. * @throws \think\exception\DbException
  88. */
  89. public function checkPrivilege($url, $strict = true)
  90. {
  91. if (!is_array($url)):
  92. return $this->checkAccess($url);
  93. else:
  94. foreach ($url as $val):
  95. if ($strict && !$this->checkAccess($val)) {
  96. return false;
  97. }
  98. if (!$strict && $this->checkAccess($val)) {
  99. return true;
  100. }
  101. endforeach;
  102. endif;
  103. return true;
  104. }
  105. /**
  106. * @param string $url
  107. * @return bool
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. * @throws \think\exception\DbException
  111. */
  112. private function checkAccess($url)
  113. {
  114. // 超级管理员无需验证
  115. if ($this->user['is_super']) {
  116. return true;
  117. }
  118. // 验证当前请求是否在白名单
  119. if (in_array($url, $this->allowAllAction)) {
  120. return true;
  121. }
  122. // 通配符支持
  123. foreach ($this->allowAllAction as $action) {
  124. if (strpos($action, '*') !== false
  125. && preg_match('/^' . str_replace('/', '\/', $action) . '/', $url)
  126. ) {
  127. return true;
  128. }
  129. }
  130. // 获取当前用户的权限url列表
  131. if (!in_array($url, $this->getAccessUrls())) {
  132. return false;
  133. }
  134. return true;
  135. }
  136. /**
  137. * 获取当前用户的权限url列表
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. * @throws \think\exception\DbException
  141. */
  142. private function getAccessUrls()
  143. {
  144. if (empty($this->accessUrls)) {
  145. // 获取当前用户的角色集
  146. $roleIds = UserRole::getRoleIds($this->user['store_user_id']);
  147. // 根据已分配的权限
  148. $accessIds = RoleAccess::getAccessIds($roleIds);
  149. // 获取当前角色所有权限链接
  150. $this->accessUrls = Access::getAccessUrls($accessIds);
  151. }
  152. return $this->accessUrls;
  153. }
  154. }