/** * @param int $userId * @param int $envId * @param JsonData $uiStorage * @param JsonData $updateDashboard */ public function xPerpetuumMobileAction($userId, $envId, JsonData $uiStorage, JsonData $updateDashboard) { $result = array(); if ($this->user) { if ($updateDashboard) { $result['updateDashboard'] = Scalr_UI_Controller::loadController('dashboard')->checkLifeCycle($updateDashboard); } if (!Scalr_Session::getInstance()->isVirtual() && $uiStorage->count()) { $this->user->setSetting(Scalr_Account_User::SETTING_UI_STORAGE_TIME, $uiStorage['time']); $this->user->setVar(Scalr_Account_User::VAR_UI_STORAGE, $uiStorage['dump']); } } $equal = $this->user && $this->user->getId() == $userId && ($this->getEnvironment() ? $this->getEnvironmentId() : 0) == $envId; $result['equal'] = $equal; $result['isAuthenticated'] = $this->user ? true : false; $this->response->data($result); }
/** * Get list of roles for listView * * @param int $roleId optional * @param string $platform optional * @param string $cloudLocation optional * @param string $imageId optional * @param string $scope optional * @param int $chefServerId optional * @param int $catId optional * @param string $osFamily optional * @param bool $isQuickStart optional * @param bool $isDeprecated optional * @param string $status optional * @param JsonData $addImage optional */ public function xListRolesAction($roleId = null, $platform = null, $cloudLocation = null, $imageId = null, $scope = null, $chefServerId = null, $catId = null, $osFamily = null, $isQuickStart = false, $isDeprecated = false, $status = null, JsonData $addImage = null) { $this->request->restrictAccess('ROLES'); $args = []; $inUseJoin = ''; $envId = $this->getEnvironmentId(true); $accountId = $this->user->getAccountId() ?: NULL; if ($accountId) { $inUseJoin = " JOIN farms ON farm_roles.farmid = farms.id AND farms.clientid = ? "; $args[] = $accountId; if ($envId) { $inUseJoin .= " AND farms.env_id = ?"; $args[] = $envId; } } $role = new Role(); $sql = "\n SELECT DISTINCT " . $role->fields('r') . ", os.name as osName, os.family as osFamily,\n (SELECT EXISTS(SELECT 1 FROM farm_roles " . $inUseJoin . "\n WHERE farm_roles.role_id = r.id)) AS inUse,\n (SELECT GROUP_CONCAT(name SEPARATOR ',') FROM `client_environments` ce LEFT JOIN role_environments re ON ce.id = re.env_id\n WHERE re.role_id = r.id) AS environments\n FROM " . $role->table('r') . "\n LEFT JOIN role_images ON r.id = role_images.role_id\n LEFT JOIN os ON r.os_id = os.id\n LEFT JOIN role_environments re ON re.role_id = r.id\n WHERE\n "; if ($this->request->getScope() == ScopeInterface::SCOPE_SCALR) { $sql .= " r.client_id IS NULL"; } else { if ($this->request->getScope() == ScopeInterface::SCOPE_ACCOUNT) { $sql .= " (r.client_id IS NULL AND role_images.role_id IS NOT NULL OR r.client_id = ? AND r.env_id IS NULL) AND r.generation = ?"; $args = array_merge($args, [$accountId, 2]); } else { $sql .= " (r.client_id IS NULL AND role_images.role_id IS NOT NULL OR r.client_id = ? AND r.env_id IS NULL AND (re.env_id IS NULL OR re.env_id = ?) OR r.env_id = ?) AND r.generation = ?"; $args = array_merge($args, [$accountId, $envId, $envId, 2]); } } if ($roleId) { $sql .= " AND r.id = ?"; $args[] = $roleId; } else { $sql .= " AND :FILTER: "; if ($scope == ScopeInterface::SCOPE_SCALR) { $sql .= " AND r.client_id IS NULL"; } else { if ($scope == ScopeInterface::SCOPE_ACCOUNT) { $sql .= " AND r.client_id = ? AND r.env_id IS NULL"; $args[] = $accountId; } else { if ($scope == ScopeInterface::SCOPE_ENVIRONMENT) { $sql .= " AND r.env_id = ?"; $args[] = $envId; } } } if ($platform) { $sql .= " AND role_images.platform = ?"; $args[] = $platform; } if ($cloudLocation) { $sql .= " AND role_images.cloud_location = ?"; $args[] = $cloudLocation; } if ($imageId) { $sql .= " AND role_images.image_id = ?"; $args[] = $imageId; } if ($catId) { $sql .= " AND r.cat_id = ?"; $args[] = $catId; } if ($osFamily) { $sql .= " AND os.family = ?"; $args[] = $osFamily; } if ($scope) { $sql .= " AND r.origin = ?"; $args[] = $scope == 'scalr' ? 'Shared' : 'Custom'; } if ($status) { $sql .= " AND ("; $used = $status == 'inUse' ? true : false; if ($this->user->getAccountId() != 0) { $sql .= "r.id " . ($used ? '' : "NOT") . " IN (SELECT role_id FROM farm_roles fr " . "JOIN farms f ON f.id = fr.farmid WHERE f." . ($envId ? "env_id" : "clientid") . " = ?)"; $args[] = $envId ?: $this->user->getAccountId(); } else { $sql .= "r.id " . ($used ? '' : "NOT") . " IN (SELECT role_id FROM farm_roles)"; } $sql .= ')'; } if ($chefServerId) { $sql .= " AND r.id IN (SELECT role_id FROM role_properties WHERE name = ? AND value = ?)"; $sql .= " AND r.id IN (SELECT role_id FROM role_properties WHERE name = ? AND value = ?)"; $args[] = \Scalr_Role_Behavior_Chef::ROLE_CHEF_SERVER_ID; $args[] = $chefServerId; $args[] = \Scalr_Role_Behavior_Chef::ROLE_CHEF_BOOTSTRAP; $args[] = 1; } if ($addImage) { if (isset($addImage['osId'])) { $sql .= " AND r.os_id = ?"; $args[] = $addImage['osId']; } if (isset($addImage['isScalarized']) && isset($addImage['hasCloudInit']) && $addImage['isScalarized'] == 0 && $addImage['hasCloudInit'] == 0) { $sql .= " AND r.is_scalarized = 0"; } } if ($isQuickStart) { $sql .= " AND r.is_quick_start = 1"; } if ($isDeprecated) { $sql .= " AND r.is_deprecated = 1"; } } $response = $this->buildResponseFromSql2($sql, ['id', 'name', 'os_id'], ['r.name'], $args); $data = []; $allPlatforms = array_flip(array_keys(SERVER_PLATFORMS::GetList())); foreach ($response['data'] as $r) { $role = new Role(); $role->load($r); $row = new stdClass(); $row->name = $role->name; $row->behaviors = $role->getBehaviors(); $row->id = $role->id; $row->accountId = $role->accountId; $row->envId = $role->envId; $row->status = $r['inUse'] ? 'In use' : 'Not used'; $row->scope = $role->getScope(); $row->os = $r['osName']; $row->osId = $role->osId; $row->osFamily = $r['osFamily']; $row->dtAdded = $role->added ? Scalr_Util_DateTime::convertTz($role->added) : null; $row->dtLastUsed = $role->lastUsed ? Scalr_Util_DateTime::convertTz($role->lastUsed) : null; $row->isQuickStart = $role->isQuickStart ? "1" : "0"; $row->isDeprecated = $role->isDeprecated ? "1" : "0"; $row->isScalarized = $role->isScalarized ? 1 : 0; $row->client_name = $role->accountId == 0 ? 'Scalr' : 'Private'; $row->environments = $r['environments'] ? explode(',', $r['environments']) : []; $platforms = array_keys($role->fetchImagesArray()); usort($platforms, function ($a, $b) use($allPlatforms) { return $allPlatforms[$a] > $allPlatforms[$b] ? 1 : -1; }); $row->platforms = $platforms; if ($addImage->count()) { try { $role->getImage($addImage['platform'], $addImage['cloudLocation']); $row->canAddImage = false; } catch (Exception $e) { $row->canAddImage = true; } } $data[] = $row; } $this->response->data(['total' => $response['total'], 'data' => $data]); }