<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/

/**
 * Class AHDB_Core_ACL
 *
 * Access Control Logic
 */
class AHDB_Core_ACL {

    private $isRestrictive = true;
    private $acl = array();

    public function restrictive( $restr ) {
        if ( is_bool( $restr ) ) {
            $this->isRestrictive = $restr;
        }

        return $this->isRestrictive ;
    }

    public function allow( $role, $hash ) {
        $this->addRole( $role );
        array_push( $this->acl[ $role ], $hash );
    }

    public function isAllowed( $role, $hash ) {
        $isAllowed = !$this->isRestrictive ;
        if ( isset( $this->acl[ $role ] ) && is_array( $this->acl[ $role ] ) ) {
            $isAllowed = in_array( $hash,  $this->acl[ $role ] );
        }

        return $isAllowed;
    }

    public function reset() {
        $this->isRestrictive  = true;
        $this->acl = array();
    }

    private function addRole( $role ) {
        if ( !isset( $this->acl[ $role ] ) ) {
            $this->acl[ $role ] = array();
        }
    }

}