Auth.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. $this->store = $this->store? $this->store : [];
  72. // 当前用户信息
  73. $storeUserId = isset($this->store['user']['store_user_id'])? $this->store['user']['store_user_id'] : 0;
  74. $this->user = User::detail($storeUserId);
  75. }
  76. /**
  77. * 私有化克隆方法
  78. */
  79. private function __clone()
  80. {
  81. }
  82. /**
  83. * 验证指定url是否有访问权限
  84. * @param string|array $url
  85. * @param bool $strict 严格模式(必须全部通过才返回true)
  86. * @return bool
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. * @throws \think\exception\DbException
  90. */
  91. public function checkPrivilege($url, $strict = true)
  92. {
  93. if (!is_array($url)):
  94. return $this->checkAccess($url);
  95. else:
  96. foreach ($url as $val):
  97. if ($strict && !$this->checkAccess($val)) {
  98. return false;
  99. }
  100. if (!$strict && $this->checkAccess($val)) {
  101. return true;
  102. }
  103. endforeach;
  104. endif;
  105. return true;
  106. }
  107. /**
  108. * @param string $url
  109. * @return bool
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. * @throws \think\exception\DbException
  113. */
  114. private function checkAccess($url)
  115. {
  116. if(empty($url)){
  117. return false;
  118. }
  119. // 超级管理员无需验证
  120. if ($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. $storeUserId = isset($this->user['store_user_id'])? $this->user['store_user_id'] : 0;
  152. $roleIds = UserRole::getRoleIds($storeUserId);
  153. // 根据已分配的权限
  154. $accessIds = RoleAccess::getAccessIds($roleIds);
  155. // 获取当前角色所有权限链接
  156. $this->accessUrls = Access::getAccessUrls($accessIds);
  157. }
  158. return $this->accessUrls;
  159. }
  160. }