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

Add ability to filter through menu items #26

Closed
AlexRegenbogen opened this issue Apr 29, 2015 · 5 comments
Closed

Add ability to filter through menu items #26

AlexRegenbogen opened this issue Apr 29, 2015 · 5 comments

Comments

@AlexRegenbogen
Copy link

I'm currently trying to have the builder check if each item will give access before it's added to the tree.
(ACL-based, defined on the routes)

Basically what I'm trying to do, is filter out items that are not accessible to the user.
(If a root item is not allowed via ACL, then all underlying items are also prohibited)

Initially I was looking into overriding Builder::add() to match the given URL to the routes, and then checking the route if it matches for access. But If an item is not allowed, I'm not able to return nothing from add().

Am I looking into the wrong direction? ;-)

@kaidesu
Copy link
Contributor

kaidesu commented Apr 29, 2015

I should add a way to filter menu items, which would cover this use case.

In the mean time, you're able to assign any additional meta data you may have to your menu items through the data() method (documentation page here). You could make use of that to filter items based on permissions somewhere in your code before the menu is passed to your view.

@kaidesu kaidesu changed the title Checking each menu-item to see if access is allowed or not? Add ability to filter through menu items Apr 29, 2015
@kaidesu
Copy link
Contributor

kaidesu commented Apr 29, 2015

Just pushed v1.0.3, which has the ability to filter menu items. Here's a simple usage example:

Menu::make('test', function($menu) {
    $menu->add('Google', 'http://google.com')->data('permissions', ['view_google']);
})->filter(function($item) {
    dd($item->data('permissions'));

    // Simply check if your users can perform the defined permissions, and return a boolean.
    // If you're using the Caffeinated Shinobi package, the usage would simply be:
    //
    // return Auth::user()->can($item->data('permissions'));
});

@kaidesu kaidesu closed this as completed Apr 29, 2015
@mackhankins
Copy link

Could this work with middleware?

@kaidesu
Copy link
Contributor

kaidesu commented May 1, 2015

To be honest, I don't know. Haven't tried or messed around with creating middleware myself.

If it helps any, the filter() method here literally forwards to the Laravel Collection filter() method. That may help you Google-fu for an answer~

@mackhankins
Copy link

Okay, that actually helps. I'm using Entrust, but to be fair all I need is simple permissions which might send me over to Shinobi. I might be better off moving my menu to a custom composer that I can send permissions to or something like that.

Custom Middleware https://laracasts.com/discuss/channels/general-discussion/check-if-user-has-admin-role-1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants