Zizaco / entrust

Role-based Permissions for Laravel 5
MIT License
6.05k stars 1.29k forks source link

@role, @permission and @ability not working in blade template. #711

Open zeeshan-nxb opened 7 years ago

zeeshan-nxb commented 7 years ago

In laravel 5.3, Entrust 5.2.x-dev Hello, i have installed the zizaco/entrust latest package and facing the problem when use @role('retailer') in the blade template. error occurs syntax error, unexpected ''retailer'' (T_CONSTANT_ENCAPSED_STRING) after a few attempts i edited the private function bladeDirectives() of vendor/zizaco/entrust/src/Entrust/EntrustServiceProvider.php i.e., add parenthesis ()
before: return "<?php if (\Entrust::hasRole{$expression}) : ?>"; after : return "<?php if (\Entrust::hasRole({$expression})) : ?>"; and also edit for @permission, @ability.

now its working.

etelyatn commented 7 years ago

Try to use double brackets: @role(('retailer'))

manojsharma20 commented 7 years ago

I have tried double brackets, but did not work for me. @ability(('admin', 'create-user,edit-user')) ..... @endability

zeeshan-nxb commented 7 years ago

@manojsharma20 i think, if you will edit the private function bladeDirectives() of vendor/zizaco/entrust/src/Entrust/EntrustServiceProvider.php i.e., add parenthesis () in each directives code i.e.,

\Blade::directive('ability', function($expression) { return "<?php if (\Entrust::ability**({$expression}) ) : ?>"; });

hope your issue will be resolved and Author should provide the best solution in next version or edit the Blade template tutorial for the package users.

ChrisThompsonTLDR commented 7 years ago
    private function bladeDirectives()
    {
        // Call to Entrust::hasRole
        \Blade::directive('role', function($expression) {
            return "<?php if (\\Entrust::hasRole({$expression})) : ?>";
        });

        \Blade::directive('endrole', function($expression) {
            return "<?php endif; // Entrust::hasRole ?>";
        });

        // Call to Entrust::can
        \Blade::directive('permission', function($expression) {
            return "<?php if (\\Entrust::can({$expression})) : ?>";
        });

        \Blade::directive('endpermission', function($expression) {
            return "<?php endif; // Entrust::can ?>";
        });

        // Call to Entrust::ability
        \Blade::directive('ability', function($expression) {
            return "<?php if (\\Entrust::ability({$expression})) : ?>";
        });

        \Blade::directive('endability', function($expression) {
            return "<?php endif; // Entrust::ability ?>";
        });
    }
zeeshan-nxb commented 7 years ago

@ChrisThompsonTLDR , I have done this job like you described. but what will happen when the package will be updated. this code will be overwritten again. so i suggest the @author to fix it for next version updates.

jacobcyl commented 7 years ago

725 the same error I think.

MelonSmasher commented 7 years ago

Calling if directives rather than entrust directives worked for me. I tried wrapping the permission name and role name in a second set of round brackets and it still was not working for me. This is my workaround:

<?php $user = Auth::user(); ?>
@if ($user->hasRole(['admin','power-user','finance-manager']))
    // Stuff
    @if ($user->can('manage-users'))
        // Stuff
    @endif
@endif
dipakagile commented 7 years ago

Parse error: syntax error, unexpected ','

code : @ability('r-admin,r-national,r-regional,r-eadmin,r-iadmin,r-sanyojak,r-local','') @endability

Still, it shows error , its working fine on local but it's not working fine on ubuntu server PHP version 5.6.30 .

Default :

private function bladeDirectives() { if (!class_exists('\Blade')) return;

    // Call to Entrust::hasRole
    \Blade::directive('role', function($expression) {
        return "<?php if (\\Entrust::hasRole({$expression})) : ?>";
    });

    \Blade::directive('endrole', function($expression) {
        return "<?php endif; // Entrust::hasRole ?>";
    });

    // Call to Entrust::can
    \Blade::directive('permission', function($expression) {
        return "<?php if (\\Entrust::can({$expression})) : ?>";
    });

    \Blade::directive('endpermission', function($expression) {
        return "<?php endif; // Entrust::can ?>";
    });

    // Call to Entrust::ability
    \Blade::directive('ability', function($expression) {
        return "<?php if (\\Entrust::ability({$expression})) : ?>";
    });

    \Blade::directive('endability', function($expression) {
        return "<?php endif; // Entrust::ability ?>";
    });
}
christracey commented 7 years ago

I'm pretty sure you need to pass role(s) as an array...

@role(['retailer'])`
<h1>this user has the retailer role</h1>
@endrole