Welcome Guest, Not a member yet? Register   Sign In
Starting with Myth:Auth - some questions
#17

My idea (and that is how my ACL library works) is to eliminate a DB query to check permission every time the user tryes to access a page...

Here is the config:

PHP Code:
<?php

namespace Config;

/**
* Acl Rules
*
* @package Config
*/

class Acl
{
    public $alwaysAllowed = [ 99 ]; //Groups that are allowed ANYWHERE - Example: Admin Group

    public $acl = [

        'users/add' => [ 
            2 => TRUE
        
],

        'candidates/*' => [
            1 => TRUE
        
],


        'candidates/index' => [
            2 => TRUE
        
]

    ];



And here is the class:

It is very simple... But works like a charm !!! And NO DB is required... 

PHP Code:
<?php

namespace App\Libraries;

class 
Acl {

    
/**
     * Configuration
     *
     * @var \App\Config\Acl
     */
    private $_rules = array();
    
    
private $_alwaysAllowed = array();


    function __construct() {

        $this->_rules config('App\\Config\\Acl')->acl;

        $this->_alwaysAllowed config('App\\Config\\Acl')->alwaysAllowed;
    }

    /**
     * Verify if a given path is allowed or not.
     * 
     * @param String $urn - Required. The Uniform Resource Name e.g.: admin/users/add
     * @param String $group - Required. Group names or ID. e.g.: admin,group1,group2,group3,1,2,3...
     * @return Boolean
     */
    public function isAllowed($urn$group) {

        //check if the group is in the Always Allowed List
        if(in_array($group$this->_alwaysAllowed))
        {

            //d('Always Allowed');

            return TRUE;

        }

        // default result is 'not allowed'
        $result FALSE;

        $urnArray explode('/'$urn);
        $urnAll $urnArray[0] . '/*';

        // if given path doesn't exists then, deny.
        // otherwise, validates it.
        if (isset($this->_rules[$urnAll])) { //Checa se existe uma regra para a seção INTEIRA, Exemplo welcome/*
            // retrieve identified groups
            $groups array_keys($this->_rules[$urnAll]);
            $groupsToTest explode(','$group);

            foreach ($groupsToTest as $g) {
                $result = (in_array($g$groups) && $this->_rules[$urnAll][$g]) ? TRUE FALSE;
                if ($result)
                    break;
            }
        }
        elseif (isset($this->_rules[$urn])) {


            // retrieve identified groups
            $groups array_keys($this->_rules[$urn]);
            $groupsToTest explode(','$group);


            foreach ($groupsToTest as $g) {
                $result = (in_array($g$groups) && $this->_rules[$urn][$g]) ? TRUE FALSE;
                if ($result)
                    break;
            }
        }// isset($this->_rules[$urn])

        return $result;
    }

// is_allowed

    /**
     * Verify exclusively if the given path is public to read.
     * @param String $urn - Required.
     * @return Boolean 
     */
    public function isPublic($urn)
    {
        
        
return (isset($this->_rules[$urn]) && isset($this->_rules[$urn][0]) && strtolower($this->_rules[$urn][0]) == 'public' ) ? TRUE FALSE;
    
    
}// is_public
}

// class

/* End of file Acl.php */
/* Location: ./application/libraries/Acl.php */ 


By the way... It would be AWESOME if you could give some comments about this code...  Smile
Reply


Messages In This Thread
RE: Starting with Myth:Auth - some questions - by Poetawd - 11-09-2019, 01:38 PM



Theme © iAndrew 2016 - Forum software by © MyBB