Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make account names case-insensitive #407

Merged
merged 15 commits into from
Nov 11, 2023
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions crates/kitsune-core/src/service/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use futures_util::{Stream, TryStreamExt};
use garde::Validate;
use iso8601_timestamp::Timestamp;
use kitsune_db::{
function::lower,
model::{
account::{Account, UpdateAccount},
follower::Follow as DbFollow,
Expand Down Expand Up @@ -301,8 +302,8 @@ impl AccountService {
async move {
accounts::table
.filter(
accounts::username
.eq(get_user.username)
lower(accounts::username)
.eq(lower(get_user.username))
.and(accounts::domain.eq(domain)),
)
.select(Account::as_select())
Expand Down Expand Up @@ -343,8 +344,8 @@ impl AccountService {
async move {
accounts::table
.filter(
accounts::username
.eq(get_user.username)
lower(accounts::username)
.eq(lower(get_user.username))
.and(accounts::local.eq(true)),
)
.select(Account::as_select())
Expand Down Expand Up @@ -674,27 +675,31 @@ impl AccountService {
..changeset
};
}

if let Some(ref mut note) = update.note {
note.clean_html();
changeset = UpdateAccount {
note: Some(note),
..changeset
};
}

if let Some(avatar) = update.avatar {
let media_attachment = self.attachment_service.upload(avatar).await?;
changeset = UpdateAccount {
avatar_id: Some(media_attachment.id),
..changeset
};
}

if let Some(header) = update.header {
let media_attachment = self.attachment_service.upload(header).await?;
changeset = UpdateAccount {
header_id: Some(media_attachment.id),
..changeset
};
}

if let Some(locked) = update.locked {
changeset = UpdateAccount {
locked: Some(locked),
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-core/src/service/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ mod test {
}

async fn handle(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok::<_, Infallible>(Response::new(Body::from("")))
Ok::<_, Infallible>(Response::new(Body::empty()))
}

async fn prepare_db(db_conn: &mut AsyncPgConnection) -> Uuid {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ CREATE TABLE accounts (
) STORED NOT NULL,

-- UNIQUE constraints
UNIQUE (username, domain)
UNIQUE (LOWER(username), domain)
aumetra marked this conversation as resolved.
Show resolved Hide resolved
);

CREATE TABLE accounts_follows (
Expand Down Expand Up @@ -88,7 +88,7 @@ CREATE TABLE users (
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

-- UNIQUE constraints
UNIQUE (username, domain),
UNIQUE (LOWER(username), domain),
UNIQUE (confirmation_token),

-- Foreign key constraints
Expand Down
5 changes: 5 additions & 0 deletions crates/kitsune-db/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ sql_function! {
fn iso_code_to_language(iso_code: LanguageIsoCode) -> RegConfig;
}

sql_function! {
/// Convert text to lowercase
fn lower(string: Text) -> Text;
}

sql_function! {
/// Return the current date with the timezone
fn now() -> Timestamptz;
Expand Down
13 changes: 8 additions & 5 deletions kitsune-cli/src/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use crate::Result;
use clap::{Args, Subcommand, ValueEnum};
use diesel::{BelongingToDsl, BoolExpressionMethods, ExpressionMethods, QueryDsl};
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use kitsune_db::model::{
user::User,
user_role::{NewUserRole, Role as DbRole, UserRole},
use kitsune_db::{
function::lower,
model::{
user::User,
user_role::{NewUserRole, Role as DbRole, UserRole},
},
};
use speedy_uuid::Uuid;

Expand Down Expand Up @@ -77,7 +80,7 @@ async fn list_roles(db_conn: &mut AsyncPgConnection, username_str: &str) -> Resu
use kitsune_db::schema::users;

let user: User = users::table
.filter(users::username.eq(username_str))
.filter(lower(users::username).eq(lower(username_str)))
.first(db_conn)
.await?;

Expand All @@ -101,7 +104,7 @@ async fn remove_role(
use kitsune_db::schema::{users, users_roles};

let user = users::table
.filter(users::username.eq(username_str))
.filter(lower(users::username).eq(lower(username_str)))
.first::<User>(db_conn)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions kitsune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ async-graphql-axum = { version = "6.0.9", optional = true }
kitsune-oidc = { path = "../crates/kitsune-oidc", optional = true }

[dev-dependencies]
deadpool-redis = "0.13.0"
kitsune-http-client = { path = "../crates/kitsune-http-client" }
kitsune-test = { path = "../crates/kitsune-test" }
pretty_assertions = "1.4.0"
serial_test = "2.0.0"
Expand Down
4 changes: 2 additions & 2 deletions kitsune/src/http/handler/oauth/authorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use axum_extra::{
use axum_flash::{Flash, IncomingFlashes};
use diesel::{ExpressionMethods, OptionalExtension, QueryDsl};
use diesel_async::RunQueryDsl;
use kitsune_db::{model::user::User, schema::users, PgPool};
use kitsune_db::{function::lower, model::user::User, schema::users, PgPool};
use oxide_auth_async::endpoint::authorization::AuthorizationFlow;
use oxide_auth_axum::{OAuthRequest, OAuthResponse};
use scoped_futures::ScopedFutureExt;
Expand Down Expand Up @@ -122,7 +122,7 @@ pub async fn post(
.with_connection(|db_conn| {
async move {
users::table
.filter(users::username.eq(form.username))
.filter(lower(users::username).eq(lower(form.username)))
.first::<User>(db_conn)
.await
.optional()
Expand Down
Loading
Loading