refactor
This commit is contained in:
@@ -26,7 +26,7 @@ use papercrate::{
|
||||
},
|
||||
storage::{ObjectStorage, S3Storage, TenantStorage},
|
||||
tenants::TenantService,
|
||||
utils::tracing::init_tracing,
|
||||
utils::{text::normalize_identifier, tracing::init_tracing},
|
||||
};
|
||||
|
||||
#[derive(Parser)]
|
||||
@@ -162,20 +162,26 @@ async fn main() -> Result<()> {
|
||||
}
|
||||
|
||||
fn create_user(pool: &PgPool, username: &str) -> Result<()> {
|
||||
if username.trim().is_empty() {
|
||||
bail!("username must not be empty");
|
||||
}
|
||||
let username = normalize_identifier(
|
||||
username,
|
||||
100,
|
||||
"username must not be empty",
|
||||
"username must not exceed 100 characters",
|
||||
Some("username may only contain printable characters"),
|
||||
|ch| !ch.is_control(),
|
||||
)
|
||||
.map_err(|err| anyhow!("{:?}", err))?;
|
||||
|
||||
let mut conn = pool.get().context("failed to get database connection")?;
|
||||
let exists: bool =
|
||||
select(exists(users::table.filter(users::username.eq(username)))).get_result(&mut conn)?;
|
||||
select(exists(users::table.filter(users::username.eq(&username)))).get_result(&mut conn)?;
|
||||
if exists {
|
||||
bail!("user '{}' already exists", username);
|
||||
}
|
||||
|
||||
let new_user = NewUser {
|
||||
id: Uuid::new_v4(),
|
||||
username: username.to_string(),
|
||||
username: username.clone(),
|
||||
};
|
||||
|
||||
diesel::insert_into(users::table)
|
||||
|
||||
Reference in New Issue
Block a user