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 16 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
2 changes: 1 addition & 1 deletion src/app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public function dashboard()
public function redirect()
{
// The '/admin' route is not to be used as a page, because it breaks the menu's active state.
return redirect(config('backpack.base.route_prefix').'/dashboard');
return redirect(backpack_url('dashboard'));
}
}
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 = backpack_guard_name();

$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);
}
}
24 changes: 18 additions & 6 deletions src/app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,25 @@ class LoginController extends Controller
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
$guard = backpack_guard_name();

$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
: config('backpack.base.route_prefix', 'admin').'/login';
: backpack_url('login');

// Redirect here after successful login.
$this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo
: config('backpack.base.route_prefix', 'admin').'/dashboard';
: backpack_url('dashboard');

// Redirect here after logout.
$this->redirectAfterLogout = property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout
: config('backpack.base.route_prefix', 'admin');
// ----------------------------------
: backpack_url();
}

// -------------------------------------------------------
Expand Down Expand Up @@ -76,9 +78,19 @@ public function showLoginForm()
public function logout(Request $request)
{
// Do the default logout procedure
$this->defaultLogout($request);
$this->guard()->logout();

// And redirect to custom location
return redirect($this->redirectAfterLogout);
}

/**
* Get the guard to be used during logout.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return backpack_auth();
}
}
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 @@ -80,6 +80,6 @@ public function postChangePasswordForm(ChangePasswordRequest $request)
*/
protected function guard()
{
return Auth::guard();
return backpack_auth();
}
}
14 changes: 13 additions & 1 deletion src/app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class RegisterController extends Controller
*/
public function __construct()
{
$this->middleware('guest');
$guard = backpack_guard_name();

$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 +115,14 @@ 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()
{
return backpack_auth();
}
}
27 changes: 26 additions & 1 deletion src/app/Http/Controllers/Auth/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 +31,9 @@ class ResetPasswordController extends Controller
*/
public function __construct()
{
$this->middleware('guest');
$guard = backpack_guard_name();

$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 +61,26 @@ 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()
{
return backpack_auth();
}
}
8 changes: 3 additions & 5 deletions src/app/Http/Middleware/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Backpack\Base\app\Http\Middleware;

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

class Admin
{
Expand All @@ -12,17 +11,16 @@ 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)
{
if (Auth::guard($guard)->guest()) {
if (backpack_auth()->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
5 changes: 2 additions & 3 deletions src/app/Http/Requests/AccountInfoRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Backpack\Base\app\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class AccountInfoRequest extends FormRequest
Expand All @@ -16,7 +15,7 @@ class AccountInfoRequest extends FormRequest
public function authorize()
{
// only allow updates if the user is logged in
return Auth::check();
return backpack_auth()->check();
}

/**
Expand All @@ -36,7 +35,7 @@ protected function validationData()
*/
public function rules()
{
$user = Auth::user();
$user = backpack_auth()->user();

return [
'email' => [
Expand Down
5 changes: 2 additions & 3 deletions src/app/Http/Requests/ChangePasswordRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Backpack\Base\app\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class ChangePasswordRequest extends FormRequest
Expand All @@ -16,7 +15,7 @@ class ChangePasswordRequest extends FormRequest
public function authorize()
{
// only allow updates if the user is logged in
return Auth::check();
return backpack_auth()->check();
}

/**
Expand Down Expand Up @@ -44,7 +43,7 @@ public function withValidator($validator)
{
$validator->after(function ($validator) {
// check old password matches
if (!Hash::check($this->input('old_password'), Auth::user()->password)) {
if (!Hash::check($this->input('old_password'), backpack_auth()->user()->password)) {
$validator->errors()->add('old_password', trans('backpack::base.old_password_incorrect'));
}
});
Expand Down
39 changes: 39 additions & 0 deletions src/app/Models/BackpackUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Backpack\Base\app\Models;

use App\User;
use Backpack\Base\app\Notifications\ResetPasswordNotification as ResetPasswordNotification;

class BackpackUser extends User
{
protected $table = 'users';

/**
* Send the password reset notification.
*
* @param string $token
*
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}

/**
* Build the mail representation of the notification.
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage())
->line([
'You are receiving this email because we received a password reset request for your account.',
Copy link
Contributor

Choose a reason for hiding this comment

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

i18n

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

'Click the button below to reset your password:',
])
->action('Reset Password', url(config('backpack.base.route_prefix').'/password/reset', $this->token))
Copy link
Contributor

Choose a reason for hiding this comment

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

i18n

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

->line('If you did not request a password reset, no further action is required.');
Copy link
Contributor

Choose a reason for hiding this comment

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

i18n

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

}
}
12 changes: 10 additions & 2 deletions src/config/backpack/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@

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

// Fully qualified namespace of the User model
'user_model_fqn' => '\App\User',
'user_model_fqn' => '\Backpack\Base\app\Models\BackpackUser',

// What kind of avatar will you like to show to the user?
// Default: gravatar (automatically use the gravatar for his email)
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
35 changes: 35 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,38 @@ function backpack_avatar_url($user)
}
}
}

if (!function_exists('backpack_guard_name')) {
/*
* Returns the name of the guard defined
* by the application config
*/
function backpack_guard_name()
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if these should be camelCase?

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... Good point. I vote no. From what I can see, all Laravel helpers are snake_case. I think we should keep that same rule, for consistency.

Copy link
Contributor

Choose a reason for hiding this comment

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

sigh Standards only exist so they can be broken 🤦‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

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

No they don't. The also exist to cover all existing standards :-) https://xkcd.com/927/

{
return config('backpack.base.guard', config('auth.defaults.guard'));
}
}

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

if (!function_exists('backpack_user')) {
/*
* Returns back a user instance without
* the admin guard, however allows you
* to pass in a custom guard if you like.
*/
function backpack_user()
{
return backpack_auth()->user();
}
}
4 changes: 2 additions & 2 deletions src/resources/views/auth/account/sidemenu.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="box">
<div class="box-body box-profile">
<img class="profile-user-img img-responsive img-circle" src="{{ backpack_avatar_url(Auth::user()) }}">
<h3 class="profile-username text-center">{{ Auth::user()->name }}</h3>
<img class="profile-user-img img-responsive img-circle" src="{{ backpack_avatar_url(backpack_auth()->user()) }}">
<h3 class="profile-username text-center">{{ backpack_auth()->user()->name }}</h3>
</div>

<hr class="m-t-0 m-b-0">
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 (backpack_auth()->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 (backpack_auth()->check())
<!-- Left side column. contains the sidebar -->
<aside class="main-sidebar">
<!-- sidebar: style can be found in sidebar.less -->
Expand Down
Loading