Skip to content

Commit

Permalink
feat(users): add support for tenant level users (#6708)
Browse files Browse the repository at this point in the history
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
  • Loading branch information
apoorvdixit88 and hyperswitch-bot[bot] authored Dec 6, 2024
1 parent b5d3d49 commit 357e8a0
Show file tree
Hide file tree
Showing 22 changed files with 701 additions and 128 deletions.
20 changes: 11 additions & 9 deletions crates/api_models/src/events/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ use crate::user::{
},
AcceptInviteFromEmailRequest, AuthSelectRequest, AuthorizeResponse, BeginTotpResponse,
ChangePasswordRequest, ConnectAccountRequest, CreateInternalUserRequest,
CreateUserAuthenticationMethodRequest, ForgotPasswordRequest, GetSsoAuthUrlRequest,
GetUserAuthenticationMethodsRequest, GetUserDetailsResponse, GetUserRoleDetailsRequest,
GetUserRoleDetailsResponseV2, InviteUserRequest, ReInviteUserRequest, RecoveryCodes,
ResetPasswordRequest, RotatePasswordRequest, SendVerifyEmailRequest, SignUpRequest,
SignUpWithMerchantIdRequest, SsoSignInRequest, SwitchMerchantRequest,
SwitchOrganizationRequest, SwitchProfileRequest, TokenResponse, TwoFactorAuthStatusResponse,
TwoFactorStatus, UpdateUserAccountDetailsRequest, UpdateUserAuthenticationMethodRequest,
UserFromEmailRequest, UserMerchantCreate, VerifyEmailRequest, VerifyRecoveryCodeRequest,
VerifyTotpRequest,
CreateTenantUserRequest, CreateUserAuthenticationMethodRequest, ForgotPasswordRequest,
GetSsoAuthUrlRequest, GetUserAuthenticationMethodsRequest, GetUserDetailsResponse,
GetUserRoleDetailsRequest, GetUserRoleDetailsResponseV2, InviteUserRequest,
ReInviteUserRequest, RecoveryCodes, ResetPasswordRequest, RotatePasswordRequest,
SendVerifyEmailRequest, SignUpRequest, SignUpWithMerchantIdRequest, SsoSignInRequest,
SwitchMerchantRequest, SwitchOrganizationRequest, SwitchProfileRequest, TokenResponse,
TwoFactorAuthStatusResponse, TwoFactorStatus, UpdateUserAccountDetailsRequest,
UpdateUserAuthenticationMethodRequest, UserFromEmailRequest, UserMerchantCreate,
UserOrgMerchantCreateRequest, VerifyEmailRequest, VerifyRecoveryCodeRequest, VerifyTotpRequest,
};

common_utils::impl_api_event_type!(
Expand All @@ -37,6 +37,8 @@ common_utils::impl_api_event_type!(
SwitchMerchantRequest,
SwitchProfileRequest,
CreateInternalUserRequest,
CreateTenantUserRequest,
UserOrgMerchantCreateRequest,
UserMerchantCreate,
AuthorizeResponse,
ConnectAccountRequest,
Expand Down
15 changes: 15 additions & 0 deletions crates/api_models/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ pub struct CreateInternalUserRequest {
pub password: Secret<String>,
}

#[derive(serde::Deserialize, Debug, serde::Serialize)]
pub struct CreateTenantUserRequest {
pub name: Secret<String>,
pub email: pii::Email,
pub password: Secret<String>,
}

#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub struct UserOrgMerchantCreateRequest {
pub organization_name: Secret<String>,
pub organization_details: Option<pii::SecretSerdeValue>,
pub metadata: Option<pii::SecretSerdeValue>,
pub merchant_name: Secret<String>,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct UserMerchantCreate {
pub company_name: String,
Expand Down
1 change: 1 addition & 0 deletions crates/common_enums/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3207,6 +3207,7 @@ pub enum ApiVersion {
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum EntityType {
Tenant = 3,
Organization = 2,
Merchant = 1,
Profile = 0,
Expand Down
2 changes: 2 additions & 0 deletions crates/common_utils/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ pub const MAX_ALLOWED_MERCHANT_NAME_LENGTH: usize = 64;
/// Default locale
pub const DEFAULT_LOCALE: &str = "en";

/// Role ID for Tenant Admin
pub const ROLE_ID_TENANT_ADMIN: &str = "tenant_admin";
/// Role ID for Org Admin
pub const ROLE_ID_ORGANIZATION_ADMIN: &str = "org_admin";
/// Role ID for Internal View Only
Expand Down
20 changes: 20 additions & 0 deletions crates/diesel_models/src/query/merchant_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ impl MerchantAccount {
.await
}

pub async fn list_all_merchant_accounts(
conn: &PgPooledConn,
limit: u32,
offset: Option<u32>,
) -> StorageResult<Vec<Self>> {
generics::generic_filter::<
<Self as HasTable>::Table,
_,
<<Self as HasTable>::Table as Table>::PrimaryKey,
_,
>(
conn,
dsl_identifier.ne_all(vec![""]),
Some(i64::from(limit)),
offset.map(i64::from),
None,
)
.await
}

pub async fn update_all_merchant_accounts(
conn: &PgPooledConn,
merchant_account: MerchantAccountUpdateInternal,
Expand Down
6 changes: 6 additions & 0 deletions crates/router/src/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,9 @@ pub mod routes {
EntityType::Organization => Some(AuthInfo::OrgLevel {
org_id: user_role.org_id.clone()?,
}),
EntityType::Tenant => Some(AuthInfo::OrgLevel {
org_id: auth.org_id.clone(),
}),
})
})
.collect();
Expand Down Expand Up @@ -2054,6 +2057,9 @@ pub mod routes {
EntityType::Organization => Some(AuthInfo::OrgLevel {
org_id: user_role.org_id.clone()?,
}),
EntityType::Tenant => Some(AuthInfo::OrgLevel {
org_id: auth.org_id.clone(),
}),
})
})
.collect();
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/consts/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub const TOTP_TOLERANCE: u8 = 1;
pub const TOTP_MAX_ATTEMPTS: u8 = 4;
/// Number of maximum attempts user has for recovery code
pub const RECOVERY_CODE_MAX_ATTEMPTS: u8 = 4;
/// The default number of organizations to fetch for a tenant-level user
pub const ORG_LIST_LIMIT_FOR_TENANT: u32 = 20;

pub const MAX_PASSWORD_LENGTH: usize = 70;
pub const MIN_PASSWORD_LENGTH: usize = 8;
Expand Down
Loading

0 comments on commit 357e8a0

Please sign in to comment.