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

feat(users): Add profile level invites #5793

Merged
merged 8 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions crates/api_models/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ pub struct AuthorizeResponse {
//this field is added for audit/debug reasons
#[serde(skip_serializing)]
pub user_id: String,
//this field is added for audit/debug reasons
#[serde(skip_serializing)]
pub merchant_id: id_type::MerchantId,
}

#[derive(serde::Deserialize, Debug, serde::Serialize)]
Expand Down Expand Up @@ -209,7 +206,6 @@ pub struct VerifyTokenResponse {
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct UpdateUserAccountDetailsRequest {
pub name: Option<Secret<String>>,
pub preferred_merchant_id: Option<id_type::MerchantId>,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
Expand Down
9 changes: 9 additions & 0 deletions crates/api_models/src/user_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,12 @@ pub struct ListInvitationForUserResponse {
pub entity_name: Option<Secret<String>>,
pub role_id: String,
}

pub type AcceptInvitationsV2Request = Vec<Entity>;
pub type AcceptInvitationsPreAuthRequest = Vec<Entity>;

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Entity {
pub entity_id: String,
pub entity_type: common_enums::EntityType,
}
2 changes: 0 additions & 2 deletions crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,8 +1333,6 @@ diesel::table! {
is_verified -> Bool,
created_at -> Timestamp,
last_modified_at -> Timestamp,
#[max_length = 64]
preferred_merchant_id -> Nullable<Varchar>,
totp_status -> TotpStatus,
totp_secret -> Nullable<Bytea>,
totp_recovery_codes -> Nullable<Array<Nullable<Text>>>,
Expand Down
2 changes: 0 additions & 2 deletions crates/diesel_models/src/schema_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,6 @@ diesel::table! {
is_verified -> Bool,
created_at -> Timestamp,
last_modified_at -> Timestamp,
#[max_length = 64]
preferred_merchant_id -> Nullable<Varchar>,
totp_status -> TotpStatus,
totp_secret -> Nullable<Bytea>,
totp_recovery_codes -> Nullable<Array<Nullable<Text>>>,
Expand Down
14 changes: 1 addition & 13 deletions crates/diesel_models/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub struct User {
pub is_verified: bool,
pub created_at: PrimitiveDateTime,
pub last_modified_at: PrimitiveDateTime,
pub preferred_merchant_id: Option<common_utils::id_type::MerchantId>,
pub totp_status: TotpStatus,
pub totp_secret: Option<Encryption>,
#[diesel(deserialize_as = OptionalDieselArray<Secret<String>>)]
Expand All @@ -38,7 +37,6 @@ pub struct UserNew {
pub is_verified: bool,
pub created_at: Option<PrimitiveDateTime>,
pub last_modified_at: Option<PrimitiveDateTime>,
pub preferred_merchant_id: Option<common_utils::id_type::MerchantId>,
pub totp_status: TotpStatus,
pub totp_secret: Option<Encryption>,
pub totp_recovery_codes: Option<Vec<Secret<String>>>,
Expand All @@ -52,7 +50,6 @@ pub struct UserUpdateInternal {
password: Option<Secret<String>>,
is_verified: Option<bool>,
last_modified_at: PrimitiveDateTime,
preferred_merchant_id: Option<common_utils::id_type::MerchantId>,
totp_status: Option<TotpStatus>,
totp_secret: Option<Encryption>,
totp_recovery_codes: Option<Vec<Secret<String>>>,
Expand All @@ -65,7 +62,6 @@ pub enum UserUpdate {
AccountUpdate {
name: Option<String>,
is_verified: Option<bool>,
preferred_merchant_id: Option<common_utils::id_type::MerchantId>,
},
TotpUpdate {
totp_status: Option<TotpStatus>,
Expand All @@ -86,22 +82,16 @@ impl From<UserUpdate> for UserUpdateInternal {
password: None,
is_verified: Some(true),
last_modified_at,
preferred_merchant_id: None,
totp_status: None,
totp_secret: None,
totp_recovery_codes: None,
last_password_modified_at: None,
},
UserUpdate::AccountUpdate {
name,
is_verified,
preferred_merchant_id,
} => Self {
UserUpdate::AccountUpdate { name, is_verified } => Self {
name,
password: None,
is_verified,
last_modified_at,
preferred_merchant_id,
totp_status: None,
totp_secret: None,
totp_recovery_codes: None,
Expand All @@ -116,7 +106,6 @@ impl From<UserUpdate> for UserUpdateInternal {
password: None,
is_verified: None,
last_modified_at,
preferred_merchant_id: None,
totp_status,
totp_secret,
totp_recovery_codes,
Expand All @@ -127,7 +116,6 @@ impl From<UserUpdate> for UserUpdateInternal {
password: Some(password),
is_verified: None,
last_modified_at,
preferred_merchant_id: None,
last_password_modified_at: Some(last_modified_at),
totp_status: None,
totp_secret: None,
Expand Down
71 changes: 36 additions & 35 deletions crates/diesel_models/src/user_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,53 @@ pub struct UserRole {
pub version: enums::UserRoleVersion,
}

pub fn get_entity_id_and_type(user_role: &UserRole) -> (Option<String>, Option<EntityType>) {
match (user_role.version, user_role.role_id.as_str()) {
(enums::UserRoleVersion::V1, consts::ROLE_ID_ORGANIZATION_ADMIN) => (
user_role
.org_id
.clone()
.map(|org_id| org_id.get_string_repr().to_string()),
Some(EntityType::Organization),
),
(enums::UserRoleVersion::V1, consts::ROLE_ID_INTERNAL_VIEW_ONLY_USER)
| (enums::UserRoleVersion::V1, consts::ROLE_ID_INTERNAL_ADMIN) => (
user_role
.merchant_id
.clone()
.map(|merchant_id| merchant_id.get_string_repr().to_string()),
Some(EntityType::Internal),
),
(enums::UserRoleVersion::V1, _) => (
user_role
.merchant_id
.clone()
.map(|merchant_id| merchant_id.get_string_repr().to_string()),
Some(EntityType::Merchant),
),
(enums::UserRoleVersion::V2, _) => (user_role.entity_id.clone(), user_role.entity_type),
impl UserRole {
pub fn get_entity_id_and_type(&self) -> Option<(String, EntityType)> {
match (self.version, self.role_id.as_str()) {
(enums::UserRoleVersion::V1, consts::ROLE_ID_ORGANIZATION_ADMIN) => {
let org_id = self.org_id.clone()?.get_string_repr().to_string();
Some((org_id, EntityType::Organization))
}
(enums::UserRoleVersion::V1, consts::ROLE_ID_INTERNAL_VIEW_ONLY_USER)
| (enums::UserRoleVersion::V1, consts::ROLE_ID_INTERNAL_ADMIN) => {
let merchant_id = self.merchant_id.clone()?.get_string_repr().to_string();
Some((merchant_id, EntityType::Internal))
}
(enums::UserRoleVersion::V1, _) => {
let merchant_id = self.merchant_id.clone()?.get_string_repr().to_string();
Some((merchant_id, EntityType::Merchant))
}
(enums::UserRoleVersion::V2, _) => self.entity_id.clone().zip(self.entity_type),
}
}
}

impl Hash for UserRole {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let (entity_id, entity_type) = get_entity_id_and_type(self);

self.user_id.hash(state);
entity_id.hash(state);
entity_type.hash(state);
if let Some((entity_id, entity_type)) = self.get_entity_id_and_type() {
entity_id.hash(state);
entity_type.hash(state);
}
}
}

impl PartialEq for UserRole {
fn eq(&self, other: &Self) -> bool {
let (self_entity_id, self_entity_type) = get_entity_id_and_type(self);
let (other_entity_id, other_entity_type) = get_entity_id_and_type(other);

self.user_id == other.user_id
&& self_entity_id == other_entity_id
&& self_entity_type == other_entity_type
match (
self.get_entity_id_and_type(),
other.get_entity_id_and_type(),
) {
(
Some((self_entity_id, self_entity_type)),
Some((other_entity_id, other_entity_type)),
) => {
self.user_id == other.user_id
&& self_entity_id == other_entity_id
&& self_entity_type == other_entity_type
}
_ => self.user_id == other.user_id,
}
}
}

Expand Down
Loading
Loading