<?php
/**
 * Mabo_Acl
 *
 * @package    MaBoBlog
 * @author     Marian Böhmer
 */
class MyWeb_Acl extends Zend_Acl 
{
	
	protected static $_instances = array();
	
	
	public static function getInstance($module = 'default')
	{
		if (!file_exists(APPLICATION_PATH. '/configs/acl/'. $module . '.ini')){
			$module = 'default';

		}
		if (array_key_exists($module, self::$_instances)){
			if (null === self::$_instances[$module]) {
				self::$_instances[$module] = new self($module);
			}
		}else {
			self::$_instances[$module] = new self($module);
		}
		
		return self::$_instances[$module];
	}
	
    /**
     * vytvoří ACL s uživatelskými rolemi, zdroji a pravidly
     */
    protected function __construct($module = 'default')
    {
    	$config = new Zend_Config_Ini(APPLICATION_PATH. '/configs/acl/'. $module . '.ini');
    	
        // vloží uživatelské role
        foreach ($config->roles as $role) {
            $this->addRole(new Zend_Acl_Role($role));
        }
        
        // vloží zdroje
        foreach ($config->ressources as $resource) {
            $this->addResource(new Zend_Acl_Resource($resource));
        }
        
        // vloží pravidla
        foreach ($config->rules as $function => $rule) {//deny
            foreach ($rule as $role => $rule2) {//guest
                foreach ($rule2 as $resource => $rule3) {//article
                    if ('all' == $rule3) {
                        $this->$function($role, $resource);
                    } else {
                        $this->$function($role, $resource, $rule3->toArray());
                    }
                }
            }
        }
    }
}
