Auth.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. $storeUserId = isset($this->store['user']['store_user_id'])? $this->store['user']['store_user_id'] : 0;
  73. $this->user = User::detail($storeUserId);
  74. }
  75. /**
  76. * 私有化克隆方法
  77. */
  78. private function __clone()
  79. {
  80. }
  81. /**
  82. * 验证指定url是否有访问权限
  83. * @param string|array $url
  84. * @param bool $strict 严格模式(必须全部通过才返回true)
  85. * @return bool
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. public function checkPrivilege($url, $strict = true)
  91. {
  92. if (!is_array($url)):
  93. return $this->checkAccess($url);
  94. else:
  95. foreach ($url as $val):
  96. if ($strict && !$this->checkAccess($val)) {
  97. return false;
  98. }
  99. if (!$strict && $this->checkAccess($val)) {
  100. return true;
  101. }
  102. endforeach;
  103. endif;
  104. return true;
  105. }
  106. /**
  107. * @param string $url
  108. * @return bool
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. * @throws \think\exception\DbException
  112. */
  113. private function checkAccess($url)
  114. {
  115. var_dump(6);
  116. // 超级管理员无需验证
  117. if ($this->user && $this->user['is_super']) {
  118. return true;
  119. }
  120. // 验证当前请求是否在白名单
  121. if (in_array($url, $this->allowAllAction)) {
  122. return true;
  123. }
  124. // 通配符支持
  125. foreach ($this->allowAllAction as $action) {
  126. if (strpos($action, '*') !== false
  127. && preg_match('/^' . str_replace('/', '\/', $action) . '/', $url)
  128. ) {
  129. return true;
  130. }
  131. }
  132. // 获取当前用户的权限url列表
  133. if (!in_array($url, $this->getAccessUrls())) {
  134. return false;
  135. }
  136. return true;
  137. }
  138. /**
  139. * 获取当前用户的权限url列表
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\ModelNotFoundException
  142. * @throws \think\exception\DbException
  143. */
  144. private function getAccessUrls()
  145. {
  146. if (empty($this->accessUrls)) {
  147. // 获取当前用户的角色集
  148. $storeUserId = isset($this->user['store_user_id'])? $this->user['store_user_id'] : 0;
  149. $roleIds = UserRole::getRoleIds($storeUserId);
  150. // 根据已分配的权限
  151. $accessIds = RoleAccess::getAccessIds($roleIds);
  152. // 获取当前角色所有权限链接
  153. $this->accessUrls = Access::getAccessUrls($accessIds);
  154. }
  155. return $this->accessUrls;
  156. }
  157. }