Skip to content

Commit 227c274

Browse files
authored
feat(users): Add email domain based restriction for dashboard entry APIs (#6940)
1 parent 3eb2eb1 commit 227c274

File tree

14 files changed

+322
-52
lines changed

14 files changed

+322
-52
lines changed

crates/api_models/src/user.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,18 +305,26 @@ pub struct CreateUserAuthenticationMethodRequest {
305305
pub owner_type: common_enums::Owner,
306306
pub auth_method: AuthConfig,
307307
pub allow_signup: bool,
308+
pub email_domain: Option<String>,
308309
}
309310

310311
#[derive(Debug, serde::Deserialize, serde::Serialize)]
311-
pub struct UpdateUserAuthenticationMethodRequest {
312-
pub id: String,
313-
// TODO: When adding more fields make config and new fields option
314-
pub auth_method: AuthConfig,
312+
#[serde(rename_all = "snake_case")]
313+
pub enum UpdateUserAuthenticationMethodRequest {
314+
AuthMethod {
315+
id: String,
316+
auth_config: AuthConfig,
317+
},
318+
EmailDomain {
319+
owner_id: String,
320+
email_domain: String,
321+
},
315322
}
316323

317324
#[derive(Debug, serde::Deserialize, serde::Serialize)]
318325
pub struct GetUserAuthenticationMethodsRequest {
319-
pub auth_id: String,
326+
pub auth_id: Option<String>,
327+
pub email_domain: Option<String>,
320328
}
321329

322330
#[derive(Debug, serde::Deserialize, serde::Serialize)]

crates/diesel_models/src/query/user_authentication_method.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,18 @@ impl UserAuthenticationMethod {
6464
)
6565
.await
6666
}
67+
68+
pub async fn list_user_authentication_methods_for_email_domain(
69+
conn: &PgPooledConn,
70+
email_domain: &str,
71+
) -> StorageResult<Vec<Self>> {
72+
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
73+
conn,
74+
dsl::email_domain.eq(email_domain.to_owned()),
75+
None,
76+
None,
77+
Some(dsl::last_modified_at.asc()),
78+
)
79+
.await
80+
}
6781
}

crates/diesel_models/src/schema.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,8 @@ diesel::table! {
14051405
allow_signup -> Bool,
14061406
created_at -> Timestamp,
14071407
last_modified_at -> Timestamp,
1408+
#[max_length = 64]
1409+
email_domain -> Varchar,
14081410
}
14091411
}
14101412

crates/diesel_models/src/schema_v2.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,8 @@ diesel::table! {
13521352
allow_signup -> Bool,
13531353
created_at -> Timestamp,
13541354
last_modified_at -> Timestamp,
1355+
#[max_length = 64]
1356+
email_domain -> Varchar,
13551357
}
13561358
}
13571359

crates/diesel_models/src/user_authentication_method.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct UserAuthenticationMethod {
1717
pub allow_signup: bool,
1818
pub created_at: PrimitiveDateTime,
1919
pub last_modified_at: PrimitiveDateTime,
20+
pub email_domain: String,
2021
}
2122

2223
#[derive(router_derive::Setter, Clone, Debug, Insertable, router_derive::DebugAsDisplay)]
@@ -32,6 +33,7 @@ pub struct UserAuthenticationMethodNew {
3233
pub allow_signup: bool,
3334
pub created_at: PrimitiveDateTime,
3435
pub last_modified_at: PrimitiveDateTime,
36+
pub email_domain: String,
3537
}
3638

3739
#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)]
@@ -40,13 +42,17 @@ pub struct OrgAuthenticationMethodUpdateInternal {
4042
pub private_config: Option<Encryption>,
4143
pub public_config: Option<serde_json::Value>,
4244
pub last_modified_at: PrimitiveDateTime,
45+
pub email_domain: Option<String>,
4346
}
4447

4548
pub enum UserAuthenticationMethodUpdate {
4649
UpdateConfig {
4750
private_config: Option<Encryption>,
4851
public_config: Option<serde_json::Value>,
4952
},
53+
EmailDomain {
54+
email_domain: String,
55+
},
5056
}
5157

5258
impl From<UserAuthenticationMethodUpdate> for OrgAuthenticationMethodUpdateInternal {
@@ -60,6 +66,13 @@ impl From<UserAuthenticationMethodUpdate> for OrgAuthenticationMethodUpdateInter
6066
private_config,
6167
public_config,
6268
last_modified_at,
69+
email_domain: None,
70+
},
71+
UserAuthenticationMethodUpdate::EmailDomain { email_domain } => Self {
72+
private_config: None,
73+
public_config: None,
74+
last_modified_at,
75+
email_domain: Some(email_domain),
6376
},
6477
}
6578
}

crates/router/src/core/errors/user.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ pub enum UserErrors {
108108
InvalidThemeLineage(String),
109109
#[error("Missing required field: email_config")]
110110
MissingEmailConfig,
111+
#[error("Invalid Auth Method Operation: {0}")]
112+
InvalidAuthMethodOperationWithMessage(String),
111113
}
112114

113115
impl common_utils::errors::ErrorSwitch<api_models::errors::types::ApiErrorResponse> for UserErrors {
@@ -280,6 +282,9 @@ impl common_utils::errors::ErrorSwitch<api_models::errors::types::ApiErrorRespon
280282
Self::MissingEmailConfig => {
281283
AER::BadRequest(ApiError::new(sub_code, 56, self.get_error_message(), None))
282284
}
285+
Self::InvalidAuthMethodOperationWithMessage(_) => {
286+
AER::BadRequest(ApiError::new(sub_code, 57, self.get_error_message(), None))
287+
}
283288
}
284289
}
285290
}
@@ -347,6 +352,9 @@ impl UserErrors {
347352
format!("Invalid field: {} in lineage", field_name)
348353
}
349354
Self::MissingEmailConfig => "Missing required field: email_config".to_string(),
355+
Self::InvalidAuthMethodOperationWithMessage(operation) => {
356+
format!("Invalid Auth Method Operation: {}", operation)
357+
}
350358
}
351359
}
352360
}

0 commit comments

Comments
 (0)