Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function isAllowed($role, $resource, $privilege)
 {
     if ($role instanceof IRole) {
         $role = $role->getRoleId();
     }
     if (!$resource instanceof PresenterResource) {
         throw new \Ark8\Security\Exceptions\SkipException(sprintf('Resource must be instance of %s, %s given.', PresenterResource::class, gettype($resource)));
     }
     $request = $resource->getRequest();
     $presenterName = $request->getPresenterName();
     list($signal, $signalReceiver) = $this->getSignal($request);
     if (!$signal) {
         throw new \Ark8\Security\Exceptions\SkipException(sprintf('No signal sent.'));
     }
     $refClass = new PresenterComponentReflection($class = $this->presenterFactory->getPresenterClass($presenterName));
     while ($name = array_shift($signalReceiver)) {
         $name = 'createComponent' . ucfirst($name);
         if (!$refClass->hasMethod($name)) {
             throw new \Nette\InvalidStateException(sprintf('Method %s::%s is not implemented.', $refClass->getName(), $name));
         }
         $refMethod = $refClass->getMethod($name);
         if (!$refMethod->hasAnnotation('return')) {
             throw new \Nette\InvalidStateException(sprintf('Method %s::%s must have fully qualified return annotation.', $refClass->getName(), $name));
         }
         $refClass = new ClassType($refMethod->getAnnotation('return'));
     }
     if (!$refClass->hasMethod($name = Presenter::formatSignalMethod($signal))) {
         throw new \Ark8\Security\Exceptions\SkipException(sprintf('Method %s::%s is not implemented.', $refClass->getName(), $name));
     }
     $refMethod = $refClass->getMethod($name);
     if (!$refMethod->hasAnnotation($privilege)) {
         throw new \Ark8\Security\Exceptions\SkipException(sprintf('Method %s::%s does not have annotation %s.', $refClass->getName(), $name, $privilege));
     }
     return in_array($role, preg_split('#\\s+#', trim((string) $refMethod->getAnnotation($privilege))));
 }
Exemple #2
0
 /**
  * @return array
  */
 public function getTypesWithin()
 {
     if ($this->typesWithing !== NULL) {
         return $this->typesWithing;
     }
     return $this->typesWithing = class_parents($class = $this->originalType->getName()) + class_implements($class) + [$class => $class];
 }
 /**
  * Generates and checks presenter class name.
  *
  * @param  string presenter name
  * @return string  class name
  * @throws Application\InvalidPresenterException
  */
 public function getPresenterClass(&$name)
 {
     if (isset($this->cache[$name])) {
         return $this->cache[$name];
     }
     if (!is_string($name) || !Nette\Utils\Strings::match($name, '#^[a-zA-Z\\x7f-\\xff][a-zA-Z0-9\\x7f-\\xff:]*\\z#')) {
         throw new Application\InvalidPresenterException("Presenter name must be alphanumeric string, '{$name}' is invalid.");
     }
     $classes = $this->formatPresenterClasses($name);
     if (!$classes) {
         throw new Application\InvalidPresenterException("Cannot load presenter '{$name}', no applicable mapping found.");
     }
     $class = $this->findValidClass($classes);
     if (!$class) {
         throw new Application\InvalidPresenterException("Cannot load presenter '{$name}', none of following classes were found: " . implode(', ', $classes));
     }
     $reflection = new Nette\Reflection\ClassType($class);
     $class = $reflection->getName();
     if (!$reflection->implementsInterface('Nette\\Application\\IPresenter')) {
         throw new Application\InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' is not Nette\\Application\\IPresenter implementor.");
     }
     if ($reflection->isAbstract()) {
         throw new Application\InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' is abstract.");
     }
     return $this->cache[$name] = $class;
 }
 /**
  * Generates markdown output for a specified class
  * @param string $class e.g. `PDO` or a user class like `diversen\markdownDocs`
  * @return void the method adds to $output
  */
 public function classToMD($class, $options = [])
 {
     $r = new ClassType($class);
     $this->output .= $this->sectionHeader('Class: ' . $r->getName(), 3);
     // Class description
     $this->output .= $r->getDescription() . $this->getNL();
     // Get methods and props
     $methods = $r->getMethods();
     $props = $r->getDefaultProperties();
     // Parse properties
     $this->output .= $this->sectionHeader("Properties");
     $this->generatePropsMD($r, $props, $options);
     // Parse methods
     $this->output .= $this->sectionHeader("Methods");
     $this->generateMethodMD($methods, $options);
 }
 /**
  * Generates and checks presenter class name.
  * @param  string  presenter name
  * @return string  class name
  * @throws InvalidPresenterException
  */
 public function getPresenterClass(&$name)
 {
     if (isset($this->cache[$name])) {
         list($class, $name) = $this->cache[$name];
         return $class;
     }
     if (!is_string($name) || !Nette\Utils\Strings::match($name, '#^[a-zA-Z\\x7f-\\xff][a-zA-Z0-9\\x7f-\\xff:]*\\z#')) {
         throw new InvalidPresenterException("Presenter name must be alphanumeric string, '{$name}' is invalid.");
     }
     $class = $this->formatPresenterClass($name);
     if (!class_exists($class)) {
         // internal autoloading
         $file = $this->formatPresenterFile($name);
         if (is_file($file) && is_readable($file)) {
             call_user_func(function () use($file) {
                 require $file;
             });
         }
         if (!class_exists($class)) {
             throw new InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' was not found in '{$file}'.");
         }
     }
     $reflection = new Nette\Reflection\ClassType($class);
     $class = $reflection->getName();
     if (!$reflection->implementsInterface('Nette\\Application\\IPresenter')) {
         throw new InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' is not Nette\\Application\\IPresenter implementor.");
     }
     if ($reflection->isAbstract()) {
         throw new InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' is abstract.");
     }
     // canonicalize presenter name
     $realName = $this->unformatPresenterClass($class);
     if ($name !== $realName) {
         if ($this->caseSensitive) {
             throw new InvalidPresenterException("Cannot load presenter '{$name}', case mismatch. Real name is '{$realName}'.");
         } else {
             $this->cache[$name] = array($class, $realName);
             $name = $realName;
         }
     } else {
         $this->cache[$name] = array($class, $realName);
     }
     return $class;
 }
 /**
  * Returns all parameters of the constructor which type is dao object.
  *
  * @param string $class
  * @return array array of parameter's names
  */
 private function findConstructorDaoParameters($class)
 {
     $reflection = new Reflection($class);
     $daoParameters = array();
     foreach ($reflection->constructor->parameters as $parameter) {
         $parameterClass = $parameter->getClassName();
         if ($parameterClass) {
             $parameterReflection = new Reflection($parameterClass);
             if ($parameterReflection->getName() === self::DAO_CLASS || $parameterReflection->isSubclassOf(self::DAO_CLASS)) {
                 $daoParameters[] = $parameter->getName();
             }
         }
     }
     return $daoParameters;
 }
 protected function bindEventProperties(Nette\DI\ServiceDefinition $def, Nette\Reflection\ClassType $class)
 {
     foreach ($class->getProperties(Nette\Reflection\Property::IS_PUBLIC) as $property) {
         if (!preg_match('#^on[A-Z]#', $name = $property->getName())) {
             continue;
         }
         if ($property->hasAnnotation('persistent') || $property->hasAnnotation('inject')) {
             // definitely not an event
             continue;
         }
         $def->addSetup('$' . $name, array(new Nette\DI\Statement($this->prefix('@manager') . '::createEvent', array(array($class->getName(), $name), new Code\PhpLiteral('$service->' . $name)))));
     }
 }
 protected function bindEventProperties(Nette\DI\ServiceDefinition $def, Nette\Reflection\ClassType $class)
 {
     foreach ($class->getProperties(Nette\Reflection\Property::IS_PUBLIC) as $property) {
         if (!preg_match('#^on[A-Z]#', $name = $property->getName())) {
             continue;
         }
         if ($property->hasAnnotation('persistent') || $property->hasAnnotation('inject')) {
             // definitely not an event
             continue;
         }
         $def->addSetup('$' . $name, [new Nette\DI\Statement($this->prefix('@manager') . '::createEvent', [[$class->getName(), $name], new Code\PhpLiteral('$service->' . $name), NULL, $property->hasAnnotation('globalDispatchFirst') ? (bool) $property->getAnnotation('globalDispatchFirst') : $this->loadedConfig['globalDispatchFirst']])]);
     }
 }
 /**
  * @param array $dirs
  * @param array $interfaces
  * @return array
  */
 private function findByInterfaces(array $dirs, array $interfaces)
 {
     $loader = $this->createLoader();
     $loader->addDirectory($dirs);
     $loader->rebuild();
     $loader->register();
     $classes = [];
     foreach (array_keys($loader->getIndexedClasses()) as $class) {
         // Skip not existing class
         if (!class_exists($class, TRUE)) {
             continue;
         }
         // Detect by reflection
         $ct = new ClassType($class);
         // Skip abstract
         if ($ct->isAbstract()) {
             continue;
         }
         // Does class implement one of given interface
         foreach ($interfaces as $interface) {
             if ($ct->implementsInterface($interface)) {
                 $classes[] = $ct->getName();
             }
         }
     }
     return $classes;
 }