You've already forked AstralRinth
forked from didirus/AstralRinth
Sessions Route + Password Auth (#649)
* Sessions Route + Password Auth * run prep + fix clippy * changing passwords + logging in * register login
This commit is contained in:
@@ -83,13 +83,13 @@ generate_ids!(
|
||||
"SELECT EXISTS(SELECT 1 FROM states WHERE id=$1)",
|
||||
StateId
|
||||
);
|
||||
generate_ids!(
|
||||
pub generate_pat_id,
|
||||
PatId,
|
||||
8,
|
||||
"SELECT EXISTS(SELECT 1 FROM pats WHERE id=$1)",
|
||||
PatId
|
||||
);
|
||||
// generate_ids!(
|
||||
// pub generate_pat_id,
|
||||
// PatId,
|
||||
// 8,
|
||||
// "SELECT EXISTS(SELECT 1 FROM pats WHERE id=$1)",
|
||||
// PatId
|
||||
// );
|
||||
|
||||
generate_ids!(
|
||||
pub generate_user_id,
|
||||
|
||||
@@ -10,8 +10,6 @@ const SESSIONS_IDS_NAMESPACE: &str = "sessions_ids";
|
||||
const SESSIONS_USERS_NAMESPACE: &str = "sessions_users";
|
||||
const DEFAULT_EXPIRY: i64 = 1800; // 30 minutes
|
||||
|
||||
// TODO: Manage sessions cache + clear cache when needed
|
||||
|
||||
pub struct SessionBuilder {
|
||||
pub session: String,
|
||||
pub user_id: UserId,
|
||||
@@ -293,10 +291,33 @@ impl Session {
|
||||
Ok(db_sessions)
|
||||
}
|
||||
|
||||
pub async fn clear_cache(
|
||||
clear_sessions: Vec<(Option<SessionId>, Option<String>, Option<UserId>)>,
|
||||
redis: &deadpool_redis::Pool,
|
||||
) -> Result<(), DatabaseError> {
|
||||
let mut redis = redis.get().await?;
|
||||
let mut cmd = cmd("DEL");
|
||||
|
||||
for (id, session, user_id) in clear_sessions {
|
||||
if let Some(id) = id {
|
||||
cmd.arg(format!("{}:{}", SESSIONS_NAMESPACE, id.0));
|
||||
}
|
||||
if let Some(session) = session {
|
||||
cmd.arg(format!("{}:{}", SESSIONS_IDS_NAMESPACE, session));
|
||||
}
|
||||
if let Some(user_id) = user_id {
|
||||
cmd.arg(format!("{}:{}", SESSIONS_USERS_NAMESPACE, user_id.0));
|
||||
}
|
||||
}
|
||||
|
||||
cmd.query_async::<_, ()>(&mut redis).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn remove(
|
||||
id: SessionId,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
// redis: &deadpool_redis::Pool,
|
||||
) -> Result<Option<()>, sqlx::error::Error> {
|
||||
sqlx::query!(
|
||||
"
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use super::ids::*;
|
||||
use crate::database::models::User;
|
||||
use crate::models::teams::Permissions;
|
||||
use crate::models::users::Badges;
|
||||
use itertools::Itertools;
|
||||
use redis::cmd;
|
||||
use rust_decimal::Decimal;
|
||||
@@ -83,6 +81,7 @@ pub struct Team {
|
||||
}
|
||||
|
||||
/// A member of a team
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct TeamMember {
|
||||
pub id: TeamMemberId,
|
||||
pub team_id: TeamId,
|
||||
@@ -95,27 +94,13 @@ pub struct TeamMember {
|
||||
pub ordering: i64,
|
||||
}
|
||||
|
||||
/// A member of a team
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct QueryTeamMember {
|
||||
pub id: TeamMemberId,
|
||||
pub team_id: TeamId,
|
||||
/// The user associated with the member
|
||||
pub user: User,
|
||||
pub role: String,
|
||||
pub permissions: Permissions,
|
||||
pub accepted: bool,
|
||||
pub payouts_split: Decimal,
|
||||
pub ordering: i64,
|
||||
}
|
||||
|
||||
impl TeamMember {
|
||||
// Lists the full members of a team
|
||||
pub async fn get_from_team_full<'a, 'b, E>(
|
||||
id: TeamId,
|
||||
executor: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
) -> Result<Vec<QueryTeamMember>, super::DatabaseError>
|
||||
) -> Result<Vec<TeamMember>, super::DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
|
||||
{
|
||||
@@ -126,7 +111,7 @@ impl TeamMember {
|
||||
team_ids: &[TeamId],
|
||||
exec: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
) -> Result<Vec<QueryTeamMember>, super::DatabaseError>
|
||||
) -> Result<Vec<TeamMember>, super::DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
|
||||
{
|
||||
@@ -155,7 +140,7 @@ impl TeamMember {
|
||||
for team_raw in teams {
|
||||
if let Some(mut team) = team_raw
|
||||
.clone()
|
||||
.and_then(|x| serde_json::from_str::<Vec<QueryTeamMember>>(&x).ok())
|
||||
.and_then(|x| serde_json::from_str::<Vec<TeamMember>>(&x).ok())
|
||||
{
|
||||
if let Some(team_id) = team.first().map(|x| x.team_id) {
|
||||
team_ids_parsed.retain(|x| &team_id.0 != x);
|
||||
@@ -167,14 +152,11 @@ impl TeamMember {
|
||||
}
|
||||
|
||||
if !team_ids_parsed.is_empty() {
|
||||
let teams: Vec<QueryTeamMember> = sqlx::query!(
|
||||
let teams: Vec<TeamMember> = sqlx::query!(
|
||||
"
|
||||
SELECT tm.id id, tm.team_id team_id, tm.role member_role, tm.permissions permissions, tm.accepted accepted, tm.payouts_split payouts_split, tm.ordering,
|
||||
u.id user_id, u.name user_name,
|
||||
u.avatar_url avatar_url, u.username username, u.bio bio,
|
||||
u.created created, u.role user_role, u.badges badges
|
||||
tm.user_id user_id
|
||||
FROM team_members tm
|
||||
INNER JOIN users u ON u.id = tm.user_id
|
||||
WHERE tm.team_id = ANY($1)
|
||||
ORDER BY tm.team_id, tm.ordering
|
||||
",
|
||||
@@ -183,39 +165,19 @@ impl TeamMember {
|
||||
.fetch_many(exec)
|
||||
.try_filter_map(|e| async {
|
||||
Ok(e.right().map(|m|
|
||||
QueryTeamMember {
|
||||
TeamMember {
|
||||
id: TeamMemberId(m.id),
|
||||
team_id: TeamId(m.team_id),
|
||||
role: m.member_role,
|
||||
permissions: Permissions::from_bits(m.permissions as u64).unwrap_or_default(),
|
||||
accepted: m.accepted,
|
||||
user: User {
|
||||
id: UserId(m.user_id),
|
||||
github_id: None,
|
||||
discord_id: None,
|
||||
gitlab_id: None,
|
||||
google_id: None,
|
||||
steam_id: None,
|
||||
name: m.user_name,
|
||||
email: None,
|
||||
avatar_url: m.avatar_url,
|
||||
username: m.username,
|
||||
bio: m.bio,
|
||||
created: m.created,
|
||||
role: m.user_role,
|
||||
badges: Badges::from_bits(m.badges as u64).unwrap_or_default(),
|
||||
balance: Decimal::ZERO,
|
||||
payout_wallet: None,
|
||||
payout_wallet_type: None,
|
||||
payout_address: None,
|
||||
microsoft_id: None,
|
||||
},
|
||||
user_id: UserId(m.user_id),
|
||||
payouts_split: m.payouts_split,
|
||||
ordering: m.ordering,
|
||||
}
|
||||
))
|
||||
})
|
||||
.try_collect::<Vec<QueryTeamMember>>()
|
||||
.try_collect::<Vec<TeamMember>>()
|
||||
.await?;
|
||||
|
||||
for (id, members) in &teams.into_iter().group_by(|x| x.team_id) {
|
||||
|
||||
@@ -12,7 +12,7 @@ const USER_USERNAMES_NAMESPACE: &str = "users_usernames";
|
||||
// const USERS_PROJECTS_NAMESPACE: &str = "users_projects";
|
||||
const DEFAULT_EXPIRY: i64 = 1800; // 30 minutes
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
||||
pub struct User {
|
||||
pub id: UserId,
|
||||
|
||||
@@ -22,10 +22,12 @@ pub struct User {
|
||||
pub google_id: Option<String>,
|
||||
pub steam_id: Option<i64>,
|
||||
pub microsoft_id: Option<String>,
|
||||
pub password: Option<String>,
|
||||
|
||||
pub username: String,
|
||||
pub name: Option<String>,
|
||||
pub email: Option<String>,
|
||||
pub email_verified: bool,
|
||||
pub avatar_url: Option<String>,
|
||||
pub bio: Option<String>,
|
||||
pub created: DateTime<Utc>,
|
||||
@@ -47,12 +49,14 @@ impl User {
|
||||
INSERT INTO users (
|
||||
id, username, name, email,
|
||||
avatar_url, bio, created,
|
||||
github_id, discord_id, gitlab_id, google_id, steam_id, microsoft_id
|
||||
github_id, discord_id, gitlab_id, google_id, steam_id, microsoft_id,
|
||||
email_verified, password
|
||||
)
|
||||
VALUES (
|
||||
$1, $2, $3, $4, $5,
|
||||
$6, $7,
|
||||
$8, $9, $10, $11, $12, $13
|
||||
$8, $9, $10, $11, $12, $13,
|
||||
$14, $15
|
||||
)
|
||||
",
|
||||
self.id as UserId,
|
||||
@@ -68,6 +72,8 @@ impl User {
|
||||
self.google_id,
|
||||
self.steam_id,
|
||||
self.microsoft_id,
|
||||
self.email_verified,
|
||||
self.password,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
@@ -197,9 +203,10 @@ impl User {
|
||||
avatar_url, username, bio,
|
||||
created, role, badges,
|
||||
balance, payout_wallet, payout_wallet_type, payout_address,
|
||||
github_id, discord_id, gitlab_id, google_id, steam_id, microsoft_id
|
||||
github_id, discord_id, gitlab_id, google_id, steam_id, microsoft_id,
|
||||
email_verified, password
|
||||
FROM users
|
||||
WHERE id = ANY($1) OR username = ANY($2)
|
||||
WHERE id = ANY($1) OR LOWER(username) = ANY($2)
|
||||
",
|
||||
&user_ids_parsed,
|
||||
&remaining_strings
|
||||
@@ -219,6 +226,7 @@ impl User {
|
||||
microsoft_id: u.microsoft_id,
|
||||
name: u.name,
|
||||
email: u.email,
|
||||
email_verified: u.email_verified,
|
||||
avatar_url: u.avatar_url,
|
||||
username: u.username,
|
||||
bio: u.bio,
|
||||
@@ -231,6 +239,7 @@ impl User {
|
||||
.payout_wallet_type
|
||||
.map(|x| RecipientType::from_string(&x)),
|
||||
payout_address: u.payout_address,
|
||||
password: u.password,
|
||||
}))
|
||||
})
|
||||
.try_collect::<Vec<User>>()
|
||||
|
||||
Reference in New Issue
Block a user