Location: PHPKode > projects > Anahita Social Engine > dependencies/plg_system_socialengine/socialengine/model/acl/acl.php
<?php
/**
 * @version		1.0.0
 * @category	Anahita Social Engine™
 * @copyright	Copyright (C) 2008 - 2010 rmdStudio Inc. and Peerglobe Technology Inc. All rights reserved.
 * @license		GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
 * @link     	http://www.anahitapolis.com
 */


class AnModelAcl extends KObject
{	
	static $_prototype;
	/**
	 * 
	 * 
	 * @var array
	 */
	static $_instances = array();
	
	/**
	 * 
	 * @var array
	 */
	public $rules = array();
	
	
	/**
	 * 
	 * @var AnModelNode
	 */
	public $node;
		
	/**
	 * Return a acl instance for a node
	 * @return AnModelAcl
	 * @param $options Array[optional]
	 */
	static public function factory( array $options = array() )
	{				
		$default = array(
			'node'  => null,
			'rules' => null
		);
		
		$options = array_merge($default, $options);
		
		if ( !$options['node'] && !$options['rules'] )
			throw new AnModelAclException("ACL object must requires a node or an identifier to instantiate");
		
		$node = null;
		
		if ( $options['node'] ) {
			
			$node    = $options['node'];	
			
			$handle = "{$node->id}{$node->getIdentifier()}";	
			
			if ( isset(self::$_instances[$handle]) )
				return self::$_instances[$handle];
		
			$identifier = $node->getIdentifier();
			
		}  else {
			
			$identifier = $options['rules'];	
		}
		
		$rules = KFactory::get('lib.anahita.model.acl.rules')->get( $identifier );				
		
		//clone the acl object for performance reasons
		
		if ( !self::$_prototype )
			self::$_prototype  = new AnModelAcl();
		
		$acl = clone self::$_prototype;
		
		$acl->rules = $rules;
		
		$acl->node  = $node;
		
		if ( $node )
			self::$_instances[$handle] = $acl;
		
		return $acl;
	}		
	
	/**
	 * Initialie an acl object for a node
	 * @return 
	 * @param $options Object
	 */
	public function __construct($options=array())
	{					
		$default = array(
			'rules' => array() ,
			'node'  => null
		);
		
		$options = array_merge($default, $options);	
		
		$this->node  = $options['node'];
		
		$this->rules = $options['rules'];
	}
	
	/**
	 * Add a rule to the ACL
	 * 
	 * @param mixed rule
	 * @return void
	 */
	public function addRule($rule)
	{
		$identifier = KFactory::identify($rule);
		array_unshift($this->rules, KFactory::get($identifier));
	}
	
	/**
	 * Captures a acl call and pass it to all the rules
	 * @return 
	 * @param $method Object
	 * @param $args Object
	 */
	public function __call($method, $args)
	{	
		array_unshift($args, $this->node);		
		array_unshift($args, AnModelAnahita::getSessionViewer());
		foreach($this->rules as $rule) 
		{   
			if ( method_exists($rule, $method) ) {
				return call_user_func_array(array($rule, $method), $args);
			}
		}
		
		return false;
	}
}
Return current item: Anahita Social Engine