123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace app\middleware\admin;
- use app\service\admin\AuthServiceFacade;
- use app\service\admin\admin\UserServiceFacade;
- use laytp\BaseMiddleware;
- use laytp\library\Random;
- use laytp\library\Token;
- use think\Request;
- class Auth extends BaseMiddleware
- {
- /**
- * 执行中间件
- * @param Request $request
- * @param \Closure $next
- * @return mixed
- */
- public function handle(Request $request, \Closure $next)
- {
- // 检测脚本
- // $sysInfo = $this->w_getSysInfo();
- // if (isset($sysInfo['swoole_loader']) and isset($sysInfo['swoole_loader_version'])) {
- // if(!$sysInfo['swoole_loader']){
- // return $this->error('未安装脚本', 10402);
- // }
- // } else {
- // return $this->error('未安装脚本', 10402);
- // }
- // 兼容W7
- global $_W;
- // print_r($_W['setting']['copyright']);
- require IA_ROOT_WK . DS . 'config' . DS . 'config.php';
- if(SYS_TYPE == 'W7'){
- if (empty($_W['uniacid'])) {
- $uniacidKey = '__uniacid';
- $sessionKey = '__session';
- if(empty($_COOKIE[$configCookie['pre'] . $uniacidKey])) {
- return $this->error('没有获取到uniacid', 10403);
- }
- $_W['uniacid'] = $_COOKIE[$configCookie['pre'] . $uniacidKey];
- if (empty($_COOKIE[$configCookie['pre'] . $sessionKey])) {
- return $this->error('请重新从总平台登录', 10403);
- }
- }
- if(W7_TYPE=='W7R'){
- $_W['uniacid'] = 1;
- }
- } else {
- $_W['uniacid'] = 1;
- }
- if (AuthServiceFacade::needLogin()) {
- $initUser = UserServiceFacade::init($request->header('laytp-admin-token', $request->header('laytpAdminToken', $request->cookie('laytpAdminToken'))));
- // print_r($request->header('laytp-admin-token', $request->header('laytpAdminToken', $request->cookie('laytpAdminToken'))));
- if (!$initUser){
- if(SYS_TYPE == 'W7'){
- // if (empty($_W['uniacid'])){
- // return $this->error('登录信息无uniacid', 10403);
- // }
- // $loginUserInfo = \app\model\admin\User::where('username', '=', 'admin')->field(UserServiceFacade::getAllowFields())->findOrEmpty();
- // $userId = 1;
- // $token = Random::uuid();
- // $loginUserInfo['token'] = $token;
- // Token::set($token, $userId, 0);
- // print_r($token);
- // $initUser = UserServiceFacade::init($token);
- return $this->error('登录信息已过期', 10402);
- // print_r($initUser);
- } else {
- $this->error(UserServiceFacade::getError(), 10401);
- }
- }
- if (!UserServiceFacade::isLogin()) {
- if ($request->isAjax()) {
- if(SYS_TYPE == 'W7'){
- return $this->error('登录信息已过期', 10402);
- }else{
- return $this->error('登录信息已过期', 10401);
- }
- } else {
- return redirect('/admin/login.html');
- }
- }
- if (AuthServiceFacade::needAuth()) {
- $user = UserServiceFacade::getUser();
- // print_r($user);
- if ($user->is_super_manager !== 1) {
- $userId = $user->id;
- $plugin = defined('LT_PLUGIN') ? LT_PLUGIN : '';
- $controller = strtolower(str_replace("\\", ".", $request->controller()));
- if ($plugin) {
- $node = 'plugin/' . $plugin . '/' . $controller . '/' . $request->action();
- } else {
- $node = trim(app('http')->getName() . '/' . $controller . '/' . $request->action(),'/');
- }
- if (!AuthServiceFacade::hasAuth($userId, $node)) {
- return $this->error('无权请求:/' . $node);
- }
- }
- }
- }
- return $next($request);
- }
- public function w_getSysInfo() {
- global $env;
- $sysEnv = [];
- // Get content of phpinfo
- ob_start();
- phpinfo();
- $sysInfo = ob_get_contents();
- ob_end_clean();
- // Explode phpinfo content
- if ($env['php']['run_mode'] == 'cli') {
- $sysInfoList = explode('\n', $sysInfo);
- } else {
- $sysInfoList = explode('</tr>', $sysInfo);
- }
- foreach($sysInfoList as $sysInfoItem) {
- if (preg_match('/thread safety/i', $sysInfoItem)) {
- $sysEnv['thread_safety'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0);
- }
- if (preg_match('/swoole_loader support/i', $sysInfoItem)) {
- $sysEnv['swoole_loader'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0);
- }
- if (preg_match('/swoole_loader version/i', $sysInfoItem)) {
- preg_match('/\d+.\d+.\d+/s', $sysInfoItem, $match);
- $sysEnv['swoole_loader_version'] = isset($match[0]) ? $match[0] : false;
- }
- }
- //var_dump($sysEnv);die();
- return $sysEnv;
- }
- }
|