/** * {@inheritdoc} * @see \Scalr\Api\DataType\ApiEntityAdapter::validateEntity() */ public function validateEntity($entity) { if (!$entity instanceof Entity\EventDefinition) { throw new \InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\EventDefinition class")); } if ($entity->id !== null) { //Checks if the event does exist if (!Entity\EventDefinition::findPk($entity->id)) { throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Event with ID: %d", $entity->name)); } } if (!preg_match('/^' . Entity\EventDefinition::NAME_REGEXP . '$/', $entity->name)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid id of the Event"); } $entity->description = $entity->description ?: ''; $this->validateString($entity->description, 'Invalid description'); if (!$this->controller->hasPermissions($entity, true)) { //Checks entity level write access permissions throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions"); } //We only allow to either create or modify Account or Environment Scope Events if ($entity->getScope() !== $this->controller->getScope()) { throw new ApiErrorException(403, ErrorMessage::ERR_SCOPE_VIOLATION, sprintf("Invalid scope")); } }
/** * @param integer $id * @param string $name * @param string $description * @param bool $replaceEvent * @throws Exception * @throws Scalr_Exception_Core */ public function xSaveAction($id = 0, $name, $description, $replaceEvent = false) { $this->request->restrictAccess(Acl::RESOURCE_GENERAL_CUSTOM_EVENTS, Acl::PERM_GENERAL_CUSTOM_EVENTS_MANAGE); $validator = new \Scalr\UI\Request\Validator(); $validator->addErrorIf(!preg_match("/^[A-Za-z0-9]+\$/si", $name), 'name', "Name should contain only alphanumeric characters"); $validator->addErrorIf(strlen($name) > 25, 'name', "Name should be less than 25 characters"); $validator->addErrorIf(in_array($name, array_keys(EVENT_TYPE::getScriptingEvents())), 'name', sprintf("'%' is reserved name for event. Please select another one.", $name)); $scope = $this->request->getScope(); if (!$id) { $criteria = [['name' => $name]]; if ($this->user->isScalrAdmin()) { $criteria[] = ['accountId' => NULL]; } else { $criteria[] = ['$or' => [['accountId' => $this->user->getAccountId()], ['accountId' => NULL]]]; if ($scope == 'account') { $criteria[] = ['envId' => NULL]; } else { $criteria[] = ['$or' => [['envId' => NULL], ['envId' => $this->getEnvironmentId(true)]]]; } } $validator->addErrorIf(EventDefinition::find($criteria)->count(), 'name', 'This name is already in use. Note that Event names are case-insensitive.'); // check replacements $replacements = NULL; if ($this->user->isScalrAdmin()) { $replacements = EventDefinition::find([['name' => $name], ['accountId' => ['$ne' => NULL]]]); } else { if ($scope == 'account') { $replacements = EventDefinition::find([['name' => $name], ['accountId' => $this->user->getAccountId()], ['envId' => ['$ne' => NULL]]]); } } } if (!$validator->isValid($this->response)) { return; } if ($replacements && $replacements->count() && !$replaceEvent) { $this->response->data(['replaceEvent' => true]); $this->response->failure(); return; } if ($id) { $event = EventDefinition::findPk($id); /* @var $event EventDefinition */ if (!$event) { throw new Exception('Event not found'); } if ($this->user->isScalrAdmin() && $event->accountId == NULL && $event->envId == NULL || $this->user->isUser() && $event->accountId == $this->user->getAccountId() && ($event->envId == NULL || $event->envId == $this->getEnvironmentId())) { $event->description = $description; } else { throw new Scalr_Exception_InsufficientPermissions(); } $event->save(); } else { $event = new EventDefinition(); if ($this->user->isScalrAdmin()) { $event->accountId = NULL; $event->envId = NULL; } else { $event->accountId = $this->user->getAccountId(); $event->envId = $scope == 'account' ? NULL : $this->getEnvironmentId(); } $event->name = $name; $event->description = $description; $event->save(); if ($replacements) { foreach ($replacements as $e) { $e->delete(); } } } $used = $event->getUsed($this->user->getAccountId(), $this->getEnvironmentId(true)); $this->response->data(['event' => ['id' => $event->id, 'name' => $event->name, 'description' => $event->description, 'used' => $used, 'scope' => $scope, 'status' => $used ? 'In use' : 'Not used']]); $this->response->success('Custom event definition successfully saved'); }