123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- namespace Dcat\Admin\Http\Controllers;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Form;
- use Dcat\Admin\Http\Repositories\Administrator;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Traits\HasFormResponse;
- use Illuminate\Auth\GuardHelpers;
- use Illuminate\Http\Request;
- use Illuminate\Routing\Controller;
- use Illuminate\Support\Facades\Lang;
- use Illuminate\Support\Facades\Redirect;
- use Illuminate\Support\Facades\Validator;
- class AuthController extends Controller
- {
- use HasFormResponse;
- /**
- * @var string
- */
- protected $view = 'admin::pages.login';
- /**
- * @var string
- */
- protected $redirectTo;
- /**
- * Show the login page.
- *
- * @return Content|\Illuminate\Http\RedirectResponse
- */
- public function getLogin(Content $content)
- {
- if ($this->guard()->check()) {
- return redirect($this->getRedirectPath());
- }
- return $content->full()->body(view($this->view));
- }
- /**
- * Handle a login request.
- *
- * @param Request $request
- *
- * @return mixed
- */
- public function postLogin(Request $request)
- {
- $credentials = $request->only([$this->username(), 'password']);
- $remember = (bool) $request->input('remember', false);
- /** @var \Illuminate\Validation\Validator $validator */
- $validator = Validator::make($credentials, [
- $this->username() => 'required',
- 'password' => 'required',
- ]);
- if ($validator->fails()) {
- return $this->validationErrorsResponse($validator);
- }
- if ($this->guard()->attempt($credentials, $remember)) {
- return $this->sendLoginResponse($request);
- }
- return $this->validationErrorsResponse([
- $this->username() => $this->getFailedLoginMessage(),
- ]);
- }
- /**
- * User logout.
- *
- * @return Redirect|string
- */
- public function getLogout(Request $request)
- {
- $this->guard()->logout();
- $request->session()->invalidate();
- $path = admin_url('auth/login');
- if ($request->pjax()) {
- return "<script>location.href = '$path';</script>";
- }
- return redirect($path);
- }
- /**
- * 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
- ->title(trans('admin.user_setting'))
- ->body($form->edit(Admin::user()->getKey()));
- }
- /**
- * Update user setting.
- *
- * @return \Symfony\Component\HttpFoundation\Response
- */
- public function putSetting()
- {
- $form = $this->settingForm();
- if (! $this->validateCredentialsWhenUpdatingPassword()) {
- $form->responseValidationMessages('old_password', trans('admin.old_password_error'));
- }
- return $form->update(Admin::user()->getKey());
- }
- protected function validateCredentialsWhenUpdatingPassword()
- {
- $user = Admin::user();
- $oldPassword = \request('old_password');
- $newPassword = \request('password');
- if (
- (! $newPassword)
- || ($newPassword === $user->getAuthPassword())
- ) {
- return true;
- }
- if (! $oldPassword) {
- return false;
- }
- return $this->guard()
- ->getProvider()
- ->validateCredentials($user, ['password' => $oldPassword]);
- }
- /**
- * Model-form for user setting.
- *
- * @return Form
- */
- protected function settingForm()
- {
- return new Form(new Administrator(), function (Form $form) {
- $form->action(admin_url('auth/setting'));
- $form->disableCreatingCheck();
- $form->disableEditingCheck();
- $form->disableViewCheck();
- $form->tools(function (Form\Tools $tools) {
- $tools->disableView();
- $tools->disableDelete();
- });
- $form->display('username', trans('admin.username'));
- $form->text('name', trans('admin.name'))->required();
- $form->image('avatar', trans('admin.avatar'))->autoUpload();
- $form->password('old_password', trans('admin.old_password'));
- $form->password('password', trans('admin.password'))
- ->minLength(5)
- ->maxLength(20)
- ->customFormat(function ($v) {
- if ($v == $this->password) {
- return;
- }
- return $v;
- });
- $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');
- $form->ignore(['password_confirmation', 'old_password']);
- $form->saving(function (Form $form) {
- if ($form->password && $form->model()->password != $form->password) {
- $form->password = bcrypt($form->password);
- }
- if (! $form->password) {
- $form->deleteInput('password');
- }
- });
- $form->saved(function (Form $form) {
- return $form
- ->response()
- ->success(trans('admin.update_succeeded'))
- ->redirect('auth/setting');
- });
- });
- }
- /**
- * @return string|\Symfony\Component\Translation\TranslatorInterface
- */
- protected function getFailedLoginMessage()
- {
- return Lang::has('admin.auth_failed')
- ? trans('admin.auth_failed')
- : 'These credentials do not match our records.';
- }
- /**
- * Get the post login redirect path.
- *
- * @return string
- */
- protected function getRedirectPath()
- {
- return $this->redirectTo ?: admin_url('/');
- }
- /**
- * Send the response after the user was authenticated.
- *
- * @param \Illuminate\Http\Request $request
- *
- * @return \Symfony\Component\HttpFoundation\Response
- */
- protected function sendLoginResponse(Request $request)
- {
- $request->session()->regenerate();
- $path = $this->getRedirectPath();
- return $this->response()
- ->success(trans('admin.login_successful'))
- ->locationToIntended($path)
- ->locationIf(Admin::app()->getEnabledApps(), $path)
- ->send();
- }
- /**
- * 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|GuardHelpers
- */
- protected function guard()
- {
- return Admin::guard();
- }
- }
|