Пример #1
0
 public function validate($entity, array $options = array())
 {
     $violations = parent::validate($entity, $options);
     if ($entity->getPassword() === '' || $entity->getPassword() === null) {
         $violations->add('password', 'devture_user.validation.password_empty');
     }
     $username = $entity->getUsername();
     if (strlen($username) < 3 || !preg_match("/^[a-z][a-z0-9\\._]+\$/", $username)) {
         $violations->add('username', 'devture_user.validation.invalid_username');
     }
     $this->validateEmail($entity, $violations);
     foreach ($entity->getRoles() as $role) {
         if (!in_array($role, $this->knownRoles)) {
             $violations->add('roles', 'devture_user.validation.invalid_roles');
             break;
         }
     }
     try {
         $user = $this->repository->findByUsername($username);
         if ($user->getId() !== $entity->getId()) {
             $violations->add('username', 'devture_user.validation.username_in_use');
         }
     } catch (NotFound $e) {
     }
     return $violations;
 }
Пример #2
0
 /**
  * @param Contact $entity
  * @param array $options
  * @return \Devture\Component\Form\Validator\ViolationsList
  */
 public function validate($entity, array $options = array())
 {
     $violations = parent::validate($entity, $options);
     $name = $entity->getName();
     if (strlen($name) < 3 || !preg_match("/^[a-z][a-z0-9_\\-\\.]+\$/", $name)) {
         $violations->add('name', 'Invalid name.');
     } else {
         try {
             $ent = $this->repository->findOneBy(array('name' => $name));
             if (spl_object_hash($ent) !== spl_object_hash($entity)) {
                 $violations->add('name', 'The name is already in use.');
             }
         } catch (NotFound $e) {
         }
     }
     if (!$entity->getTimePeriod() instanceof TimePeriod) {
         $violations->add('timePeriod', 'The time period is not valid.');
     }
     if (!$entity->getServiceNotificationCommand() instanceof Command) {
         $violations->add('serviceNotificationCommand', 'The service notification command is not valid.');
     }
     if ($this->isEmpty($entity->getEmail()) && count($entity->getAddresses()) === 0) {
         $violations->add('__other__', 'An email address or other addresses need to be entered.');
     }
     foreach ($entity->getAddresses() as $slot => $address) {
         $slot = (int) $slot;
         if ($slot < 1 || $slot > Contact::ADDRESS_SLOTS_COUNT) {
             $violations->add('addresses', 'Slot %slot% is not allowed.', array('%slot%' => $slot));
         }
     }
     return $violations;
 }
Пример #3
0
 /**
  * @param TimePeriod $entity
  * @param array $options
  * @return \Devture\Component\Form\Validator\ViolationsList
  */
 public function validate($entity, array $options = array())
 {
     $violations = parent::validate($entity, $options);
     $name = $entity->getName();
     if (strlen($name) < 3 || !preg_match("/^[a-z][a-z0-9_\\-\\.]+\$/", $name)) {
         $violations->add('name', 'Invalid name.');
     } else {
         try {
             $ent = $this->repository->findOneBy(array('name' => $name));
             if (spl_object_hash($ent) !== spl_object_hash($entity)) {
                 $violations->add('name', 'The name is already in use.');
             }
         } catch (NotFound $e) {
         }
     }
     if ($this->isEmpty($entity->getTitle())) {
         $violations->add('title', 'The title cannot be empty.');
     }
     foreach ($entity->getRules() as $rule) {
         $dateRange = $rule->getDateRange();
         if ($this->isEmpty($dateRange)) {
             $violations->add('rules', 'Rule date range cannot be empty.');
             continue;
         }
         $timeRange = $rule->getTimeRange();
         if ($this->isEmpty($timeRange)) {
             $violations->add('rules', 'Rule time range cannot be empty.');
             continue;
         }
     }
     return $violations;
 }
Пример #4
0
 /**
  * @param Host $entity
  * @param array $options
  * @return \Devture\Component\Form\Validator\ViolationsList
  */
 public function validate($entity, array $options = array())
 {
     $violations = parent::validate($entity, $options);
     $name = $entity->getName();
     if (strlen($name) < 3 || !NameValidator::isValid($name)) {
         $violations->add('name', 'Invalid name.');
     } else {
         try {
             $ent = $this->repository->findOneBy(array('name' => $name));
             if (spl_object_hash($ent) !== spl_object_hash($entity)) {
                 $violations->add('name', 'The name is already in use.');
             }
         } catch (NotFound $e) {
         }
     }
     if ($this->isEmpty($entity->getAddress())) {
         $violations->add('address', 'The address cannot be empty.');
     }
     foreach ($entity->getGroups() as $groupName) {
         if (!NameValidator::isValid($groupName)) {
             $violations->add('groups', 'The group name %name% is not valid.', array('%name%' => $groupName));
         }
     }
     return $violations;
 }
Пример #5
0
 /**
  * @param Resource $entity
  * @param array $options
  * @return \Devture\Component\Form\Validator\ViolationsList
  */
 public function validate($entity, array $options = array())
 {
     $violations = parent::validate($entity, $options);
     foreach ($entity->getVariables() as $name => $value) {
         if (preg_match('/^\\$USER([0-9]+)\\$$/', $name, $matches)) {
             $slotNumber = (int) $matches[1];
             if ($slotNumber < 1 || $slotNumber > Resource::USER_VARIABLES_COUNT) {
                 $violations->add('variables', 'Variable slot number for `%name%` is invalid.', array('%name%' => $name));
             }
         } else {
             $violations->add('variables', 'Variable name for `%name%` is invalid.', array('%name%' => $name));
         }
     }
     return $violations;
 }
Пример #6
0
 /**
  * @param Command $entity
  * @param array $options
  * @return \Devture\Component\Form\Validator\ViolationsList
  */
 public function validate($entity, array $options = array())
 {
     $violations = parent::validate($entity, $options);
     $name = $entity->getName();
     if (strlen($name) < 3 || !preg_match("/^[a-z][a-z0-9_\\-\\.]+\$/", $name)) {
         $violations->add('name', 'Invalid name.');
     } else {
         try {
             $ent = $this->repository->findOneBy(array('name' => $name));
             if (spl_object_hash($ent) !== spl_object_hash($entity)) {
                 $violations->add('name', 'The name is already in use.');
             }
         } catch (NotFound $e) {
         }
     }
     if (!in_array($entity->getType(), Command::getTypes())) {
         $violations->add('type', 'Invalid type.');
     }
     $line = $entity->getLine();
     if ($this->isEmpty($line)) {
         $violations->add('line', 'The command line cannot be empty.');
     } else {
         if (strpos($line, "\n") !== false) {
             $violations->add('line', 'The command cannot contain a new line.');
         }
         if (strpos($line, ';') !== false) {
             $violations->add('line', 'The command cannot contain `;` as that is considered a comment.');
         }
     }
     $expectedArgumentsCount = $entity->getLineArgumentsCount();
     $arguments = $entity->getArguments();
     if (count($arguments) !== $expectedArgumentsCount) {
         $violations->add('arguments', 'Command expects %expected% arguments. Got only %provided%.', array('{{expected}}' => $expectedArgumentsCount, '{{provided}}' => count($arguments)));
     }
     foreach ($arguments as $argument) {
         $argumentId = $argument->getId();
         if ($argumentId === null || !preg_match('/^\\$ARG([0-9]+)\\$$/', $argumentId)) {
             $violations->add('arguments', 'Invalid command argument id: %id%', array('%id%' => $argumentId));
         }
         if ($this->isEmpty($argument->getDescription())) {
             $violations->add('arguments', 'Command argument descriptions cannot be empty.');
         }
     }
     return $violations;
 }
Пример #7
0
 /**
  * @param Service $entity
  * @param array $options
  * @return \Devture\Component\Form\Validator\ViolationsList
  */
 public function validate($entity, array $options = array())
 {
     $violations = parent::validate($entity, $options);
     $name = $entity->getName();
     if (strlen($name) < 3 || !preg_match("/^[a-zA-Z0-9\\s\\-_\\.]+\$/", $name)) {
         $violations->add('name', 'Invalid name.');
     } else {
         try {
             $host = $entity->getHost();
             if ($host instanceof Host) {
                 $ent = $this->repository->findOneBy(array('name' => $name, 'hostId' => $host->getId()));
                 if (spl_object_hash($ent) !== spl_object_hash($entity)) {
                     $violations->add('name', 'The name is already in use by another service on that host.');
                 }
             }
         } catch (NotFound $e) {
         }
     }
     $expectedNotBlank = array('maxCheckAttempts', 'checkInterval', 'retryInterval', 'notificationInterval');
     foreach ($expectedNotBlank as $fieldName) {
         $getter = 'get' . ucfirst($fieldName);
         if ($this->isEmpty($entity->{$getter}())) {
             $violations->add($fieldName, 'This field cannot be blank.');
         }
     }
     $expectedNumeric = array('maxCheckAttempts', 'checkInterval', 'retryInterval', 'notificationInterval');
     foreach ($expectedNotBlank as $fieldName) {
         $getter = 'get' . ucfirst($fieldName);
         if (!is_numeric($entity->{$getter}())) {
             $violations->add($fieldName, 'This field should be numeric.');
         }
     }
     if (!$entity->getHost() instanceof Host) {
         $violations->add('host', 'Invalid host.');
     }
     $command = $entity->getCommand();
     if (!$command instanceof Command || $command->getType() !== Command::TYPE_SERVICE_CHECK) {
         $violations->add('command', 'Invalid command.');
     } else {
         $requiredArgumentIds = array_map(function (CommandArgument $arg) {
             return $arg->getId();
         }, $command->getArguments());
         foreach ($entity->getArguments() as $argument) {
             $argumentId = $argument->getId();
             if (!in_array($argumentId, $requiredArgumentIds)) {
                 $violations->add('arguments', 'Argument %id% is not valid for the selected command.', array('%id%' => $argumentId));
             } else {
                 $argumentValue = $argument->getValue();
                 if (strpos($argumentValue, "\n") !== false) {
                     $violations->add('arguments', 'Arguments cannot contain new lines.');
                 }
             }
         }
         foreach ($requiredArgumentIds as $argumentId) {
             if ($entity->getArgumentById($argumentId) === null) {
                 $violations->add('arguments', 'Argument %id% is required by the command, but not specified.', array('%id%' => $argumentId));
             }
         }
     }
     return $violations;
 }