1use crate::{
2 error::Error,
3 run::{EventDetail, EventRecord},
4 state::*,
5};
6use std::time::{Duration, Instant};
7
8use kanidm_client::{ClientError, KanidmClient};
9
10use async_trait::async_trait;
11use serde::{Deserialize, Serialize};
12
13pub enum TransitionAction {
14 Login,
15 Logout,
16 PrivilegeReauth,
17 WriteAttributePersonMail,
18 ReadSelfAccount,
19 ReadSelfMemberOf,
20 WriteSelfPassword,
21}
22
23pub struct Transition {
26 pub delay: Option<Duration>,
27 pub action: TransitionAction,
28}
29
30impl Transition {
31 #[allow(dead_code)]
32 pub fn delay(&self) -> Option<Duration> {
33 self.delay
34 }
35}
36
37#[derive(Eq, PartialEq, Ord, PartialOrd)]
38pub enum TransitionResult {
39 Ok,
41 Error,
45}
46
47#[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
48pub enum ActorRole {
49 #[default]
50 None,
51 PeoplePiiReader,
52 PeopleSelfMailWrite,
53 PeopleSelfReadProfile,
54 PeopleSelfReadMemberOf,
55 PeopleSelfSetPassword,
56 PeopleGroupAdmin,
57}
58
59impl ActorRole {
60 pub fn requires_membership_to(&self) -> Option<&[&str]> {
61 match self {
62 ActorRole::None
63 | ActorRole::PeopleSelfReadProfile
64 | ActorRole::PeopleSelfReadMemberOf
65 | ActorRole::PeopleSelfSetPassword => None,
66 ActorRole::PeoplePiiReader => Some(&["idm_people_pii_read"]),
67 ActorRole::PeopleSelfMailWrite => Some(&["idm_people_self_mail_write"]),
68 ActorRole::PeopleGroupAdmin => Some(&["idm_group_admins"]),
69 }
70 }
71}
72
73#[async_trait]
74pub trait ActorModel {
75 async fn transition(
76 &mut self,
77 client: &KanidmClient,
78 person: &Person,
79 ) -> Result<Vec<EventRecord>, Error>;
80}
81
82pub async fn login(
83 client: &KanidmClient,
84 person: &Person,
85) -> Result<(TransitionResult, Vec<EventRecord>), Error> {
86 let start = Instant::now();
88 let result = match &person.credential {
89 Credential::Password { plain } => {
90 client
91 .auth_simple_password(person.username.as_str(), plain.as_str())
92 .await
93 }
94 };
95
96 let duration = Instant::now().duration_since(start);
97 Ok(parse_call_result_into_transition_result_and_event_record(
98 result,
99 EventDetail::Login,
100 start,
101 duration,
102 ))
103}
104
105pub async fn person_set_self_mail(
106 client: &KanidmClient,
107 person: &Person,
108 values: &[&str],
109) -> Result<(TransitionResult, Vec<EventRecord>), Error> {
110 let person_username = person.username.as_str();
112
113 let start = Instant::now();
114 let result = client
115 .idm_person_account_set_attr(person_username, "mail", values)
116 .await;
117
118 let duration = Instant::now().duration_since(start);
119 let parsed_result = parse_call_result_into_transition_result_and_event_record(
120 result,
121 EventDetail::PersonSetSelfMail,
122 start,
123 duration,
124 );
125
126 Ok(parsed_result)
127}
128
129pub async fn person_create_group(
130 client: &KanidmClient,
131 group_name: &str,
132) -> Result<(TransitionResult, Vec<EventRecord>), Error> {
133 let start = Instant::now();
134 let result = client.idm_group_create(group_name, None).await;
135
136 let duration = Instant::now().duration_since(start);
137 let parsed_result = parse_call_result_into_transition_result_and_event_record(
138 result,
139 EventDetail::PersonCreateGroup,
140 start,
141 duration,
142 );
143
144 Ok(parsed_result)
145}
146
147pub async fn person_add_group_members(
148 client: &KanidmClient,
149 group_name: &str,
150 group_members: &[&str],
151) -> Result<(TransitionResult, Vec<EventRecord>), Error> {
152 let start = Instant::now();
153 let result = client
154 .idm_group_add_members(group_name, group_members)
155 .await;
156
157 let duration = Instant::now().duration_since(start);
158 let parsed_result = parse_call_result_into_transition_result_and_event_record(
159 result,
160 EventDetail::PersonAddGroupMembers,
161 start,
162 duration,
163 );
164
165 Ok(parsed_result)
166}
167
168pub async fn person_set_self_password(
169 client: &KanidmClient,
170 person: &Person,
171 pw: &str,
172) -> Result<(TransitionResult, Vec<EventRecord>), Error> {
173 let person_username = person.username.as_str();
175
176 let start = Instant::now();
177 let result = client
178 .idm_person_account_primary_credential_set_password(person_username, pw)
179 .await;
180
181 let duration = Instant::now().duration_since(start);
182 let parsed_result = parse_call_result_into_transition_result_and_event_record(
183 result,
184 EventDetail::PersonSetSelfPassword,
185 start,
186 duration,
187 );
188
189 Ok(parsed_result)
190}
191
192pub async fn privilege_reauth(
193 client: &KanidmClient,
194 person: &Person,
195) -> Result<(TransitionResult, Vec<EventRecord>), Error> {
196 let start = Instant::now();
197
198 let result = match &person.credential {
199 Credential::Password { plain } => client.reauth_simple_password(plain.as_str()).await,
200 };
201
202 let duration = Instant::now().duration_since(start);
203
204 let parsed_result = parse_call_result_into_transition_result_and_event_record(
205 result,
206 EventDetail::PersonReauth,
207 start,
208 duration,
209 );
210 Ok(parsed_result)
211}
212
213pub async fn logout(
214 client: &KanidmClient,
215 _person: &Person,
216) -> Result<(TransitionResult, Vec<EventRecord>), Error> {
217 let start = Instant::now();
218 let result = client.logout().await;
219 let duration = Instant::now().duration_since(start);
220
221 Ok(parse_call_result_into_transition_result_and_event_record(
222 result,
223 EventDetail::Logout,
224 start,
225 duration,
226 ))
227}
228
229pub async fn person_get_self_account(
230 client: &KanidmClient,
231 person: &Person,
232) -> Result<(TransitionResult, Vec<EventRecord>), Error> {
233 let start = Instant::now();
234 let result = client.idm_person_account_get(&person.username).await;
235 let duration = Instant::now().duration_since(start);
236 Ok(parse_call_result_into_transition_result_and_event_record(
237 result,
238 EventDetail::PersonGetSelfAccount,
239 start,
240 duration,
241 ))
242}
243
244pub async fn person_get_self_memberof(
245 client: &KanidmClient,
246 person: &Person,
247) -> Result<(TransitionResult, Vec<EventRecord>), Error> {
248 let start = Instant::now();
249 let result = client
250 .idm_person_account_get_attr(&person.username, "memberof")
251 .await;
252 let duration = Instant::now().duration_since(start);
253 Ok(parse_call_result_into_transition_result_and_event_record(
254 result,
255 EventDetail::PersonGetSelfMemberOf,
256 start,
257 duration,
258 ))
259}
260
261fn parse_call_result_into_transition_result_and_event_record<T>(
262 result: Result<T, ClientError>,
263 details: EventDetail,
264 start: Instant,
265 duration: Duration,
266) -> (TransitionResult, Vec<EventRecord>) {
267 match result {
268 Ok(_) => (
269 TransitionResult::Ok,
270 vec![EventRecord {
271 start,
272 duration,
273 details,
274 }],
275 ),
276 Err(client_err) => {
277 debug!(?client_err);
278 (
279 TransitionResult::Error,
280 vec![EventRecord {
281 start,
282 duration,
283 details: EventDetail::Error,
284 }],
285 )
286 }
287 }
288}