2017 © Pedro Peláez
 

library user-privilege-mapper

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

image

vsch/user-privilege-mapper

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  • Saturday, February 13, 2016
  • by vsch
  • Repository
  • 1 Watchers
  • 3 Stars
  • 6,687 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 19 Versions
  • 1 % Grown

The README.md

Laravel User Privilege Mapper

This package implements a simple Service Provider that enables mapping of user privileges independent of the implementation of the User model. It uses Laravel macros() to create an interface between a user privilege test and the actual implementation of the code to check whether the current user has this privilege enabled., (*1)

I use this package to allow my other packages to test for security privileges without having to dictate how the user model is defined within a project., (*2)

For Laravel 4 use the Laravel4 branch, or require: "vsch/user-privilege-mapper": "~1.0", (*3)

For Laravel 5 use the master branch, or require: "vsch/user-privilege-mapper": "~2.0", (*4)

Installation

  1. Require this package in your composer.json and run composer update (or run composer require vsch/user-privilege-mapper:* directly):, (*5)

    "vsch/user-privilege-mapper": "~2.0"
  2. After updating composer, add the ServiceProviders to the providers array in config/app.php, (*6)

    Vsch\UserPrivilegeMapper\UserPrivilegeMapperServiceProvider::class,
  3. add the Facade to the aliases array in config/app.php:, (*7)

    'UserCan'   => Vsch\UserPrivilegeMapper\Facade\Privilege::class,
  4. To create a mapping layer between your User model implementation and the need to test user privileges without knowing the implementation. You need to create named privileges for the UserPrivilegeMapper via the Laravel macro mechanism. This should be done in the initialization files., (*8)

    A good place is the app/Providers/AppServiceProvider.php file, add the following to the boot() function, if your User model has is_admin and is_editor attributes to identify users that have Admin and Editor privileges or just return true in both cases, if you don't have any way of determining user privileges:, (*9)

    \UserCan::macro("admin", function ()
    {
        return ($user = Auth::user()) && $user->is_admin;
    });
    
    \UserCan::macro("edit", function ()
    {
        return ($user = Auth::user()) && ($user->is_admin || $user->is_editor);
    });
  5. Testing whether a privilege is available is as simple as:, (*10)

    if (\UserCan::admin())
    {
        // user has admin privileges
    }
    elseif (\UserCan::edit())
    {
        // user has edit privileges
    }

    If a macro was not previously defined then the privilege test will return false. Effectively, if the macro is not implemented then the privilege is treated as not existent for every user., (*11)

The implementation is more of a convention that is implemented by Laravel's Macroable trait. I don't see this package changing unless I get an epiphany. So consider this package code complete. However, suggestions are appreciated and welcome. :), (*12)

The Versions

13/02 2016

dev-master

9999999-dev

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

laravel authentication auth

13/02 2016

2.0.5

2.0.5.0

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

laravel authentication auth

26/12 2015

2.0.4

2.0.4.0

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

laravel authentication auth

15/10 2015

dev-laravel4

dev-laravel4

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

23/09 2015

dev-Laravel-Translation

dev-Laravel-Translation

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

laravel authentication auth

25/07 2015

2.0.3

2.0.3.0

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

laravel authentication auth

24/07 2015

2.0.2

2.0.2.0

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

laravel authentication auth

24/07 2015

2.0.1

2.0.1.0

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

laravel authentication auth

24/07 2015

1.0.10

1.0.10.0

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

24/07 2015

1.0.9

1.0.9.0

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

23/07 2015

1.0.8

1.0.8.0

A Laravel Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

22/07 2015

1.0.7

1.0.7.0

A Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

MIT

The Requires

 

by Vladimir Schneider

22/07 2015

1.0.6

1.0.6.0

A Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

The Requires

 

by Vladimir Schneider

22/07 2015

1.0.5

1.0.5.0

A Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

The Requires

 

by Vladimir Schneider

22/07 2015

1.0.4

1.0.4.0

A Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

The Requires

 

by Vladimir Schneider

22/07 2015

1.0.3

1.0.3.0

A Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

The Requires

 

by Vladimir Schneider

22/07 2015

1.0.1

1.0.1.0

A Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

The Requires

 

by Vladimir Schneider

22/07 2015

1.0.2

1.0.2.0

A Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

The Requires

 

by Vladimir Schneider

22/07 2015

1.0.0

1.0.0.0

A Service Provider to handle testing of available user security privileges independent of User Model implementation

  Sources   Download

The Requires

 

by Vladimir Schneider