AuthController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace Dcat\Admin\Controllers;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Layout\Content;
  6. use Dcat\Admin\Models\Repositories\Administrator;
  7. use Illuminate\Auth\GuardHelpers;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Routing\Controller;
  10. use Illuminate\Support\Facades\Lang;
  11. use Illuminate\Support\Facades\Redirect;
  12. use Illuminate\Support\Facades\Validator;
  13. class AuthController extends Controller
  14. {
  15. /**
  16. * @var string
  17. */
  18. protected $redirectTo;
  19. /**
  20. * Show the login page.
  21. *
  22. * @return \Illuminate\Contracts\View\Factory|Redirect|\Illuminate\View\View
  23. */
  24. public function getLogin()
  25. {
  26. if ($this->guard()->check()) {
  27. return redirect($this->redirectPath());
  28. }
  29. return view(config('admin.auth.login_view') ?: 'admin::login');
  30. }
  31. /**
  32. * Handle a login request.
  33. *
  34. * @param Request $request
  35. *
  36. * @return mixed
  37. */
  38. public function postLogin(Request $request)
  39. {
  40. $credentials = $request->only([$this->username(), 'password']);
  41. $remember = (bool) $request->input('remember', false);
  42. /** @var \Illuminate\Validation\Validator $validator */
  43. $validator = Validator::make($credentials, [
  44. $this->username() => 'required',
  45. 'password' => 'required',
  46. ]);
  47. if ($validator->fails()) {
  48. return back()->withInput()->withErrors($validator);
  49. }
  50. if ($this->guard()->attempt($credentials, $remember)) {
  51. return $this->sendLoginResponse($request);
  52. }
  53. return back()->withInput()->withErrors([
  54. $this->username() => $this->getFailedLoginMessage(),
  55. ]);
  56. }
  57. /**
  58. * User logout.
  59. *
  60. * @return Redirect|string
  61. */
  62. public function getLogout(Request $request)
  63. {
  64. $this->guard()->logout();
  65. $request->session()->invalidate();
  66. $path = admin_url('auth/login');
  67. if ($request->pjax()) {
  68. return "<script>location.href = '$path';</script>";
  69. }
  70. return redirect($path);
  71. }
  72. /**
  73. * User setting page.
  74. *
  75. * @param Content $content
  76. *
  77. * @return Content
  78. */
  79. public function getSetting(Content $content)
  80. {
  81. $form = $this->settingForm();
  82. $form->tools(
  83. function (Form\Tools $tools) {
  84. $tools->disableList();
  85. }
  86. );
  87. return $content
  88. ->title(trans('admin.user_setting'))
  89. ->body($form->edit(Admin::user()->getKey()));
  90. }
  91. /**
  92. * Update user setting.
  93. *
  94. * @return \Symfony\Component\HttpFoundation\Response
  95. */
  96. public function putSetting()
  97. {
  98. $form = $this->settingForm();
  99. if (! $this->validateCredentialsWhenUpdatingPassword()) {
  100. $form->responseValidationMessages('old_password', trans('admin.old_password_error'));
  101. }
  102. return $form->update(Admin::user()->getKey());
  103. }
  104. protected function validateCredentialsWhenUpdatingPassword()
  105. {
  106. $user = Admin::user();
  107. $oldPassword = \request('old_password');
  108. $newPassword = \request('password');
  109. if (
  110. (! $newPassword)
  111. || ($newPassword === $user->getAuthPassword())
  112. ) {
  113. return true;
  114. }
  115. if (! $oldPassword) {
  116. return false;
  117. }
  118. return $this->guard()
  119. ->getProvider()
  120. ->validateCredentials($user, ['password' => $oldPassword]);
  121. }
  122. /**
  123. * Model-form for user setting.
  124. *
  125. * @return Form
  126. */
  127. protected function settingForm()
  128. {
  129. $form = new Form(new Administrator());
  130. $form->action(admin_url('auth/setting'));
  131. $form->disableCreatingCheck();
  132. $form->disableEditingCheck();
  133. $form->disableViewCheck();
  134. $form->tools(function (Form\Tools $tools) {
  135. $tools->disableView();
  136. $tools->disableDelete();
  137. });
  138. $form->display('username', trans('admin.username'));
  139. $form->text('name', trans('admin.name'))->rules('required');
  140. $form->image('avatar', trans('admin.avatar'));
  141. $form->password('old_password', trans('admin.old_password'));
  142. $form->password('password', trans('admin.password'))
  143. ->minLength(5)
  144. ->maxLength(20)
  145. ->customFormat(function ($v) {
  146. if ($v == $this->password) {
  147. return;
  148. }
  149. return $v;
  150. });
  151. $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');
  152. $form->ignore(['password_confirmation', 'old_password']);
  153. $form->saving(function (Form $form) {
  154. if ($form->password && $form->model()->password != $form->password) {
  155. $form->password = bcrypt($form->password);
  156. }
  157. if (! $form->password) {
  158. $form->deleteInput('password');
  159. }
  160. });
  161. $form->saved(function (Form $form) {
  162. return $form->redirect(
  163. admin_url('auth/setting'),
  164. trans('admin.update_succeeded')
  165. );
  166. });
  167. return $form;
  168. }
  169. /**
  170. * @return string|\Symfony\Component\Translation\TranslatorInterface
  171. */
  172. protected function getFailedLoginMessage()
  173. {
  174. return Lang::has('auth.failed')
  175. ? trans('auth.failed')
  176. : 'These credentials do not match our records.';
  177. }
  178. /**
  179. * Get the post login redirect path.
  180. *
  181. * @return string
  182. */
  183. protected function redirectPath()
  184. {
  185. if (method_exists($this, 'redirectTo')) {
  186. return $this->redirectTo();
  187. }
  188. return $this->redirectTo ?: config('admin.route.prefix');
  189. }
  190. /**
  191. * Send the response after the user was authenticated.
  192. *
  193. * @param \Illuminate\Http\Request $request
  194. *
  195. * @return \Illuminate\Http\Response
  196. */
  197. protected function sendLoginResponse(Request $request)
  198. {
  199. admin_alert(trans('admin.login_successful'));
  200. $request->session()->regenerate();
  201. return redirect()->intended($this->redirectPath());
  202. }
  203. /**
  204. * Get the login username to be used by the controller.
  205. *
  206. * @return string
  207. */
  208. protected function username()
  209. {
  210. return 'username';
  211. }
  212. /**
  213. * Get the guard to be used during authentication.
  214. *
  215. * @return \Illuminate\Contracts\Auth\StatefulGuard|GuardHelpers
  216. */
  217. protected function guard()
  218. {
  219. return Admin::guard();
  220. }
  221. }