-
-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5.x] Organize user controllers (#10093)
- Loading branch information
1 parent
1211509
commit 233738b
Showing
6 changed files
with
223 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Statamic\Http\Controllers\User; | ||
|
||
use Illuminate\Support\Facades\Auth; | ||
use Statamic\Auth\ThrottlesLogins; | ||
use Statamic\Http\Controllers\Controller; | ||
use Statamic\Http\Requests\UserLoginRequest; | ||
|
||
class LoginController extends Controller | ||
{ | ||
use ThrottlesLogins; | ||
|
||
public function login(UserLoginRequest $request) | ||
{ | ||
if ($this->hasTooManyLoginAttempts($request)) { | ||
$this->fireLockoutEvent($request); | ||
|
||
return $this->sendLockoutResponse($request); | ||
} | ||
|
||
if (Auth::attempt($request->only('email', 'password'), $request->has('remember'))) { | ||
return redirect($request->input('_redirect', '/'))->withSuccess(__('Login successful.')); | ||
} | ||
|
||
$this->incrementLoginAttempts($request); | ||
|
||
$errorResponse = $request->has('_error_redirect') ? redirect($request->input('_error_redirect')) : back(); | ||
|
||
return $errorResponse->withInput()->withErrors(__('Invalid credentials.')); | ||
} | ||
|
||
public function logout() | ||
{ | ||
Auth::logout(); | ||
|
||
return redirect(request()->get('redirect', '/')); | ||
} | ||
|
||
protected function username() | ||
{ | ||
return 'email'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Statamic\Http\Controllers\User; | ||
|
||
use Statamic\Facades\User; | ||
use Statamic\Http\Requests\UserPasswordRequest; | ||
|
||
class PasswordController | ||
{ | ||
public function __invoke(UserPasswordRequest $request) | ||
{ | ||
$user = User::current(); | ||
|
||
$user->password($request->password); | ||
|
||
$user->save(); | ||
|
||
return $this->successfulResponse(); | ||
} | ||
|
||
private function successfulResponse() | ||
{ | ||
$response = request()->has('_redirect') ? redirect(request()->get('_redirect')) : back(); | ||
|
||
if (request()->ajax() || request()->wantsJson()) { | ||
return response([ | ||
'success' => true, | ||
'redirect' => $response->getTargetUrl(), | ||
]); | ||
} | ||
|
||
session()->flash('user.password.success', __('Change successful.')); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Statamic\Http\Controllers\User; | ||
|
||
use Statamic\Facades\User; | ||
use Statamic\Http\Requests\UserProfileRequest; | ||
|
||
class ProfileController | ||
{ | ||
public function __invoke(UserProfileRequest $request) | ||
{ | ||
$user = User::current(); | ||
|
||
if ($request->email) { | ||
$user->email($request->email); | ||
} | ||
|
||
foreach ($request->processedValues() as $key => $value) { | ||
$user->set($key, $value); | ||
} | ||
|
||
$user->save(); | ||
|
||
return $this->successfulResponse(); | ||
} | ||
|
||
private function successfulResponse() | ||
{ | ||
$response = request()->has('_redirect') ? redirect(request()->get('_redirect')) : back(); | ||
|
||
if (request()->ajax() || request()->wantsJson()) { | ||
return response([ | ||
'success' => true, | ||
'redirect' => $response->getTargetUrl(), | ||
]); | ||
} | ||
|
||
session()->flash('user.profile.success', __('Update successful.')); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Statamic\Http\Controllers\User; | ||
|
||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\MessageBag; | ||
use Illuminate\Validation\ValidationException; | ||
use Statamic\Events\UserRegistered; | ||
use Statamic\Events\UserRegistering; | ||
use Statamic\Exceptions\SilentFormFailureException; | ||
use Statamic\Facades\User; | ||
use Statamic\Http\Requests\UserRegisterRequest; | ||
use Statamic\Support\Arr; | ||
|
||
class RegisterController | ||
{ | ||
public function __invoke(UserRegisterRequest $request) | ||
{ | ||
$user = User::make() | ||
->email($request->email) | ||
->password($request->password) | ||
->data($request->processedValues()); | ||
|
||
if ($roles = config('statamic.users.new_user_roles')) { | ||
$user->explicitRoles($roles); | ||
} | ||
|
||
if ($groups = config('statamic.users.new_user_groups')) { | ||
$user->groups($groups); | ||
} | ||
|
||
try { | ||
if ($honeypot = config('statamic.users.registration_form_honeypot_field')) { | ||
throw_if(Arr::get($request->input(), $honeypot), new SilentFormFailureException); | ||
} | ||
|
||
throw_if(UserRegistering::dispatch($user) === false, new SilentFormFailureException); | ||
} catch (ValidationException $e) { | ||
return $this->failureResponse($e); | ||
} catch (SilentFormFailureException $e) { | ||
return $this->successfulResponse(silentFailure: true); | ||
} | ||
|
||
$user->save(); | ||
|
||
UserRegistered::dispatch($user); | ||
|
||
Auth::login($user); | ||
|
||
return $this->successfulResponse(); | ||
} | ||
|
||
private function successfulResponse(bool $silentFailure = false) | ||
{ | ||
$response = request()->has('_redirect') ? redirect(request()->get('_redirect')) : back(); | ||
|
||
if (request()->ajax() || request()->wantsJson()) { | ||
return response([ | ||
'success' => true, | ||
'user_created' => ! $silentFailure, | ||
'redirect' => $response->getTargetUrl(), | ||
]); | ||
} | ||
|
||
session()->flash('user.register.success', __('Registration successful.')); | ||
session()->flash('user.register.user_created', ! $silentFailure); | ||
|
||
return $response; | ||
} | ||
|
||
private function failureResponse($validator) | ||
{ | ||
$errors = $validator->errors(); | ||
|
||
if (request()->ajax()) { | ||
return response([ | ||
'errors' => (new MessageBag($errors))->all(), | ||
'error' => collect($errors)->map(function ($errors, $field) { | ||
return $errors[0]; | ||
})->all(), | ||
], 400); | ||
} | ||
|
||
if (request()->wantsJson()) { | ||
return (new ValidationException($validator))->errorBag(new MessageBag($errors)); | ||
} | ||
|
||
$errorResponse = request()->has('_error_redirect') ? redirect(request()->input('_error_redirect')) : back(); | ||
|
||
return $errorResponse->withInput()->withErrors($errors, 'user.register'); | ||
} | ||
} |
Oops, something went wrong.