AuthController.php 6.6 KB

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