<commit_before><?php

/**
* @package   s9e\Acl
* @copyright Copyright (c) 2010-2014 The s9e Authors
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\Acl;

class Matrix
{
	/**
	* Internal value used for granted permissions
	*/
	const ALLOW = 1;

	/**
	* Internal value used for revoked permissions
	*/
	const DENY = 0;

	/**
	* @var array List of settings, grouped by action
	*/
	protected $acl;

	/**
	* @var array Multi-dimensional array containing the permissions granted by given permission.
	*            First level uses the source (grantor) permission's action as key, then the
	*            permission index then the target (grantee) permission's action
	*/
	protected $grantees;

	/**
	* @var array Multi-dimensional array containing the permissions that granted given permission.
	*            First level uses the target (grantee) permission's action as key, then the
	*            permission index then the source (grantee) permission's action
	*/
	protected $grantors;

	/**
	* @var array List of parent indices for each individual index
	*/
	protected $inherit;

	/**
	* @var integer Size of the matrix (number of settings per action)
	*/
	protected $matrixSize;

	/**
	* @var array Offset of each scope value
	*/
	protected $offsets;

	/**
	* 
	*
	* @return void
	*/
	public function __construct(array $settings, array $rules)
	{
		$this->computeOffsets($settings);
		$this->fillMatrix($settings);
		$this->solve($rules);
	}

	/**
	* 
	*
	* @return array
	*/
	public function getBitfields()
	{
		return array_map(
			function ($settings)
			{
				return implode('', array_map('intval', $settings));
			},
			$this->acl
		);
	}

	/**
	* 
	*
	* @return array
	*/
	public function getOffsets()
	{
		return $this->offsets;
	}

	/**
	* 
	// The target perm's setting is either self::ALLOW, in which case there's
	// nothing to do, or self::DENY, which can't be overwritten anyway.
	//
	// We also check whether the target perm has been granted during this
	// iteration. If it was, we record all the grantors so that we only revoke
	// the target perm if ALL of them get revoked.
	*
	* @param  string $srcAction Source action (grantor)
	* @param  string $trgAction Target action (grantee)
	* @return bool              Whether a new permission has been granted
	*/
	protected function applyGrantRule($srcAction, $trgAction)
	{
		$granted = false;
		foreach (array_filter($this->acl[$srcAction]) as $k => $setting)
		{
			if (!isset($this->acl[$trgAction][$k]) || isset($this->grantees[$trgAction][$k]))
			{
				if (!isset($this->acl[$trgAction][$k]))
				{
					$granted = true;
				}

				$this->acl[$trgAction][$k] = self::ALLOW;
				$this->grantees[$srcAction][$k][$trgAction] = $trgAction;
				$this->grantors[$trgAction][$k][$srcAction] = $srcAction;
			}
		}

		return $granted;
	}

	/**
	* 
	*
	* @return void
	*/
	protected function applyGrantRules(array $rules)
	{
		$this->grantees = $this->grantors = [];
		$this->applyRules('applyGrantRule', $rules);
	}

	/**
	* 
	*
	* @return void
	*/
	protected function applyInheritance()
	{
		foreach ($this->acl as $action => &$settings)
		{
			$i = 0;
			while (++$i < $this->matrixSize)
			{
				foreach ($this->inherit[$i] as $inherit)
				{
					if ($settings[$i] === self::DENY)
					{
						break;
					}

					if (isset($settings[$inherit]))
					{
						$settings[$i] = $settings[$inherit];
					}
				}
			}
		}
	}

	/**
	* 
	*
	* @return void
	*/
	protected function applyRequireRules(array $rules)
	{
		$this->applyRules('applyRequireRule', $rules);

		// TODO: test A grant B, C grant D, B grant C, B require C
		// TODO: A grant C, B require C, C require D
		// TODO: A grant C, B require C, A require D
	}

	/**
	* 
	*
	* @param  string $srcAction Source action
	* @param  string $trgAction Target action (the required permission)
	* @return bool              Whether a permission has been revoked
	*/
	protected function applyRequireRule($srcAction, $trgAction)
	{
		$revoked = false;
		foreach (array_filter($this->acl[$srcAction]) as $k => &$setting)
		{
			if ($this->acl[$trgAction][$k] !== self::ALLOW)
			{
				$setting = $this->acl[$trgAction][$k];
				$revoked = true;

				if (isset($this->grantees[$srcAction][$k]))
				{
					$this->cancelGrantsFrom($srcAction, $k);
				}
			}
		}

		return $revoked;
	}

	/**<commit_msg>Fixed reference<commit_after><?php

/**
* @package   s9e\Acl
* @copyright Copyright (c) 2010-2014 The s9e Authors
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\Acl;

class Matrix
{
	/**
	* Internal value used for granted permissions
	*/
	const ALLOW = 1;

	/**
	* Internal value used for revoked permissions
	*/
	const DENY = 0;

	/**
	* @var array List of settings, grouped by action
	*/
	protected $acl;

	/**
	* @var array Multi-dimensional array containing the permissions granted by given permission.
	*            First level uses the source (grantor) permission's action as key, then the
	*            permission index then the target (grantee) permission's action
	*/
	protected $grantees;

	/**
	* @var array Multi-dimensional array containing the permissions that granted given permission.
	*            First level uses the target (grantee) permission's action as key, then the
	*            permission index then the source (grantee) permission's action
	*/
	protected $grantors;

	/**
	* @var array List of parent indices for each individual index
	*/
	protected $inherit;

	/**
	* @var integer Size of the matrix (number of settings per action)
	*/
	protected $matrixSize;

	/**
	* @var array Offset of each scope value
	*/
	protected $offsets;

	/**
	* 
	*
	* @return void
	*/
	public function __construct(array $settings, array $rules)
	{
		$this->computeOffsets($settings);
		$this->fillMatrix($settings);
		$this->solve($rules);
	}

	/**
	* 
	*
	* @return array
	*/
	public function getBitfields()
	{
		return array_map(
			function ($settings)
			{
				return implode('', array_map('intval', $settings));
			},
			$this->acl
		);
	}

	/**
	* 
	*
	* @return array
	*/
	public function getOffsets()
	{
		return $this->offsets;
	}

	/**
	* 
	// The target perm's setting is either self::ALLOW, in which case there's
	// nothing to do, or self::DENY, which can't be overwritten anyway.
	//
	// We also check whether the target perm has been granted during this
	// iteration. If it was, we record all the grantors so that we only revoke
	// the target perm if ALL of them get revoked.
	*
	* @param  string $srcAction Source action (grantor)
	* @param  string $trgAction Target action (grantee)
	* @return bool              Whether a new permission has been granted
	*/
	protected function applyGrantRule($srcAction, $trgAction)
	{
		$granted = false;
		foreach (array_filter($this->acl[$srcAction]) as $k => $setting)
		{
			if (!isset($this->acl[$trgAction][$k]) || isset($this->grantees[$trgAction][$k]))
			{
				if (!isset($this->acl[$trgAction][$k]))
				{
					$granted = true;
				}

				$this->acl[$trgAction][$k] = self::ALLOW;
				$this->grantees[$srcAction][$k][$trgAction] = $trgAction;
				$this->grantors[$trgAction][$k][$srcAction] = $srcAction;
			}
		}

		return $granted;
	}

	/**
	* 
	*
	* @return void
	*/
	protected function applyGrantRules(array $rules)
	{
		$this->grantees = $this->grantors = [];
		$this->applyRules('applyGrantRule', $rules);
	}

	/**
	* 
	*
	* @return void
	*/
	protected function applyInheritance()
	{
		foreach ($this->acl as $action => &$settings)
		{
			$i = 0;
			while (++$i < $this->matrixSize)
			{
				foreach ($this->inherit[$i] as $inherit)
				{
					if ($settings[$i] === self::DENY)
					{
						break;
					}

					if (isset($settings[$inherit]))
					{
						$settings[$i] = $settings[$inherit];
					}
				}
			}
		}
	}

	/**
	* 
	*
	* @return void
	*/
	protected function applyRequireRules(array $rules)
	{
		$this->applyRules('applyRequireRule', $rules);

		// TODO: test A grant B, C grant D, B grant C, B require C
		// TODO: A grant C, B require C, C require D
		// TODO: A grant C, B require C, A require D
	}

	/**
	* 
	*
	* @param  string $srcAction Source action
	* @param  string $trgAction Target action (the required permission)
	* @return bool              Whether a permission has been revoked
	*/
	protected function applyRequireRule($srcAction, $trgAction)
	{
		$revoked = false;
		foreach (array_filter($this->acl[$srcAction]) as $k => $setting)
		{
			if ($this->acl[$trgAction][$k] !== self::ALLOW)
			{
				$this->acl[$srcAction][$k] = $this->acl[$trgAction][$k];
				$revoked = true;

				if (isset($this->grantees[$srcAction][$k]))
				{
					$this->cancelGrantsFrom($srcAction, $k);
				}
			}
		}

		return $revoked;
	}

	/**
{'fraction_non_alphanumeric': 0.14900927728782498, 'fraction_numerical': 0.003206963692589623, 'mean_word_length': 3.0519721577726218, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 58, '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': '454271', 'n_tokens_mistral': 3334, 'n_tokens_neox': 2906, 'n_words': 813}