Auth.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. var_dump(7);
  93. var_dump($url);
  94. if (!is_array($url)):
  95. return $this->checkAccess($url);
  96. else:
  97. foreach ($url as $val):
  98. if ($strict && !$this->checkAccess($val)) {
  99. return false;
  100. }
  101. if (!$strict && $this->checkAccess($val)) {
  102. return true;
  103. }
  104. endforeach;
  105. endif;
  106. return true;
  107. }
  108. /**
  109. * @param string $url
  110. * @return bool
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @throws \think\exception\DbException
  114. */
  115. private function checkAccess($url)
  116. {
  117. if(empty($url)){
  118. return false;
  119. }
  120. // 超级管理员无需验证
  121. if ($this->user && $this->user['is_super']) {
  122. return true;
  123. }
  124. // 验证当前请求是否在白名单
  125. if (in_array($url, $this->allowAllAction)) {
  126. return true;
  127. }
  128. // 通配符支持
  129. foreach ($this->allowAllAction as $action) {
  130. if (strpos($action, '*') !== false
  131. && preg_match('/^' . str_replace('/', '\/', $action) . '/', $url)
  132. ) {
  133. return true;
  134. }
  135. }
  136. // 获取当前用户的权限url列表
  137. if (!in_array($url, $this->getAccessUrls())) {
  138. return false;
  139. }
  140. return true;
  141. }
  142. /**
  143. * 获取当前用户的权限url列表
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\ModelNotFoundException
  146. * @throws \think\exception\DbException
  147. */
  148. private function getAccessUrls()
  149. {
  150. if (empty($this->accessUrls)) {
  151. // 获取当前用户的角色集
  152. $storeUserId = isset($this->user['store_user_id'])? $this->user['store_user_id'] : 0;
  153. $roleIds = UserRole::getRoleIds($storeUserId);
  154. // 根据已分配的权限
  155. $accessIds = RoleAccess::getAccessIds($roleIds);
  156. // 获取当前角色所有权限链接
  157. $this->accessUrls = Access::getAccessUrls($accessIds);
  158. }
  159. return $this->accessUrls;
  160. }
  161. }