Skip to main content

reifydb_transaction/transaction/catalog/
user_authentication.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::interface::catalog::{
5	change::CatalogTrackUserAuthenticationChangeOperations,
6	user::UserId,
7	user_authentication::{UserAuthenticationDef, UserAuthenticationId},
8};
9use reifydb_type::Result;
10
11use crate::{
12	change::{
13		Change,
14		OperationType::{Create, Delete},
15		TransactionalUserAuthenticationChanges,
16	},
17	transaction::{admin::AdminTransaction, subscription::SubscriptionTransaction},
18};
19
20impl CatalogTrackUserAuthenticationChangeOperations for AdminTransaction {
21	fn track_user_authentication_def_created(&mut self, auth: UserAuthenticationDef) -> Result<()> {
22		let change = Change {
23			pre: None,
24			post: Some(auth),
25			op: Create,
26		};
27		self.changes.add_user_authentication_def_change(change);
28		Ok(())
29	}
30
31	fn track_user_authentication_def_deleted(&mut self, auth: UserAuthenticationDef) -> Result<()> {
32		let change = Change {
33			pre: Some(auth),
34			post: None,
35			op: Delete,
36		};
37		self.changes.add_user_authentication_def_change(change);
38		Ok(())
39	}
40}
41
42impl TransactionalUserAuthenticationChanges for AdminTransaction {
43	fn find_user_authentication(&self, id: UserAuthenticationId) -> Option<&UserAuthenticationDef> {
44		for change in self.changes.user_authentication_def.iter().rev() {
45			if let Some(auth) = &change.post {
46				if auth.id == id {
47					return Some(auth);
48				}
49			} else if let Some(auth) = &change.pre {
50				if auth.id == id && change.op == Delete {
51					return None;
52				}
53			}
54		}
55		None
56	}
57
58	fn find_user_authentication_by_user_and_method(
59		&self,
60		user_id: UserId,
61		method: &str,
62	) -> Option<&UserAuthenticationDef> {
63		self.changes
64			.user_authentication_def
65			.iter()
66			.rev()
67			.find_map(|change| change.post.as_ref().filter(|a| a.user_id == user_id && a.method == method))
68	}
69
70	fn is_user_authentication_deleted(&self, id: UserAuthenticationId) -> bool {
71		self.changes
72			.user_authentication_def
73			.iter()
74			.rev()
75			.any(|change| change.op == Delete && change.pre.as_ref().map(|a| a.id) == Some(id))
76	}
77}
78
79impl CatalogTrackUserAuthenticationChangeOperations for SubscriptionTransaction {
80	fn track_user_authentication_def_created(&mut self, auth: UserAuthenticationDef) -> Result<()> {
81		self.inner.track_user_authentication_def_created(auth)
82	}
83
84	fn track_user_authentication_def_deleted(&mut self, auth: UserAuthenticationDef) -> Result<()> {
85		self.inner.track_user_authentication_def_deleted(auth)
86	}
87}
88
89impl TransactionalUserAuthenticationChanges for SubscriptionTransaction {
90	fn find_user_authentication(&self, id: UserAuthenticationId) -> Option<&UserAuthenticationDef> {
91		self.inner.find_user_authentication(id)
92	}
93
94	fn find_user_authentication_by_user_and_method(
95		&self,
96		user_id: UserId,
97		method: &str,
98	) -> Option<&UserAuthenticationDef> {
99		self.inner.find_user_authentication_by_user_and_method(user_id, method)
100	}
101
102	fn is_user_authentication_deleted(&self, id: UserAuthenticationId) -> bool {
103		self.inner.is_user_authentication_deleted(id)
104	}
105}