1

Recently, i viewed a couple of videos about ACL in laravel (https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/16) and after i copied all of the code, i've ran into an error when i'm trying to do anything like migrating, opening tinker and etc. (basically everything that will trigger the frameworks code). It's because tables are not being created yet.

AuthServiceProvider.php

public function boot()
    {
        $this->registerPolicies();

        foreach($this->getPermissions() as $permission) {
            Gate::define($permission->name, function ($user) use ($permission) {
                return $user->hasRole($permission->roles);
            });
        }
    }

    protected function getPermissions()
    {
        return Permission::with('roles')->get();
    }

So you can't run php artisan migrate because it'll throw an error (Missing 'permissions' table).

Temp solution is to add this code into boot function, BEFORE foreach loop

if (App::runningInConsole())
{
            return;
}

But i just don't feel right to include this check for every request. Any ideas how can i fix this or defer this loop until the actual check is called?

Src
  • 5,252
  • 5
  • 28
  • 56
  • Do you really need to define a gate for all of these permissions? Could you not query for the permission at run-time instead? Also, you could use `Schema` to check if the table exists instead of checking if running in console. But it's much the same - a workaround. – fubar Jul 18 '17 at 23:56
  • @fubar I'm using can directives in my code, so Yes, i must define all those gates. I don't know how i can do that, it's actually is my question! Use Schema on every request = +1 query on every request. So, no. – Src Jul 19 '17 at 00:24
  • I'm curious, how are you using them in your code? By name obviously? So why not define each gate by name, rather than automatically from the database instead? – fubar Jul 19 '17 at 00:26
  • @fubar Because i'm building an Admin panel, where administrator can assign and unassing permissions for different roles. So these are dynamic. – Src Jul 19 '17 at 00:27
  • Okay. I understand that. But how are you then checking the permissions? If you use hardcoded gate names when checking them, e.g. `Gate::allows('update-post', $post)`, what happens if that permission has been removed? The gate wouldn't exist? – fubar Jul 19 '17 at 00:45
  • @fubar Like `@can('view-post')`. I'm not removing permissions anywhere in my code, so you shouldn't be worried about that. – Src Jul 19 '17 at 01:06
  • This is what I mean. If you're not removing permission names, why can't you hard-code the Gate definitions? You can still dynamically check a user has permission thereafter. – fubar Jul 19 '17 at 01:09
  • @fubar I got what you are trying to say, but now imagine how you going to sync that togather. At the moment, you have `blongsToMany` relationships and it's easy to retrieve all needed records, compare them and modify. Also, you can't reference permission (if it's static as you say) at the pivot table (permission_role) to implement ACL functionality. Just try to imagine that and if everything seems cool to you, post your answer. – Src Jul 19 '17 at 01:32

0 Answers0