/** * @param string * @param string * @return array */ public static function parseAnnotations($class, $method = NULL) { if (strpos($class, '::') !== FALSE && !$method) { list($class, $method) = explode('::', $class); } $ref = new Reflection\Method($class, $method); $cRef = new Reflection\ClassType($class); $anntations = (array)$ref->getAnnotation('allowed'); $role = isset($anntations['role']) ? $anntations['role'] : ($ref->hasAnnotation('role') ? $ref->getAnnotation('role') : NULL); $resource = isset($anntations['resource']) ? $anntations['resource'] : ($ref->hasAnnotation('resource') ? $ref->getAnnotation('resource') : ($cRef->hasAnnotation('resource') ? $cRef->getAnnotation('resource') : NULL)); $privilege = isset($anntations['privilege']) ? $anntations['privilege'] : ($ref->hasAnnotation('privilege') ? $ref->getAnnotation('privilege') : NULL); return array( static::ROLE => $role, static::RESOURCE => $resource, static::PRIVILEGE => $privilege, ); }
/** * @param Method $element * @throws \Flame\Rest\Security\ForbiddenRequestException */ public function authenticate(Method $element) { $user = (array) $element->getAnnotation('User'); if (in_array('loggedIn', $user)) { if (!$this->user->isLoggedIn()) { throw new ForbiddenRequestException('Please sign in.'); } } }
private function getMethodExpressionsToEvaluate(Method $method) { $annotation = $method->getAnnotation('Security'); if ($annotation) { if (!is_string($annotation)) { throw new \InvalidArgumentException('Security annotation must be simple string with expression.'); } return [new Expression($annotation)]; } return []; }
/** * @param Method $reflection * @return array * * @throws InvalidArgumentException */ public function parse($reflection) { if (!$reflection instanceof Method) { throw new InvalidArgumentException('RouteAnnotation can be parsed only on method'); } $result = array(); foreach ($this->methods as $methodName => $methodFlag) { if ($reflection->hasAnnotation($methodName)) { $result[$methodFlag] = $reflection->getAnnotation($methodName); } } return $result; }
/** * Parse cronner values from annotations. * * @param \Nette\Reflection\Method $method * @return array */ public static function parseParameters(Method $method) { $taskName = NULL; if ($method->hasAnnotation(Parameters::TASK)) { $className = $method->getDeclaringClass()->getName(); $methodName = $method->getName(); $taskName = $className . ' - ' . $methodName; } $parameters = array(static::TASK => Parser::parseName($method->getAnnotation(Parameters::TASK)) ?: $taskName, static::PERIOD => $method->hasAnnotation(Parameters::PERIOD) ? Parser::parsePeriod($method->getAnnotation(Parameters::PERIOD)) : NULL, static::DAYS => $method->hasAnnotation(Parameters::DAYS) ? Parser::parseDays($method->getAnnotation(Parameters::DAYS)) : NULL, static::TIME => $method->hasAnnotation(Parameters::TIME) ? Parser::parseTimes($method->getAnnotation(Parameters::TIME)) : NULL); return $parameters; }
/** * Get name for a job * * @param \Nette\Reflection\Method $method * @return string */ protected function getJobName(\Nette\Reflection\Method $method) { if ($method->hasAnnotation("test")) { return (string) $method->getAnnotation("test"); } else { return $this->getSuitName() . "::" . $method->getName(); } }
/** * @param \Nette\Reflection\Method $element * @return bool */ protected function isMethodAllowed(\Nette\Reflection\Method $element) { $classRef = new \Nette\Application\UI\PresenterComponentReflection($element->class); $ref = ClassType::from($element->class); if (!$this->isPresenterAllowedCached($classRef)) { return FALSE; } $ref = $ref->getMethod($element->name); // is not secured if (!$ref->hasAnnotation('secured')) { return TRUE; } // resource & privilege $secured = $ref->getAnnotation('secured'); $resource = isset($secured['resource']) ? $secured['resource'] : NULL; if (!$resource) { $s = $classRef->getAnnotation('secured'); $resource = isset($s['resource']) ? $s['resource'] : $classRef->getNamespaceName(); } $privilege = isset($secured['privilege']) ? $secured['privilege'] : $element->name; if (!parent::isAllowed($resource, $privilege)) { return FALSE; } // roles if (isset($secured['roles'])) { $userRoles = $this->getRoles(); $roles = explode(',', $secured['roles']); array_walk($roles, function (&$val) { $val = trim($val); }); if (count(array_intersect($userRoles, $roles)) == 0) { return FALSE; } } // users if (isset($secured['users'])) { $users = explode(',', $secured['users']); array_walk($users, function (&$val) { $val = trim($val); }); $users = (array) $element->getAnnotation('User'); if (in_array($this->getId(), $users)) { return FALSE; } } return TRUE; }