<commit_before><?php
declare (strict_types = 1);
namespace memCrab\Access;

use memCrab\Exceptions\AccessException;

/**
 *  Router for core project
 *
 *  @author <NAME>
 */
class Access {
	private $servicesMatrix;
	private $groupsMatrix;
	private $rolesMatrix;
	private $rules;

	public function loadRules(array $rules): void {
		if (empty($rules)) {
			throw new AccessException(_("Empty rules"), 500);
		}

		$this->rules = $rules;

		foreach ($this->rules as $groupName => $accessRule) {
			if (empty($accessRule['roles']) || !is_array($accessRule['roles'])) {
				throw new AccessException(_("Empty roles in group:") . " " . $groupName, 500);
			}
			if (empty($accessRule['services']) || !is_array($accessRule['services'])) {
				throw new AccessException(_("Empty services in group:") . " " . $groupName, 500);
			}

			foreach ($accessRule['services'] as $serviceName => $actions) {
				if (empty($actions) || !is_array($actions)) {
					throw new AccessException(_("Empty actions in service:") . " " . $serviceName, 500);
				}
				foreach ($actions as $action) {
					foreach ($accessRule['roles'] as $role) {
						$this->servicesMatrix[$serviceName][$action][$role][] = $groupName;
						$this->groupsMatrix[$groupName][$role][$serviceName][] = $action;
						$this->rolesMatrix[$role][$groupName][$serviceName][] = $action;
					}
				}
			}
		}
	}

	public function checkRights(string $service, string $action, string $userRole): bool {
		return isset($this->servicesMatrix[$service][$action][$userRole]);
	}

	public function getRoleAllowedAccessGroups(string $role): array{
		$allowedAccessGroups = array();
		if (!isset($this->rolesMatrix[$role])) {
			throw new AccessException(_("Role not exist: ") . " " . $role, 401);
		}

		if (is_array($this->rolesMatrix[$role])) {
			return array_keys($this->rolesMatrix[$role]);
		} else {
			return array();
		}
	}

	public function getRightsByServices(): array{
		return $this->servicesMatrix;
	}

	public function getRightsByRoles(): array{
		return $this->rolesMatrix;
	}

	public function getRightsByGroups(): array{
		return $this->groupsMatrix;
	}
}<commit_msg>improve performance for check access logic<commit_after><?php
declare (strict_types = 1);
namespace memCrab\Access;

use memCrab\Exceptions\AccessException;

/**
 *  Router for core project
 *
 *  @author <NAME>
 */
class Access {
	private $rules;
	private $accessMatrix;

	public function loadRules(array $rules): void {
		if (empty($rules)) {
			throw new AccessException(_("Empty rules"), 500);
		}

		$this->rules = $rules;
		$this->accessMatrix = array();

		foreach ($this->rules as $groupName => $accessRule) {
			if (empty($accessRule['roles'])) {
				throw new AccessException(_("Empty roles in Access Group:") . " " . $groupName, 500);
			}
			if (empty($accessRule['services'])) {
				throw new AccessException(_("Empty services in Access Group:") . " " . $groupName, 500);
			}

			$this->accessMatrix = array_merge_recursive(
				$this->accessMatrix,
				array_fill_keys($accessRule['roles'], $accessRule['services'])
			);
		}
	}

	public function checkRights(string $role, string $service, string $action): bool {
		return isset($this->accessMatrix[$service][$action][$userRole]);
	}
}
{'fraction_non_alphanumeric': 0.16759603469640644, 'fraction_numerical': 0.0080545229244114, 'mean_word_length': 4.482173174872665, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3547455', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1002, 'n_words': 231}