public static function initializeInstance($type, $userId, $envId) { $instance = new Scalr_UI_Request($type); if ($userId) { try { $user = Scalr_Account_User::init(); $user->loadById($userId); } catch (Exception $e) { throw new Exception('User account is no longer available.'); } if ($user->status != Scalr_Account_User::STATUS_ACTIVE) { throw new Exception('User account has been deactivated. Please contact your account owner.'); } if ($user->getType() != Scalr_Account_User::TYPE_SCALR_ADMIN) { $environment = $user->getDefaultEnvironment($envId); $user->getPermissions()->setEnvironmentId($environment->id); } if ($user->getAccountId()) { if ($user->getAccount()->status == Scalr_Account::STATUS_INACIVE) { if ($user->getType() == Scalr_Account_User::TYPE_TEAM_USER) { throw new Exception('Scalr account has been deactivated. Please contact scalr team.'); } } else { if ($user->getAccount()->status == Scalr_Account::STATUS_SUSPENDED) { if ($user->getType() == Scalr_Account_User::TYPE_TEAM_USER) { throw new Exception('Account was suspended. Please contact your account owner to solve this situation.'); } } } } $instance->user = $user; $instance->environment = $environment; } self::$_instance = $instance; return $instance; }
/** * @param $type * @param $headers * @param $server * @param $params * @param $files * @param $userId * @param $envId int optional Could be null, when we check headers (for UI) * @return Scalr_UI_Request * @throws Scalr_Exception_Core * @throws Exception */ public static function initializeInstance($type, $headers, $server, $params, $files, $userId, $envId = null) { if (self::$_instance) { self::$_instance = null; } $class = get_called_class(); /* @var $instance Scalr_UI_Request */ $instance = new $class($type, $headers, $server, $params, $files); $container = Scalr::getContainer(); if ($userId) { try { $user = Scalr_Account_User::init(); $user->loadById($userId); } catch (Exception $e) { throw new Exception('User account is no longer available.'); } if ($user->status != Scalr_Account_User::STATUS_ACTIVE) { throw new Exception('User account has been deactivated. Please contact your account owner.'); } $scope = $instance->getHeaderVar('Scope'); // ajax file upload, download files if (empty($scope)) { $scope = $instance->getParam('X-Scalr-Scope'); } if (empty($envId)) { $envId = $instance->getParam('X-Scalr-Envid'); } if ($user->isAdmin()) { if ($scope != 'scalr') { $scope = 'scalr'; } } else { if (!in_array($scope, ['account', 'environment'])) { $scope = 'environment'; } } if (!$user->isAdmin()) { if ($envId || $scope == 'environment') { if (!$envId) { $envId = $instance->getHeaderVar('Envid'); } $environment = $user->getDefaultEnvironment($envId); $user->getPermissions()->setEnvironmentId($environment->id); $scope = 'environment'; } } if ($user->getAccountId()) { if ($user->getAccount()->status == Scalr_Account::STATUS_INACIVE) { if ($user->getType() == Scalr_Account_User::TYPE_TEAM_USER) { throw new Exception('Scalr account has been deactivated. Please contact scalr team.'); } } else { if ($user->getAccount()->status == Scalr_Account::STATUS_SUSPENDED) { if ($user->getType() == Scalr_Account_User::TYPE_TEAM_USER) { throw new Exception('Account was suspended. Please contact your account owner to solve this situation.'); } } } } $ipWhitelist = $user->getVar(Scalr_Account_User::VAR_SECURITY_IP_WHITELIST); if ($ipWhitelist) { $ipWhitelist = unserialize($ipWhitelist); if (!Scalr_Util_Network::isIpInSubnets($instance->getRemoteAddr(), $ipWhitelist)) { throw new Exception('The IP address isn\'t authorized.'); } } // check header's variables $headerUserId = !is_null($instance->getHeaderVar('Userid')) ? intval($instance->getHeaderVar('Userid')) : null; if (!empty($headerUserId) && $headerUserId != $user->getId()) { throw new Scalr_Exception_Core('Session expired. Please refresh page.', 1); } $instance->user = $user; $instance->environment = isset($environment) ? $environment : null; $instance->scope = $scope; } $container->request = $instance; $container->environment = isset($instance->environment) ? $instance->environment : null; self::$_instance = $instance; $container->set('auditlogger.request', function () { return self::$_instance; }); return $instance; }