Skip to main content

haste_repository/types/
user.rs

1use haste_fhir_model::r4::generated::terminology::{BoundCode, UserRole as FHIRUserRole};
2use haste_jwt::TenantId;
3use serde::{Deserialize, Serialize};
4
5#[derive(sqlx::FromRow, Debug, Deserialize, Serialize)]
6pub struct User {
7    pub id: String,
8    pub tenant: TenantId,
9    pub email: Option<String>,
10    pub role: UserRole,
11    pub method: AuthMethod,
12    pub provider_id: Option<String>,
13    /// The address accepted the account. Password login requires it.
14    #[serde(default)]
15    pub email_verified: bool,
16}
17
18pub struct UpdateUser {
19    pub id: String,
20    pub email: Option<String>,
21    pub role: Option<UserRole>,
22    pub method: Option<AuthMethod>,
23    pub provider_id: Option<String>,
24    pub password: Option<String>,
25    pub email_verified: Option<bool>,
26}
27
28pub enum LoginMethod {
29    OIDC { email: String, provider_id: String },
30    EmailPassword { email: String, password: String },
31}
32
33pub enum LoginResult {
34    Success { user: User },
35    Failure,
36}
37
38pub struct UserSearchClauses {
39    pub email: Option<String>,
40    pub role: Option<UserRole>,
41    pub method: Option<AuthMethod>,
42}
43
44pub struct CreateUser {
45    pub id: String,
46    pub email: Option<String>,
47    pub role: UserRole,
48    pub method: AuthMethod,
49    pub provider_id: Option<String>,
50    pub password: Option<String>,
51    pub email_verified: bool,
52}
53
54#[derive(Clone, Debug, PartialEq, PartialOrd, sqlx::Type, serde::Deserialize, serde::Serialize)]
55#[sqlx(type_name = "auth_method", rename_all = "lowercase")] // only for PostgreSQL to match a type definition
56pub enum AuthMethod {
57    #[sqlx(rename = "email-password")]
58    EmailPassword,
59    #[sqlx(rename = "oidc-provider")]
60    OIDC,
61}
62
63#[derive(Clone, Debug, PartialEq, PartialOrd, sqlx::Type, serde::Deserialize, serde::Serialize)]
64#[sqlx(type_name = "user_role", rename_all = "lowercase")] // only for PostgreSQL to match a type definition
65#[serde(rename_all = "lowercase")]
66pub enum UserRole {
67    Owner,
68    Admin,
69    Member,
70}
71
72impl From<BoundCode<FHIRUserRole>> for UserRole {
73    fn from(role: BoundCode<FHIRUserRole>) -> Self {
74        if role == FHIRUserRole::owner() {
75            UserRole::Owner
76        } else if role == FHIRUserRole::admin() {
77            UserRole::Admin
78        } else {
79            UserRole::Member
80        }
81    }
82}