Example #1
0
File: Farm.php Project: scalr/scalr
 /**
  * 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;
 }