mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Implement federated login; #123
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
pub struct Migration;
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
"m20260705_000001_create_federation_table"
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
// Define how to apply this migration: Create the Federation table.
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(crate::schema::federation::Entity)
|
||||
.col(
|
||||
ColumnDef::new(crate::schema::federation::Column::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::federation::Column::CreationTime).big_integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::federation::Column::LastUsedTime).big_integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::federation::Column::Domain).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::federation::Column::Auth).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::federation::Column::Cdn).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::federation::Column::Factory).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::federation::Column::Society).string().not_null())
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
// Define how to rollback this migration: Drop the Federation table.
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(crate::schema::federation::Entity).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
pub struct Migration;
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
"m20260705_000002_add_user_federation"
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
// Define how to apply this migration: Add user federation id column
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
// SQLite doesn't support multiple alter options in one operation
|
||||
manager
|
||||
.alter_table(
|
||||
Table::alter()
|
||||
.table(crate::schema::user::Entity)
|
||||
.add_column(ColumnDef::new(crate::schema::user::Column::FederationId).integer().null())
|
||||
.to_owned()
|
||||
)
|
||||
.await?;
|
||||
if manager.get_connection().get_database_backend() != sea_orm::DbBackend::Sqlite {
|
||||
manager
|
||||
.alter_table(
|
||||
Table::alter()
|
||||
.table(crate::schema::user::Entity)
|
||||
.add_foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk-user_federation_id")
|
||||
.from(crate::schema::user::Entity, crate::schema::user::Column::FederationId)
|
||||
.to(crate::schema::federation::Entity, crate::schema::federation::Column::Id)
|
||||
.get_foreign_key(),
|
||||
)
|
||||
.to_owned()
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
// SQLite doesn't support altering foreign keys of an existing table
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Define how to rollback this migration: Drop the added column
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
// SQLite doesn't support multiple alter options in one operation
|
||||
if manager.get_connection().get_database_backend() != sea_orm::DbBackend::Sqlite {
|
||||
// SQLite doesn't support altering foreign keys of an existing table
|
||||
manager
|
||||
.alter_table(
|
||||
Table::alter()
|
||||
.table(crate::schema::user::Entity)
|
||||
.drop_foreign_key("fk-user_federation_id")
|
||||
.to_owned()
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
manager
|
||||
.alter_table(
|
||||
Table::alter()
|
||||
.table(crate::schema::user::Entity)
|
||||
.drop_column(crate::schema::user::Column::FederationId)
|
||||
.to_owned()
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,8 @@ mod m20260215_000001_create_friend_table;
|
||||
mod m20260221_000001_create_clan_table;
|
||||
mod m20260221_000002_create_clan_member_table;
|
||||
mod m20260317_000001_add_game_overrides;
|
||||
mod m20260705_000001_create_federation_table;
|
||||
mod m20260705_000002_add_user_federation;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
@@ -45,6 +47,8 @@ impl MigratorTrait for Migrator {
|
||||
Box::new(m20260221_000001_create_clan_table::Migration),
|
||||
Box::new(m20260221_000002_create_clan_member_table::Migration),
|
||||
Box::new(m20260317_000001_add_game_overrides::Migration),
|
||||
Box::new(m20260705_000001_create_federation_table::Migration),
|
||||
Box::new(m20260705_000002_add_user_federation::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
29
rc_database/src/schema/federation.rs
Normal file
29
rc_database/src/schema/federation.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "federations")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub creation_time: i64, // seconds since unix epoch
|
||||
pub last_used_time: i64, // seconds since unix epoch
|
||||
pub domain: String, // root domain
|
||||
pub auth: String,
|
||||
pub cdn: String,
|
||||
pub factory: String,
|
||||
pub society: String,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::user::Entity")]
|
||||
User,
|
||||
}
|
||||
|
||||
impl Related<super::user::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::User.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
@@ -15,6 +15,7 @@ pub mod factory;
|
||||
pub mod friend;
|
||||
pub mod clan;
|
||||
pub mod clan_member;
|
||||
pub mod federation;
|
||||
|
||||
pub fn parse_int_csv(s: &str) -> Vec<u32> {
|
||||
s.split(',').filter_map(|i_as_s| {
|
||||
|
||||
@@ -11,6 +11,7 @@ pub struct Model {
|
||||
pub password: String,
|
||||
pub email: String,
|
||||
pub steam_id: Option<String>, // u64
|
||||
pub federation_id: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
@@ -29,6 +30,12 @@ pub enum Relation {
|
||||
FactoryUploads,
|
||||
#[sea_orm(has_many = "super::friend::Entity")]
|
||||
Friends, // this will probably join the wrong column (i.e. in the wrong direction)
|
||||
#[sea_orm(
|
||||
belongs_to = "super::federation::Entity",
|
||||
from = "Column::FederationId",
|
||||
to = "super::federation::Column::Id"
|
||||
)]
|
||||
Federation,
|
||||
}
|
||||
|
||||
impl Related<super::permissions::Entity> for Entity {
|
||||
@@ -73,4 +80,10 @@ impl Related<super::friend::Entity> for Entity {
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::federation::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Federation.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
@@ -39,6 +39,17 @@ impl Database {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn user_by_display_name_and_federation(&self, public_id: String, federation_id: i32) -> Result<Option<crate::schema::user::Model>, sea_orm::DbErr> {
|
||||
crate::schema::user::Entity::find()
|
||||
.filter(sea_orm::sea_query::Expr::expr(
|
||||
sea_orm::sea_query::Func::lower(crate::schema::user::Column::DisplayName.into_expr())
|
||||
).eq(public_id.to_lowercase())
|
||||
)
|
||||
.filter(crate::schema::user::Column::FederationId.eq(Some(federation_id)))
|
||||
.one(self.orm.as_ref())
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn user_by_public_id(&self, public_id: String) -> Result<Option<crate::schema::user::Model>, sea_orm::DbErr> {
|
||||
crate::schema::user::Entity::find()
|
||||
.filter(crate::schema::user::Column::PublicId.eq(public_id))
|
||||
@@ -919,6 +930,31 @@ impl Database {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn federation_by_id(&self, id: i32) -> Result<Option<crate::schema::federation::Model>, sea_orm::DbErr> {
|
||||
crate::schema::federation::Entity::find_by_id(id)
|
||||
.one(self.orm.as_ref())
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn federation_by_domain(&self, domain: &str) -> Result<Option<crate::schema::federation::Model>, sea_orm::DbErr> {
|
||||
crate::schema::federation::Entity::find()
|
||||
.filter(crate::schema::federation::Column::Id.eq(domain))
|
||||
.one(self.orm.as_ref())
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn insert_federation(&self, entity: crate::schema::federation::ActiveModel) -> Result<crate::schema::federation::Model, sea_orm::DbErr> {
|
||||
#[cfg(debug_assertions)]
|
||||
assert!(matches!(entity.id, sea_orm::ActiveValue::NotSet));
|
||||
entity.insert(self.orm.as_ref()).await
|
||||
}
|
||||
|
||||
pub async fn update_federation(&self, entity: crate::schema::federation::ActiveModel) -> Result<crate::schema::federation::Model, sea_orm::DbErr> {
|
||||
crate::schema::federation::Entity::update(entity)
|
||||
.exec(self.orm.as_ref())
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn metrics(&self) -> super::DatabaseMetrics {
|
||||
self.metrics.lock().unwrap().snapshot()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user