2017 © Pedro Peláez
 

package sentry

Simple Laravel Resource Access Control

image

wesleyalmeida/sentry

Simple Laravel Resource Access Control

  • Monday, July 6, 2015
  • by wesleyalmeida
  • Repository
  • 1 Watchers
  • 4 Stars
  • 393 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 3 % Grown

The README.md

Sentry

Simple Laravel Resource Access Control, (*1)

Usage

Sentry is a simple Laravel resource access control plugin that works without specifying resources. The sentry_user_roles database table stores the relationship between the user and the user's roles. Roles can be any arbitrary string that an organization chooses to use. Because this system does not care about resources, developers can validate a user's roles at any time simply by running a check of Sentry::hasRole("my_role"). The developer's script can continue or hault based on the boolean result of that check., (*2)

Sentry requires knowledge of the user's roles before it is effective. The best place to load Sentry with this information is immediately after the user has been authorized in your application; typically after logging in., (*3)

Example, (*4)

public function doLogin() {

    $credentials = [
        'username' => 'foo',
        'password' => 'bar',
    ]

    if(Auth::attempt($credentials)) {

        // Retrieve SentryUserRoles from storage
        // Below is the Query way, but you can use
        // any other database driver.
        $table      = DB::table('sentry_user_roles');
        $query      = $table->where('user_id', "=", $user_id);
        $user_roles = $query->lists('role');

        // Add user roles to Sentry
        Sentry::setUserRoles($user_roles);

        // Success Authentication
        return Redirect::intended('/');

    } else {

        // Fail Authentication
        return Redirect::route('login');
    }
}

Once the developer has completed loading Sentry with the user roles it is not necessary to perform this step again., (*5)

Validation is simple. The developer can perform this anywhere, but the most common use-case is probably in a Controller's Action., (*6)

Example, (*7)

class HomeController extends BaseController {

    public function myAdminAction() {

        // Sentry::requireRole accepts a string, or an array
        // String usage is below
        $isAllowed = Sentry::requireRole('admin');

        if($isAllowed) {
            dd("Success, I'm allowed to do this!");
        }
        dd("Bummer, I am not allowed to do this...");
    }

    public function myPowerUserAction() {

        // Sentry::requireRole accepts a string, or an array
        // Array usage is below
        $isAllowed = Sentry::requireRole(['sales', 'sales_admin', 'sales_intern']);

        if($isAllowed) {
            dd("Success, I'm allowed to do this!");
        }
        dd("Bummer, I am not allowed to do this...");
    }
}

Instead of passing a string or an array to Sentry::requireRole(), a developer can allow Roles by using the Sentry::allowFooRole magic method. A third way of allowing roles is to use Sentry::allow("foo_role"). If the developer chooses this method, then he or she can call Sentry::requireRole() without any parameters., (*8)

Example, (*9)

Sentry::allowUser();
Sentry::allowGuest();
$isAllowed = Sentry::requireRole();

is the same as, (*10)

$isAllowed = Sentry::requireRole(['user', 'guest']);

which is the same as, (*11)

Sentry::allow('user');
Sentry::allow('guest');
$isAllowed = Sentry::requireRole();

Additionally, the configuration file for this package includes the parameter super_admin. The role assigned to this key will always be allowed whenever Sentry::requireRole() is invoked. In other words, Sentry::requireRole() will return TRUE for users who's roles include the value that matches the value in super_admin., (*12)

Example, (*13)

// config/packages/wesleyalmeida/sentry/config.php
    'super_admin' => 'admin',


// login action
    // User Roles
    $user_roles = ['user', 'sales', 'admin']
    // Add user roles to Sentry
    Sentry::setUserRoles($user_roles);

// someAction()
    $isAllowed = Sentry::requireRole(); // returns true

Final Note, (*14)

  1. The user roles are not case sensitive. All user roles are normalized to lowercase as soon as the developer provides them to Sentry. Underscores are not converted to camelCase. Therefore, salesAdmin is the same as salesadmin, but neither are the same as sales_admin., (*15)

  2. Sentry uses Laravel's Session to store the user roles. If you want to store the user roles in the Auth::user() object, you can do so by adding the following method to the User object that your UserProvider class demands. In the event that the user roles expire within the Session, Sentry will throw a SentryKeyNotFoundException. Catch this exception and reset the user roles with:, (*16)

    Sentry::setUserRoles($user_roles);, (*17)

Sample, (*18)

// Eloquent User
public function roles() {
    $this->hasMany('SentryUserRoles', 'user_id', 'id); // SentryUserRoles must also be an Eloquent model
}

// Using QueryBuilder
public function roles() {
    $table = DB::table('sentry_user_roles');

    $query = $table->where('user_id', "=", $user_id);

    return $query->lists('role');
}

Installation

Composer

"require": {
    "wesleyalmeida/sentry": "dev-master"
},
"repositories": [
    { "type": "vcs", "url": "git@github.com:wesleyalmeida/sentry.git" }
],

Configuration File

php artisan config:publish wesleyalmeida/sentry"

Database Table

php artisan migrate --package="wesleyalmeida/sentry"

The Versions

06/07 2015

dev-master

9999999-dev

Simple Laravel Resource Access Control

  Sources   Download

Apache-2.0

The Requires

  • php >=5.4.0

 

by Wes Almeida

laravel acl rbac basic access control