public function environmentAuthenticationMiddleware()
{
if (empty($this->settings[self::SETTING_SCALR_ENVIRONMENT]) || $this->settings[self::SETTING_SCALR_ENVIRONMENT] <= 0) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Environment must be specified");
}
$envId = $this->settings[self::SETTING_SCALR_ENVIRONMENT];
$environment = Entity\Account\Environment::findPk($envId);
if (!$environment instanceof Entity\Account\Environment) {
throw new ApiErrorException(403, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Invalid environment");
}
$user = $this->getUser();
if (!$user instanceof Entity\Account\User) {
throw new \Exception("User had to be set by the previous middleware, but he weren't");
}
if (!$user->hasAccessToEnvironment($environment->id)) {
throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "You don't have access to the environment.");
}
$this->setEnvironment($environment);
//Sets Environment to Audit Logger
$this->getContainer()->auditlogger->setEnvironmentId($environment->id);
}