Skip to main content

openauth_core/options/
user.rs

1use std::collections::BTreeMap;
2
3use crate::db::{DbFieldType, DbValue};
4
5/// User lifecycle configuration.
6#[derive(Debug, Clone, Default, PartialEq)]
7pub struct UserOptions {
8    pub change_email: ChangeEmailOptions,
9    pub delete_user: DeleteUserOptions,
10    pub additional_fields: BTreeMap<String, UserAdditionalField>,
11}
12
13impl UserOptions {
14    pub fn new() -> Self {
15        Self::default()
16    }
17
18    pub fn builder() -> Self {
19        Self::new()
20    }
21
22    #[must_use]
23    pub fn change_email(mut self, change_email: ChangeEmailOptions) -> Self {
24        self.change_email = change_email;
25        self
26    }
27
28    #[must_use]
29    pub fn delete_user(mut self, delete_user: DeleteUserOptions) -> Self {
30        self.delete_user = delete_user;
31        self
32    }
33
34    #[must_use]
35    pub fn additional_field(mut self, name: impl Into<String>, field: UserAdditionalField) -> Self {
36        self.additional_fields.insert(name.into(), field);
37        self
38    }
39}
40
41/// Runtime metadata for custom user fields accepted by user-writing endpoints.
42#[derive(Debug, Clone, PartialEq)]
43pub struct UserAdditionalField {
44    pub field_type: DbFieldType,
45    pub required: bool,
46    pub input: bool,
47    pub returned: bool,
48    pub default_value: Option<DbValue>,
49    pub db_name: Option<String>,
50}
51
52impl UserAdditionalField {
53    pub fn new(field_type: DbFieldType) -> Self {
54        Self {
55            field_type,
56            required: true,
57            input: true,
58            returned: true,
59            default_value: None,
60            db_name: None,
61        }
62    }
63
64    #[must_use]
65    pub fn optional(mut self) -> Self {
66        self.required = false;
67        self
68    }
69
70    #[must_use]
71    pub fn generated(mut self) -> Self {
72        self.input = false;
73        self
74    }
75
76    #[must_use]
77    pub fn hidden(mut self) -> Self {
78        self.returned = false;
79        self
80    }
81
82    #[must_use]
83    pub fn default_value(mut self, value: DbValue) -> Self {
84        self.default_value = Some(value);
85        self
86    }
87
88    #[must_use]
89    pub fn db_name(mut self, db_name: impl Into<String>) -> Self {
90        self.db_name = Some(db_name.into());
91        self
92    }
93}
94
95/// Email change behavior.
96#[derive(Debug, Clone, Default, PartialEq, Eq)]
97pub struct ChangeEmailOptions {
98    pub enabled: bool,
99    pub update_email_without_verification: bool,
100}
101
102impl ChangeEmailOptions {
103    pub fn new() -> Self {
104        Self::default()
105    }
106
107    pub fn builder() -> Self {
108        Self::new()
109    }
110
111    #[must_use]
112    pub fn enabled(mut self, enabled: bool) -> Self {
113        self.enabled = enabled;
114        self
115    }
116
117    #[must_use]
118    pub fn update_email_without_verification(mut self, enabled: bool) -> Self {
119        self.update_email_without_verification = enabled;
120        self
121    }
122}
123
124/// User deletion behavior.
125#[derive(Debug, Clone, Default, PartialEq, Eq)]
126pub struct DeleteUserOptions {
127    pub enabled: bool,
128}
129
130impl DeleteUserOptions {
131    pub fn new() -> Self {
132        Self::default()
133    }
134
135    pub fn builder() -> Self {
136        Self::new()
137    }
138
139    #[must_use]
140    pub fn enabled(mut self, enabled: bool) -> Self {
141        self.enabled = enabled;
142        self
143    }
144}