Skip to content

danieleds/SimpleRoles

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Getting started

First, you have to create a new class where you will define all your rules. Let's start with this:

require_once "SimpleRoles.php";

class Ability extends SimpleRoles {

    function __construct($user) {
        parent::__construct();

        // You should define here your rules
    }

}

We place our rules inside the __construct function.

function __construct($user) {
    parent::__construct();

    // We put the current user roles in $user_roles.
    // Please note that you can use whatever method you
    // prefer for checking role membership.
    $user_roles = array();
    if($user != null)
        $user_roles = $user->getRoles();

    if(in_array('admin', $user_roles)) {
        $this->can('read_article');
        $this->can('write_article');
        $this->can('delete_article');
    }

    if(in_array('moderator', $user_roles)) {
        $this->can('read_article');
        $this->can('write_article');
    }

    if(in_array('guest', $user_roles)) {
        $this->cannot('all');
    }
}

About

A simple and lightweight PHP authorization library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages