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 6 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 @@ -42,6 +43,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 @@ -111,6 +113,11 @@ public function registerAdminMiddleware(Router $router)
}
}

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('backpack.base.guard') : config('auth.defaults.guard');
Copy link
Contributor

@jorenvanhee jorenvanhee Jul 12, 2017

Choose a reason for hiding this comment

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

I would've kept it like this: config('backpack.base.guard') ?: config('auth.defaults.guard') (also at the other places with a similar situation)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, that's PHP 7+ though, as far as I know. Just changed it for it to work on PHP 5.3 too :-) Still have people using Backpack on that one.

Copy link
Contributor

Choose a reason for hiding this comment

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

PHP 7 has the null coalescing operator ??. ?: works fine in php 5.3, it is also used in the Laravel source.

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.

That is a textbook case for getting rid of the ternary and/or coalescing operators.

We probably should turn this into somethng readable.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, ok :-) ?: looks clean enough to me then.


$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('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('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('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('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('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('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('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('backpack.base.guard') : config('auth.defaults.guard');

return Auth::guard($guard);
}
}
5 changes: 3 additions & 2 deletions src/app/Http/Middleware/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ 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('backpack.base.guard') : config('auth.defaults.guard');

if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response(trans('backpack::base.unauthorized'), 401);
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 @@ -68,11 +68,19 @@

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

// Fully qualified namespace of the User model
'user_model_fqn' => '\App\User',
Copy link
Member Author

Choose a reason for hiding this comment

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

@jorenvanhee , I think as long as we don't eliminate user_model_fqn at this time, this is a non-breaking change. Right?

Copy link
Contributor

Choose a reason for hiding this comment

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

It will not break if we keep user_model_fqn (and leave it in the RegisterController). But if you then want to use the option to switch authentication guards, you will have to republish your views. Because the new views will contain $backpackAuth or backpackAuth().

So it's not really breaking I guess? You just can't use the new feature straight away.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right... that might be very confusing for someone who wants to start using the feature, but doesn't have the views, you're right. Let's call it a breaking change then.


// 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,

];
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 ($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="{{ 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 ($backpackAuth->check())
<!-- 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($backpackAuth->user()->name, 0, 1) }}" class="img-circle" alt="User Image">
</div>
<div class="pull-left info">
<p>{{ Auth::user()->name }}</p>
<p>{{ $backpackAuth->user()->name }}</p>
<a href="#"><i class="fa fa-circle text-success"></i> Online</a>
</div>
</div>
Expand Down