/** * Gets default search criteria according environment scope * * @return array Returns array of the search criteria */ private function getDefaultCriteria() { $environment = $this->getEnvironment(); $criteria = [['envId' => $environment->id]]; if (!$this->hasPermissions(Acl::RESOURCE_FARMS)) { $where = []; $farm = new Farm(); $farmTeam = new FarmTeam(); if ($this->hasPermissions(Acl::RESOURCE_OWN_FARMS)) { $where[] = "{$farm->columnOwnerId()} = " . $farm->qstr('ownerId', $this->getUser()->id); } if ($this->hasPermissions(Acl::RESOURCE_TEAM_FARMS)) { $join[] = "\n LEFT JOIN {$farmTeam->table('ft')} ON {$farmTeam->columnFarmId('ft')} = {$farm->columnId()}\n LEFT JOIN `account_team_users` `atu` ON `atu`.`team_id` = {$farmTeam->columnTeamId('ft')}\n LEFT JOIN `account_team_envs` `ate` ON `ate`.`team_id` = {$farmTeam->columnTeamId('ft')} AND `ate`.`env_id` = {$farm->columnEnvId()}\n "; $where[] = "`atu`.`user_id` = " . $farmTeam->db()->qstr($this->getUser()->id) . " AND `ate`.`team_id` IS NOT NULL"; } if (!empty($where)) { $criteria[Farm::STMT_WHERE] = '(' . join(' OR ', $where) . ')'; } if (!empty($join)) { if (empty($criteria[Farm::STMT_FROM])) { $criteria[Farm::STMT_FROM] = $farm->table(); } $criteria[Farm::STMT_FROM] .= implode(' ', $join); } } return $criteria; }
/** * Gets Default search criteria for the Environment scope * * @return array Returns array of the default search criteria for the Environment scope */ private function getDefaultCriteria() { $server = new Server(); $defaultCondition = sprintf("{$server->columnAccountId()} = %d AND {$server->columnEnvId()} = %d ", $this->getUser()->accountId, $this->getEnvironment()->id); $and = " AND {$server->columnFarmId()} IS NOT NULL "; if (!$this->hasPermissions(Acl::RESOURCE_FARMS)) { $where = []; $farm = new Farm(); $farmTeam = new FarmTeam(); $join[] = " LEFT JOIN {$farm->table('f')} ON {$farm->columnId('f')} = {$server->columnFarmId()}"; if ($this->hasPermissions(Acl::RESOURCE_OWN_FARMS)) { $where[] = "{$farm->columnOwnerId('f')} = " . $farm->qstr('ownerId', $this->getUser()->id); } if ($this->hasPermissions(Acl::RESOURCE_TEAM_FARMS)) { $join[] = "\n LEFT JOIN {$farmTeam->table('ft')} ON {$farmTeam->columnFarmId('ft')} = {$farm->columnId('f')}\n LEFT JOIN `account_team_users` `atu` ON `atu`.`team_id` = {$farmTeam->columnTeamId('ft')}\n LEFT JOIN `account_team_envs` `ate` ON `ate`.`team_id` = {$farmTeam->columnTeamId('ft')} AND `ate`.`env_id` = {$farm->columnEnvId('f')}\n "; $where[] = "`atu`.`user_id` = " . $farmTeam->db()->qstr($this->getUser()->id) . " AND `ate`.`team_id` IS NOT NULL"; } if (!empty($where)) { $criteria[Farm::STMT_WHERE] = '(' . $defaultCondition . $and . ' AND ' . join(' OR ', $where) . ')'; } $criteria[Farm::STMT_FROM] = $server->table() . implode(' ', $join); } // add Temporary and Importing Servers to response if ($this->hasPermissions(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) { $extraCondition = sprintf("({$defaultCondition} AND {$server->columnFarmId()} IS NULL AND {$server->columnStatus()} IN ('%s', '%s'))", Server::STATUS_IMPORTING, Server::STATUS_TEMPORARY); if (empty($criteria[Farm::STMT_WHERE]) && $this->hasPermissions(Acl::RESOURCE_FARMS)) { $criteria[Farm::STMT_WHERE] = "(({$defaultCondition}{$and}) OR {$extraCondition})"; } else { if (empty($criteria[Farm::STMT_WHERE])) { $criteria[Farm::STMT_WHERE] = "{$extraCondition}"; } else { $criteria[Farm::STMT_WHERE] = "({$criteria[Farm::STMT_WHERE]} OR {$extraCondition})"; } } } if (empty($criteria[Farm::STMT_WHERE])) { $criteria[Farm::STMT_WHERE] = $defaultCondition; } return $criteria; }