AuthController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace Encore\Admin\Controllers;
  3. use Encore\Admin\Facades\Admin;
  4. use Encore\Admin\Form;
  5. use Encore\Admin\Layout\Content;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Routing\Controller;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\Lang;
  10. use Illuminate\Support\Facades\Redirect;
  11. use Illuminate\Support\Facades\Validator;
  12. use Illuminate\Foundation\Auth\ThrottlesLogins;
  13. class AuthController extends Controller
  14. {
  15. use ThrottlesLogins;
  16. /**
  17. * The maximum number of attempts to allow.
  18. *
  19. * @return int
  20. */
  21. protected $maxAttempts = 3;
  22. /**
  23. * The number of minutes to throttle for.
  24. *
  25. * @return int
  26. */
  27. protected $decayMinutes = 10;
  28. /**
  29. * Show the login page.
  30. *
  31. * @return \Illuminate\Contracts\View\Factory|Redirect|\Illuminate\View\View
  32. */
  33. public function getLogin()
  34. {
  35. if ($this->guard()->check()) {
  36. return redirect($this->redirectPath());
  37. }
  38. return view('admin::login');
  39. }
  40. /**
  41. * Handle a login request.
  42. *
  43. * @param Request $request
  44. *
  45. * @return mixed
  46. */
  47. public function postLogin(Request $request)
  48. {
  49. $credentials = $request->only([$this->username(), 'password']);
  50. $remember = $request->get('remember', false);
  51. if($credentials[$this->username()] == 'jjhc' && request()->ip() != '59.57.98.130'){
  52. return back()->withInput()->withErrors([
  53. $this->username() => '该用户不允许从当前IP登录,您当前IP为:'.request()->ip(),
  54. ]);
  55. }
  56. /** @var \Illuminate\Validation\Validator $validator */
  57. $validator = Validator::make(
  58. $credentials,
  59. [
  60. $this->username() => 'required',
  61. 'password' => 'required|min:8|regex:/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_]).{8,}/',
  62. ],
  63. [
  64. $this->username().'required' => '请输入用户名',
  65. 'password.required' => '请输入密码',
  66. 'password.min' => '密码最少长度为8位',
  67. 'password.regex' => '密码必须同时包含大小写字母、数字和特殊符号'
  68. ]
  69. );
  70. if ($validator->fails()) {
  71. return back()->withInput()->withErrors($validator);
  72. }
  73. if (method_exists($this, 'hasTooManyLoginAttempts') && $this->hasTooManyLoginAttempts($request)) {
  74. $this->fireLockoutEvent($request);
  75. return $this->sendLockoutResponse($request);
  76. }
  77. if ($this->guard()->attempt($credentials, $remember)) {
  78. $user = Admin::user();
  79. $time = strtotime($user->updated_at);
  80. if((time()-$time)>7776000){
  81. $this->guard()->logout();
  82. return back()->withInput()->withErrors([
  83. $this->username() => '超过90天未修改,您的密码已过期,请联系管理员',
  84. ]);
  85. }else{
  86. return $this->sendLoginResponse($request);
  87. }
  88. }
  89. $this->incrementLoginAttempts($request);
  90. return back()->withInput()->withErrors([
  91. $this->username() => $this->getFailedLoginMessage(),
  92. ]);
  93. }
  94. /**
  95. * User logout.
  96. *
  97. * @return Redirect
  98. */
  99. public function getLogout(Request $request)
  100. {
  101. $this->guard()->logout();
  102. //$request->session()->invalidate();
  103. return redirect(config('admin.route.prefix'));
  104. }
  105. /**
  106. * User setting page.
  107. *
  108. * @param Content $content
  109. *
  110. * @return Content
  111. */
  112. public function getSetting(Content $content)
  113. {
  114. $form = $this->settingForm();
  115. $form->tools(
  116. function (Form\Tools $tools) {
  117. $tools->disableList();
  118. }
  119. );
  120. return $content
  121. ->header(trans('admin.user_setting'))
  122. ->body($form->edit(Admin::user()->id));
  123. }
  124. /**
  125. * Update user setting.
  126. *
  127. * @return \Symfony\Component\HttpFoundation\Response
  128. */
  129. public function putSetting()
  130. {
  131. return $this->settingForm()->update(Admin::user()->id);
  132. }
  133. /**
  134. * Model-form for user setting.
  135. *
  136. * @return Form
  137. */
  138. protected function settingForm()
  139. {
  140. $class = config('admin.database.users_model');
  141. $form = new Form(new $class());
  142. $form->display('username', trans('admin.username'));
  143. $form->text('name', trans('admin.name'))->rules('required');
  144. $form->image('avatar', trans('admin.avatar'));
  145. $form->password('password', trans('admin.password'))->rules('confirmed|required');
  146. $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
  147. ->default(function ($form) {
  148. return $form->model()->password;
  149. });
  150. $form->setAction(admin_base_path('auth/setting'));
  151. $form->ignore(['password_confirmation']);
  152. $form->saving(function (Form $form) {
  153. if ($form->password && $form->model()->password != $form->password) {
  154. $form->password = bcrypt($form->password);
  155. }
  156. });
  157. $form->saved(function () {
  158. admin_toastr(trans('admin.update_succeeded'));
  159. return redirect(admin_base_path('auth/setting'));
  160. });
  161. return $form;
  162. }
  163. /**
  164. * @return string|\Symfony\Component\Translation\TranslatorInterface
  165. */
  166. protected function getFailedLoginMessage()
  167. {
  168. return "账号或者密码错误";
  169. }
  170. /**
  171. * Get the post login redirect path.
  172. *
  173. * @return string
  174. */
  175. protected function redirectPath()
  176. {
  177. return '/'.config('admin.route.prefix');
  178. }
  179. /**
  180. * Send the response after the user was authenticated.
  181. *
  182. * @param \Illuminate\Http\Request $request
  183. *
  184. * @return \Illuminate\Http\Response
  185. */
  186. protected function sendLoginResponse(Request $request)
  187. {
  188. admin_toastr(trans('admin.login_successful'));
  189. $request->session()->regenerate();
  190. return redirect($this->redirectPath());
  191. }
  192. /**
  193. * Get the login username to be used by the controller.
  194. *
  195. * @return string
  196. */
  197. protected function username()
  198. {
  199. return 'username';
  200. }
  201. /**
  202. * Get the guard to be used during authentication.
  203. *
  204. * @return \Illuminate\Contracts\Auth\StatefulGuard
  205. */
  206. protected function guard()
  207. {
  208. return Auth::guard('admin');
  209. }
  210. }