New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

cubekit/laracan

Easy define abilities of the current user.
125 1
Install
composer require cubekit/laracan
Latest Version:v0.2.1
PHP:>=5.4.0
Laravel:5
License:MIT
Last Updated:Apr 10, 2015
Links: GitHub  ·  Packagist
Maintainer: anatoliyarkhipov

laracan

Installation

  • Require the package with composer:

composer require cubekit/laracan

  • Add provider to your config/app.php:

	'providers' => [

	    // ...

        'Cubekit\Laracan\LaracanServiceProvider',

        // ...

	],

  • Publish config:

php artisan vendor:publish --provider="Cubekit\Laracan\LaracanServiceProvider"

  • Add the Ability class to the app folder and implement the Cubekit\Laracan\AbilityContract

Note: the default config assumes that the Ability class is placed in the app folder. You are free to change it and place the class where would you want.

Usage

  • Define abilities
class Ability implements AbilityContract {

    public function initialize($user, Closure $can)
    {
        $user = $user ?: new App\User;

        // NOTE: Laracan does not provide any roles behavior! Assume that some
        // package already installed for this, like Entrust
        if ($user->hasRole('admin')) {

            // Admin can edit posts and comments unconditionally
            $can('edit', 'Post');
            $can('edit', 'Comment');

            return;
        }

        // User can edit a post only if he is its author
        $can('edit', 'Post', ['author_id' => $user->getKey()]);

        $can('edit', 'Comment', function($comment) use ($user)
        {
            // User can edit a comment only if he is its author
            // and comment is not older than 15 minutes
            return (
                $comment->author_id == $user->getKey() &&
                $comment->created_at >= Carbon::now()->subMinutes(15)
            );
        });

    }
}
  • Check ability in a request
class EditPostRequest {

    public function rules()
    {
        // ...
    }

    public function authorize()
    {
        $post = Post::find( $this->route('post') );

        return can('edit', $post);
    }

}
  • Check ability in a view
@foreach($post->comments as $comment)

<div class="comment">

    <div class="comment-body">{{ $comment->body }}</div>

    @can('edit', $comment)

        <div class="comment-footer">
            <a href="{{ route('comment.edit', $comment) }}">Edit</a>
        </div>

    @endcan

    </div>

</div>

@endforeach
  • Or you can use the can function directly to force IDE understand this code
@foreach($post->comments as $comment)

<div class="comment">

    <div class="comment-body">{{ $comment->body }}</div>

    @if( can('edit', $comment) )

        <div class="comment-footer">
            <a href="{{ route('comment.edit', $comment) }}">Edit</a>
        </div>

    @endif

    </div>

</div>

@endforeach

License

MIT

Related Packages

eneadm/ladder

Feather light permissions for Laravel.

27,347 263
ems/permit

Permission based authentication system

8,852 0
tadeas-j/permissions

An easy permissions for Laravel 5.

15 0
miracuthbert/laravel-roles

An Eloquent roles and permissions package for Laravel 5.8 and up

1,684 2
larrytech/auth

Authentication, Permissions and Roles for Laravel

961 2

Version History

Version Released PHP Laravel License
v0.2.1 >=5.4.0 5.0.*
v0.2.0 >=5.4.0 5.0.*
v0.1.0 >=5.4.0 5.0.*