public function validateEntity($entity)
{
if (!$entity instanceof Script) {
throw new InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\Script class"));
}
if ($entity->id !== null) {
if (!Script::findPk($entity->id)) {
throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Script with ID: %d", $entity->id));
}
}
if (empty($entity->name)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Property name cannot be empty");
} else {
switch ($entity->getScope()) {
case ScopeInterface::SCOPE_ACCOUNT:
$existed = Script::findOne([['name' => $entity->name], ['accountId' => $this->controller->getUser()->accountId], ['envId' => null]]);
break;
case ScopeInterface::SCOPE_ENVIRONMENT:
$existed = Script::findOne([['name' => $entity->name], ['accountId' => $this->controller->getUser()->accountId], ['envId' => $this->controller->getEnvironment()->id]]);
break;
case ScopeInterface::SCOPE_SCALR:
$existed = Script::findOne([['name' => $entity->name], ['accountId' => null], ['envId' => null]]);
break;
}
/* @var $existed Script */
if (!empty($existed) && $entity->id !== $existed->id) {
throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, "Script '{$entity->name}' already exists on scope {$existed->getScope()}");
}
}
if (empty($entity->os)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property osType");
} else {
if (!in_array($entity->os, [Script::OS_LINUX, Script::OS_WINDOWS])) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid osType");
}
}
//NOTE: scalr-scope currently restricted in APIv2
switch ($entity->getScope()) {
case ScopeInterface::SCOPE_ENVIRONMENT:
if (!$this->controller->getEnvironment()) {
throw new ApiErrorException(403, ErrorMessage::ERR_SCOPE_VIOLATION, "Invalid scope");
}
break;
case ScopeInterface::SCOPE_ACCOUNT:
if ($this->controller->getEnvironment()) {
throw new ApiErrorException(403, ErrorMessage::ERR_SCOPE_VIOLATION, "Invalid scope");
}
break;
case ScopeInterface::SCOPE_SCALR:
case ScopeInterface::SCOPE_FARM:
case ScopeInterface::SCOPE_FARMROLE:
case ScopeInterface::SCOPE_ROLE:
case ScopeInterface::SCOPE_SERVER:
throw new ApiErrorException(403, ErrorMessage::ERR_SCOPE_VIOLATION, "Invalid scope");
default:
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid scope");
}
if (!$this->controller->hasPermissions($entity, true)) {
//Checks entity level write access permissions
throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions");
}
}