JG2BQCRDWKCGVC56DCRBFCOXPHGT33LJ4FHWLIPOM64W6343D7CQC
YDWTHWAI7RPLW3KGM32J2JVUOSFTW4WOV3XPUMNTU3MDNGMQIOZAC
D3RL62X5NQTWDZXMDHAXS7K5GSTCCEH7L3YT5JMTPWJON3WKRKUAC
B7PYGLH3IXJXPUF7XUBMESMSEH26MEINQJIVMBDDOY4RLT356K4AC
6NYILMKIHYQMGMCRZANU2FDO66DGINOMCX2J4JFTUI6VL3HZ6D5QC
6BDGQ4VMUAGK5BTJAJVQOGTRHSUOQ5GRRUCZ3M6AVLWKCJNY52HAC
5RQCVFRHI353OFKZPVCNJLJLQPEKJVNEDCTM2224U4WABT2YLMQQC
TEB4R7OU65GZAGPNINWHOR4Q23OJYVUQK5RT75SV26W7JJEVT5DQC
EVP2FSBHQUCAXQ6IIMBD6IS24ODKHP6HFWYCHIMYG6KOFRQG3RVQC
WW3KRXX63EYWW4563QM3VSEN4PGCPR76KJQBYIKCKOCNMDG6JW4QC
4MZ4VIR7FU3PQ3WKJI6TJIKYOIIBODFEPKLMQ32S4AKPZSDFO6AQC
DNFB7TNICXFHRGPNZTT2I4VQNBPGVA2HQMUJWLNEPHF2M4SRZ3SQC
HZDCKIXQ3LCD7YPL7ZZBCRMD7YMKDJ2QAALETTG3FYMBF4TNFUBAC
IXY6NZLMLIDWQFPI2FBNF2PYJCM2TLNQIZKFUDLTTARHFWFQYCGQC
{{/if}}
{{#if player.query_delegate_data}}
<form id="query-delegate-{{ player.query_delegate_data.csrf }}" action="query-delegate.do" method="post">
<fieldset>
<label for="delegate_name">Delegate name:</label>
<input name="delegate_name" id="delegate_name-{{ player.query_delegate_data.csrf }}" type="text" placeholder="Username" required />
<input name="csrf" type="hidden" value="{{ player.query_delegate_data.csrf }}">
<input name="submit" type="submit" value="Query Delegate">
</fieldset>
</form>
let query_delegate_data = if status.map_or(true, |x| x == "started")
&& player_itself
&& delegate_name.is_none()
{
let csrf = Uuid::new_v4();
let mut cache = data.cache_query_delegation_game.lock().await;
cache.insert(
csrf,
(gameuid.clone(), user.to_lowercase()),
std::time::Duration::from_secs(data.cache_duration_sec),
);
Some(JoinLeaveData { csrf })
} else {
None
};
}
HttpResponse::Found()
.append_header((
header::LOCATION,
format!("slow-game-{}.html", cached_data.0),
))
.finish()
}
pub async fn post_query_delegate(
request: HttpRequest,
form: web::Form<QueryDelegationData>,
data: web::Data<WebData<'_>>,
data_rw: web::Data<DataBaseRw>,
) -> HttpResponse {
let cached_data = {
let mut cache = data.cache_query_delegation_game.lock().await;
cache.remove(&form.csrf)
};
let cached_data = if let Some(cd) = cached_data {
cd
} else {
log::warn!("Unknown data for CSRF: {}", form.csrf);
return HttpResponse::BadRequest().body("Incorrect");
};
let jar = request_to_jar(request);
if jar
.private(&data.cookies_key)
.get("auth")
.map_or(true, |x| x.value().to_lowercase() != cached_data.1)
{
log::warn!("Incorrect user");
return HttpResponse::BadRequest().body("Incorrect");
let dbclient_rw = match data_rw.0.get().await {
Ok(c) => c,
Err(e) => {
log::error!("Pool RW error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
let stmt = match dbclient_rw
.prepare("insert into games.query_delegation (game_uid, player_name, delegate_name) values ($1, $2, $3) on conflict do nothing;")
.await
{
Ok(stmt) => stmt,
Err(e) => {
log::error!("Pool RW statement error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
match dbclient_rw
.execute(
&stmt,
&[&cached_data.0, &cached_data.1, &form.delegate_name],
)
.await
{
Ok(_) => (),
Err(e) => {
log::error!("Pool RW execute insert error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
-- Add table to store queries to delegation
CREATE TABLE IF NOT EXISTS games.query_delegation (
game_uid VARCHAR(20) REFERENCES games.games(game_uid),
player_name CITEXT REFERENCES auth.users(player_name),
delegate_name CITEXT REFERENCES auth.users(player_name),
CONSTRAINT pk_query_delegation PRIMARY KEY (game_uid, player_name, delegate_name)
);
GRANT SELECT, INSERT, DELETE ON games.query_delegation TO freeorion;