Skip to content
This repository was archived by the owner on Nov 23, 2020. It is now read-only.
Closed
29 changes: 25 additions & 4 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 All @@ -22,19 +23,30 @@ 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'
);

// -------------
// AUTH GUARD CONFIGURATION
// -------------
$appAuthGuards = Config::get('auth.guards');
$backpackAuthGuard = Config::get('backpack.base.admin_guard');
$appAuthGuards[$backpackAuthGuard['name']] = $backpackAuthGuard;
Config::set('auth.guards', $appAuthGuards);

$this->setupRoutes($this->app->router);

// -------------
Expand All @@ -52,6 +64,11 @@ public function boot(\Illuminate\Routing\Router $router)
$this->publishes([__DIR__.'/public' => public_path('vendor/backpack')], 'public');
// publish public AdminLTE assets
$this->publishes([base_path('vendor/almasaeed2010/adminlte') => public_path('vendor/adminlte')], 'adminlte');

// -------------
// HELPERS
// -------------
require_once __DIR__.'/helpers.php';
}

/**
Expand All @@ -64,7 +81,11 @@ public function boot(\Illuminate\Routing\Router $router)
public function setupRoutes(Router $router)
{
// register the 'admin' middleware
$router->middleware('admin', app\Http\Middleware\Admin::class);
$router->middleware('backpack.base.admin', app\Http\Middleware\BackpackBaseAdmin::class);

if (config('backpack.base.separate_admin_session')) {
$router->middleware('backpack.admin.guard', app\Http\Middleware\BackpackAdminGuard::class);
}

$router->group(['namespace' => 'Backpack\Base\app\Http\Controllers'], function ($router) {
Route::group(
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
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
31 changes: 31 additions & 0 deletions src/app/Http/Middleware/BackpackAdminGuard.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 BackpackAdminGuard
{
/**
* 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()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not: $request->expectsJson() unless we don't like its pjson clause.

That said, why couldn't someone access admin functionality via JSON?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm... this might be an issue. @OwenMelbz wouldn't this prevent ajax calls to pages that are behind the backpack_middleware(), if the new guard is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This only applies is they are using separate sessions - if they're using the default laravel guard it will never affect it as that will have already handled the permission - baring in mind this whole implementation was based around the idea of using the default laravel guard for everything - then if turned on it simply adds another condition into the chain for the backend

return response(trans('backpack::base.unauthorized'), 401);
}

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

return $next($request);
}
}
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 BackpackBaseAdmin
{
/**
* Handle an incoming request.
Expand Down
8 changes: 8 additions & 0 deletions src/config/backpack/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@
// Fully qualified namespace of the User model
'user_model_fqn' => '\App\User',

'separate_admin_session' => false,

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

];
67 changes: 67 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* 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')) {
$middlware = config('backpack.base.admin_guard.name');
} else {
$middlware = 'backpack.base.admin';
}

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

return $middlware;
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no e in the middle of $middleware. Errk.

}
}

/*
* 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_admin')) {
function backpack_admin()
{
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 (Auth::guest())
@if (!backpack_admin())
<li><a href="{{ url(config('backpack.base.route_prefix', 'admin').'/login') }}">{{ trans('backpack::base.login') }}</a></li>
@if (config('backpack.base.registration_open'))
<li><a href="{{ url(config('backpack.base.route_prefix', 'admin').'/register') }}">{{ trans('backpack::base.register') }}</a></li>
Expand Down
6 changes: 3 additions & 3 deletions src/resources/views/inc/sidebar.blade.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
@if (Auth::check())
@if (backpack_admin())
<!-- Left side column. contains the sidebar -->
<aside class="main-sidebar">
<!-- sidebar: style can be found in sidebar.less -->
<section class="sidebar">
<!-- Sidebar user panel -->
<div class="user-panel">
<div class="pull-left image">
<img src="https://placehold.it/160x160/00a65a/ffffff/&text={{ mb_substr(Auth::user()->name, 0, 1) }}" class="img-circle" alt="User Image">
<img src="https://placehold.it/160x160/00a65a/ffffff/&text={{ mb_substr(backpack_admin()->name, 0, 1) }}" class="img-circle" alt="User Image">
</div>
<div class="pull-left info">
<p>{{ Auth::user()->name }}</p>
<p>{{ backpack_admin()->name }}</p>
<a href="#"><i class="fa fa-circle text-success"></i> Online</a>
</div>
</div>
Expand Down