<commit_before><?php 
/**
 * Jailson for CakePHP
 *   Access Control Plugin
 * 
 * @category CakePHP
 * @author <NAME> <<EMAIL>>
 * @package plugins.jailson
 * @subpackage plugins.jailson.behaviors
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 * @link http://github.com/m3nt0r/cakephp-jailson Repository/Docs
 * @copyright (c) 2010, <NAME> (http://cakealot.com)
 */
App::import('Lib', 'Jailson.Storage');
/**
 * Jailson - Guarded Behavior
 * 
 * @category CakePHP
 * @author <NAME> <<EMAIL>>
 * @package plugins.jailson
 * @subpackage plugins.jailson.behaviors
 */
class GuardedBehavior extends ModelBehavior {
	
	/**
	 * Jailson storage model
	 * @var Model
	 */
	public $Inmate;
	
	/**
	 * The scope of this guard
	 * @var Model
	 */
	public $GuardedObject;
	
	/**
	 * Init Storage Model
	 */
	public function setup(&$model, $config = array()) {
		// options
		$_defaultConfig = array(
			'inmateModel' => 'Jailson.Inmate',
			
			'find' => array(),
			'save' => array(),
			'delete' => array(),
		);
		$this->settings[$model->alias] = array_merge($_defaultConfig, $config);	
		
		// load storage model
		if (!is_object($this->Inmate)) {
			$this->Inmate = ClassRegistry::init($this->settings[$model->alias]['inmateModel']);
		}
	}
	
	/**
	 * Enable permission chain
	 */
	public function beforeFind(&$model, $query) {
		$this->_bind($model, $this->_perms($model, 'find'));
	}
	
	/**
	 * Remove permission chain
	 */
	public function afterFind(&$model) {
		$this->_unbind($model);
	}
	
	public function beforeSave(&$model) { }
	public function beforeDelete(&$model, $cascade = true) { }
	

	/**
	 * Set chain
	 */
	protected function _bind(&$model, $roles = array()) {
		
		$inmateModel = $this->settings[$model->alias]['inmateModel'];
		
		$conditions = array(
			'Jailson.what' => $model->alias
		);
		
		if (!empty($roles)) {
			$conditions = array_merge($conditions, 
				array('Jailson.role' => $roles)
			);
		} else {
			return false; // dont bind if there are no rules.
		}
		
		$model->bindModel(array('hasOne' => array(
			'Jailson' => array(
				'className' => $inmateModel,
				'type' => 'INNER',
				'foreignKey' => 'whatId',
				'conditions' => $conditions
			)
		)));
	}
	
	/**
	 * Remove chain
	 */
	protected function _unbind(&$model) {
		$model->unbindModel(array('hasOne' => array(
			'Jailson'
		)), $reset = false);
	}
	
	/**
	 * Read from settings array (shortcut)<commit_msg>guarded: find supports allow/deny now using "in" and "not in"<commit_after><?php 
/**
 * Jailson for CakePHP
 *   Access Control Plugin
 * 
 * @category CakePHP
 * @author <NAME> <<EMAIL>>
 * @package plugins.jailson
 * @subpackage plugins.jailson.behaviors
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 * @link http://github.com/m3nt0r/cakephp-jailson Repository/Docs
 * @copyright (c) 2010, <NAME> (http://cakealot.com)
 */
App::import('Lib', 'Jailson.Storage');
/**
 * Jailson - Guarded Behavior
 * 
 * @category CakePHP
 * @author <NAME> <<EMAIL>>
 * @package plugins.jailson
 * @subpackage plugins.jailson.behaviors
 */
class GuardedBehavior extends ModelBehavior {
	
	/**
	 * Jailson storage model
	 * @var Model
	 */
	public $Inmate;
	
	/**
	 * The scope of this guard
	 * @var Model
	 */
	public $GuardedObject;
	
	/**
	 * Init Storage Model
	 */
	public function setup(&$model, $config = array()) {
		// options
		$_defaultConfig = array(
			'inmateModel' => 'Jailson.Inmate',
			
			'find' => array(
				'allow' => array(),
				'deny' => array()
			),
			'save' => array(),
			'delete' => array(),
		);
		$this->settings[$model->alias] = array_merge($_defaultConfig, $config);	
		
		// load storage model
		if (!is_object($this->Inmate)) {
			$inmateModel = $this->settings[$model->alias]['inmateModel'];
			$this->Inmate = ClassRegistry::init($inmateModel);
		}
	}
	
	/**
	 * Enable permission chain
	 */
	public function beforeFind(&$model, $query) {
		$this->_bind($model, $this->_perms($model, 'find'));
	}
	
	/**
	 * Remove permission chain
	 */
	public function afterFind(&$model) {
		$this->_unbind($model);
	}
	
	public function beforeSave(&$model) { }
	public function beforeDelete(&$model, $cascade = true) { }
	

	/**
	 * Set chain
	 */
	protected function _bind(&$model, $roles = array()) {
		
		$inmateModel = $this->settings[$model->alias]['inmateModel'];
		
		$conditions = array(
			'Jailson.what' => $model->alias
		);
		
		if (!empty($roles)) {
			
			// if no keyword present, but not empty, assume allow-list
			if (empty($roles['allow']) && empty($roles['deny'])) {
				$_roles = $roles;
				$roles = array(
					'deny' => array(),
					'allow' => $_roles
				);
			}
			
			if (!empty($roles['allow'])) {
				if(sizeof($roles['allow'])==1)
					$roles['allow'][] = '__'; // fix for query builder
				
				$conditions = array_merge($conditions, 
					array('Jailson.role' => $roles['allow'])
				);
			}
			
			if (!empty($roles['deny'])) {
				if(sizeof($roles['deny'])==1)
					$roles['deny'][] = '__'; // fix for query builder
				
				$conditions = array_merge($conditions, 
					array('Jailson.role NOT' => $roles['deny'])
				);
			}
			
		} else {
			return false; // dont bind if there are no rules.
		}
		
		$model->bindModel(array('hasOne' => array(
			'Jailson' => array(
				'className' => $inmateModel,
				'type' => 'INNER',
				'foreignKey' => 'whatId',
				'conditions' => $conditions
			)
		)));
	}
	
	/**
	 * Remove chain
	 */
	protected function _unbind(&$model) {
		$model->unbindModel(array('hasOne' => array(
			'Jailson'
		)), $reset = false);
	}
	
	/**
	 * Read from settings array (shortcut)
{'fraction_non_alphanumeric': 0.1878453038674033, 'fraction_numerical': 0.0024950989128497596, 'mean_word_length': 3.2709284627092847, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3193670', 'n_tokens_mistral': 2231, 'n_tokens_neox': 1889, 'n_words': 399}