Skip to content

Commit 882e501

Browse files
authoredOct 19, 2022
Merge pull request #2168 from erikn69/fix_2165
Support static arrays on blade directives
2 parents 9e74206 + b8e717b commit 882e501

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed
 

‎src/PermissionServiceProvider.php

+12-21
Original file line numberDiff line numberDiff line change
@@ -84,62 +84,53 @@ protected function registerModelBindings()
8484
$this->app->bind(RoleContract::class, $config['role']);
8585
}
8686

87+
public static function bladeMethodWrapper($method, $role, $guard = null)
88+
{
89+
return auth($guard)->check() && auth($guard)->user()->{$method}($role);
90+
}
91+
8792
protected function registerBladeExtensions($bladeCompiler)
8893
{
8994
$bladeCompiler->directive('role', function ($arguments) {
90-
list($role, $guard) = explode(',', $arguments.',');
91-
92-
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
95+
return "<?php if(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasRole', {$arguments})): ?>";
9396
});
9497
$bladeCompiler->directive('elserole', function ($arguments) {
95-
list($role, $guard) = explode(',', $arguments.',');
96-
97-
return "<?php elseif(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
98+
return "<?php elseif(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasRole', {$arguments})): ?>";
9899
});
99100
$bladeCompiler->directive('endrole', function () {
100101
return '<?php endif; ?>';
101102
});
102103

103104
$bladeCompiler->directive('hasrole', function ($arguments) {
104-
list($role, $guard) = explode(',', $arguments.',');
105-
106-
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
105+
return "<?php if(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasRole', {$arguments})): ?>";
107106
});
108107
$bladeCompiler->directive('endhasrole', function () {
109108
return '<?php endif; ?>';
110109
});
111110

112111
$bladeCompiler->directive('hasanyrole', function ($arguments) {
113-
list($roles, $guard) = explode(',', $arguments.',');
114-
115-
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
112+
return "<?php if(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasAnyRole', {$arguments})): ?>";
116113
});
117114
$bladeCompiler->directive('endhasanyrole', function () {
118115
return '<?php endif; ?>';
119116
});
120117

121118
$bladeCompiler->directive('hasallroles', function ($arguments) {
122-
list($roles, $guard) = explode(',', $arguments.',');
123-
124-
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
119+
return "<?php if(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasAllRoles', {$arguments})): ?>";
125120
});
126121
$bladeCompiler->directive('endhasallroles', function () {
127122
return '<?php endif; ?>';
128123
});
129124

130125
$bladeCompiler->directive('unlessrole', function ($arguments) {
131-
list($role, $guard) = explode(',', $arguments.',');
132-
133-
return "<?php if(!auth({$guard})->check() || ! auth({$guard})->user()->hasRole({$role})): ?>";
126+
return "<?php if(! \\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasRole', {$arguments})): ?>";
134127
});
135128
$bladeCompiler->directive('endunlessrole', function () {
136129
return '<?php endif; ?>';
137130
});
138131

139132
$bladeCompiler->directive('hasexactroles', function ($arguments) {
140-
list($roles, $guard) = explode(',', $arguments.',');
141-
142-
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasExactRoles({$roles})): ?>";
133+
return "<?php if(\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper('hasExactRoles', {$arguments})): ?>";
143134
});
144135
$bladeCompiler->directive('endhasexactroles', function () {
145136
return '<?php endif; ?>';

‎tests/BladeTest.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public function all_blade_directives_will_evaluate_false_when_there_is_nobody_lo
3131
$this->assertEquals('does not have permission', $this->renderView('can', ['permission' => $permission]));
3232
$this->assertEquals('does not have role', $this->renderView('role', compact('role', 'elserole')));
3333
$this->assertEquals('does not have role', $this->renderView('hasRole', compact('role', 'elserole')));
34-
$this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', $roles));
34+
$this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', compact('roles')));
3535
$this->assertEquals('does not have all of the given roles', $this->renderView('hasAllRoles', ['roles' => implode('|', $roles)]));
36-
$this->assertEquals('does not have any of the given roles', $this->renderView('hasAnyRole', $roles));
36+
$this->assertEquals('does not have any of the given roles', $this->renderView('hasAnyRole', compact('roles')));
3737
$this->assertEquals('does not have any of the given roles', $this->renderView('hasAnyRole', ['roles' => implode('|', $roles)]));
3838
}
3939

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

265+
/** @test */
266+
public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_user_does_have_all_required_roles_in_array()
267+
{
268+
$guard = 'admin';
269+
270+
$admin = $this->getSuperAdmin();
271+
272+
$admin->assignRole('moderator');
273+
274+
auth('admin')->setUser($admin);
275+
276+
$this->assertEquals('does have all of the given roles', $this->renderView('guardHasAllRolesArray', compact('guard')));
277+
}
278+
279+
/** @test */
280+
public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in_user_doesnt_have_all_required_roles_in_array()
281+
{
282+
$guard = '';
283+
$user = $this->getMember();
284+
285+
$user->assignRole('writer');
286+
287+
auth()->setUser($user);
288+
289+
$this->assertEquals('does not have all of the given roles', $this->renderView('guardHasAllRolesArray', compact('guard')));
290+
}
291+
265292
protected function getWriter()
266293
{
267294
$this->testUser->assignRole('writer');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@hasallroles(['super-admin', 'moderator'], $guard)
2+
does have all of the given roles
3+
@else
4+
does not have all of the given roles
5+
@endhasallroles

0 commit comments

Comments
 (0)
Please sign in to comment.