steid

@jamesgill /

steid/src/domain/repository/user_repo.rs
Rust2.4 KBCode·Blame·History·Raw
1use super::RepositoryResult;
2use crate::domain::{Email, OrgId, User, UserId};
3
4/// Persistence for [`User`].
5pub trait UserRepository: Send + Sync {
6 /// Looks a user up by id.
7 fn find_by_id(
8 &self,
9 id: &UserId,
10 ) -> impl Future<Output = RepositoryResult<Option<User>>> + Send;
11
12 /// Looks a user up by email — the credential used to sign in.
13 fn find_by_email(
14 &self,
15 email: &Email,
16 ) -> impl Future<Output = RepositoryResult<Option<User>>> + Send;
17
18 /// Looks a user up by the organisation whose name is their public handle.
19 ///
20 /// The way from a handle to an account, which is what an operator has on the command
21 /// line: `steid reset-password ada` knows a handle and nothing else. The join runs
22 /// through the org because a handle is the org's name, not the user's — see
23 /// [`User::personal_org_id`](crate::domain::User::personal_org_id).
24 fn find_by_personal_org(
25 &self,
26 org_id: &OrgId,
27 ) -> impl Future<Output = RepositoryResult<Option<User>>> + Send;
28
29 /// Inserts or replaces a user.
30 ///
31 /// The user's organisation must already exist: the foreign key runs that
32 /// direction, and saving in the other order fails.
33 fn save(&self, user: &User) -> impl Future<Output = RepositoryResult<()>> + Send;
34
35 /// Whether any user exists. Drives first-boot bootstrap.
36 fn any_exist(&self) -> impl Future<Output = RepositoryResult<bool>> + Send;
37
38 /// How many users exist.
39 ///
40 /// Separate from [`any_exist`](Self::any_exist) rather than derived from it: a
41 /// control plane asking how big a tenant has become wants the number, and
42 /// "is there at least one" is a cheaper question a boot path asks on every start.
43 fn count(&self) -> impl Future<Output = RepositoryResult<u64>> + Send;
44
45 /// The only user, when there is exactly one.
46 ///
47 /// `None` when the instance is unclaimed **and** when more than one user exists —
48 /// the second case is deliberate rather than a shortcut. "Who owns this instance"
49 /// has no answer once registration exists, so this refuses to guess instead of
50 /// returning whichever row happened to come back first. Milestone 7 has to define
51 /// the concept properly; until then a personal instance has one user and this is
52 /// unambiguous.
53 fn sole_user(&self) -> impl Future<Output = RepositoryResult<Option<User>>> + Send;
54}