]>
Commit | Line | Data |
---|---|---|
2b7f8dd5 WB |
1 | use serde::{Deserialize, Serialize}; |
2 | ||
6ef1b649 WB |
3 | use proxmox_schema::{ |
4 | api, BooleanSchema, IntegerSchema, Schema, StringSchema, Updater, | |
b65dfff5 | 5 | }; |
2b7f8dd5 WB |
6 | |
7 | use super::{SINGLE_LINE_COMMENT_FORMAT, SINGLE_LINE_COMMENT_SCHEMA}; | |
8 | use super::userid::{Authid, Userid, PROXMOX_TOKEN_ID_SCHEMA}; | |
9 | ||
10 | pub const ENABLE_USER_SCHEMA: Schema = BooleanSchema::new( | |
11 | "Enable the account (default). You can set this to '0' to disable the account.") | |
12 | .default(true) | |
13 | .schema(); | |
14 | ||
15 | pub const EXPIRE_USER_SCHEMA: Schema = IntegerSchema::new( | |
16 | "Account expiration date (seconds since epoch). '0' means no expiration date.") | |
17 | .default(0) | |
18 | .minimum(0) | |
19 | .schema(); | |
20 | ||
21 | pub const FIRST_NAME_SCHEMA: Schema = StringSchema::new("First name.") | |
22 | .format(&SINGLE_LINE_COMMENT_FORMAT) | |
23 | .min_length(2) | |
24 | .max_length(64) | |
25 | .schema(); | |
26 | ||
27 | pub const LAST_NAME_SCHEMA: Schema = StringSchema::new("Last name.") | |
28 | .format(&SINGLE_LINE_COMMENT_FORMAT) | |
29 | .min_length(2) | |
30 | .max_length(64) | |
31 | .schema(); | |
32 | ||
33 | pub const EMAIL_SCHEMA: Schema = StringSchema::new("E-Mail Address.") | |
34 | .format(&SINGLE_LINE_COMMENT_FORMAT) | |
35 | .min_length(2) | |
36 | .max_length(64) | |
37 | .schema(); | |
38 | ||
39 | #[api( | |
40 | properties: { | |
41 | userid: { | |
42 | type: Userid, | |
43 | }, | |
44 | comment: { | |
45 | optional: true, | |
46 | schema: SINGLE_LINE_COMMENT_SCHEMA, | |
47 | }, | |
48 | enable: { | |
49 | optional: true, | |
50 | schema: ENABLE_USER_SCHEMA, | |
51 | }, | |
52 | expire: { | |
53 | optional: true, | |
54 | schema: EXPIRE_USER_SCHEMA, | |
55 | }, | |
56 | firstname: { | |
57 | optional: true, | |
58 | schema: FIRST_NAME_SCHEMA, | |
59 | }, | |
60 | lastname: { | |
61 | schema: LAST_NAME_SCHEMA, | |
62 | optional: true, | |
63 | }, | |
64 | email: { | |
65 | schema: EMAIL_SCHEMA, | |
66 | optional: true, | |
67 | }, | |
68 | tokens: { | |
69 | type: Array, | |
70 | optional: true, | |
71 | description: "List of user's API tokens.", | |
72 | items: { | |
73 | type: ApiToken | |
74 | }, | |
75 | }, | |
76 | } | |
77 | )] | |
78 | #[derive(Serialize,Deserialize)] | |
79 | /// User properties with added list of ApiTokens | |
80 | pub struct UserWithTokens { | |
81 | pub userid: Userid, | |
82 | #[serde(skip_serializing_if="Option::is_none")] | |
83 | pub comment: Option<String>, | |
84 | #[serde(skip_serializing_if="Option::is_none")] | |
85 | pub enable: Option<bool>, | |
86 | #[serde(skip_serializing_if="Option::is_none")] | |
87 | pub expire: Option<i64>, | |
88 | #[serde(skip_serializing_if="Option::is_none")] | |
89 | pub firstname: Option<String>, | |
90 | #[serde(skip_serializing_if="Option::is_none")] | |
91 | pub lastname: Option<String>, | |
92 | #[serde(skip_serializing_if="Option::is_none")] | |
93 | pub email: Option<String>, | |
94 | #[serde(skip_serializing_if="Vec::is_empty", default)] | |
95 | pub tokens: Vec<ApiToken>, | |
96 | } | |
97 | ||
98 | #[api( | |
99 | properties: { | |
100 | tokenid: { | |
101 | schema: PROXMOX_TOKEN_ID_SCHEMA, | |
102 | }, | |
103 | comment: { | |
104 | optional: true, | |
105 | schema: SINGLE_LINE_COMMENT_SCHEMA, | |
106 | }, | |
107 | enable: { | |
108 | optional: true, | |
109 | schema: ENABLE_USER_SCHEMA, | |
110 | }, | |
111 | expire: { | |
112 | optional: true, | |
113 | schema: EXPIRE_USER_SCHEMA, | |
114 | }, | |
115 | } | |
116 | )] | |
117 | #[derive(Serialize,Deserialize)] | |
118 | /// ApiToken properties. | |
119 | pub struct ApiToken { | |
120 | pub tokenid: Authid, | |
121 | #[serde(skip_serializing_if="Option::is_none")] | |
122 | pub comment: Option<String>, | |
123 | #[serde(skip_serializing_if="Option::is_none")] | |
124 | pub enable: Option<bool>, | |
125 | #[serde(skip_serializing_if="Option::is_none")] | |
126 | pub expire: Option<i64>, | |
127 | } | |
128 | ||
129 | impl ApiToken { | |
130 | pub fn is_active(&self) -> bool { | |
131 | if !self.enable.unwrap_or(true) { | |
132 | return false; | |
133 | } | |
134 | if let Some(expire) = self.expire { | |
6ef1b649 | 135 | let now = proxmox_time::epoch_i64(); |
2b7f8dd5 WB |
136 | if expire > 0 && expire <= now { |
137 | return false; | |
138 | } | |
139 | } | |
140 | true | |
141 | } | |
142 | } | |
143 | ||
144 | #[api( | |
145 | properties: { | |
146 | userid: { | |
147 | type: Userid, | |
148 | }, | |
149 | comment: { | |
150 | optional: true, | |
151 | schema: SINGLE_LINE_COMMENT_SCHEMA, | |
152 | }, | |
153 | enable: { | |
154 | optional: true, | |
155 | schema: ENABLE_USER_SCHEMA, | |
156 | }, | |
157 | expire: { | |
158 | optional: true, | |
159 | schema: EXPIRE_USER_SCHEMA, | |
160 | }, | |
161 | firstname: { | |
162 | optional: true, | |
163 | schema: FIRST_NAME_SCHEMA, | |
164 | }, | |
165 | lastname: { | |
166 | schema: LAST_NAME_SCHEMA, | |
167 | optional: true, | |
168 | }, | |
169 | email: { | |
170 | schema: EMAIL_SCHEMA, | |
171 | optional: true, | |
172 | }, | |
173 | } | |
174 | )] | |
b65dfff5 | 175 | #[derive(Serialize,Deserialize,Updater)] |
2b7f8dd5 WB |
176 | /// User properties. |
177 | pub struct User { | |
b65dfff5 | 178 | #[updater(skip)] |
2b7f8dd5 WB |
179 | pub userid: Userid, |
180 | #[serde(skip_serializing_if="Option::is_none")] | |
181 | pub comment: Option<String>, | |
182 | #[serde(skip_serializing_if="Option::is_none")] | |
183 | pub enable: Option<bool>, | |
184 | #[serde(skip_serializing_if="Option::is_none")] | |
185 | pub expire: Option<i64>, | |
186 | #[serde(skip_serializing_if="Option::is_none")] | |
187 | pub firstname: Option<String>, | |
188 | #[serde(skip_serializing_if="Option::is_none")] | |
189 | pub lastname: Option<String>, | |
190 | #[serde(skip_serializing_if="Option::is_none")] | |
191 | pub email: Option<String>, | |
192 | } | |
193 | ||
194 | impl User { | |
195 | pub fn is_active(&self) -> bool { | |
196 | if !self.enable.unwrap_or(true) { | |
197 | return false; | |
198 | } | |
199 | if let Some(expire) = self.expire { | |
6ef1b649 | 200 | let now = proxmox_time::epoch_i64(); |
2b7f8dd5 WB |
201 | if expire > 0 && expire <= now { |
202 | return false; | |
203 | } | |
204 | } | |
205 | true | |
206 | } | |
207 | } |