2017 © Pedro Peláez
 

library laracan

Easy define abilities of the current user.

image

cubekit/laracan

Easy define abilities of the current user.

  • Friday, April 10, 2015
  • by anatoliyarkhipov
  • Repository
  • 3 Watchers
  • 1 Stars
  • 115 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

laracan

Installation

  • Require the package with composer:

composer require cubekit/laracan, (*1)

  • Add provider to your config/app.php:

'providers' => [ // ... 'Cubekit\Laracan\LaracanServiceProvider', // ... ],
  • Publish config:

php artisan vendor:publish --provider="Cubekit\Laracan\LaracanServiceProvider", (*2)

  • 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., (*3)

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)



{{ $comment->body }}
@can('edit', $comment) @endcan
</div> @endforeach
  • Or you can use the can function directly to force IDE understand this code
@foreach($post->comments as $comment)



{{ $comment->body }}
@if( can('edit', $comment) ) @endif
</div> @endforeach

License

MIT, (*4)

The Versions

10/04 2015

dev-master

9999999-dev

Easy define abilities of the current user.

  Sources   Download

MIT

The Requires

 

by Anatoliy Arkhipov

laravel permissions

10/04 2015

v0.2.1

0.2.1.0

Easy define abilities of the current user.

  Sources   Download

The Requires

 

by Anatoliy Arkhipov

laravel permissions

10/04 2015

v0.2.0

0.2.0.0

Easy define abilities of the current user.

  Sources   Download

The Requires

 

by Anatoliy Arkhipov

02/04 2015

v0.1.0

0.1.0.0

Easy define abilities of the current user.

  Sources   Download

The Requires

 

by Anatoliy Arkhipov