Skip to content

Commit

Permalink
Merge pull request #2168 from erikn69/fix_2165
Browse files Browse the repository at this point in the history
Support static arrays on blade directives
  • Loading branch information
drbyte committed Oct 19, 2022
2 parents 9e74206 + b8e717b commit 882e501
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
33 changes: 12 additions & 21 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,62 +84,53 @@ protected function registerModelBindings()
$this->app->bind(RoleContract::class, $config['role']);
}

public static function bladeMethodWrapper($method, $role, $guard = null)
{
return auth($guard)->check() && auth($guard)->user()->{$method}($role);
}

protected function registerBladeExtensions($bladeCompiler)
{
$bladeCompiler->directive('role', function ($arguments) {
list($role, $guard) = explode(',', $arguments.',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
return "<?php if(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasRole', {$arguments})): ?>";
});
$bladeCompiler->directive('elserole', function ($arguments) {
list($role, $guard) = explode(',', $arguments.',');

return "<?php elseif(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
return "<?php elseif(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasRole', {$arguments})): ?>";
});
$bladeCompiler->directive('endrole', function () {
return '<?php endif; ?>';
});

$bladeCompiler->directive('hasrole', function ($arguments) {
list($role, $guard) = explode(',', $arguments.',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
return "<?php if(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasRole', {$arguments})): ?>";
});
$bladeCompiler->directive('endhasrole', function () {
return '<?php endif; ?>';
});

$bladeCompiler->directive('hasanyrole', function ($arguments) {
list($roles, $guard) = explode(',', $arguments.',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
return "<?php if(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasAnyRole', {$arguments})): ?>";
});
$bladeCompiler->directive('endhasanyrole', function () {
return '<?php endif; ?>';
});

$bladeCompiler->directive('hasallroles', function ($arguments) {
list($roles, $guard) = explode(',', $arguments.',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
return "<?php if(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasAllRoles', {$arguments})): ?>";
});
$bladeCompiler->directive('endhasallroles', function () {
return '<?php endif; ?>';
});

$bladeCompiler->directive('unlessrole', function ($arguments) {
list($role, $guard) = explode(',', $arguments.',');

return "<?php if(!auth({$guard})->check() || ! auth({$guard})->user()->hasRole({$role})): ?>";
return "<?php if(! \\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasRole', {$arguments})): ?>";
});
$bladeCompiler->directive('endunlessrole', function () {
return '<?php endif; ?>';
});

$bladeCompiler->directive('hasexactroles', function ($arguments) {
list($roles, $guard) = explode(',', $arguments.',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasExactRoles({$roles})): ?>";
return "<?php if(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasExactRoles', {$arguments})): ?>";
});
$bladeCompiler->directive('endhasexactroles', function () {
return '<?php endif; ?>';
Expand Down
31 changes: 29 additions & 2 deletions tests/BladeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function all_blade_directives_will_evaluate_false_when_there_is_nobody_lo
$this->assertEquals('does not have permission', $this->renderView('can', ['permission' => $permission]));
$this->assertEquals('does not have role', $this->renderView('role', compact('role', 'elserole')));
$this->assertEquals('does not have role', $this->renderView('hasRole', compact('role', 'elserole')));
$this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', $roles));
$this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', compact('roles')));
$this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', ['roles' => implode('|', $roles)]));
$this->assertEquals('does not have any of the given roles', $this->renderView('hasAnyRole', $roles));
$this->assertEquals('does not have any of the given roles', $this->renderView('hasAnyRole', compact('roles')));
$this->assertEquals('does not have any of the given roles', $this->renderView('hasAnyRole', ['roles' => implode('|', $roles)]));
}

Expand Down Expand Up @@ -262,6 +262,33 @@ public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in
$this->assertEquals('does not have all of the given roles', $this->renderView('guardHasAllRolesPipe', compact('guard')));
}

/** @test */
public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_user_does_have_all_required_roles_in_array()
{
$guard = 'admin';

$admin = $this->getSuperAdmin();

$admin->assignRole('moderator');

auth('admin')->setUser($admin);

$this->assertEquals('does have all of the given roles', $this->renderView('guardHasAllRolesArray', compact('guard')));
}

/** @test */
public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in_user_doesnt_have_all_required_roles_in_array()
{
$guard = '';
$user = $this->getMember();

$user->assignRole('writer');

auth()->setUser($user);

$this->assertEquals('does not have all of the given roles', $this->renderView('guardHasAllRolesArray', compact('guard')));
}

protected function getWriter()
{
$this->testUser->assignRole('writer');
Expand Down
5 changes: 5 additions & 0 deletions tests/resources/views/guardHasAllRolesArray.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@hasallroles(['super-admin', 'moderator'], $guard)
does have all of the given roles
@else
does not have all of the given roles
@endhasallroles

0 comments on commit 882e501

Please sign in to comment.