2017 © Pedro Peláez
 

library lock-laravel

A Laravel Driver for Lock.

image

beatswitch/lock-laravel

A Laravel Driver for Lock.

  • Tuesday, January 2, 2018
  • by driesvints
  • Repository
  • 17 Watchers
  • 158 Stars
  • 12,295 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 24 Forks
  • 3 Open issues
  • 16 Versions
  • 2 % Grown

The README.md

Lock - Laravel 5 Driver

Build Status Code Climate Test Coverage Software License Packagist Version Total Downloads, (*1)

I'm sad to say that Lock is currently not maintained. I won't be able to offer support or accept new contributions for the current time being. Other priorities are keeping me from putting the work into Lock that it deserves. Eventually I'll try to pick up work again but unfortunately I cannot say when. My thanks goes out to all the contributors and users., (*2)

-- Dries, (*3)

This package is a Laravel 5 driver for Lock. Check the documentation of Lock for more info. It requires at least PHP 5.6., (*4)

Table of Contents

Installation

Install this package through Composer., (*5)

$ composer require beatswitch/lock-laravel

Register the service provider in your app.php config file., (*6)

BeatSwitch\Lock\Integrations\Laravel\LockServiceProvider::class,

Register the facades in your app.php config file., (*7)

'Lock' => BeatSwitch\Lock\Integrations\Laravel\Facades\Lock::class,
'LockManager' => BeatSwitch\Lock\Integrations\Laravel\Facades\LockManager::class,

Publish the configuration file. After publishing you can edit the configuration options at config/lock.php., (*8)

$ php artisan vendor:publish --provider="BeatSwitch\Lock\Integrations\Laravel\LockServiceProvider" --tag="config"

If you're using the database driver you should run the package's migrations. This will create the database table where all permissions will be stored., (*9)

$ php artisan vendor:publish --provider="BeatSwitch\Lock\Integrations\Laravel\LockServiceProvider" --tag="migrations"
$ php artisan migrate

Please read the main Lock documentation for setting up the caller contract on your User model and for more in-depth documentation on how Lock works., (*10)

Also make sure to set the BeatSwitch\Lock\LockAware trait on your User model. That way your authenticated user will receive a Lock instance of itself so you can call permissions directly from your user object. If no user is authenticated, a SimpleCaller object will be bootstrapped which has the guest role. That way you can still use the Lock facade. If you want the LockAware trait to work on the User model you'll need to activate it with a middleware. Register the middleware below after the StartSession middleware., (*11)

\BeatSwitch\Lock\Integrations\Laravel\Middleware\InitLockAwareTrait::class,

Usage

Setting roles and aliases

You can register roles and aliases beforehand through the permissions callback in the config file. Here you can say which actions should be grouped under an alias or set which roles should inherit permissions from each other., (*12)

<?php

use BeatSwitch\Lock\Manager;

return [

    ...

    'permissions' => function (Manager $manager) {
        // Set your configuration here.
        $manager->alias('manage', ['create', 'read', 'update', 'delete']);
        $manager->setRole('user', 'guest');
        $manager->setRole(['editor', 'admin'], 'user');
    },
];

Setting permissions with the array driver

If you're using the array driver you can set all your permissions beforehand in the same permissions callback from above., (*13)

<?php

use BeatSwitch\Lock\Callers\SimpleCaller;
use BeatSwitch\Lock\Drivers\ArrayDriver;
use BeatSwitch\Lock\Manager;

return [

    ...

    'permissions' => function (Manager $manager) {
        // Only set permissions beforehand when using the array driver.
        if ($manager->getDriver() instanceof ArrayDriver) {
            // Set some role permissions.
            $manager->role('guest')->allow('read', 'posts');
            $manager->role('user')->allow('create', 'posts');
            $manager->role('editor')->allow('publish', 'posts');

            // Set permissions for a specific user.
            $manager->caller(new SimpleCaller('users', 1))->allow('publish', 'posts');
        }
    },
];

Using the database driver

Enable the database driver by switching the driver type in the config file. The database driver will use your default database connection to store permissions to your database. You can choose which table to store the permissions into by changing the setting in the config file., (*14)

Now that you have your database driver set up, you're ready to create a UI for your permissions and use the lock manager instance in your application to change permissions for callers or roles., (*15)

Using the facades

This package ships with two facades: the Lock facade which holds the BeatSwitch\Lock\Lock instance for your current authenticated user (or the guest user if no user is authenticated) and the LockManager class which can be used to bootstrap new lock instances for callers or roles., (*16)

Checking permissions for the current user is easy., (*17)

Lock::can('create', 'posts');
Lock::cannot('publish', $post);

// Or use the auth instance. This is possible because your User model has the LockAware trait.
Auth::user()->can('create', 'posts');

Use the manager to set permissions., (*18)

LockManager::caller($user)->allow('create', 'posts');
LockManager::caller($user)->allow('all');
LockManager::role('editor')->allow('create', 'posts');

Using dependency injection

You can use Laravel's IoC container to insert an instance of the current user's lock instance or the lock manager instance into your classes or controllers., (*19)

<?php

use BeatSwitch\Lock\Manager;

class UserManagementController extends BaseController
{
    protected $lockManager;

    public function __construct(Manager $lockManager)
    {
        $this->lockManager = $lockManager;
    }

    public function togglePermission()
    {
        $userId = Input::get('user');
        $action = Input::get('action');
        $resource = Input::get('resource');

        $user = User::find($userId);

        $this->lockManager->caller($user)->toggle($action, $resource);

        return Redirect::route('user_management');
    }
}

Maintainer

Lock is currently unmaintained., (*20)

This package is currently maintained by Dries Vints.
If you have any questions please don't hesitate to ask them in an issue., (*21)

Contributing

Please see the contributing file for details., (*22)

Changelog

You can see a list of changes for each release in the changelog file., (*23)

License

The MIT License. Please see the license file for more information., (*24)

The Versions

02/01 2018

dev-master

9999999-dev https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

17/10 2017

0.5.2

0.5.2.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

11/03 2017

0.5.1

0.5.1.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

18/12 2016

0.5.0

0.5.0.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

05/09 2015

0.4.2

0.4.2.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

14/08 2015

0.4.1

0.4.1.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

09/08 2015

0.4.0

0.4.0.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

04/06 2015

0.3.0

0.3.0.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

09/03 2015

0.2.1

0.2.1.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

08/03 2015

0.2.0

0.2.0.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

15/01 2015

0.1.3

0.1.3.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

13/01 2015

0.1.2

0.1.2.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

17/12 2014

0.1.1

0.1.1.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

17/12 2014

0.1.0

0.1.0.0 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

09/12 2014

0.1.0-alpha.2

0.1.0.0-alpha2 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts

08/12 2014

0.1.0-alpha.1

0.1.0.0-alpha1 https://github.com/BeatSwitch/lock

A Laravel Driver for Lock.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl roles permissions security conditions drivers beatswitch asserts