Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using a custom guard for authorization by setting config #265

Merged
merged 1 commit into from
May 24, 2021

Conversation

kfriars
Copy link
Contributor

@kfriars kfriars commented Mar 31, 2021

I have come across your lovely package, but my project uses several custom guards which means that $this->authorize('viewLarecipe', $documentation) will always fail, as the web gaurd's $user will always be null.

This is a pretty straightforward PR that is non-breaking, but allows people to use custom guards to protect their LaRecipe Docs.

Copy link
Owner

@saleem-hadad saleem-hadad left a comment

Choose a reason for hiding this comment

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

Thanks for the effort, why not using the same auth config without creating a new one?
for example:

'auth' => 'your gate'

then in the controller, we can use something like this:

if (config('larecipe.settings.auth') == true)  // normal auth
if (config('larecipe.settings.auth') == false) // without auth
(config('larecipe.settings.auth') != null) // custom gate

what do you think about this?

@kfriars
Copy link
Contributor Author

kfriars commented Mar 31, 2021

With strict equality, ie:

if (config('larecipe.settings.auth') === true)  // normal auth
if (config('larecipe.settings.auth') === false) // without auth
(config('larecipe.settings.auth') !== null) // custom gate

I could get behind this.

Whatever your preference is, is good by me 😄

@saleem-hadad
Copy link
Owner

yes exactly. What do you think I just feel it's redundant to have both configs unless you have a certain case that you might need the two of them?

@saleem-hadad
Copy link
Owner

@WillGoldstein
Copy link
Contributor

Hey @saleem-hadad and @kfriars I already implemented this I think in a PR long ago.

image

So really when you set auth to false, you can then fall back on any middleware you want which lets you then use gates.

@kfriars
Copy link
Contributor Author

kfriars commented Mar 31, 2021

I will provide some updated docs in the PR too, I agree it can feel redundant but I usually lean on the side of being blatant.

I see no issue with stuffing all of the functionality in the larecipe.settings.auth config.

I will update my PR soon and you can have a look.

@WillGoldstein
Copy link
Contributor

is this helping? https://github.com/larecipe/larecipe-docs/pull/8/files

haha my bad, that's the exact PR I did and referred to above. Good find there @saleem-hadad

@kfriars
Copy link
Contributor Author

kfriars commented Mar 31, 2021

Thanks for helping out @WillGoldstein!

My issue is not the middleware, but the guard configured (and ultimately the UserProvider).

The way the package is currently configured will only use the default Guard resolved from the service container, but in my project I have several and I want to select a particular one for docs.

If I am missing something, please let me know.

@WillGoldstein
Copy link
Contributor

@kfriars thats awesome and makes sense! Sorry I didn’t see that benefit. Your addition is a supplement and would be great to have.

@kfriars
Copy link
Contributor Author

kfriars commented Apr 1, 2021

Here is my attempt at shoehorning the guard setting into the auth setting and I think it becomes less readable.

We would end up with:

        if (config('larecipe.settings.auth')) {
            if (config('larecipe.settings.auth') !== true) {
                Auth::shouldUse(config('larecipe.settings.auth'));
            }
            
            $this->middleware(['auth']);
        } else {
            if (config('larecipe.settings.middleware')) {
                $this->middleware(config('larecipe.settings.middleware'));
            }
        }

I have also realized a situation where this implementation is less flexible (But would still accommodate my use-case). Imagine inside your viewLarecipe gate you wanted to allow both guests and authenticated users, but you wanted the those authenticated users to be provided by and authenticated by a custom guard. For example:

Gate::define('viewLarecipe', function ($user, $documentation) {
    if ($documentation->title === 'Admin Panel') {
        return $user && $user->isAdmin();
    }

    return true;
});

With the implementation in this comment, this would not be possible, since the auth middleware would fail and would 403 before hitting the gate, if no $user was authenticated.

If you would like this version I will update the PR, otherwise please accept as is, or give me some details on further changes.

@kfriars
Copy link
Contributor Author

kfriars commented Apr 6, 2021

@saleem-hadad or @WillGoldstein it would be ideal for me to have this merged before Thursday. I can use my fork for now, but would like to close this out soon. Please let me know if there is anything else you would like in the PR.

@kfriars
Copy link
Contributor Author

kfriars commented Apr 12, 2021

Hey @saleem-hadad and @WillGoldstein !

Just a friendly nudge that this still exists 😄

Again, if there is anything else you would like me to include in the PR, please let me know.

@WillGoldstein
Copy link
Contributor

I actually can’t merge it... but thanks for doing this! Hopefully @saleem-hadad will soon

@kfriars
Copy link
Contributor Author

kfriars commented Apr 20, 2021

@saleem-hadad ☝️ ❓

@saleem-hadad saleem-hadad merged commit a0150f9 into saleem-hadad:master May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants