PermissionService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 08/03/2017
  6. * Time: 09:39
  7. */
  8. namespace app\common\services;
  9. use app\common\exceptions\ShopException;
  10. use app\common\helpers\Cache;
  11. use app\common\models\Menu;
  12. use app\common\models\user\UniAccountUser;
  13. use app\common\models\user\User;
  14. class PermissionService
  15. {
  16. public static function validate()
  17. {
  18. $item = \app\common\models\Menu::getCurrentItemByRoute(request()->getRoute(), \app\backend\modules\menu\Menu::current()->getItems());
  19. //检测权限
  20. if (!PermissionService::can($item)) {
  21. $exception = new ShopException('Sorry,您没有操作权限,请联系管理员!');
  22. $exception->setRedirect(yzWebUrl('index.index'));
  23. throw $exception;
  24. }
  25. return true;
  26. }
  27. public static function isAuth()
  28. {
  29. return \YunShop::app()->uid;
  30. }
  31. /**
  32. * 检测是否有权限
  33. * @param $item
  34. * @return bool
  35. */
  36. public static function can($item)
  37. {
  38. /*if(!$item){
  39. return false;
  40. }*/
  41. if (\Yunshop::isPHPUnit()) {
  42. return true;
  43. }
  44. if (self::isFounder()) {
  45. return true;
  46. //todo 临时增加创始人私有管理插件权限,非创始人用户地址栏访问创始人私有页面时$item为null
  47. } elseif (
  48. in_array(request()->getRoute(), static::founderPermission())
  49. or
  50. in_array($item, static::founderPermission())
  51. ) {
  52. return false;
  53. }
  54. if (self::isOwner()) {
  55. return true;
  56. }
  57. if (self::isManager()) {
  58. return true;
  59. }
  60. if (self::checkNoPermission($item) === true) {
  61. return true;
  62. }
  63. return in_array($item, User::userPermissionCache());
  64. }
  65. /**
  66. * 检测是否存在白名单
  67. * @param $route
  68. * @return bool
  69. */
  70. public static function checkNoPermission($route)
  71. {
  72. $key = 'noPermissions'.\YunShop::app()->uid; //key拼上uid,放止有的如供应商菜单有根据登录的账号资格来设置菜单的
  73. if (Cache::has($key)) {
  74. $noPermissions = Cache::get($key);
  75. } else {
  76. $noPermissions = self::getNoPermissionList(\app\backend\modules\menu\Menu::current()->getItems());
  77. Cache::put($key, $noPermissions,120);
  78. }
  79. if (in_array($route, $noPermissions)) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. /**
  85. * 创始人私有的页面与功能
  86. * @return string[]
  87. */
  88. public static function founderPermission()
  89. {
  90. return [
  91. // 插件管理
  92. // route
  93. 'plugins.get-plugin-data',
  94. 'plugins.enable',
  95. 'plugins.disable',
  96. 'plugins.manage',
  97. 'plugins.delete',
  98. 'plugins.update',
  99. // key
  100. 'founder_plugins',
  101. 'plugins_enable',
  102. 'plugins_disable',
  103. 'plugins_manage',
  104. 'plugins_delete',
  105. 'plugins_update',
  106. // 系统工具
  107. // route
  108. 'supervisord.supervisord.index',
  109. 'supervisord.supervisord.index',
  110. 'supervisord.supervisord.store',
  111. 'siteSetting.index.index',
  112. 'siteSetting.index.queue',
  113. 'siteSetting.index.physics-path',
  114. 'siteSetting.index.redis-config',
  115. 'siteSetting.index.mongoDB-config',
  116. 'site_setting.store.index',
  117. 'setting.cache.index',
  118. 'setting.cron_log.index',
  119. 'setting.trojan.check',
  120. 'setting.trojan.del',
  121. // key
  122. 'site_setting',
  123. 'supervisord_supervisord_index',
  124. 'supervisord_supervisord_store',
  125. 'site_setting.index',
  126. 'site_setting.queue',
  127. 'site_setting.physics_path',
  128. 'site_setting.redis_config',
  129. 'site_setting.mongoDB_config',
  130. 'site_setting.store',
  131. 'cache_setting',
  132. 'setting_shop_log',
  133. 'trojan',
  134. 'work_order_store_page',
  135. // 工单管理
  136. // route
  137. 'setting.work-order.index',
  138. 'setting.work-order.store-page',
  139. 'setting.work-order.details',
  140. // key
  141. 'work_order',
  142. 'work_order_store_page',
  143. 'work_order_details',
  144. // 系统更新
  145. // route
  146. 'update.index',
  147. // key
  148. 'setting_shop_update',
  149. // 安装应用
  150. // route
  151. 'plugins.jump',// 这个是中转方法,因为还要提示信息
  152. 'plugin.plugins-market.Controllers.new-market.show',
  153. // key
  154. 'install_plugins',
  155. // 清除小程序粉丝
  156. 'plugin.min-app.admin.clear',
  157. 'plugin.min-app.admin.clear-fan'
  158. ];
  159. }
  160. /**
  161. * 获取权限白名单
  162. * @param $menus
  163. * @return array
  164. */
  165. public static function getNoPermissionList($menus)
  166. {
  167. $noPermissions = [];
  168. if ($menus) {
  169. foreach ($menus as $key => $m) {
  170. if (!isset($m['permit']) || (isset($m['permit']) && !$m['permit'])) {
  171. $noPermissions[] = $key;
  172. }
  173. if (isset($m['child']) && $m['child']) {
  174. $noPermissions = array_merge($noPermissions, self::getNoPermissionList($m['child']));
  175. }
  176. }
  177. }
  178. return $noPermissions;
  179. }
  180. /**
  181. * 是否是创始人
  182. * @return bool
  183. */
  184. public static function isFounder()
  185. {
  186. return \YunShop::app()->role === 'founder' && \YunShop::app()->isfounder === true;
  187. }
  188. /**
  189. * 是否是主管理员
  190. * @return bool
  191. */
  192. public static function isOwner()
  193. {
  194. return \YunShop::app()->role === 'owner';
  195. }
  196. /**
  197. * 是否是管理员
  198. * @return bool
  199. */
  200. public static function isManager()
  201. {
  202. return \YunShop::app()->role === 'manager';
  203. }
  204. /**
  205. * 是否是操作员
  206. * @return bool
  207. */
  208. public static function isOperator()
  209. {
  210. return \YunShop::app()->role === 'operator';
  211. }
  212. }