/** * Gets default search criteria according current account * * @return array Returns array of the search criteria */ private function getDefaultCriteria() { $criteria = [['accountId' => $this->getUser()->getAccountId()]]; $user = $this->getUser(); if (!$user->canManageAcl()) { $env = new Account\Environment(); $teamEnv = new Account\TeamEnvs(); $team = new Account\Team(); $teamUser = new Account\TeamUser(); $criteria = array_merge($criteria, [AbstractEntity::STMT_DISTINCT => true, AbstractEntity::STMT_FROM => " {$env->table()}\n JOIN {$teamEnv->table('te')} ON {$teamEnv->columnEnvId('te')} = {$env->columnId()}\n JOIN {$team->table('at')} ON {$team->columnId('at')} = {$teamEnv->columnTeamId('te')}\n JOIN {$teamUser->table('tu')} ON {$teamUser->columnTeamId('tu')} = {$team->columnId('at')}\n ", AbstractEntity::STMT_WHERE => "{$teamUser->columnUserId('tu')} = " . $teamUser->qstr('userId', $user->id) . " AND {$team->columnAccountId('at')} = " . $team->qstr('accountId', $user->getAccountId())]); } return $criteria; }
/** * Generate SQL query like "EXISTS(SELECT 1 FROM farm_teams .... WHERE ...) to check FARM_TEAMS permission. * Table `farms` should have alias `f`. * If farmId is set, when JOIN table farms to get envId from it. * * @param int $userId Identifier of User * @param int $farmId optional Identifier of Farm * @return string */ public static function getUserTeamOwnershipSql($userId, $farmId = null) { $farm = new Farm(); $farmTeam = new FarmTeam(); $accountTeamUser = new Account\TeamUser(); $accountTeamEnv = new Account\TeamEnvs(); $sql = "EXISTS(" . "SELECT 1 FROM {$farmTeam->table()}" . "JOIN {$accountTeamUser->table()} ON {$accountTeamUser->columnTeamId} = {$farmTeam->columnTeamId} " . "JOIN {$accountTeamEnv->table()} ON {$accountTeamEnv->columnTeamId} = {$farmTeam->columnTeamId} " . ($farmId ? "JOIN {$farm->table('f')} ON {$farmTeam->columnFarmId} = {$farm->columnId('f')}" : "") . "WHERE {$accountTeamEnv->columnEnvId()} = {$farm->columnEnvId('f')} " . "AND " . ($farmId ? "{$farm->columnId('f')} = " . $farm->db()->qstr($farmId) : "{$farm->columnId('f')} = {$farmTeam->columnFarmId}") . " " . "AND {$accountTeamUser->columnUserId} = " . $farm->db()->qstr($userId) . ")"; return $sql; }