public function vote(TokenInterface $token, $subject, array $attributes) { $logger = Logger::getLogger(); $logger->info("Vote for attr array " . print_r($attributes, true)); $user = $token->getUser(); $user_role = $user->getUserRole(); $result = $this->default_result; if ($this->container->hasParameter(self::ACL_CONFIG)) { $acl_config = $this->container->getParameter(self::ACL_CONFIG); if (array_key_exists($user_role, $acl_config)) { $role_config = $acl_config[$user_role]; $logger->debug("Config for role {$user_role}: " . print_r($role_config, true)); $class = new \ReflectionClass($subject); $entity_name = $class->getShortName(); if (array_key_exists($entity_name, $role_config)) { $role_entity_config = $role_config[$entity_name]; foreach ($attributes as $attribute) { $logger->info("Checking attribute {$attribute}"); if (array_key_exists($attribute, $role_entity_config)) { $result = $role_entity_config[$attribute]; $logger->info("Exists, result = {$result}"); if ($result == true) { return true; } } else { //action for this role is not specified $logger->warn("Action {$attribute} is not configured"); } } } else { //the entity was not configured for this role $logger->warn("Entity {$entity_name} for role {$user_role} is not configured"); } } else { //role was not configured $logger->warn("Role {$user_role} is not configured"); } } else { //acl is not configured $logger->warn("ACL is not configured"); } return $result; }
/** * Perform a single access check operation on a given attribute, subject and token. * * @param string $attribute * @param mixed $subject * @param TokenInterface $token * * @return bool */ protected function voteOnAttribute($attribute, $subject, TokenInterface $token) { $logger = Logger::getLogger(array("path" => "/home/asiliuk/Projects/voter_bundle_test/myapp.log", "date_format" => "Y.m.d H:i:s.u", "error_level" => "debug", "backtrace_enabled" => false)); $user = $token->getUser(); if (!$user instanceof User) { // the user must be logged in; if not, deny access return false; } $role = $user->getUserRole(); try { $config = $this->container->getParameter("post_" . $role . "_access"); $logger->info("Config " . print_r($config, true)); if (array_key_exists($attribute, $config)) { $result = $config[$attribute]; } else { $result = self::DEFAULT_RESULT; } } catch (InvalidArgumentException $e) { $result = self::DEFAULT_RESULT; } return $result; }