Skip to content
This repository was archived by the owner on Nov 23, 2020. It is now read-only.
Closed
33 changes: 27 additions & 6 deletions src/BaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Backpack\Base;

use Config;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Route;
Expand Down Expand Up @@ -33,20 +34,24 @@ class BaseServiceProvider extends ServiceProvider
*/
public function boot(\Illuminate\Routing\Router $router)
{
// -------------
// LOAD THE VIEWS
// - first the published views (in case they have any changes)
// -------------
// first the published views (in case they have any changes)
$this->loadViewsFrom(resource_path('views/vendor/backpack/base'), 'backpack');
// - then the stock views that come with the package, in case a published view might be missing
// then the stock views that come with the package, in case a published view might be missing
$this->loadViewsFrom(realpath(__DIR__.'/resources/views'), 'backpack');

$this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack');

// use the vendor configuration file as fallback
$this->mergeConfigFrom(
__DIR__.'/config/backpack/base.php', 'backpack.base'
__DIR__.'/config/backpack/base.php',
'backpack.base'
);

$this->registerAdminMiddleware($this->app->router);
$this->registerGuards();
$this->registerMiddleware($this->app->router);
$this->setupRoutes($this->app->router);
$this->publishFiles();
$this->loadHelpers();
Expand Down Expand Up @@ -80,6 +85,18 @@ public function setupRoutes(Router $router)
$this->loadRoutesFrom($routeFilePathInUse);
}

/**
* Register custom backpack authentication guard.
*/
public function registerGuards()
{
$backpackAuthGuard = Config::get('backpack.base.admin_guard');
$existingGuards = Config::get('auth.guards');
$existingGuards[$backpackAuthGuard['name']] = $backpackAuthGuard;

Config::set('auth.guards', $existingGuards);
}

/**
* Register any package services.
*
Expand Down Expand Up @@ -117,9 +134,13 @@ public function register()
$this->commands($this->commands);
}

public function registerAdminMiddleware(Router $router)
public function registerMiddleware(Router $router)
{
Route::aliasMiddleware('admin', \Backpack\Base\app\Http\Middleware\Admin::class);
Route::aliasMiddleware('backpack.auth', \Backpack\Base\app\Http\Middleware\BackpackAuth::class);

if (config('backpack.base.separate_admin_session')) {
Route::aliasMiddleware('backpack.auth.guard', \Backpack\Base\app\Http\Middleware\BackpackAuthGuard::class);
}
}

public function publishFiles()
Expand Down
2 changes: 1 addition & 1 deletion src/app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AdminController extends Controller
*/
public function __construct()
{
$this->middleware('admin');
$this->middleware(backpack_middleware());
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ class ForgotPasswordController extends Controller
*/
public function __construct()
{
$this->middleware('guest');
$this->middleware(backpack_middleware('guest'));
}

public function guard()
{
return \Auth::guard(backpack_guard());
}

// -------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion src/app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LoginController extends Controller
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
$this->middleware(backpack_middleware('guest'), ['except' => 'logout']);

// ----------------------------------
// Use the admin prefix in all routes
Expand All @@ -50,6 +50,11 @@ public function __construct()
// ----------------------------------
}

public function guard()
{
return \Auth::guard(backpack_guard());
}

// -------------------------------------------------------
// Laravel overwrites for loading backpack views
// -------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/app/Http/Controllers/Auth/MyAccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MyAccountController extends Controller

public function __construct()
{
$this->middleware('admin');
$this->middleware(backpack_auth());
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ class RegisterController extends Controller
*/
public function __construct()
{
$this->middleware('guest');
$this->middleware(backpack_middleware('guest'));

// Where to redirect users after login / registration.
$this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo
: config('backpack.base.route_prefix', 'dashboard');
}

public function guard()
{
return \Auth::guard(backpack_guard());
}

/**
* Get a validator for an incoming registration request.
*
Expand Down
7 changes: 6 additions & 1 deletion src/app/Http/Controllers/Auth/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ class ResetPasswordController extends Controller
*/
public function __construct()
{
$this->middleware('guest');
$this->middleware(backpack_middleware('guest'), ['except' => 'logout']);

// where to redirect after password was reset
$this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo : config('backpack.base.route_prefix', 'admin').'/dashboard';
}

public function guard()
{
return \Auth::guard(backpack_guard());
}

// -------------------------------------------------------
// Laravel overwrites for loading backpack views
// -------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Closure;
use Illuminate\Support\Facades\Auth;

class Admin
class BackpackAuth
{
/**
* Handle an incoming request.
Expand Down
31 changes: 31 additions & 0 deletions src/app/Http/Middleware/BackpackAuthGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Backpack\Base\app\Http\Middleware;

use Closure;

class BackpackAuthGuard
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
if (config('backpack.base.separate_admin_session')) {
if (!\Auth::guard(config('backpack.base.admin_guard.name'))->check()) {
if ($request->ajax() || $request->wantsJson()) {
return response(trans('backpack::base.unauthorized'), 401);
}

return redirect(config('backpack.base.route_prefix').'/login');
}
}

return $next($request);
}
}
10 changes: 9 additions & 1 deletion src/config/backpack/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,21 @@

/*
|--------------------------------------------------------------------------
| User Model
| Authentication
|--------------------------------------------------------------------------
*/

// Fully qualified namespace of the User model
'user_model_fqn' => '\App\User',

'separate_admin_session' => false,

'admin_guard' => [
'name' => 'admin',
'driver' => 'session',
'provider' => 'users',
],

// What kind of avatar will you like to show to the user?
// Default: gravatar (automatically use the gravatar for his email)
// Other options:
Expand Down
67 changes: 67 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,70 @@ function backpack_avatar_url($user)
}
}
}

/*
* Returns the name of the middleware
* defined by the application config
* if a param is passed in, it will chain
* the backpack middelware to it.
* e.g guest:backpack.admin.
*/
if (!function_exists('backpack_middleware')) {
function backpack_middleware($chainedWith = null)
{
if (config('backpack.base.separate_admin_session')) {
$middleware = config('backpack.base.admin_guard.name');
} else {
$middleware = 'backpack.auth';
}

if ($chainedWith && config('backpack.base.separate_admin_session')) {
$middleware = $chainedWith.':'.$middleware;
} elseif ($chainedWith) {
$middleware = $chainedWith;
}

return $middleware;
}
}

/*
* Returns the name of the guard defined
* by the application config
*/
if (!function_exists('backpack_guard')) {
function backpack_guard()
{
if (config('backpack.base.separate_admin_session')) {
$guard = config('backpack.base.admin_guard.name');
} else {
$guard = null;
}

return $guard;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return config('backpack.base.separate_admin_session', null); seems more succinct.

}
}

/*
* Returns the user instance if it exists
* of the currently authenticated admin
* based off the defined guard.
*/
if (!function_exists('backpack_auth')) {
function backpack_auth()
{
return \Auth::guard(backpack_guard())->user();
}
}

/*
* Returns back a user instance without
* the admin guard, however allows you
* to pass in a custom guard if you like.
*/
if (!function_exists('backpack_user')) {
function backpack_user($guard = null)
{
return \Auth::guard($guard)->user();
}
}
2 changes: 1 addition & 1 deletion src/resources/views/inc/menu.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<!-- <li><a href="{{ url('/') }}"><i class="fa fa-home"></i> <span>Home</span></a></li> -->
@if (config('backpack.base.setup_auth_routes'))
@if (Auth::guest())
@if (!backpack_auth())
<li><a href="{{ route('backpack.auth.login') }}">{{ trans('backpack::base.login') }}</a></li>
@if (config('backpack.base.registration_open'))
<li><a href="{{ route('backpack.auth.register') }}">{{ trans('backpack::base.register') }}</a></li>
Expand Down
2 changes: 1 addition & 1 deletion src/resources/views/inc/sidebar.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@if (Auth::check())
@if (backpack_auth())
<!-- Left side column. contains the sidebar -->
<aside class="main-sidebar">
<!-- sidebar: style can be found in sidebar.less -->
Expand Down
4 changes: 2 additions & 2 deletions src/resources/views/inc/sidebar_user_panel.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="user-panel">
<a class="pull-left image" href="{{ route('backpack.account.info') }}">
<img src="{{ backpack_avatar_url(Auth::user()) }}" class="img-circle" alt="User Image">
<img src="{{ backpack_avatar_url(backpack_auth()) }}" class="img-circle" alt="User Image">
</a>
<div class="pull-left info">
<p><a href="{{ route('backpack.account.info') }}">{{ Auth::user()->name }}</a></p>
<p><a href="{{ route('backpack.account.info') }}">{{ backpack_auth()->name }}</a></p>
<small><small><a href="{{ route('backpack.account.info') }}"><span><i class="fa fa-user-circle-o"></i> {{ trans('backpack::base.my_account') }}</span></a> &nbsp; &nbsp; <a href="{{ backpack_url('logout') }}"><i class="fa fa-sign-out"></i> <span>{{ trans('backpack::base.logout') }}</span></a></small></small>
</div>
</div>