@@ -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; ?> ' ;
0 commit comments