@@ -84,62 +84,53 @@ protected function registerModelBindings()
84
84
$ this ->app ->bind (RoleContract::class, $ config ['role ' ]);
85
85
}
86
86
87
+ public static function bladeMethodWrapper ($ method , $ role , $ guard = null )
88
+ {
89
+ return auth ($ guard )->check () && auth ($ guard )->user ()->{$ method }($ role );
90
+ }
91
+
87
92
protected function registerBladeExtensions ($ bladeCompiler )
88
93
{
89
94
$ 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 })): ?> " ;
93
96
});
94
97
$ 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 })): ?> " ;
98
99
});
99
100
$ bladeCompiler ->directive ('endrole ' , function () {
100
101
return '<?php endif; ?> ' ;
101
102
});
102
103
103
104
$ 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 })): ?> " ;
107
106
});
108
107
$ bladeCompiler ->directive ('endhasrole ' , function () {
109
108
return '<?php endif; ?> ' ;
110
109
});
111
110
112
111
$ 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 })): ?> " ;
116
113
});
117
114
$ bladeCompiler ->directive ('endhasanyrole ' , function () {
118
115
return '<?php endif; ?> ' ;
119
116
});
120
117
121
118
$ 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 })): ?> " ;
125
120
});
126
121
$ bladeCompiler ->directive ('endhasallroles ' , function () {
127
122
return '<?php endif; ?> ' ;
128
123
});
129
124
130
125
$ 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 })): ?> " ;
134
127
});
135
128
$ bladeCompiler ->directive ('endunlessrole ' , function () {
136
129
return '<?php endif; ?> ' ;
137
130
});
138
131
139
132
$ 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 })): ?> " ;
143
134
});
144
135
$ bladeCompiler ->directive ('endhasexactroles ' , function () {
145
136
return '<?php endif; ?> ' ;
0 commit comments