AuthController.php 6.9 KB

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