From 499610add7b696a939c5d949d0f490d27696bc7c Mon Sep 17 00:00:00 2001 From: yenicelik Date: Wed, 15 Feb 2023 16:29:30 +0100 Subject: [PATCH] added admin trail table --- entities/src/admin_trail.rs | 37 ++++++++ entities/src/mod.rs | 1 + entities/src/prelude.rs | 2 + migration/src/m20230215_152254_admin_trail.rs | 93 +++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 entities/src/admin_trail.rs create mode 100644 migration/src/m20230215_152254_admin_trail.rs diff --git a/entities/src/admin_trail.rs b/entities/src/admin_trail.rs new file mode 100644 index 00000000..26ad1e3b --- /dev/null +++ b/entities/src/admin_trail.rs @@ -0,0 +1,37 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "admin_trail")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub caller: u64, + pub imitating_user: Option, + pub endpoint: String, + pub payload: String, + pub timestamp: DateTimeUtc, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::Caller", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + User2, + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::ImitatingUser", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + User1, +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/entities/src/mod.rs b/entities/src/mod.rs index f0ac2711..2121477c 100644 --- a/entities/src/mod.rs +++ b/entities/src/mod.rs @@ -3,6 +3,7 @@ pub mod prelude; pub mod admin; +pub mod admin_trail; pub mod login; pub mod pending_login; pub mod revert_log; diff --git a/entities/src/prelude.rs b/entities/src/prelude.rs index 218a3d47..bb19388d 100644 --- a/entities/src/prelude.rs +++ b/entities/src/prelude.rs @@ -1,5 +1,7 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.5 +pub use super::admin::Entity as Admin; +pub use super::admin_trail::Entity as AdminTrail; pub use super::login::Entity as Login; pub use super::pending_login::Entity as PendingLogin; pub use super::revert_log::Entity as RevertLog; diff --git a/migration/src/m20230215_152254_admin_trail.rs b/migration/src/m20230215_152254_admin_trail.rs new file mode 100644 index 00000000..994361ee --- /dev/null +++ b/migration/src/m20230215_152254_admin_trail.rs @@ -0,0 +1,93 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(AdminTrail::Table) + .if_not_exists() + .col( + ColumnDef::new(AdminTrail::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col( + ColumnDef::new(AdminTrail::Caller) + .big_unsigned() + .not_null() + // TODO: Add Foreign Key + ) + .foreign_key( + sea_query::ForeignKey::create() + .from(AdminTrail::Table, AdminTrail::Caller) + .to(User::Table, User::Id), + ) + .col( + ColumnDef::new(AdminTrail::ImitatingUser) + .big_unsigned() + // Can be null bcs maybe we're just logging in / using endpoints that don't imitate a user + // TODO: Add Foreign Key + ) + .foreign_key( + sea_query::ForeignKey::create() + .from(AdminTrail::Table, AdminTrail::ImitatingUser) + .to(User::Table, User::Id), + ) + .col( + ColumnDef::new(AdminTrail::Endpoint) + .string() + .not_null() + ) + .col( + ColumnDef::new(AdminTrail::Payload) + .string() + .not_null() + ) + .col( + ColumnDef::new(AdminTrail::Timestamp) + .timestamp() + .not_null() + .extra("DEFAULT CURRENT_TIMESTAMP".to_string()) + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + // Replace the sample below with your own migration scripts + manager + .drop_table(Table::drop().table(AdminTrail::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum AdminTrail { + Table, + Id, + Caller, + ImitatingUser, + Endpoint, + Payload, + Timestamp +} + + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum User { + Table, + Id, + Address, + Description, + Email, +} \ No newline at end of file