Skip to content

Commit

Permalink
Independent Auth Guard for Statamic (statamic#3143)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Mar 8, 2021
1 parent 4b23fde commit 3dddbd9
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 1 deletion.
16 changes: 16 additions & 0 deletions config/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,20 @@

'database' => config('database.default'),

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| By default, Statamic will use the `web` authentication guard. However,
| if you want to run Statamic alongside the default Laravel auth
| guard, you can configure that for your cp and/or frontend.
|
*/

'guards' => [
'cp' => 'web',
'web' => 'web',
],

];
2 changes: 2 additions & 0 deletions routes/cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

Route::get('password/reset', 'ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'ResetPasswordController@reset')->name('password.reset.action');

Route::get('token', 'CsrfTokenController')->name('token');
Route::get('extend', 'ExtendSessionController')->name('extend');
Expand Down
4 changes: 3 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@
Route::get('protect/password', '\Statamic\Auth\Protect\Protectors\Password\Controller@show')->name('protect.password.show');
Route::post('protect/password', '\Statamic\Auth\Protect\Protectors\Password\Controller@store')->name('protect.password.store');

Route::group(['prefix' => 'auth'], function () {
Route::group(['prefix' => 'auth', 'middleware' => [\Statamic\Http\Middleware\AuthGuard::class]], function () {
Route::post('login', 'UserController@login')->name('login');
Route::get('logout', 'UserController@logout')->name('logout');
Route::post('register', 'UserController@register')->name('register');

Route::post('password/email', 'ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'ResetPasswordController@reset')->name('password.reset.action');
});

Route::group(['prefix' => 'auth', 'middleware' => [\Statamic\Http\Middleware\CP\AuthGuard::class]], function () {
Route::get('activate/{token}', 'ActivateAccountController@showResetForm')->name('account.activate');
Route::post('activate', 'ActivateAccountController@reset')->name('account.activate.action');
});
Expand Down
10 changes: 10 additions & 0 deletions src/Auth/Passwords/PasswordReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ class PasswordReset
const BROKER_ACTIVATIONS = 'activations';

protected static $url;
protected static $route;
protected static $redirect;

public static function url($token, $broker)
{
$route = $broker === self::BROKER_ACTIVATIONS ? 'statamic.account.activate' : 'statamic.password.reset';

if (static::$route) {
$route = static::$route;
}

$defaultUrl = route($route, $token);

$url = static::$url
Expand All @@ -36,6 +41,11 @@ public static function resetFormUrl($url)
static::$url = $url;
}

public static function resetFormRoute($route)
{
static::$route = $route;
}

public static function redirectAfterReset($redirect)
{
static::$redirect = $redirect;
Expand Down
13 changes: 13 additions & 0 deletions src/Http/Controllers/CP/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Http\Controllers\CP\Auth;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Statamic\Auth\Passwords\PasswordReset;
use Statamic\Http\Controllers\ForgotPasswordController as Controller;
use Statamic\Http\Middleware\CP\RedirectIfAuthorized;
Expand All @@ -16,8 +17,20 @@ public function __construct()

public function sendResetLinkEmail(Request $request)
{
PasswordReset::resetFormRoute('statamic.cp.password.reset');
PasswordReset::redirectAfterReset(route('statamic.cp.index'));

return parent::sendResetLinkEmail($request);
}

public function broker()
{
$broker = config('statamic.users.passwords.'.PasswordReset::BROKER_RESETS);

if (is_array($broker)) {
$broker = $broker['cp'];
}

return Password::broker($broker);
}
}
32 changes: 32 additions & 0 deletions src/Http/Controllers/CP/Auth/ResetPasswordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Statamic\Http\Controllers\CP\Auth;

use Illuminate\Support\Facades\Password;
use Statamic\Auth\Passwords\PasswordReset;
use Statamic\Http\Controllers\ResetPasswordController as Controller;
use Statamic\Http\Middleware\CP\RedirectIfAuthorized;

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

public function broker()
{
$broker = config('statamic.users.passwords.'.PasswordReset::BROKER_RESETS);

if (is_array($broker)) {
$broker = $broker['cp'];
}

return Password::broker($broker);
}

protected function resetFormAction()
{
return route('statamic.cp.password.reset.action');
}
}
4 changes: 4 additions & 0 deletions src/Http/Controllers/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public function broker()
{
$broker = config('statamic.users.passwords.'.PasswordReset::BROKER_RESETS);

if (is_array($broker)) {
$broker = $broker['web'];
}

return Password::broker($broker);
}
}
4 changes: 4 additions & 0 deletions src/Http/Controllers/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function broker()
{
$broker = config('statamic.users.passwords.'.PasswordReset::BROKER_RESETS);

if (is_array($broker)) {
$broker = $broker['web'];
}

return Password::broker($broker);
}
}
16 changes: 16 additions & 0 deletions src/Http/Middleware/AuthGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Statamic\Http\Middleware;

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

class AuthGuard
{
public function handle($request, Closure $next)
{
Auth::shouldUse(config('statamic.users.guards.web', 'web'));

return $next($request);
}
}
16 changes: 16 additions & 0 deletions src/Http/Middleware/CP/AuthGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Statamic\Http\Middleware\CP;

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

class AuthGuard
{
public function handle($request, Closure $next)
{
Auth::shouldUse(config('statamic.users.guards.cp', 'web'));

return $next($request);
}
}
1 change: 1 addition & 0 deletions src/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ protected function registerMiddlewareGroup()
$this->app->make(Router::class)->middlewareGroup('statamic.web', [
\Statamic\Http\Middleware\StacheLock::class,
\Statamic\Http\Middleware\Localize::class,
\Statamic\Http\Middleware\AuthGuard::class,
\Statamic\StaticCaching\Middleware\Cache::class,
]);
}
Expand Down
1 change: 1 addition & 0 deletions src/Providers/CpServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ protected function registerMiddlewareGroups()
\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Statamic\Http\Middleware\CP\ContactOutpost::class,
\Statamic\Http\Middleware\CP\AuthGuard::class,
]);

$router->middlewareGroup('statamic.cp.authenticated', [
Expand Down

0 comments on commit 3dddbd9

Please sign in to comment.