Skip to content

Commit

Permalink
[4.x] Use RedirectIfAuthorized middleware on password reset & activat…
Browse files Browse the repository at this point in the history
…e pages (#9053)

Co-authored-by: Duncan McClean <[email protected]>
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
3 people authored Nov 27, 2023
1 parent 1d14155 commit 952dacb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/Http/Controllers/ActivateAccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

use Illuminate\Support\Facades\Password;
use Statamic\Auth\Passwords\PasswordReset;
use Statamic\Http\Middleware\CP\RedirectIfAuthorized;

class ActivateAccountController extends ResetPasswordController
{
public function __construct()
{
$this->middleware(RedirectIfAuthorized::class);
}

protected function resetFormAction()
{
return route('statamic.account.activate.action');
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
use Statamic\Auth\Passwords\PasswordReset;
use Statamic\Auth\ResetsPasswords;
use Statamic\Contracts\Auth\User;
use Statamic\Http\Middleware\RedirectIfAuthenticated;
use Statamic\Http\Middleware\CP\RedirectIfAuthorized;

class ResetPasswordController extends Controller
{
use ResetsPasswords;

public function __construct()
{
$this->middleware(RedirectIfAuthenticated::class);
$this->middleware(RedirectIfAuthorized::class);
}

public function showResetForm(Request $request, $token = null)
Expand Down
11 changes: 8 additions & 3 deletions src/Http/Middleware/CP/RedirectIfAuthorized.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Http\Middleware\CP;

use Closure;
use Illuminate\Support\Facades\Auth;
use Statamic\Facades\User;

class RedirectIfAuthorized
Expand All @@ -16,10 +17,14 @@ class RedirectIfAuthorized
*/
public function handle($request, Closure $next, $guard = null)
{
if (User::current()) {
return redirect(cp_route('index'));
if (! Auth::guard($guard)->check()) {
return $next($request);
}

return $next($request);
$user = User::current();

$url = $user->can('access cp') ? cp_route('index') : '/';

return redirect($url)->withError(__("You can't do this while logged in"));
}
}

0 comments on commit 952dacb

Please sign in to comment.