|
| 1 | +//! Who may join and who administers: signup invitations and administrator |
| 2 | +//! rights. |
| 3 | +
|
| 4 | +use super::AdminService; |
| 5 | +use crate::{ |
| 6 | + account::AccountService, |
| 7 | + admin::{AdminError, InviteExpiry, Operator}, |
| 8 | + routes, |
| 9 | +}; |
| 10 | +use chrono::Utc; |
| 11 | +use piquel_common::{ |
| 12 | + AccountStatus, TokenId, UserId, |
| 13 | + admin::{Administrator, CreatedInvitation, Invitation}, |
| 14 | + config::Origin, |
| 15 | + events::DomainEvent, |
| 16 | +}; |
| 17 | +use piquel_crypto::Secret; |
| 18 | +use piquel_ui::{LoginQuery, Query}; |
| 19 | + |
| 20 | +impl AdminService { |
| 21 | + /// Lists invitations that have not been purged yet, newest first. |
| 22 | + pub(crate) async fn list_invitations( |
| 23 | + &self, |
| 24 | + operator: &Operator, |
| 25 | + ) -> Result<Vec<Invitation>, AdminError> { |
| 26 | + operator.authorize_read(&self.database).await?; |
| 27 | + Ok(self.database.security().list_invitations().await?) |
| 28 | + } |
| 29 | + |
| 30 | + /// Creates a single-use signup link to the registration page on `origin`, auth's |
| 31 | + /// own. Only the secret's digest is stored, so the link cannot be shown again. |
| 32 | + /// |
| 33 | + /// # Errors |
| 34 | + /// Fails when the operator may not make changes, or secret generation or |
| 35 | + /// persistence fails. |
| 36 | + pub async fn create_invitation( |
| 37 | + &self, |
| 38 | + operator: &Operator, |
| 39 | + origin: &Origin, |
| 40 | + expires_in: InviteExpiry, |
| 41 | + ) -> Result<CreatedInvitation, AdminError> { |
| 42 | + let secret = Secret::generate()?; |
| 43 | + let id = TokenId::new(); |
| 44 | + let expires_at = Utc::now() + expires_in.duration(); |
| 45 | + let mut tx = self.database.begin().await?; |
| 46 | + tx.security() |
| 47 | + .create_invitation(id, secret.digest().as_bytes(), expires_at) |
| 48 | + .await?; |
| 49 | + let created = DomainEvent::InvitationCreated { expires_at }; |
| 50 | + Self::finish(tx, operator, None, vec![created]).await?; |
| 51 | + let url = LoginQuery { |
| 52 | + invitation: Some(secret.encoded()), |
| 53 | + ..LoginQuery::default() |
| 54 | + } |
| 55 | + .url(origin.as_url(), routes::page::REGISTER)?; |
| 56 | + Ok(CreatedInvitation { |
| 57 | + id, |
| 58 | + url, |
| 59 | + expires_at, |
| 60 | + }) |
| 61 | + } |
| 62 | + |
| 63 | + /// Revokes an unredeemed invitation. |
| 64 | + pub(crate) async fn revoke_invitation( |
| 65 | + &self, |
| 66 | + operator: &Operator, |
| 67 | + invitation_id: TokenId, |
| 68 | + ) -> Result<(), AdminError> { |
| 69 | + let mut tx = self.database.begin().await?; |
| 70 | + if !tx.security().revoke_invitation(invitation_id).await? { |
| 71 | + return Err(AdminError::InvitationNotFound); |
| 72 | + } |
| 73 | + let revoked = DomainEvent::InvitationRevoked { invitation_id }; |
| 74 | + Self::finish(tx, operator, None, vec![revoked]).await |
| 75 | + } |
| 76 | + |
| 77 | + /// Lists every administrator, oldest grant first. |
| 78 | + /// |
| 79 | + /// # Errors |
| 80 | + /// Fails when the operator may not read, or the query fails. |
| 81 | + pub async fn list_administrators( |
| 82 | + &self, |
| 83 | + operator: &Operator, |
| 84 | + ) -> Result<Vec<Administrator>, AdminError> { |
| 85 | + operator.authorize_read(&self.database).await?; |
| 86 | + Ok(self.database.administrators().list().await?) |
| 87 | + } |
| 88 | + |
| 89 | + /// Makes the active account `user_id` an administrator. Administrators cannot |
| 90 | + /// grant themselves rights: one whose rights were revoked while the request |
| 91 | + /// waited would otherwise restore them. While a recovery of the account can |
| 92 | + /// still be undone, it is refused, by the CLI too: granted during a probation, |
| 93 | + /// the rights would go live for whoever used its link once it settles. The |
| 94 | + /// migration's `administrators_no_grant_mid_recovery` backs it for every |
| 95 | + /// writer. |
| 96 | + /// |
| 97 | + /// # Errors |
| 98 | + /// Fails when the operator may not make changes, or the account is unknown, |
| 99 | + /// disabled, already an administrator, the caller's own, or has a recovery |
| 100 | + /// that can still be undone. |
| 101 | + pub(crate) async fn grant_administrator( |
| 102 | + &self, |
| 103 | + operator: &Operator, |
| 104 | + user_id: UserId, |
| 105 | + ) -> Result<(), AdminError> { |
| 106 | + operator.refuse_own_rights(user_id)?; |
| 107 | + let mut tx = self.database.begin().await?; |
| 108 | + tx.administrators().lock().await?; |
| 109 | + let user = tx |
| 110 | + .users() |
| 111 | + .find_by_id_for_update(user_id) |
| 112 | + .await? |
| 113 | + .ok_or(AdminError::UserNotFound)?; |
| 114 | + if user.status != AccountStatus::Active { |
| 115 | + return Err(AdminError::AccountDisabled); |
| 116 | + } |
| 117 | + if AccountService::undoable(&mut tx, user_id).await?.is_some() { |
| 118 | + return Err(AdminError::GrantDuringRecovery); |
| 119 | + } |
| 120 | + if !tx.administrators().grant(user_id).await? { |
| 121 | + return Err(AdminError::AlreadyAdministrator); |
| 122 | + } |
| 123 | + let granted = DomainEvent::AdministratorGranted {}; |
| 124 | + Self::finish(tx, operator, Some(user_id), vec![granted]).await |
| 125 | + } |
| 126 | + |
| 127 | + /// Removes the administrator rights of `user_id`. Administrators cannot remove |
| 128 | + /// their own, and nobody can remove the last active administrator's: someone |
| 129 | + /// must be left to administer. |
| 130 | + /// |
| 131 | + /// # Errors |
| 132 | + /// Fails when the operator may not make changes, the account is not an |
| 133 | + /// administrator, or either rule forbids it. |
| 134 | + pub async fn revoke_administrator( |
| 135 | + &self, |
| 136 | + operator: &Operator, |
| 137 | + user_id: UserId, |
| 138 | + ) -> Result<(), AdminError> { |
| 139 | + operator.refuse_own_rights(user_id)?; |
| 140 | + let mut tx = self.database.begin().await?; |
| 141 | + let locked = tx.administrators().lock().await?; |
| 142 | + if tx.administrators().is_last_active(&locked, user_id).await? { |
| 143 | + return Err(AdminError::LastAdministrator); |
| 144 | + } |
| 145 | + if !tx.administrators().revoke(user_id).await? { |
| 146 | + return Err(AdminError::NotAnAdministrator); |
| 147 | + } |
| 148 | + let revoked = DomainEvent::AdministratorRevoked {}; |
| 149 | + Self::finish(tx, operator, Some(user_id), vec![revoked]).await |
| 150 | + } |
| 151 | +} |
0 commit comments