Auth.php 4.6 KB

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