romanbican / roles

Powerful package for handling roles and permissions in Laravel 5
MIT License
1.15k stars 296 forks source link

AttachPermission with slug. #155

Open dokicro opened 8 years ago

dokicro commented 8 years ago

Hi,

I would be cool to add feature to allow attachPermission('slug').

Thanks!

Isfirs commented 8 years ago

I don't know, if this works, but it should give you an idea how to make a trait yourself and add it to your user model. Me myself prefer the way using helper functions:

if (! function_exists('attachPermissionFromSlug')) {

    /**
     * app/Support/helpers.php
     *
     * Get a permission by slug and attach it.
     *
     * @param App\User $user            
     * @param string $slug            
     *
     * @return bool true when succeed
     */
    function attachPermissionFromSLug(\App\User $user, $slug)
    {
        $permission = Permission::where('slug', $slug)->first();

        if (! is_null($permission)) {
            $user->attachPermission($permission);

            return true;
        }

        return false;
    }
}