Browse Source

move to uuid bid

main
John Doe 5 months ago
parent
commit
a61584b47a
  1. 6
      server/lemmy_db/src/schema.rs
  2. 39
      server/lemmy_db/src/user_ban_id.rs
  3. 6
      server/migrations/2021-03-28-005913_create_ban_id_tables/up.sql
  4. 16
      server/src/api/user.rs

6
server/lemmy_db/src/schema.rs

@ -11,9 +11,9 @@ table! {
table!{
hexbear.ban_id (id) {
id -> Int4,
id -> Uuid,
created -> Timestamp,
aliased_to -> Nullable<Int4>,
aliased_to -> Nullable<Uuid>,
}
}
@ -514,7 +514,7 @@ table! {
table! {
hexbear.user_ban_id (bid, uid) {
bid -> Int4,
bid -> Uuid,
uid -> Int4,
}
}

39
server/lemmy_db/src/user_ban_id.rs

@ -3,19 +3,20 @@ use crate::schema::{
{ban_id, ban_id::dsl::*},
{user_ban_id, user_ban_id::dsl::*}
};
use uuid::Uuid;
#[derive(Queryable, Insertable)]
#[table_name = "ban_id"]
pub struct BanId {
pub id: i32,
pub id: Uuid,
pub created: chrono::NaiveDateTime,
pub aliased_to: Option<i32>,
pub aliased_to: Option<Uuid>,
}
#[derive(Queryable, Insertable)]
#[table_name = "user_ban_id"]
pub struct UserBanId {
pub bid: i32,
pub bid: Uuid,
pub uid: i32,
}
@ -24,29 +25,33 @@ impl BanId {
insert_into(ban_id).default_values().get_result::<Self>(conn)
}
pub fn read(conn: &PgConnection, ban_id_val: i32) -> Result<Self, Error> {
pub fn read(conn: &PgConnection, ban_id_val: Uuid) -> Result<Self, Error> {
ban_id.find(ban_id_val).first::<Self>(conn)
}
pub fn update_alias(conn: &PgConnection, old_bid_val: i32, new_bid_val: i32) -> Result<Vec<Self>, Error> {
pub fn read_opt(conn: &PgConnection, ban_id_val: Uuid) -> Result<Option<Self>, Error> {
ban_id.find(ban_id_val).first::<Self>(conn).optional()
}
pub fn update_alias(conn: &PgConnection, old_bid_val: Uuid, new_bid_val: Uuid) -> Result<Vec<Self>, Error> {
update(ban_id.filter(id.eq(old_bid_val).or(aliased_to.eq(old_bid_val)))).set(aliased_to.eq(new_bid_val)).get_results(conn)
}
}
impl UserBanId {
fn simple_associate(conn: &PgConnection, ban_id_val: i32, user_id_val: i32) -> Result<Self, Error> {
fn simple_associate(conn: &PgConnection, ban_id_val: Uuid, user_id_val: i32) -> Result<Self, Error> {
insert_into(user_ban_id)
.values(UserBanId { bid: ban_id_val, uid: user_id_val })
.get_result::<Self>(conn)
}
fn overwriting_associate(conn: &PgConnection, old_bid_val: i32, new_bid_val: i32) -> Result<Self, Error> {
fn overwriting_associate(conn: &PgConnection, old_bid_val: Uuid, new_bid_val: Uuid) -> Result<Self, Error> {
BanId::update_alias(conn, old_bid_val, new_bid_val)?;
update(user_ban_id.filter(bid.eq(old_bid_val))).set(bid.eq(new_bid_val)).get_result(conn)
}
pub fn associate(conn: &PgConnection, ban_id_val: i32, user_id_val: i32) -> Result<Self, Error> {
match Self::get_by_user(conn, user_id_val) {
pub fn associate(conn: &PgConnection, ban_id_val: Uuid, user_id_val: i32) -> Result<Self, Error> {
return match Self::get_by_user(conn, user_id_val) {
//UserBanId found attached to user, which is not the same as the incoming one.
Ok(Some(old_bid)) if old_bid.bid != ban_id_val => {
let incoming_bid = BanId::read(conn, ban_id_val)?;
@ -54,14 +59,22 @@ impl UserBanId {
if incoming_bid.aliased_to.is_none() || incoming_bid.aliased_to.unwrap() != old_bid.bid {
return Self::overwriting_associate(conn, old_bid.bid, ban_id_val);
}
return Ok(old_bid);
Ok(old_bid)
},
//UserBanId found, but it's the same as the incoming one.
Ok(Some(k)) => return Ok(k),
Ok(Some(k)) => Ok(k),
//There wasn't any UBID attached to the user. Associate and move on.
Ok(None) => Self::simple_associate(conn, ban_id_val, user_id_val),
Ok(None) => {
//Check for an alias
let bid_read = BanId::read_opt(conn, ban_id_val)?;
if bid_read.is_some() && bid_read.as_ref().unwrap().aliased_to.is_some() {
Self::simple_associate(conn, bid_read.unwrap().aliased_to.unwrap(), user_id_val)
} else {
Self::simple_associate(conn, ban_id_val, user_id_val)
}
},
//Breaking error, bubble it up.
Err(e) => return Err(e),
Err(e) => Err(e),
}
}

6
server/migrations/2021-03-28-005913_create_ban_id_tables/up.sql

@ -1,11 +1,11 @@
create table hexbear.ban_id (
id serial primary key,
id uuid primary key default uuid_generate_v4(),
created timestamp not null default now(),
aliased_to int references hexbear.ban_id on update cascade on delete cascade
aliased_to uuid references hexbear.ban_id on update cascade on delete cascade
);
create table hexbear.user_ban_id (
bid int references hexbear.ban_id on update cascade on delete cascade,
bid uuid references hexbear.ban_id on update cascade on delete cascade,
uid int references user_ on update cascade on delete cascade,
primary key (bid, uid)
);

16
server/src/api/user.rs

@ -81,6 +81,7 @@ use crate::{
LemmyContext,
};
use lemmy_db::user_token::{UserToken, UserTokenForm};
use lemmy_db::user_ban_id::UserBanId;
#[async_trait::async_trait(?Send)]
impl Perform for SetUserTag {
@ -199,14 +200,9 @@ impl Perform for Login {
match &data.code_2fa {
Some(code) => match context.code_cache_2fa().check_2fa(&user, code) {
Ok(matches) => {
if matches {
let jwt = generate_token(context, user.id).await?;
return Ok(LoginResponse {
requires_2fa: false,
jwt: jwt.token_hash,
});
if !matches {
return Err(APIError::err("invalid_2fa_code").into());
}
return Err(APIError::err("invalid_2fa_code").into());
}
Err(e) => return Err(e),
},
@ -223,11 +219,15 @@ impl Perform for Login {
}
}
//get bid (if any)
let uid = user.id.clone();
let bid = blocking(&context.pool, move |conn| UserBanId::get_by_user(conn, uid)).await??.map_or("".to_string(), |ubid| ubid.bid.to_string());
// Return the jwt
let jwt = generate_token(context, user.id).await?;
Ok(LoginResponse {
requires_2fa: false,
jwt: jwt.token_hash,
jwt: format!("{}{}", jwt.token_hash, bid),
})
}
}

Loading…
Cancel
Save