Skip to content
This repository was archived by the owner on Nov 23, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/BaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Backpack\Base;

use Illuminate\Routing\Router;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Route;

Expand Down Expand Up @@ -46,6 +47,7 @@ public function boot(\Illuminate\Routing\Router $router)
__DIR__.'/config/backpack/base.php', 'backpack.base'
);

$this->registerCustomAuthGuard();
$this->registerAdminMiddleware($this->app->router);
$this->setupRoutes($this->app->router);
$this->publishFiles();
Expand Down Expand Up @@ -122,6 +124,11 @@ public function registerAdminMiddleware(Router $router)
Route::aliasMiddleware('admin', \Backpack\Base\app\Http\Middleware\Admin::class);
}

public function registerCustomAuthGuard()
{
View::composer('backpack::*', \Backpack\Base\app\Http\ViewComposers\AuthComposer::class);
Copy link
Member Author

@tabacitu tabacitu Jul 6, 2017

Choose a reason for hiding this comment

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

@jorenvanhee - I'm wondering if this is broad enough. Wouldn't this make the $backpackAuth guard available only in views that are loaded within the backpack namespace?

If so, I see the following problem:

  • developer uses $backpackAuth when he overwrites backpack views;
  • developer create supplementary views, that he considers to be part of the admin panel; he tries to use $backpackAuth there too; he expects $backpackAuth to be there too, but it's not;

I may be mistaken, though. It's been known to happen :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. What do you think about a helper function backpackAuth()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm... I think a more Laravely way to go would be a Facade. Something like AdminAuth or BackpackAuth?

Copy link
Contributor

Choose a reason for hiding this comment

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

A more Laravely way would be to have both a Façade and a helper and then to have arguments on slack/gitter about which is best :(

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ok with having both a facade and a helper function. Just like the auth helper in Laravel. The documentation says: "The auth function returns an authenticator instance. You may use it instead of the Auth facade for convenience:". I'll update the code if you're ok with that :)

Copy link
Contributor

@lloy0076 lloy0076 Aug 14, 2017

Choose a reason for hiding this comment

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

Having both is probably the better way, given that Laravel does that too. We might be able to avoid the arguments on slack/gitter bit though with a bit of luck (or just agreeing with me 👿).

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed!

}

public function publishFiles()
{
// publish config file
Expand Down
17 changes: 16 additions & 1 deletion src/app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Backpack\Base\app\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Support\Facades\Password;

class ForgotPasswordController extends Controller
{
Expand All @@ -29,7 +30,9 @@ class ForgotPasswordController extends Controller
*/
public function __construct()
{
$this->middleware('guest');
$guard = config('backpack.base.guard', config('auth.defaults.guard'));

$this->middleware("guest:$guard");
}

// -------------------------------------------------------
Expand All @@ -47,4 +50,16 @@ public function showLinkRequestForm()

return view('backpack::auth.passwords.email', $this->data);
}

/**
* Get the broker to be used during password reset.
*
* @return \Illuminate\Contracts\Auth\PasswordBroker
*/
public function broker()
{
$passwords = config('backpack.base.passwords', config('auth.defaults.passwords'));

return Password::broker($passwords);
}
}
14 changes: 12 additions & 2 deletions src/app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Backpack\Base\app\Http\Controllers\Auth;

use Auth;
use Backpack\Base\app\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -31,10 +32,13 @@ class LoginController extends Controller
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
$guard = config('backpack.base.guard', config('auth.defaults.guard'));

$this->middleware("guest:$guard", ['except' => 'logout']);

// ----------------------------------
// Use the admin prefix in all routes
// ----------------------------------

// If not logged in redirect here.
$this->loginPath = property_exists($this, 'loginPath') ? $this->loginPath
Expand All @@ -47,7 +51,6 @@ public function __construct()
// Redirect here after logout.
$this->redirectAfterLogout = property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout
: config('backpack.base.route_prefix', 'admin');
// ----------------------------------
}

// -------------------------------------------------------
Expand Down Expand Up @@ -81,4 +84,11 @@ public function logout(Request $request)
// And redirect to custom location
return redirect($this->redirectAfterLogout);
}

protected function guard()
{
$guard = config('backpack.base.guard', config('auth.defaults.guard'));

return Auth::guard($guard);
}
}
17 changes: 16 additions & 1 deletion src/app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Backpack\Base\app\Http\Controllers\Auth;

use Auth;
use Backpack\Base\app\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -30,7 +31,9 @@ class RegisterController extends Controller
*/
public function __construct()
{
$this->middleware('guest');
$guard = config('backpack.base.guard', config('auth.defaults.guard'));

$this->middleware("guest:$guard");

// Where to redirect users after login / registration.
$this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo
Expand Down Expand Up @@ -113,4 +116,16 @@ public function register(Request $request)

return redirect($this->redirectPath());
}

/**
* Get the guard to be used during registration.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
$guard = config('backpack.base.guard', config('auth.defaults.guard'));

return Auth::guard($guard);
}
}
30 changes: 29 additions & 1 deletion src/app/Http/Controllers/Auth/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Backpack\Base\app\Http\Controllers\Auth;

use Auth;
use Backpack\Base\app\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;

class ResetPasswordController extends Controller
{
Expand All @@ -30,7 +32,9 @@ class ResetPasswordController extends Controller
*/
public function __construct()
{
$this->middleware('guest');
$guard = config('backpack.base.guard', config('auth.defaults.guard'));

$this->middleware("guest:$guard");

// where to redirect after password was reset
$this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo : config('backpack.base.route_prefix', 'admin').'/dashboard';
Expand Down Expand Up @@ -58,4 +62,28 @@ public function showResetForm(Request $request, $token = null)
['token' => $token, 'email' => $request->email]
);
}

/**
* Get the broker to be used during password reset.
*
* @return \Illuminate\Contracts\Auth\PasswordBroker
*/
public function broker()
{
$passwords = config('backpack.base.passwords', config('auth.defaults.passwords'));

return Password::broker($passwords);
}

/**
* Get the guard to be used during password reset.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
$guard = config('backpack.base.guard', config('auth.defaults.guard'));

return Auth::guard($guard);
}
}
7 changes: 4 additions & 3 deletions src/app/Http/Middleware/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ class Admin
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
*
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
public function handle($request, Closure $next)
{
$guard = config('backpack.base.guard', config('auth.defaults.guard'));

if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response(trans('backpack::base.unauthorized'), 401);
} else {
return redirect()->guest(config('backpack.base.route_prefix', 'admin').'/login');
return redirect()->guest(backpack_url('login'));
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/app/Http/ViewComposers/AuthComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Backpack\Base\app\Http\ViewComposers;

use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;

class AuthComposer
{
public function compose(View $view)
{
$guard = config('backpack.base.guard') ? config('backpack.base.guard') : config('auth.defaults.guard');

$view->with([
'backpackAuth' => Auth::guard($guard),
]);
}
}
10 changes: 9 additions & 1 deletion src/config/backpack/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

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

Expand All @@ -88,6 +88,14 @@
// - example_method_name (specify the method on the User model that returns the URL)
'avatar_type' => 'gravatar',

// The guard that protects the Backpack admin panel.
// If null, the config.auth.defaults.guard value will be used.
'guard' => null,

// The password reset configuration for Backpack.
// If null, the config.auth.defaults.passwords value will be used.
'passwords' => null,

/*
|--------------------------------------------------------------------------
| License Code
Expand Down
4 changes: 2 additions & 2 deletions src/resources/views/inc/menu.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

<!-- <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())
<li><a href="{{ route('backpack.auth.login') }}">{{ trans('backpack::base.login') }}</a></li>
@if ($backpackAuth->guest())
<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="{{ route('backpack.auth.register') }}">{{ trans('backpack::base.register') }}</a></li>
@endif
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 ($backpackAuth->check())
<!-- 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($backpackAuth->user()) }}" 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') }}">{{ $backpackAuth->user()->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>