PHP code example of smart-crowd / laravel-rbac

1. Go to this page and download the library: Download smart-crowd/laravel-rbac library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

smart-crowd / laravel-rbac example snippets


    'providers' => [
        ...
    
        SmartCrowd\Rbac\RbacServiceProvider::class,
    ],
    ...
    
    'aliases' => [
        ...
        
        'Rbac' => 'SmartCrowd\Rbac\Facades\Rbac'
    ]
    

    use SmartCrowd\Rbac\Traits\AllowedTrait;
    use SmartCrowd\Rbac\Contracts\Assignable;
    
    class User extends Model implements Assignable
    {
        use AllowedTrait;
    
        /**
         * Should return array of permissions and roles names,
         * assigned to user.
         *
         * @return array Array of user assignments.
         */
        public function getAssignments()
        {
            // your implementation here
        }
        ...
    }
    

    if (Auth::user()->allowed('article.delete', ['article' => $article])) {
        // user has access to 'somePermission.name' permission
    }
    

    Route::delete('/articles/{article}', [
        'middleware' => 'rbac:article.delete', 
        'uses' => 'ArticlesController@delete'
    ]);
    

    protected $routeMiddleware = [
        ...
        'rbac' => 'SmartCrowd\Rbac\Middleware\RbacMiddleware',
    ];
    

    public function boot(Router $router)
    {
        //...
        $router->model('article', '\App\Article');
    
        parent::boot($router);
    }
    

    @allowed('article.edit', ['article' => $article])
        <a href="{{ route('edit', ['article' => $article]) }}">edit</a>
    @else
        <span>You can not edit this article</span>
    @endallowed
    

    @allowedArticleEdit(['article' => $article])
        {{ $some }}
    @endallowed
    
    @allowedIndex
        {{ $some }}
    @endallowed
    

@allowed('group.post.delete', ['post' => $post, 'group' => $group]) // or $post->group
    post delete button
@endallowed

Route::delete('/post/{post}', [
    'middleware' => 'rbac:group.post.delete', 
    'uses' => 'PostController@delete'
]);

@allowed('group.post.delete', ['post' => $post])
    post delete button
@endallowed

Route::delete('/group/{group}/post/{post}', [
    'middleware' => 'rbac:group.post.delete', 
    'uses' => 'PostController@delete'
]);
bash
    php artisan vendor:publish