qm_customer/model/
user.rs

1use async_graphql::{Enum, InputObject, SimpleObject};
2use qm_entity::ids::{InfraContext, InstitutionId, PartialEqual};
3use sqlx::types::Uuid;
4use sqlx::FromRow;
5use std::collections::{HashMap, HashSet};
6use std::sync::Arc;
7
8use super::GroupDetail;
9
10#[derive(Debug, serde::Deserialize)]
11pub struct UserEntityUpdate {
12    pub id: Arc<str>,
13    pub username: Arc<str>,
14    pub email: Option<Arc<str>>,
15    pub first_name: Option<Arc<str>>,
16    pub last_name: Option<Arc<str>>,
17    pub realm_id: Option<Arc<str>>,
18    pub enabled: bool,
19}
20
21impl UserEntityUpdate {
22    pub fn has_all_fields(&self) -> bool {
23        self.email.is_some() && self.first_name.is_some() && self.last_name.is_some()
24    }
25}
26
27pub struct TmpUser {
28    pub id: Arc<str>,
29    pub username: Arc<str>,
30    pub email: Arc<str>,
31    pub firstname: Arc<str>,
32    pub lastname: Arc<str>,
33    pub groups: HashSet<Arc<str>>,
34    pub roles: HashSet<Arc<str>>,
35    pub enabled: bool,
36}
37pub type TmpUserMap = HashMap<Arc<str>, TmpUser>;
38
39#[derive(Debug, FromRow)]
40pub struct KcUserQuery {
41    pub id: Option<String>,
42    pub firstname: Option<String>,
43    pub lastname: Option<String>,
44    pub username: Option<String>,
45    pub email: Option<String>,
46    pub enabled: bool,
47}
48
49impl KcUserQuery {
50    pub fn has_all_fields(&self) -> bool {
51        [
52            self.id.as_ref(),
53            self.firstname.as_ref(),
54            self.lastname.as_ref(),
55            self.username.as_ref(),
56            self.email.as_ref(),
57        ]
58        .iter()
59        .all(Option::is_some)
60    }
61}
62
63#[derive(Debug, FromRow)]
64pub struct KcUserGroupQuery {
65    pub user_id: Option<String>,
66    pub group_id: Option<String>,
67}
68
69impl KcUserGroupQuery {
70    pub fn has_all_fields(&self) -> bool {
71        [self.group_id.as_ref(), self.user_id.as_ref()]
72            .iter()
73            .all(Option::is_some)
74    }
75}
76
77#[derive(Debug, FromRow)]
78pub struct KcUserRoleQuery {
79    pub user_id: Option<String>,
80    pub role_id: Option<String>,
81}
82
83impl KcUserRoleQuery {
84    pub fn has_all_fields(&self) -> bool {
85        [self.user_id.as_ref(), self.role_id.as_ref()]
86            .iter()
87            .all(Option::is_some)
88    }
89}
90
91#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, Enum, Copy, Eq, PartialEq)]
92pub enum QmRequiredUserAction {
93    #[graphql(name = "VERIFY_EMAIL")]
94    VerifyEmail,
95    #[graphql(name = "UPDATE_PROFILE")]
96    UpdateProfile,
97    #[graphql(name = "CONFIGURE_TOTP")]
98    ConfigureTotp,
99    #[graphql(name = "UPDATE_PASSWORD")]
100    UpdatePassword,
101    #[graphql(name = "TERMS_AND_CONDITIONS")]
102    TermsAndConditions,
103}
104
105impl std::fmt::Display for QmRequiredUserAction {
106    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107        let str = match self {
108            QmRequiredUserAction::VerifyEmail => "VERIFY_EMAIL",
109            QmRequiredUserAction::UpdateProfile => "UPDATE_PROFILE",
110            QmRequiredUserAction::ConfigureTotp => "CONFIGURE_TOTP",
111            QmRequiredUserAction::UpdatePassword => "UPDATE_PASSWORD",
112            QmRequiredUserAction::TermsAndConditions => "TERMS_AND_CONDITIONS",
113        }
114        .to_string();
115        write!(f, "{}", str)
116    }
117}
118
119#[derive(Default, serde::Deserialize, serde::Serialize, Debug, Clone, InputObject)]
120#[serde(rename_all = "camelCase")]
121pub struct QmCreateUserInput {
122    pub username: String,
123    pub firstname: String,
124    pub lastname: String,
125    pub password: String,
126    pub email: String,
127    pub phone: Option<String>,
128    pub salutation: Option<String>,
129    pub room_number: Option<String>,
130    pub job_title: Option<String>,
131    pub enabled: Option<bool>,
132    pub required_actions: Option<Vec<QmRequiredUserAction>>,
133}
134
135#[derive(Debug)]
136pub struct CreateUserPayload {
137    pub user: QmCreateUserInput,
138    pub group_id: Option<String>,
139    pub access: Option<String>,
140    pub context: Option<InfraContext>,
141}
142
143#[derive(Debug, Clone, SimpleObject)]
144pub struct QmUser {
145    pub id: Arc<str>,
146    pub username: Arc<str>,
147    pub email: Arc<str>,
148    pub firstname: Arc<str>,
149    pub lastname: Arc<str>,
150    pub enabled: bool,
151}
152
153pub type UserMap = HashMap<Arc<str>, Arc<QmUser>>;
154pub type UserUidMap = HashMap<Uuid, Arc<QmUser>>;
155pub type UserGroupMap = HashMap<Arc<str>, HashSet<Arc<str>>>;
156pub type UserRoleMap = HashMap<Arc<str>, HashSet<Arc<str>>>;
157
158#[derive(Debug, serde::Deserialize)]
159pub struct UserRoleMappingUpdate {
160    pub role_id: Arc<str>,
161    pub user_id: Arc<str>,
162}
163
164#[derive(Debug, serde::Deserialize)]
165pub struct UserGroupMembershipUpdate {
166    pub group_id: Arc<str>,
167    pub user_id: Arc<str>,
168}
169
170#[derive(Debug)]
171pub struct UserGroupMembership {
172    pub group_id: Arc<str>,
173    pub user_id: Arc<str>,
174}
175
176#[derive(Debug, Clone, SimpleObject)]
177pub struct QmUserList {
178    pub items: Arc<[QmUserDetails]>,
179    pub limit: Option<i64>,
180    pub total: Option<i64>,
181    pub page: Option<i64>,
182}
183
184#[derive(Debug, Clone, SimpleObject)]
185#[graphql(complex)]
186pub struct QmUserDetails {
187    #[graphql(flatten)]
188    pub user: Arc<QmUser>,
189    #[graphql(skip)]
190    pub context: Option<InfraContext>,
191    #[graphql(skip)]
192    pub access: Option<qm_role::Access>,
193    #[graphql(skip)]
194    pub group: Option<Arc<GroupDetail>>,
195}
196
197impl PartialEqual<'_, InfraContext> for QmUserDetails {
198    fn partial_equal(&'_ self, r: &'_ InfraContext) -> bool {
199        if let Some(context) = self.context.as_ref() {
200            match r {
201                InfraContext::Customer(v) => context.has_customer(v),
202                InfraContext::Organization(v) => context.has_organization(v),
203                InfraContext::Institution(v) => context.has_institution(v),
204            }
205        } else {
206            false
207        }
208    }
209}
210
211impl PartialEqual<'_, InstitutionId> for QmUserDetails {
212    fn partial_equal(&'_ self, r: &'_ InstitutionId) -> bool {
213        if let Some(context) = self.context.as_ref() {
214            context.has_institution(r)
215        } else {
216            false
217        }
218    }
219}
220
221#[derive(Debug, Clone)]
222pub struct UserGroup {
223    pub group_id: Arc<str>,
224    pub group_detail: Arc<GroupDetail>,
225}