Skip to main content

alibi_core/store/contracts/
hooks.rs

1use super::*;
2
3/// Registered model defaults applied by an adapter after its creation hooks.
4/// Keeping this phase separate preserves a validation candidate's absent fields.
5#[derive(Clone, Default)]
6pub struct UserCreationDefaults(pub(crate) Vec<UserCreateTransform>);
7impl UserCreationDefaults {
8    pub fn apply(self, data: CreateUser) -> AuthResult<CreateUser> {
9        create_data(data, &self.0)
10    }
11}
12
13/// Application/plugin adapter hook observing a successfully persisted session.
14/// Transactional hooks execute only after commit against the finalized store,
15/// including registered user transforms. Hook failures do not roll back commit.
16#[async_trait]
17pub trait SessionCreatedHook<S: AuthSchema>: Send + Sync {
18    async fn after_create(
19        &self,
20        session: &S::Session,
21        database: &dyn AuthStore<S>,
22    ) -> AuthResult<()>;
23}
24
25/// A persisted adapter result after declared output transforms. Hidden fields
26/// remain present; public response filtering has not run on this record.
27pub enum AdapterEvent<S: AuthSchema> {
28    UserCreated(crate::AdapterRecord<S::User>),
29    UserUpdated(crate::AdapterRecord<S::User>),
30    SessionCreated(crate::AdapterRecord<S::Session>),
31    SessionUpdated(crate::AdapterRecord<S::Session>),
32    AccountCreated(crate::AdapterRecord<S::Account>),
33    AccountUpdated(crate::AdapterRecord<S::Account>),
34}
35
36/// Record-aware application adapter after observer. Output errors prevent this
37/// observer; transaction observers run only after commit. Observer errors do not
38/// roll back committed writes. Typed physical storage hooks remain separate.
39#[async_trait]
40pub trait AdapterAfterHook<S: AuthSchema>: Send + Sync {
41    async fn after_write(
42        &self,
43        event: &AdapterEvent<S>,
44        database: &dyn AuthStore<S>,
45    ) -> AuthResult<()>;
46}
47
48use std::sync::Arc;
49
50pub(crate) type UserCreateTransform =
51    Arc<dyn Fn(CreateUser) -> AuthResult<CreateUser> + Send + Sync>;
52
53pub(in crate::store) fn create_data(
54    mut data: CreateUser,
55    transforms: &[UserCreateTransform],
56) -> AuthResult<CreateUser> {
57    for transform in transforms {
58        data = transform(data)?;
59    }
60    Ok(data)
61}