-
Notifications
You must be signed in to change notification settings - Fork 267
[Feature][0.9][Merged] Custom auth guard #165
Conversation
…envanhee/Base into jorenvanhee-different-user-model-issue-fix
src/BaseServiceProvider.php
Outdated
@@ -111,6 +113,11 @@ public function registerAdminMiddleware(Router $router) | |||
} | |||
} | |||
|
|||
public function registerCustomAuthGuard() | |||
{ | |||
View::composer('backpack::*', \Backpack\Base\app\Http\ViewComposers\AuthComposer::class); |
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
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()
?
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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 :(
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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 👿).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed!
src/config/backpack/base.php
Outdated
@@ -68,11 +68,19 @@ | |||
|
|||
/* | |||
|-------------------------------------------------------------------------- | |||
| User Model | |||
| Authentication | |||
|-------------------------------------------------------------------------- | |||
*/ | |||
|
|||
// Fully qualified namespace of the User model | |||
'user_model_fqn' => '\App\User', |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@@ -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'); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Can we make sure we address the issue of Allowing separate user sessions e.g from different models - without having to specifically add your own custom middleware as this is the underlying issue users are facing - this example here was a deviation that doesn't solve the original issue |
@OwenMelbz that's exactly what I was trying to figure out right now. Wouldn't using a different guard use a separate session? |
Theoretically yeah - I think my previously solution meant backpack came shipped with its own guard. Then that allows the user to use the default laravel guard for the frontend. This means the user has to do absolutely nothing except enable the functionality. All the other solutions I've seen mean users have to create their own guards for the frontend and the backend? |
I think Laravel stores the information about two different guards in one session. So for instance if you are already logged in with the User guard, and then log in with the Admin guard, it will just add that information to the same session. Doesn't really matter how it works behind the scenes, one session or two. It should work :) . I'll make sure I make a test project with two guards. Also, if the user of Backpack only wants to use one guard, not much has to be configured. Since it will just use the default guard then. |
I would like to finish this PR. Could I get write access to this branch? Or should I make a PR to this PR, seems unnecessary. |
@jorenvanhee - I can't grant access to the branch but you could start work in a fork of that branch, if that makes sense? |
Note to self: To do:
Maybe:
|
Update: I don't think it makes much sense to create custom guards, providers and passwords by default. Hence, the task is now Instead, we can specify clear steps on HOW TO USE SEPARATE LOGINS in the docs: [STEP 1] In your 1.1. Add a guard: 'guards' => [
// ...
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
], 1.2. Add a provider: 'providers' => [
// ...
'admins' => [
'driver' => 'eloquent',
'model' => Backpack\Base\app\Models\BackpackUser::class,
],
], 1.3. Add a password reset configuration: 'passwords' => [
// ...
'admin' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
],
], [STEP 2] In your // The guard that protects the Backpack admin panel.
// If null, the config.auth.defaults.guard value will be used.
'guard' => 'admin',
// The password reset configuration for Backpack.
// If null, the config.auth.defaults.passwords value will be used.
'passwords' => 'admin', @OwenMelbz what do you think? Good solution? Basically instead of having a simple
I think it's a great compromise :-) |
@tabacitu as this was such a long time ago lol I can't completely remember. However I think it's overly simplified in the steps, was much more involved. e.g If you do things like I can't remember off the top of my head - but thats why the PR did more. It might be that the original PR might need to be looked over - seeing what change was made - and see how that fits into your suggestion |
i'm one of those like to separate sessions, routes and models etc also i have disable if i integrate Backpack for Laravel in MultiAuthCommand will permits us to use it as Admin Panel and Backpack for Laravel features for any User Types what do you think guys ? |
Thank for the answer @OwenMelbz ! I actually studied PR #87 quite a bit, even resolved all conflicts with Ok, great. If you're saying the steps required with that PR to make separate sessions work were more than just changing PR ready! :-) |
Hmm... @iMokhles what do you mean by
Could you please rephrase? PS. Great work on IMPanel and MultiAuthCommand. I imagine you created them because in new projects you want your opinionated setup, not the Backpack default? This is something I imagined quite a lot of people would do - create Laravel instances with their opinionated setup. Like the Demo, but for their own personal use. Basically a barebone inhouse CMS, that they reuse for all projects, that is powered by Backpack. Let us know some other stuff you included there, by creating Github issues, if you think we could make those the Backpack default. |
i was meaning by calling this but we can call the same command with different guard so we get and about IMPanel it has features like |
src/app/Models/BackpackUser.php
Outdated
{ | ||
return (new MailMessage()) | ||
->line([ | ||
'You are receiving this email because we received a password reset request for your account.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i18n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
src/app/Models/BackpackUser.php
Outdated
'You are receiving this email because we received a password reset request for your account.', | ||
'Click the button below to reset your password:', | ||
]) | ||
->action('Reset Password', url(config('backpack.base.route_prefix').'/password/reset', $this->token)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i18n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
src/app/Models/BackpackUser.php
Outdated
'Click the button below to reset your password:', | ||
]) | ||
->action('Reset Password', url(config('backpack.base.route_prefix').'/password/reset', $this->token)) | ||
->line('If you did not request a password reset, no further action is required.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i18n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
* Returns the name of the guard defined | ||
* by the application config | ||
*/ | ||
function backpack_guard_name() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🤦♂️
There was a problem hiding this comment.
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/
…github.com/Laravel-Backpack/Base into jorenvanhee-different-user-model-issue-fix
Merged into upgrade branch #259 . We'll leave this PR and branch open, and push fixes here if anything comes up in testing. |
Merged this PR #141 into a branch, so we can work on it a little bit more.
I've already implemented all of @lloy0076 's comments regarding spacing, docblocks, etc.