public function registerImageAction($roleId)
{
$this->checkScopedPermissions('ROLES', 'MANAGE');
//Gets role checking Environment scope
$role = $this->getRole($roleId, true, true);
$object = $this->request->getJsonBody();
$objectImageId = static::getBareId($object, 'image');
$objectRoleId = static::getBareId($object, 'role');
if (empty($objectImageId)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Invalid body");
}
if (!preg_match('/' . ApiApplication::REGEXP_UUID . '/', $objectImageId)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid image identifier");
}
if (!empty($objectRoleId) && $roleId != $objectRoleId) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid identifier of the role");
}
if (is_object($object->image)) {
$imageAdapter = $this->adapter('image');
//Pre validates the request object
$imageAdapter->validateObject($object->image);
}
$criteria = $this->getScopeCriteria();
$criteria[] = ['hash' => $objectImageId];
/* @var $image Entity\Image */
$image = Entity\Image::findOne($criteria);
if (empty($image)) {
throw new ApiErrorException(404, ErrorMessage::ERR_INVALID_VALUE, "The Image either does not exist or isn't in scope for the current Environment.");
}
$roleImage = Entity\RoleImage::findOne([['roleId' => $roleId], ['platform' => $image->platform], ['cloudLocation' => $image->cloudLocation]]);
if (!empty($roleImage)) {
throw new ApiErrorException(400, ErrorMessage::ERR_BAD_REQUEST, sprintf("Image with cloud location %s has already been registered", $image->cloudLocation));
}
$this->setImage($role, $image->platform, $image->cloudLocation, $image->id, $this->getUser()->id, $this->getUser()->email);
$this->response->setStatus(201);
return $this->result(['image' => ['id' => $image->hash], 'role' => ['id' => $role->id]]);
}