Auth.php 4.6 KB

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