0

I've been trying to wrap my mind around this RBAC system.

The goal is with a static method from a class, I can get the current user/actor, then check if he has capability to do something for specific Entity:

say, my App class has a static method:

class App {

    .
    .

    public static function get_user() {
        $user_id = $_SESSION['user']->id;
        return new Actor( $user_id )
    }

    .
    .

}

then on the fly, I can get the Actor object with:

$actor = App::get_user();

then I can easily check its access for each entity by:

$is_allowed = $actor->can_do_on_entity_id( 'edit_details', 5 );

^ the goal

Okay cool, so I have uniform entities which shares the same interface.

class CompanyEntity implements EntityInterface {

    public function get_entity_type() {
        return 'company';
    }

}

class ProductEntity implements EntityInterface {

    public function get_entity_type() {
        return 'product';
    }
}

wrapping them in a Factory:

class EntityFactory {

    private $company_entity;
    private $product_entity;

    public function __construct() {
        $this->company_entity = new CompanyEntity();
        $this->product_entity = new ProductEntity();
    }

    public function get_company_entity() {
        return $this->company_entity;
    }

    public function get_product_entity() {
        return $this->product_entity;
    }

    public function get_object_by_row_id( $entity_id ) {

        $row = get_row( $entity_id );

        switch( $row->entity_type ) {
            case 'company':
                return $this->product_entity;
                break;
            case 'product':
                return $this->product_entity;
                break;
        }
    }
}

and since we will definitely need Roles, let's write them now.

class AdminRole implements RoleInterface {

    public function get_role_unique_name {
        return 'administrator';
    }

}

class EditorRole implements RoleInterface {

    public function get_role_unique_name {
        return 'editor';
    }

}

now working with the Actor Class:

class Actor {

    private $entity_factory;
    private $user_object;

    private function __construct( $user_id ) {
        $this->entity_factory = new EntityFactory();
        $this->user_object = get_user( $user_id );
    }

    public function add_role( RoleInterface $role_object ) {

        $inserted_row = $this->user_object->insert(
                'roles_table', 
                $role_object->get_role_unique_name()
            );

        return $inserted_row;
    }

    public function remove_role( RoleInterface $role_object ) {

        $deleted_row = $this->user_object->delete(
                'roles_table', 
                $role_object->get_role_unique_name()
            );

        return $deleted_row;
    }

    public function get_all_roles() {
        return $this->user_object->get( 'roles_table' )->mapToObject();
    }

    /*
     * @var $action string
     * @var $entity_id int
     */
    public function can_do_on_entity_id( $action, $entity_id ) {

        $entity_object = $this->entity_factory->get_object_by_row_id( $entity_id );
        $role_objects = $this->get_all_roles();

        /**
         *   TODO: How/What to proceed doing here?
         *   
         *   Now I'm confused... Where should I insert the "Roles",
         *   and "Permissions."
         *   
         *   I imagine, given the $action string, I'll do a `in_array()` search
         *   with all the permissions for that Entity. I'm lost.
         *   Where should these `$action`s be defined? in EntityObject?
         *   or in `Permission Class`? what should `Permission Class` do?
         */


    }
}

Writing the can_do_on_entity_id( $action, $entity_id ) method is giving me headache, I'm completely blank 😢

I imagine, given the $action string, I'll do a in_array() search with all the permissions for that Entity. I'm lost.

Where should these $actions be defined? in the object of these Entity Types? or in Permission Class? what should Permission Class do?

Hope somebody can help.

Regards,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.