refactor
This commit is contained in:
+15
-1
@@ -8,6 +8,7 @@ use crate::{
|
||||
jobs::{enqueue_job, JOB_PROVISION_TENANT},
|
||||
models::{Tenant, TenantStatus},
|
||||
schema::tenants::dsl,
|
||||
utils::text::normalize_identifier,
|
||||
};
|
||||
|
||||
pub struct TenantRepository;
|
||||
@@ -96,6 +97,8 @@ impl TenantService {
|
||||
return Err(AppError::bad_request("tenant name must not be empty"));
|
||||
}
|
||||
|
||||
let name = normalize_tenant_name(name)?;
|
||||
|
||||
let id = Uuid::new_v4();
|
||||
let storage_root = normalize_storage_root(storage_root, id);
|
||||
let quickwit_index = normalize_quickwit_index(quickwit_index, id);
|
||||
@@ -103,7 +106,7 @@ impl TenantService {
|
||||
diesel::insert_into(dsl::tenants)
|
||||
.values((
|
||||
dsl::id.eq(id),
|
||||
dsl::name.eq(name),
|
||||
dsl::name.eq(&name),
|
||||
dsl::storage_root.eq(Some(storage_root.clone())),
|
||||
dsl::quickwit_index.eq(Some(quickwit_index.clone())),
|
||||
dsl::config.eq(json!({})),
|
||||
@@ -186,6 +189,17 @@ pub fn apply_api_token_prefix(conn: &mut PgConnection, prefix: &str) -> AppResul
|
||||
.map_err(AppError::from)
|
||||
}
|
||||
|
||||
fn normalize_tenant_name(value: &str) -> AppResult<String> {
|
||||
normalize_identifier(
|
||||
value,
|
||||
255,
|
||||
"tenant name must not be empty",
|
||||
"tenant name must not exceed 255 characters",
|
||||
Some("tenant name may only contain printable characters"),
|
||||
|ch| !ch.is_control(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn clear_api_token_prefix(conn: &mut PgConnection) -> AppResult<()> {
|
||||
diesel::sql_query("SELECT set_config('papercrate.api_token_prefix', '', false)")
|
||||
.execute(conn)
|
||||
|
||||
Reference in New Issue
Block a user