diff --git a/src/app/Http/Controllers/AdminController.php b/src/app/Http/Controllers/AdminController.php index 7ee790bd..a6f2003a 100644 --- a/src/app/Http/Controllers/AdminController.php +++ b/src/app/Http/Controllers/AdminController.php @@ -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')); } } diff --git a/src/app/Http/Controllers/Auth/ForgotPasswordController.php b/src/app/Http/Controllers/Auth/ForgotPasswordController.php index 65060e77..6a0245e1 100644 --- a/src/app/Http/Controllers/Auth/ForgotPasswordController.php +++ b/src/app/Http/Controllers/Auth/ForgotPasswordController.php @@ -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 { @@ -29,7 +30,9 @@ class ForgotPasswordController extends Controller */ public function __construct() { - $this->middleware('guest'); + $guard = backpack_guard_name(); + + $this->middleware("guest:$guard"); } // ------------------------------------------------------- @@ -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); + } } diff --git a/src/app/Http/Controllers/Auth/LoginController.php b/src/app/Http/Controllers/Auth/LoginController.php index 9cde6abf..e3775e4f 100644 --- a/src/app/Http/Controllers/Auth/LoginController.php +++ b/src/app/Http/Controllers/Auth/LoginController.php @@ -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(); } // ------------------------------------------------------- @@ -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(); + } } diff --git a/src/app/Http/Controllers/Auth/MyAccountController.php b/src/app/Http/Controllers/Auth/MyAccountController.php index af08292d..86ffc1a1 100644 --- a/src/app/Http/Controllers/Auth/MyAccountController.php +++ b/src/app/Http/Controllers/Auth/MyAccountController.php @@ -80,6 +80,6 @@ public function postChangePasswordForm(ChangePasswordRequest $request) */ protected function guard() { - return Auth::guard(); + return backpack_auth(); } } diff --git a/src/app/Http/Controllers/Auth/RegisterController.php b/src/app/Http/Controllers/Auth/RegisterController.php index 9836bca4..78187006 100644 --- a/src/app/Http/Controllers/Auth/RegisterController.php +++ b/src/app/Http/Controllers/Auth/RegisterController.php @@ -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 @@ -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(); + } } diff --git a/src/app/Http/Controllers/Auth/ResetPasswordController.php b/src/app/Http/Controllers/Auth/ResetPasswordController.php index efeb8ee0..f435dd5f 100644 --- a/src/app/Http/Controllers/Auth/ResetPasswordController.php +++ b/src/app/Http/Controllers/Auth/ResetPasswordController.php @@ -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 { @@ -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'; @@ -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(); + } } diff --git a/src/app/Http/Middleware/Admin.php b/src/app/Http/Middleware/Admin.php index 8a622c15..7901e87e 100644 --- a/src/app/Http/Middleware/Admin.php +++ b/src/app/Http/Middleware/Admin.php @@ -3,7 +3,6 @@ namespace Backpack\Base\app\Http\Middleware; use Closure; -use Illuminate\Support\Facades\Auth; class Admin { @@ -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')); } } diff --git a/src/app/Http/Requests/AccountInfoRequest.php b/src/app/Http/Requests/AccountInfoRequest.php index 99b32ce5..9d4a247b 100644 --- a/src/app/Http/Requests/AccountInfoRequest.php +++ b/src/app/Http/Requests/AccountInfoRequest.php @@ -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 @@ -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(); } /** @@ -36,7 +35,7 @@ protected function validationData() */ public function rules() { - $user = Auth::user(); + $user = backpack_auth()->user(); return [ 'email' => [ diff --git a/src/app/Http/Requests/ChangePasswordRequest.php b/src/app/Http/Requests/ChangePasswordRequest.php index d0fa182b..518c339f 100644 --- a/src/app/Http/Requests/ChangePasswordRequest.php +++ b/src/app/Http/Requests/ChangePasswordRequest.php @@ -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 @@ -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(); } /** @@ -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')); } }); diff --git a/src/app/Models/BackpackUser.php b/src/app/Models/BackpackUser.php new file mode 100644 index 00000000..abc99def --- /dev/null +++ b/src/app/Models/BackpackUser.php @@ -0,0 +1,39 @@ +notify(new ResetPasswordNotification($token)); + } + + /** + * Build the mail representation of the notification. + * + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + return (new MailMessage()) + ->line([ + trans('backpack.base.password_reset.line_1'), + trans('backpack.base.password_reset.line_2'), + ]) + ->action(trans('backpack.base.password_reset.button'), url(config('backpack.base.route_prefix').'/password/reset', $this->token)) + ->line(trans('backpack.base.password_reset.notice')); + } +} diff --git a/src/config/backpack/base.php b/src/config/backpack/base.php index bff9fee8..664dcc89 100644 --- a/src/config/backpack/base.php +++ b/src/config/backpack/base.php @@ -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) @@ -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 diff --git a/src/helpers.php b/src/helpers.php index 0444e472..4e11eec0 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -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() + { + 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(); + } +} diff --git a/src/resources/lang/en/base.php b/src/resources/lang/en/base.php index 8597403a..f9f4d8c6 100644 --- a/src/resources/lang/en/base.php +++ b/src/resources/lang/en/base.php @@ -47,4 +47,11 @@ 'account_updated' => 'Account updated successfully.', 'unknown_error' => 'An unknown error has occured. Please try again.', 'error_saving' => 'Error while saving. Please try again.', + + 'password_reset' => [ + 'line_1' => 'You are receiving this email because we received a password reset request for your account.', + 'line_2' => 'Click the button below to reset your password:', + 'button' => 'Reset Password', + 'notice' => 'If you did not request a password reset, no further action is required.', + ], ]; diff --git a/src/resources/views/auth/account/sidemenu.blade.php b/src/resources/views/auth/account/sidemenu.blade.php index 75b19f62..ac768cd0 100644 --- a/src/resources/views/auth/account/sidemenu.blade.php +++ b/src/resources/views/auth/account/sidemenu.blade.php @@ -1,7 +1,7 @@