Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ユーザー認証を追加する #303

Merged
merged 10 commits into from
Oct 21, 2023
683 changes: 344 additions & 339 deletions server/Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions server/domain/src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
pub mod form_repository;
pub mod user_repository;

pub trait Repositories: Send + Sync {
type ConcreteFormRepository: form_repository::FormRepository;
type ConcreteUserRepository: user_repository::UserRepository;

fn form_repository(&self) -> &Self::ConcreteFormRepository;
fn user_repository(&self) -> &Self::ConcreteUserRepository;
}
17 changes: 12 additions & 5 deletions server/domain/src/repository/form_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ use async_trait::async_trait;
use errors::Error;
use mockall::automock;

use crate::form::models::{
Form, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, FormUpdateTargets,
OffsetAndLimit, PostedAnswers,
use crate::{
form::models::{
Form, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, FormUpdateTargets,
OffsetAndLimit, PostedAnswers,
},
user::models::User,
};

#[automock]
#[async_trait]
pub trait FormRepository: Send + Sync + 'static {
async fn create(&self, title: FormTitle, description: FormDescription)
-> Result<FormId, Error>;
async fn create(
&self,
title: FormTitle,
description: FormDescription,
user: User,
) -> Result<FormId, Error>;
async fn list(&self, offset_and_limit: OffsetAndLimit) -> Result<Vec<Form>, Error>;
async fn get(&self, id: FormId) -> Result<Form, Error>;
async fn delete(&self, id: FormId) -> Result<FormId, Error>;
Expand Down
11 changes: 11 additions & 0 deletions server/domain/src/repository/user_repository.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use async_trait::async_trait;
use errors::Error;
use mockall::automock;

use crate::user::models::User;

#[automock]
#[async_trait]
pub trait UserRepository: Send + Sync + 'static {
async fn upsert_user(&self, user: &User) -> Result<(), Error>;
}
4 changes: 2 additions & 2 deletions server/domain/src/user/models.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct User {
pub name: String,
pub uuid: Uuid,
pub id: Uuid,
}
5 changes: 4 additions & 1 deletion server/entrypoint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ async fn main() -> anyhow::Result<()> {
.with_state(shared_repository.to_owned())
.route("/health", get(health_check))
.layer(layer)
.route_layer(middleware::from_fn(auth))
.route_layer(middleware::from_fn_with_state(
shared_repository.to_owned(),
auth,
))
.layer(
CorsLayer::new()
.allow_methods([Method::GET, Method::POST, Method::DELETE, Method::PATCH])
Expand Down
17 changes: 15 additions & 2 deletions server/infra/entities/src/answers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub form_id: i32,
#[sea_orm(column_type = "Binary(BlobSize::Blob(Some(16)))")]
pub user: Vec<u8>,
pub user: i32,
pub title: String,
pub time_stamp: DateTimeUtc,
}
Expand All @@ -26,6 +25,14 @@ pub enum Relation {
FormMetaData,
#[sea_orm(has_many = "super::real_answers::Entity")]
RealAnswers,
#[sea_orm(
belongs_to = "super::users::Entity",
from = "Column::User",
to = "super::users::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
Users,
}

impl Related<super::form_meta_data::Entity> for Entity {
Expand All @@ -40,4 +47,10 @@ impl Related<super::real_answers::Entity> for Entity {
}
}

impl Related<super::users::Entity> for Entity {
fn to() -> RelationDef {
Relation::Users.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
18 changes: 18 additions & 0 deletions server/infra/entities/src/form_meta_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub struct Model {
pub title: String,
pub description: Option<String>,
pub created_at: DateTimeUtc,
pub created_by: i32,
pub updated_at: DateTimeUtc,
pub updated_by: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand All @@ -25,6 +27,22 @@ pub enum Relation {
FormWebhooks,
#[sea_orm(has_many = "super::response_period::Entity")]
ResponsePeriod,
#[sea_orm(
belongs_to = "super::users::Entity",
from = "Column::CreatedBy",
to = "super::users::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
Users2,
#[sea_orm(
belongs_to = "super::users::Entity",
from = "Column::UpdatedBy",
to = "super::users::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
Users1,
}

impl Related<super::answers::Entity> for Entity {
Expand Down
1 change: 1 addition & 0 deletions server/infra/entities/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pub mod form_webhooks;
pub mod real_answers;
pub mod response_period;
pub mod sea_orm_active_enums;
pub mod users;
1 change: 1 addition & 0 deletions server/infra/entities/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pub use super::{
form_choices::Entity as FormChoices, form_meta_data::Entity as FormMetaData,
form_questions::Entity as FormQuestions, form_webhooks::Entity as FormWebhooks,
real_answers::Entity as RealAnswers, response_period::Entity as ResponsePeriod,
users::Entity as Users,
};
27 changes: 27 additions & 0 deletions server/infra/entities/src/users.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "users")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(column_type = "Binary(BlobSize::Blob(Some(16)))")]
pub uuid: Vec<u8>,
pub name: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::answers::Entity")]
Answers,
}

impl Related<super::answers::Entity> for Entity {
fn to() -> RelationDef {
Relation::Answers.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
1 change: 1 addition & 0 deletions server/infra/resource/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod components;
pub mod config;
pub mod connection;
pub mod form;
pub mod user;
18 changes: 15 additions & 3 deletions server/infra/resource/src/database/components.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use async_trait::async_trait;
use domain::form::models::{
FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, FormUpdateTargets,
OffsetAndLimit, PostedAnswers,
use domain::{
form::models::{
FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, FormUpdateTargets,
OffsetAndLimit, PostedAnswers,
},
user::models::User,
};
use errors::infra::InfraError;
use mockall::automock;
Expand All @@ -11,10 +14,12 @@ use crate::dto::{FormDto, PostedAnswersDto};
#[async_trait]
pub trait DatabaseComponents: Send + Sync {
type ConcreteFormDatabase: FormDatabase;
type ConcreteUserDatabase: UserDatabase;
type TransactionAcrossComponents: Send + Sync;

async fn begin_transaction(&self) -> anyhow::Result<Self::TransactionAcrossComponents>;
fn form(&self) -> &Self::ConcreteFormDatabase;
fn user(&self) -> &Self::ConcreteUserDatabase;
}

#[automock]
Expand All @@ -24,6 +29,7 @@ pub trait FormDatabase: Send + Sync {
&self,
title: FormTitle,
description: FormDescription,
user: User,
) -> Result<FormId, InfraError>;
async fn list(&self, offset_and_limit: OffsetAndLimit) -> Result<Vec<FormDto>, InfraError>;
async fn get(&self, form_id: FormId) -> Result<FormDto, InfraError>;
Expand All @@ -38,3 +44,9 @@ pub trait FormDatabase: Send + Sync {
async fn create_questions(&self, questions: FormQuestionUpdateSchema)
-> Result<(), InfraError>;
}

#[automock]
#[async_trait]
pub trait UserDatabase: Send + Sync {
async fn upsert_user(&self, user: &User) -> Result<(), InfraError>;
}
5 changes: 5 additions & 0 deletions server/infra/resource/src/database/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl ConnectionPool {
#[async_trait]
impl DatabaseComponents for ConnectionPool {
type ConcreteFormDatabase = Self;
type ConcreteUserDatabase = Self;
type TransactionAcrossComponents = DatabaseTransaction;

async fn begin_transaction(&self) -> anyhow::Result<Self::TransactionAcrossComponents> {
Expand All @@ -51,4 +52,8 @@ impl DatabaseComponents for ConnectionPool {
fn form(&self) -> &Self::ConcreteFormDatabase {
self
}

fn user(&self) -> &Self::ConcreteUserDatabase {
self
}
}
Loading