-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
@haspermission directive - Non Default guard #2515
Conversation
@drbyte hi, couldn't this be solved by adding the guard on the gate directive? laravel-permission/src/PermissionRegistrar.php Lines 125 to 129 in a10d72d
$gate->before(function (Authorizable $user, string $ability, array &$args = []) {
if(is_string($args[0] ?? null) && !class_exists($args[0])) {
$guard = array_shift($args);
}
if (method_exists($user, 'checkPermissionTo')) {
return $user->checkPermissionTo($ability, $guard ?? null) ?: null;
}
}); Now we could do @can('admin.users.view', 'admin')
...
@endcan auth()->user()->can('admin.users.view', 'admin')
auth()->user()->can('admin.users.view', ['admin', $post]) |
That would be preferable. Until now we've avoided adding blade helpers for permissions because they basically duplicate what Laravel already provides in the can/canany/canall series. A few versions ago I explored your suggestion when tinkering with making guards simpler but all kinds of things broke .... but I think the part you mention might have been fine. I rolled back the whole thing, but probably could have kept that part. |
Current status: undecided between merging this (after tests are added for this new directive), or simply adding it to the |
I did test it with the |
Ya, we don't want to break the normal Policy stuff. |
@parallels999 Maybe I've been at the keyboard too long today, but I can't get a Blade test to pass when passing a guard. eg: Added this to /** @test */
public function the_can_directive_can_accept_a_guard_name()
{
$this->testAdminRole->givePermissionTo($this->testAdminPermission);
$this->testAdmin->assignRole($this->testAdminRole);
// log in as the Admin with the permission-via-role
auth('admin')->setUser($this->testAdmin);
$permission = 'admin-permission'; // from $this->testAdminPermission
$guard = 'admin'; // from $this->testAdminPermission
$this->assertEquals('has permission', $this->renderView('can', compact('permission', 'guard')));
} and revised the @can($permission, $guard ?? null)
has permission
@else
does not have permission
@endcan |
Turns out it was the |
Just thinking "aloud" ... Regarding policies ... since this package is only checking for the permission names provided by this package, does it really need to also check against a policy? Policies should be checking the ability (permission name) against the model, inside the policy, so I don't think we need to worry about cascading the arguments list down to policy names like core Laravel does. |
@axlwild ... still considering this. But I'd like to add tests. Can you update the PR to "allow edits by maintainers"? (tick the checkbox) EDIT: I decided to add the tests separately. But in future, it's always better to "allow edits by maintainers" when submitting PR's! |
As discussed in comments on 2515: #2515 (comment) Co-authored-by: parallels999 <parallels999@users.noreply.github.com>
* Add guard parameter to can() As discussed in comments on 2515: #2515 (comment) Co-authored-by: parallels999 <parallels999@users.noreply.github.com>
Has permission directive to use instead can directive when using non-default guard
Creating this PR to be able to use another blade directive when you use another guard distinct from the default one.
This issue is mentioned here.
With this change, you can use the following directive
instead of