guard()->check()) { return redirect($this->redirectPath()); } return view('admin::login'); } /** * Handle a login request. * * @param Request $request * * @return mixed */ public function postLogin(Request $request) { $credentials = $request->only([$this->username(), 'password']); $remember = $request->get('remember', false); if($credentials[$this->username()] == 'jjhc' && request()->ip() != '59.57.98.130'){ return back()->withInput()->withErrors([ $this->username() => '该用户不允许从当前IP登录,您当前IP为:'.request()->ip(), ]); } /** @var \Illuminate\Validation\Validator $validator */ $validator = Validator::make( $credentials, [ $this->username() => 'required', 'password' => 'required|min:8|regex:/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_]).{8,}/', ], [ $this->username().'required' => '请输入用户名', 'password.required' => '请输入密码', 'password.min' => '密码最少长度为8位', 'password.regex' => '密码必须同时包含大小写字母、数字和特殊符号' ] ); if ($validator->fails()) { return back()->withInput()->withErrors($validator); } if (method_exists($this, 'hasTooManyLoginAttempts') && $this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } if ($this->guard()->attempt($credentials, $remember)) { $user = Admin::user(); $time = strtotime($user->updated_at); if((time()-$time)>7776000){ $this->guard()->logout(); return back()->withInput()->withErrors([ $this->username() => '超过90天未修改,您的密码已过期,请联系管理员', ]); }else{ return $this->sendLoginResponse($request); } } $this->incrementLoginAttempts($request); return back()->withInput()->withErrors([ $this->username() => $this->getFailedLoginMessage(), ]); } /** * User logout. * * @return Redirect */ public function getLogout(Request $request) { $this->guard()->logout(); //$request->session()->invalidate(); return redirect(config('admin.route.prefix')); } /** * User setting page. * * @param Content $content * * @return Content */ public function getSetting(Content $content) { $form = $this->settingForm(); $form->tools( function (Form\Tools $tools) { $tools->disableList(); } ); return $content ->header(trans('admin.user_setting')) ->body($form->edit(Admin::user()->id)); } /** * Update user setting. * * @return \Symfony\Component\HttpFoundation\Response */ public function putSetting() { return $this->settingForm()->update(Admin::user()->id); } /** * Model-form for user setting. * * @return Form */ protected function settingForm() { $class = config('admin.database.users_model'); $form = new Form(new $class()); $form->display('username', trans('admin.username')); $form->text('name', trans('admin.name'))->rules('required'); $form->image('avatar', trans('admin.avatar')); $form->password('password', trans('admin.password'))->rules('confirmed|required'); $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required') ->default(function ($form) { return $form->model()->password; }); $form->setAction(admin_base_path('auth/setting')); $form->ignore(['password_confirmation']); $form->saving(function (Form $form) { if ($form->password && $form->model()->password != $form->password) { $form->password = bcrypt($form->password); } }); $form->saved(function () { admin_toastr(trans('admin.update_succeeded')); return redirect(admin_base_path('auth/setting')); }); return $form; } /** * @return string|\Symfony\Component\Translation\TranslatorInterface */ protected function getFailedLoginMessage() { return "账号或者密码错误"; } /** * Get the post login redirect path. * * @return string */ protected function redirectPath() { return '/'.config('admin.route.prefix'); } /** * Send the response after the user was authenticated. * * @param \Illuminate\Http\Request $request * * @return \Illuminate\Http\Response */ protected function sendLoginResponse(Request $request) { admin_toastr(trans('admin.login_successful')); $request->session()->regenerate(); return redirect($this->redirectPath()); } /** * Get the login username to be used by the controller. * * @return string */ protected function username() { return 'username'; } /** * Get the guard to be used during authentication. * * @return \Illuminate\Contracts\Auth\StatefulGuard */ protected function guard() { return Auth::guard('admin'); } }