Auth.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. 'statistics.data/survey',
  49. ];
  50. /** @var array $accessUrls 商家用户权限url */
  51. private $accessUrls = [];
  52. /**
  53. * 公有化获取实例方法
  54. * @return Auth
  55. * @throws \think\exception\DbException
  56. */
  57. public static function getInstance()
  58. {
  59. if (!(self::$instance instanceof Auth)) {
  60. self::$instance = new self;
  61. }
  62. return self::$instance;
  63. }
  64. /**
  65. * 私有化构造方法
  66. * Auth constructor.
  67. * @throws \think\exception\DbException
  68. */
  69. private function __construct()
  70. {
  71. // 商家登录信息
  72. $this->store = Session::get('yoshop_store');
  73. // 当前用户信息
  74. if (!empty($this->store)) {
  75. $this->user = User::detail($this->store['user']['store_user_id']);
  76. }
  77. }
  78. /**
  79. * 私有化克隆方法
  80. */
  81. private function __clone()
  82. {
  83. }
  84. /**
  85. * 验证指定url是否有访问权限
  86. * @param string|array $url
  87. * @param bool $strict 严格模式(必须全部通过才返回true)
  88. * @return bool
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. * @throws \think\exception\DbException
  92. */
  93. public function checkPrivilege($url, $strict = true)
  94. {
  95. if (!is_array($url)):
  96. return $this->checkAccess($url);
  97. else:
  98. foreach ($url as $val):
  99. if ($strict && !$this->checkAccess($val)) {
  100. return false;
  101. }
  102. if (!$strict && $this->checkAccess($val)) {
  103. return true;
  104. }
  105. endforeach;
  106. endif;
  107. return true;
  108. }
  109. /**
  110. * 验证url的权限
  111. * @param string $url
  112. * @return bool
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. * @throws \think\exception\DbException
  116. */
  117. private function checkAccess($url)
  118. {
  119. // 超级管理员无需验证
  120. if (!empty($this->user) && $this->user['is_super']) {
  121. return true;
  122. }
  123. // 验证当前请求是否在白名单
  124. if (in_array($url, $this->allowAllAction)) {
  125. return true;
  126. }
  127. // 通配符支持
  128. foreach ($this->allowAllAction as $action) {
  129. if (strpos($action, '*') !== false
  130. && preg_match('/^' . str_replace('/', '\/', $action) . '/', $url)
  131. ) {
  132. return true;
  133. }
  134. }
  135. // 获取当前用户的权限url列表
  136. if (!in_array($url, $this->getAccessUrls())) {
  137. return false;
  138. }
  139. return true;
  140. }
  141. /**
  142. * 获取当前用户的权限url列表
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. * @throws \think\exception\DbException
  146. */
  147. private function getAccessUrls()
  148. {
  149. if (empty($this->accessUrls)) {
  150. // 获取当前用户的角色集
  151. $roleIds = UserRole::getRoleIds($this->user['store_user_id']);
  152. // 根据已分配的权限
  153. $accessIds = RoleAccess::getAccessIds($roleIds);
  154. // 获取当前角色所有权限链接
  155. $this->accessUrls = Access::getAccessUrls($accessIds);
  156. }
  157. return $this->accessUrls;
  158. }
  159. }