| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace Modules\Admin\Http\Controllers;
- use Illuminate\Foundation\Auth\AuthenticatesUsers;
- use Illuminate\Http\Request;
- use Illuminate\Routing\Controller;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Log;
- use Modules\Admin\Services\FilterService;
- use Modules\Admin\Services\SettingService;
- use DB;
- class LoginController extends Controller
- {
- use AuthenticatesUsers;
- /**
- * 登陆后的跳转页面
- *
- * @var string
- */
- protected $redirectTo = '/admin';
- /**
- * Create a new controller instance.
- *
- * @return void
- */
- public function __construct()
- {
- $this->middleware('guest')->except('logout');
- }
- //登录页面
- public function loginView(Request $request)
- {
- $filter = FilterService::check_ip();
- if (Auth::guard('web')->check()) {
- return redirect('/admin');
- }
- // 获取当前访问者的 IP 地址
- $ipAddress = $_SERVER['REMOTE_ADDR'];
- // 定义私有 IP 段
- $privateRanges = [
- ['10.0.0.0', '10.255.255.255'],
- ['172.16.0.0', '172.31.255.255'],
- ['192.168.0.0', '192.168.255.255']
- ];
- $ipInPrivateRange = false;
- // 判断 IP 是否在任何一个私有 IP 段内
- foreach ($privateRanges as $range) {
- if ($this->isIpInRange($ipAddress, $range[0], $range[1])) {
- $ipInPrivateRange = true;
- break;
- }
- }
- if (!$ipInPrivateRange) {
- http_response_code(404);
- exit;
- }
- return view('admin::login');
- }
- function isIpInRange($ip, $rangeStart, $rangeEnd) {
- $ipLong = ip2long($ip);
- $startLong = ip2long($rangeStart);
- $endLong = ip2long($rangeEnd);
- return ($ipLong >= $startLong && $ipLong <= $endLong);
- }
- //登录动作
- public function doLogin(Request $request)
- {
- $request['password'] = base64_decode($request->input('password'));
- $this->validateLogin($request);
- // If the class is using the ThrottlesLogins trait, we can automatically throttle
- // the login attempts for this application. We'll key this by the username and
- // the IP address of the client making these requests into this application.
- if (method_exists($this, 'hasTooManyLoginAttempts') &&
- $this->hasTooManyLoginAttempts($request)) {
- $this->fireLockoutEvent($request);
- return $this->sendLockoutResponse($request);
- }
- $valid = validPass($request['password']);
- if (is_string($valid)) {
- $this->guard()->logout();
- $request->session()->invalidate();
- return [
- 'code' => 1,
- 'message' => '密码限制:' . $valid . ',请找管理员修改',
- ];
- }
- if ($this->attemptLogin($request)) {
- // $roles = $this->guard()->user()->getRoleNames()->toArray();
- // if (count($roles) == 0 || (count($roles) == 1 && $roles[0] == 'PuTongZhiYuan')) {
- // $this->guard()->logout();
- // $request->session()->invalidate();
- // return [
- // 'code' => 1,
- // 'message' => '没有登录权限',
- // ];
- // }
- $token = $request->input('staff_num') . "," . $request->input('password') . "," . $request->getClientIp();
- $token = base64_encode($token);
- setcookie("token", $token, time() + 7200, "/", ".nxjiewei.com");
- return $this->sendLoginResponse($request);
- }
- // If the login attempt was unsuccessful we will increment the number of attempts
- // to login and redirect the user back to the login form. Of course, when this
- // user surpasses their maximum number of attempts they will get locked out.
- $this->incrementLoginAttempts($request);
- return $this->sendFailedLoginResponse($request);
- }
- protected function sendLoginResponse(Request $request)
- {
- $request->session()->regenerate();
- $this->clearLoginAttempts($request);
- return [
- 'code' => 0,
- 'message' => '登录成功',
- ];
- }
- /**
- * 用户名验证字段
- * @return string
- */
- public function username()
- {
- return 'staff_num';
- }
- }
|