Skip to content

Commit

Permalink
Data model and storage layer for storing user registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Jan 13, 2025
1 parent e49d5a3 commit 73f2840
Show file tree
Hide file tree
Showing 21 changed files with 1,489 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/data-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ pub use self::{
users::{
Authentication, AuthenticationMethod, BrowserSession, Password, User, UserEmail,
UserEmailAuthentication, UserEmailAuthenticationCode, UserRecoverySession,
UserRecoveryTicket,
UserRecoveryTicket, UserRegistration, UserRegistrationPassword,
},
};
22 changes: 22 additions & 0 deletions crates/data-model/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use chrono::{DateTime, Utc};
use rand::Rng;
use serde::Serialize;
use ulid::Ulid;
use url::Url;

use crate::UserAgent;

Expand Down Expand Up @@ -112,6 +113,7 @@ impl UserRecoveryTicket {
pub struct UserEmailAuthentication {
pub id: Ulid,
pub user_session_id: Option<Ulid>,
pub user_registration_id: Option<Ulid>,
pub email: String,
pub created_at: DateTime<Utc>,
pub completed_at: Option<DateTime<Utc>>,
Expand Down Expand Up @@ -192,3 +194,23 @@ impl UserEmail {
]
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct UserRegistrationPassword {
pub hashed_password: String,
pub version: u16,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct UserRegistration {
pub id: Ulid,
pub username: Option<String>,
pub display_name: Option<String>,
pub terms_url: Option<Url>,
pub email_authentication_id: Option<Ulid>,
pub password: Option<UserRegistrationPassword>,
pub ip_address: Option<IpAddr>,
pub user_agent: Option<UserAgent>,
pub created_at: DateTime<Utc>,
pub completed_at: Option<DateTime<Utc>>,
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions crates/storage-pg/migrations/20250113102144_user_registrations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Copyright 2025 New Vector Ltd.
--
-- SPDX-License-Identifier: AGPL-3.0-only
-- Please see LICENSE in the repository root for full details.

-- Add a table for storing user registrations
CREATE TABLE "user_registrations" (
"user_registration_id" UUID PRIMARY KEY,
"ip_address" INET,
"user_agent" TEXT,
"username" TEXT,
"display_name" TEXT,
"terms_url" TEXT,
"email_authentication_id" UUID
REFERENCES "user_email_authentications" ("user_email_authentication_id")
ON DELETE SET NULL,
"hashed_password" TEXT,
"hashed_password_version" INTEGER,
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL,
"completed_at" TIMESTAMP WITH TIME ZONE
);

-- Allow using user email authentications for user registrations
ALTER TABLE "user_email_authentications"
ADD COLUMN "user_registration_id" UUID
REFERENCES "user_registrations" ("user_registration_id")
ON DELETE CASCADE;
9 changes: 8 additions & 1 deletion crates/storage-pg/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ use crate::{
},
user::{
PgBrowserSessionRepository, PgUserEmailRepository, PgUserPasswordRepository,
PgUserRecoveryRepository, PgUserRepository, PgUserTermsRepository,
PgUserRecoveryRepository, PgUserRegistrationRepository, PgUserRepository,
PgUserTermsRepository,
},
DatabaseError,
};
Expand Down Expand Up @@ -191,6 +192,12 @@ where
Box::new(PgUserTermsRepository::new(self.conn.as_mut()))
}

fn user_registration<'c>(
&'c mut self,
) -> Box<dyn mas_storage::user::UserRegistrationRepository<Error = Self::Error> + 'c> {
Box::new(PgUserRegistrationRepository::new(self.conn.as_mut()))
}

fn browser_session<'c>(
&'c mut self,
) -> Box<dyn BrowserSessionRepository<Error = Self::Error> + 'c> {
Expand Down
Loading

0 comments on commit 73f2840

Please sign in to comment.