reifydb_transaction/transaction/catalog/
authentication.rs1use reifydb_core::interface::catalog::{
5 authentication::{Authentication, AuthenticationId},
6 change::CatalogTrackAuthenticationChangeOperations,
7};
8use reifydb_type::{Result, value::identity::IdentityId};
9
10use crate::{
11 change::{
12 Change,
13 OperationType::{Create, Delete},
14 TransactionalAuthenticationChanges,
15 },
16 interceptor::authentication::{AuthenticationPostCreateContext, AuthenticationPreDeleteContext},
17 transaction::admin::AdminTransaction,
18};
19
20impl CatalogTrackAuthenticationChangeOperations for AdminTransaction {
21 fn track_authentication_created(&mut self, auth: Authentication) -> Result<()> {
22 self.interceptors.authentication_post_create.execute(AuthenticationPostCreateContext::new(&auth))?;
23 let change = Change {
24 pre: None,
25 post: Some(auth),
26 op: Create,
27 };
28 self.changes.add_authentication_change(change);
29 Ok(())
30 }
31
32 fn track_authentication_deleted(&mut self, auth: Authentication) -> Result<()> {
33 self.interceptors.authentication_pre_delete.execute(AuthenticationPreDeleteContext::new(&auth))?;
34 let change = Change {
35 pre: Some(auth),
36 post: None,
37 op: Delete,
38 };
39 self.changes.add_authentication_change(change);
40 Ok(())
41 }
42}
43
44impl TransactionalAuthenticationChanges for AdminTransaction {
45 fn find_authentication(&self, id: AuthenticationId) -> Option<&Authentication> {
46 for change in self.changes.authentication.iter().rev() {
47 if let Some(auth) = &change.post {
48 if auth.id == id {
49 return Some(auth);
50 }
51 } else if let Some(auth) = &change.pre
52 && auth.id == id && change.op == Delete
53 {
54 return None;
55 }
56 }
57 None
58 }
59
60 fn find_authentication_by_identity_and_method(
61 &self,
62 identity: IdentityId,
63 method: &str,
64 ) -> Option<&Authentication> {
65 self.changes.authentication.iter().rev().find_map(|change| {
66 change.post.as_ref().filter(|a| a.identity == identity && a.method == method)
67 })
68 }
69
70 fn is_authentication_deleted(&self, id: AuthenticationId) -> bool {
71 self.changes
72 .authentication
73 .iter()
74 .rev()
75 .any(|change| change.op == Delete && change.pre.as_ref().map(|a| a.id) == Some(id))
76 }
77
78 fn is_authentication_deleted_by_identity_and_method(&self, identity: IdentityId, method: &str) -> bool {
79 self.changes.authentication.iter().rev().any(|change| {
80 change.op == Delete
81 && change
82 .pre
83 .as_ref()
84 .map(|a| a.identity == identity && a.method == method)
85 .unwrap_or(false)
86 })
87 }
88}