/** * @param string $query * @param string $platform * @param string $cloudLocation * @param JsonData $sort * @param int $start * @param int $limit * @throws Exception */ public function xListAction($query = null, $platform = null, $cloudLocation = null, JsonData $sort, $start = 0, $limit = 20) { $this->request->restrictAccess(Acl::RESOURCE_FARMS_ROLES); $criteria = []; $criteria[] = ['envId' => $this->getEnvironmentId()]; if ($query) { $querySql = '%' . $query . '%'; $criteria[] = ['$or' => [['id' => ['$like' => $querySql]]]]; } if ($platform) { $criteria[] = ['platform' => $platform]; } if ($cloudLocation) { $criteria[] = ['cloudLocation' => $cloudLocation]; } $result = Image::find($criteria, \Scalr\UI\Utils::convertOrder($sort, ['id' => 'ASC'], ['id', 'platform', 'cloudLocation', 'os', 'dtAdded', 'architecture', 'source']), $limit, $start, true); $data = []; foreach ($result as $image) { /* @var Image $image */ $s = get_object_vars($image); $s['dtAdded'] = Scalr_Util_DateTime::convertTz($image->dtAdded); $s['status'] = $image->isUsed() ? 'In use' : 'Not used'; $data[] = $s; } $this->response->data(['total' => $result->totalNumber, 'data' => $data]); }
/** * @param string $query * @param string $sshKeyId * @param int $farmId * @param string $platform * @param string $cloudLocation * @param JsonData $sort * @param int $start * @param int $limit */ public function xListAction($query = null, $sshKeyId = null, $farmId = null, $platform = null, $cloudLocation = null, JsonData $sort, $start = 0, $limit = 20) { $criteria = [['envId' => $this->getEnvironmentId()]]; if ($this->request->isAllowed(Acl::RESOURCE_FARMS, Acl::PERM_FARMS_SERVERS)) { if (!$this->request->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) { $criteria[] = ['farmId' => ['$ne' => NULL]]; } } else { $farmSql = "SELECT id FROM farms WHERE env_id = ?"; $farmArgs = [$this->getEnvironmentId()]; list($farmSql, $farmArgs) = $this->request->prepareFarmSqlQuery($farmSql, $farmArgs, '', Acl::PERM_FARMS_SERVERS); $farms = $this->db->GetCol($farmSql, $farmArgs); if ($this->request->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) { $criteria[] = ['$or' => [['farmId' => NULL], ['farmId' => ['$in' => $farms]]]]; } else { if (count($farms)) { $criteria[] = ['farmId' => ['$in' => $farms]]; } else { // user doesn't have access to any farm. try to find better solution $criteria[] = ['farmId' => -1]; } } } if ($sshKeyId) { $criteria[] = ['id' => $sshKeyId]; } if ($farmId) { $criteria[] = ['farmId' => $farmId]; } if ($query) { $querySql = '%' . $query . '%'; $criteria[] = ['$or' => [['cloudKeyName' => ['$like' => $querySql]]]]; } if ($platform) { $criteria[] = ['platform' => $platform]; if ($cloudLocation) { $criteria[] = ['cloudLocation' => $cloudLocation]; } } $result = SshKey::find($criteria, null, \Scalr\UI\Utils::convertOrder($sort, ['id' => true], ['id', 'cloudKeyName', 'platform', 'cloudLocation']), $limit, $start, true); $data = []; foreach ($result as $key) { /* @var SshKey $key */ $data[] = $this->getSshKeyObject($key); } $this->response->data(['total' => $result->totalNumber, 'data' => $data]); }
/** * @param string $scriptId * @param string $query * @param string $scope * @param JsonData $sort * @param int $start * @param int $limit */ public function xListAction($scriptId = null, $query = null, $scope = null, JsonData $sort, $start = 0, $limit = 20) { $this->request->restrictAccess('SCRIPTS'); $criteria = []; if ($this->user->isScalrAdmin()) { $criteria[] = ['accountId' => NULL]; } else { if ($scope == ScopeInterface::SCOPE_SCALR) { $criteria[] = ['accountId' => NULL]; } else { if ($scope == ScopeInterface::SCOPE_ACCOUNT) { $criteria[] = ['accountId' => $this->user->getAccountId()]; $criteria[] = ['envId' => NULL]; } else { if ($scope == ScopeInterface::SCOPE_ENVIRONMENT) { $criteria[] = ['accountId' => $this->user->getAccountId()]; $criteria[] = ['envId' => $this->getEnvironmentId(true)]; } else { $criteria[] = ['$or' => [['accountId' => $this->user->getAccountId()], ['accountId' => NULL]]]; if ($this->request->getScope() == ScopeInterface::SCOPE_ENVIRONMENT) { $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId(true)], ['envId' => NULL]]]; } else { $criteria[] = ['envId' => $this->getEnvironmentId(true)]; } } } } } if ($query) { $querySql = '%' . $query . '%'; $criteria[] = ['$or' => [['id' => ['$like' => $query]], ['name' => ['$like' => $querySql]], ['description' => ['$like' => $querySql]]]]; } if ($scriptId) { $criteria[] = ['id' => $scriptId]; } $result = Script::find($criteria, null, Utils::convertOrder($sort, ['name' => true], ['id', 'name', 'description', 'isSync', 'dtCreated', 'dtChanged']), $limit, $start, true); $data = []; foreach ($result as $script) { /* @var $script Script */ $data[] = $this->getScript($script); } $this->response->data(['total' => $result->totalNumber, 'data' => $data]); }
/** * @param string $query * @param string $platform * @param string $cloudLocation * @param string $scope * @param string $osFamily * @param string $osId * @param string $id * @param string $hash * @param JsonData $sort * @param int $start * @param int $limit * @param JsonData $hideLocation * @param bool $hideNotActive * @throws Exception */ public function xListAction($query = null, $platform = null, $cloudLocation = null, $scope = null, $osFamily = null, $osId = null, $id = null, $hash = null, JsonData $sort, $start = 0, $limit = 20, JsonData $hideLocation, $hideNotActive = false) { $this->restrictAccess('IMAGES'); $osIds = $criteria = []; $accountId = $this->user->getAccountId() ?: NULL; $envId = $this->getEnvironmentId(true); if ($this->request->getScope() == ScopeInterface::SCOPE_SCALR) { $criteria[] = ['accountId' => NULL]; } else { if ($this->request->getScope() == ScopeInterface::SCOPE_ACCOUNT) { $criteria[] = ['$or' => [['accountId' => $accountId], ['accountId' => NULL]]]; $criteria[] = ['envId' => NULL]; } else { $enabledPlatforms = $this->getEnvironment()->getEnabledPlatforms(); $criteria[] = ['$or' => [['$and' => [['accountId' => NULL], ['platform' => empty($enabledPlatforms) ? NULL : ['$in' => $enabledPlatforms]]]], ['$and' => [['accountId' => $accountId], ['envId' => NULL]]], ['envId' => $envId]]]; } } if ($hash) { $criteria[] = ['hash' => $hash]; } else { if ($query) { $querySql = '%' . $query . '%'; $criteria[] = ['name' => ['$like' => $querySql]]; } if ($platform) { $criteria[] = ['platform' => $platform]; } if ($cloudLocation) { $criteria[] = ['cloudLocation' => $cloudLocation]; } if ($scope == ScopeInterface::SCOPE_SCALR) { $criteria[] = ['accountId' => NULL]; } else { if ($scope == ScopeInterface::SCOPE_ACCOUNT) { $criteria[] = ['accountId' => $accountId]; $criteria[] = ['envId' => NULL]; } else { if ($scope == ScopeInterface::SCOPE_ENVIRONMENT) { $criteria[] = ['envId' => $envId]; } } } if ($osFamily) { $osIds = Os::findIdsBy($osFamily); } if ($osId) { $os = Os::find([['id' => $osId]]); $osIds = []; foreach ($os as $i) { /* @var $i Os */ array_push($osIds, $i->id); } } if (!empty($osIds)) { $criteria[] = ['osId' => ['$in' => $osIds]]; } if ($id) { $criteria[] = ['id' => ['$like' => $id . '%']]; } if ($hideLocation) { foreach ($hideLocation as $platform => $locations) { foreach ($locations as $loc) { if ($loc) { $criteria[] = ['$or' => [['platform' => ['$ne' => $platform]], ['cloudLocation' => ['$ne' => $loc]]]]; } else { $criteria[] = ['platform' => ['$ne' => $platform]]; } } } } if ($hideNotActive) { $criteria[] = ['status' => Image::STATUS_ACTIVE]; } } $image = new Image(); $os = new Os(); $order = \Scalr\UI\Utils::convertOrder($sort, ['dtAdded' => false], ['id', 'platform', 'cloudLocation', 'name', 'osId', 'dtAdded', 'architecture', 'createdByEmail', 'source', 'type']); if (!empty($order)) { $sOrder = ''; foreach ($order as $k => $v) { if ($k == 'osId') { $sOrder .= ', IF(os.family IS NOT NULL, os.family, images.os_id)' . ($v ? '' : ' DESC') . ", CAST(os.version AS DECIMAL(10,2))" . ($v ? '' : ' DESC'); } else { $field = $image->getIterator()->getField($k); if (!$field) { throw new InvalidArgumentException(sprintf("Property %s does not exist in %s", $k, get_class($image))); } $sOrder .= ', ' . $field->getColumnName() . ($v ? '' : ' DESC'); } } $sOrder = $sOrder != '' ? 'ORDER BY ' . substr($sOrder, 2) : ''; } $result = $this->db->Execute("\n SELECT " . (isset($limit) ? 'SQL_CALC_FOUND_ROWS ' : '') . $image->fields('images') . "\n FROM " . $image->table('images') . "\n LEFT JOIN " . $os->table('os') . " ON {$os->columnId} = {$image->columnOsId}\n WHERE " . $image->_buildQuery($criteria, 'AND', 'images')['where'] . "\n " . (!empty($sOrder) ? $sOrder : "") . "\n " . (isset($limit) ? "LIMIT " . ($start ? intval($start) . ',' : '') . intval($limit) : "") . "\n "); if (isset($limit)) { $totalNumber = $this->db->getOne('SELECT FOUND_ROWS()'); } else { $totalNumber = $result->RowCount(); } $data = []; while ($rec = $result->FetchRow()) { $image = new Image(); $image->load($rec); $s = get_object_vars($image); $dtAdded = $image->getDtAdded(); $s['dtAdded'] = $dtAdded ? Scalr_Util_DateTime::convertTz($dtAdded) : ''; $s['dtLastUsed'] = $image->dtLastUsed ? Scalr_Util_DateTime::convertTz($image->dtLastUsed) : ''; $s['used'] = $image->getUsed($accountId, $envId); $s['software'] = $image->getSoftwareAsString(); $s['osFamily'] = $image->getOs()->family; $s['osGeneration'] = $image->getOs()->generation; $s['osVersion'] = $image->getOs()->version; $s['os'] = $image->getOs()->name; $s['scope'] = $image->getScope(); $data[] = $s; } $this->response->data(['total' => $totalNumber, 'data' => $data]); }
/** * @param JsonData $sort Sorting order, defaults to added desc * @param string $query optional If set search in fields `id` (exact match), `title`, `msg` (partial match) * @param int $start optional Offset * @param int $limit optional Limit * @throws Exception * @throws Scalr_Exception_InsufficientPermissions */ public function xListAction(JsonData $sort, $query = null, $start = 0, $limit = 25) { $this->request->restrictAccess(Acl::RESOURCE_ANNOUNCEMENTS); $criteria = []; $scope = $this->request->getScope(); switch ($scope) { case ScopeInterface::SCOPE_SCALR: //scalr-scoped messages only $criteria[] = ['accountId' => null]; break; case ScopeInterface::SCOPE_ACCOUNT: //account-related messages $criteria[] = ['$or' => [['accountId' => null], ['accountId' => $this->getUser()->accountId ?: null]]]; break; default: throw new Scalr_Exception_InsufficientPermissions(); } if ($query) { $queryLike = '%' . $query . '%'; $criteria[] = ['$or' => [['id' => ['$like' => $query]], ['title' => ['$like' => $queryLike]], ['msg' => ['$like' => $queryLike]]]]; } $result = Announcement::find($criteria, null, Utils::convertOrder($sort, ['added' => false], ['id', 'title', 'msg', 'added']), $limit, $start, true); $data = []; /* @var $announcement Scalr\Model\Entity\Announcement */ foreach ($result as $announcement) { $data[] = $this->prepareDataForList($announcement); } $this->response->data(['total' => $result->totalNumber, 'data' => $data]); }
/** * @param string $query * @param string $origin * @param JsonData $sort * @param int $start * @param int $limit */ public function xListAction($query = null, $origin = null, JsonData $sort, $start = 0, $limit = 20) { $this->request->restrictAccess(Acl::RESOURCE_ADMINISTRATION_SCRIPTS); $criteria = []; if ($this->user->isScalrAdmin()) { $criteria[] = ['accountId' => NULL]; } else { if ($origin == 'Shared') { $criteria[] = ['accountId' => NULL]; } else { if ($origin == 'Custom') { $criteria[] = ['accountId' => $this->user->getAccountId()]; $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId()], ['envId' => NULL]]]; } else { $criteria[] = ['$or' => [['accountId' => $this->user->getAccountId()], ['accountId' => NULL]]]; $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId()], ['envId' => NULL]]]; } } } if ($query) { $querySql = '%' . $query . '%'; $criteria[] = ['$or' => [['id' => ['$like' => $query]], ['name' => ['$like' => $querySql]], ['description' => ['$like' => $querySql]]]]; } $result = Script::find($criteria, \Scalr\UI\Utils::convertOrder($sort, ['name' => 'ASC'], ['id', 'name', 'description', 'isSync', 'dtCreated', 'dtChanged']), $limit, $start, true); $data = []; foreach ($result as $script) { /* @var Script $script */ $s = get_object_vars($script); $s['dtCreated'] = Scalr_Util_DateTime::convertTz($script->dtCreated); $s['dtChanged'] = Scalr_Util_DateTime::convertTz($script->dtChanged); $s['version'] = $script->getLatestVersion()->version; $data[] = $s; } $this->response->data(['total' => $result->totalNumber, 'data' => $data]); }
/** * @param string $query * @param string $platform * @param string $cloudLocation * @param string $scope * @param string $osFamily * @param string $osId * @param string $id * @param string $hash * @param JsonData $sort * @param int $start * @param int $limit * @param JsonData $os * @param JsonData $hideLocation * @param bool $hideNotActive * @throws Exception */ public function xListAction($query = null, $platform = null, $cloudLocation = null, $scope = null, $osFamily = null, $osId = null, $id = null, $hash = null, JsonData $sort, $start = 0, $limit = 20, JsonData $hideLocation, $hideNotActive = false) { $this->request->restrictAccess(Acl::RESOURCE_FARMS_IMAGES); $criteria = []; if ($hash) { $criteria[] = ['hash' => $hash]; if ($this->getEnvironment()) { $enabledPlatforms = $this->getEnvironment()->getEnabledPlatforms(); $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId(true)], ['$and' => [['envId' => NULL], ['platform' => empty($enabledPlatforms) ? NULL : ['$in' => $enabledPlatforms]]]]]]; } else { $criteria[] = ['envId' => NULL]; } } else { if ($query) { $querySql = '%' . $query . '%'; $criteria[] = ['name' => ['$like' => $querySql]]; } if ($platform) { $criteria[] = ['platform' => $platform]; } if ($cloudLocation) { $criteria[] = ['cloudLocation' => $cloudLocation]; } if ($this->getEnvironment()) { $enabledPlatforms = $this->getEnvironment()->getEnabledPlatforms(); if ($scope) { if ($scope == 'env') { $criteria[] = ['envId' => $this->getEnvironmentId(true)]; } else { // hide shared images, which platforms are not configured if (empty($enabledPlatforms)) { $criteria[] = ['platform' => NULL]; } else { $criteria[] = ['envId' => NULL]; $criteria[] = ['platform' => ['$in' => $enabledPlatforms]]; } } } else { $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId(true)], ['$and' => [['envId' => NULL], ['platform' => empty($enabledPlatforms) ? NULL : ['$in' => $enabledPlatforms]]]]]]; } } else { $criteria[] = ['envId' => NULL]; } if ($osFamily) { $osIds = Os::findIdsBy($osFamily); } if ($osId) { $os = Os::find([['id' => $osId]]); $osIds = []; foreach ($os as $i) { /* @var $i Os */ array_push($osIds, $i->id); } } if (count($osIds) > 0) { $criteria[] = ['osId' => ['$in' => $osIds]]; } if ($id) { $criteria[] = ['id' => ['$like' => $id . '%']]; } if ($hideLocation) { foreach ($hideLocation as $platform => $locations) { foreach ($locations as $loc) { if ($loc) { $criteria[] = ['$or' => [['platform' => ['$ne' => $platform]], ['cloudLocation' => ['$ne' => $loc]]]]; } else { $criteria[] = ['platform' => ['$ne' => $platform]]; } } } } if ($hideNotActive) { $criteria[] = ['status' => Image::STATUS_ACTIVE]; } } $result = Image::find($criteria, \Scalr\UI\Utils::convertOrder($sort, ['id' => 'ASC'], ['id', 'platform', 'cloudLocation', 'name', 'osId', 'dtAdded', 'architecture', 'createdByEmail', 'source']), $limit, $start, true); $data = []; foreach ($result as $image) { /* @var $image Image */ $s = get_object_vars($image); $dtAdded = $image->getDtAdded(); $s['dtAdded'] = $dtAdded ? Scalr_Util_DateTime::convertTz($dtAdded) : ''; $s['dtLastUsed'] = $image->dtLastUsed ? Scalr_Util_DateTime::convertTz($image->dtLastUsed) : ''; $s['used'] = $image->getUsed($this->getEnvironmentId(true)); $s['software'] = $image->getSoftwareAsString(); $s['osFamily'] = $image->getOs()->family; $s['osGeneration'] = $image->getOs()->generation; $s['osVersion'] = $image->getOs()->version; $s['os'] = $image->getOs()->name; $data[] = $s; } $this->response->data(['total' => $result->totalNumber, 'data' => $data, 'debug' => $criteria]); }