feat: bal and user permission checks
This commit is contained in:
parent
e3f954679a
commit
e078bffc25
13 changed files with 207 additions and 19 deletions
37
src/entities/bal.rs
Normal file
37
src/entities/bal.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize, utoipa::ToSchema)]
|
||||||
|
#[sea_orm(table_name = "BAL")]
|
||||||
|
#[schema(title="Book", as=entities::BAL)]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key, auto_increment = true)]
|
||||||
|
pub id: u32,
|
||||||
|
pub user_id: u32,
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {
|
||||||
|
#[sea_orm(has_many = "super::book_instance::Entity")]
|
||||||
|
BookInstance,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::book_instance::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::BookInstance.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Entity {
|
||||||
|
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
|
||||||
|
where C: ConnectionTrait,
|
||||||
|
{
|
||||||
|
match Self::find_by_id(id).one(db_conn).await {
|
||||||
|
Ok(res) => res,
|
||||||
|
Err(_) => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
@ -11,9 +11,20 @@ pub struct Model {
|
||||||
pub sold_price: Option<i32>,
|
pub sold_price: Option<i32>,
|
||||||
pub status: BookStatus,
|
pub status: BookStatus,
|
||||||
pub book_id: u32,
|
pub book_id: u32,
|
||||||
pub owner_id: u32
|
pub owner_id: u32,
|
||||||
|
pub bal_id: u32
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Entity {
|
||||||
|
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
|
||||||
|
where C: ConnectionTrait,
|
||||||
|
{
|
||||||
|
match Self::find_by_id(id).one(db_conn).await {
|
||||||
|
Ok(res) => res,
|
||||||
|
Err(_) => None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Missing: Bal
|
|
||||||
|
|
||||||
#[derive(EnumIter, DeriveActiveEnum, PartialEq, Eq ,Deserialize, Serialize, Clone, Copy, Debug, utoipa::ToSchema)]
|
#[derive(EnumIter, DeriveActiveEnum, PartialEq, Eq ,Deserialize, Serialize, Clone, Copy, Debug, utoipa::ToSchema)]
|
||||||
#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(1))")]
|
#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(1))")]
|
||||||
|
|
@ -40,6 +51,12 @@ pub enum Relation {
|
||||||
to = "super::owner::Column::Id"
|
to = "super::owner::Column::Id"
|
||||||
)]
|
)]
|
||||||
Owner,
|
Owner,
|
||||||
|
#[sea_orm(
|
||||||
|
belongs_to = "super::bal::Entity",
|
||||||
|
from = "Column::BalId",
|
||||||
|
to = "super::bal::Column::Id"
|
||||||
|
)]
|
||||||
|
Bal,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Related<super::book::Entity> for Entity {
|
impl Related<super::book::Entity> for Entity {
|
||||||
|
|
@ -54,4 +71,10 @@ impl Related<super::owner::Entity> for Entity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Related<super::bal::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::Bal.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
pub mod prelude;
|
pub mod prelude;
|
||||||
|
|
||||||
|
pub mod bal;
|
||||||
pub mod book;
|
pub mod book;
|
||||||
pub mod book_instance;
|
pub mod book_instance;
|
||||||
pub mod owner;
|
pub mod owner;
|
||||||
|
|
|
||||||
|
|
@ -7,15 +7,33 @@ use serde::{Deserialize, Serialize};
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
#[sea_orm(primary_key, auto_increment = true)]
|
#[sea_orm(primary_key, auto_increment = true)]
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
|
pub user_id: u32,
|
||||||
pub first_name: String,
|
pub first_name: String,
|
||||||
pub last_name: String,
|
pub last_name: String,
|
||||||
pub contact: String
|
pub contact: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Entity {
|
||||||
|
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
|
||||||
|
where C: ConnectionTrait,
|
||||||
|
{
|
||||||
|
match Self::find_by_id(id).one(db_conn).await {
|
||||||
|
Ok(res) => res,
|
||||||
|
Err(_) => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
pub enum Relation {
|
pub enum Relation {
|
||||||
#[sea_orm(has_many = "super::book_instance::Entity")]
|
#[sea_orm(has_many = "super::book_instance::Entity")]
|
||||||
BookInstance
|
BookInstance,
|
||||||
|
#[sea_orm(
|
||||||
|
belongs_to = "super::user::Entity",
|
||||||
|
from = "Column::UserId",
|
||||||
|
to = "super::user::Column::Id"
|
||||||
|
)]
|
||||||
|
User,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Related<super::book_instance::Entity> for Entity {
|
impl Related<super::book_instance::Entity> for Entity {
|
||||||
|
|
@ -24,4 +42,10 @@ impl Related<super::book_instance::Entity> for Entity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Related<super::user::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::User.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
|
|
||||||
|
pub use super::bal::Entity as Bal;
|
||||||
pub use super::book::Entity as Book;
|
pub use super::book::Entity as Book;
|
||||||
pub use super::book_instance::Entity as BookInstance;
|
pub use super::book_instance::Entity as BookInstance;
|
||||||
pub use super::owner::Entity as Owner;
|
pub use super::owner::Entity as Owner;
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,14 @@ impl Model {
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
pub enum Relation {
|
pub enum Relation {
|
||||||
|
#[sea_orm(has_many = "super::owner::Entity")]
|
||||||
|
Owner,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::owner::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::Owner.def()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,7 @@ pub async fn auth(State(state): State<Arc<AppState>>, Json(payload): Json<AuthPa
|
||||||
let claims = Claims {
|
let claims = Claims {
|
||||||
sub: user.username,
|
sub: user.username,
|
||||||
exp: 2000000000,
|
exp: 2000000000,
|
||||||
|
user_id: user.id
|
||||||
};
|
};
|
||||||
let token = encode(&Header::default(), &claims, &KEYS.encoding)
|
let token = encode(&Header::default(), &claims, &KEYS.encoding)
|
||||||
.map_err(|_| AuthError::TokenCreation)?;
|
.map_err(|_| AuthError::TokenCreation)?;
|
||||||
|
|
@ -121,6 +122,7 @@ impl Keys {
|
||||||
pub struct Claims {
|
pub struct Claims {
|
||||||
sub: String,
|
sub: String,
|
||||||
exp: usize,
|
exp: usize,
|
||||||
|
pub user_id: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, utoipa::ToSchema)]
|
#[derive(Debug, Serialize, utoipa::ToSchema)]
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,11 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use axum::{extract::{Path, State}, Json};
|
use axum::{extract::{Path, State}, Json};
|
||||||
use reqwest::{StatusCode};
|
use reqwest::{StatusCode};
|
||||||
use sea_orm::{ActiveModelTrait, ActiveValue::Set, EntityTrait, TryIntoModel};
|
use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set}, EntityTrait, TryIntoModel};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use utoipa::IntoParams;
|
use utoipa::IntoParams;
|
||||||
|
|
||||||
use crate::{entities::{book_instance, prelude::BookInstance}, AppState};
|
use crate::{entities::{book_instance, prelude::*}, routes::auth::Claims, utils::auth::{user_is_bal_owner, user_is_book_instance_owner}, AppState};
|
||||||
|
|
||||||
|
|
||||||
#[derive(IntoParams)]
|
#[derive(IntoParams)]
|
||||||
|
|
@ -43,6 +43,7 @@ pub async fn get_book_instance_by_id(
|
||||||
pub struct BookInstanceCreateParams {
|
pub struct BookInstanceCreateParams {
|
||||||
book_id: u32,
|
book_id: u32,
|
||||||
owner_id: u32,
|
owner_id: u32,
|
||||||
|
bal_id: u32,
|
||||||
price: i32,
|
price: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,6 +55,7 @@ pub struct BookInstanceCreateParams {
|
||||||
security(("jwt" = [])),
|
security(("jwt" = [])),
|
||||||
responses(
|
responses(
|
||||||
(status = OK, body = book_instance::Model, description = "Successfully created book instance"),
|
(status = OK, body = book_instance::Model, description = "Successfully created book instance"),
|
||||||
|
(status = FORBIDDEN, description = "You don't own the specified BAL"),
|
||||||
),
|
),
|
||||||
summary = "Create a new book instance",
|
summary = "Create a new book instance",
|
||||||
description = "Create a new book instance",
|
description = "Create a new book instance",
|
||||||
|
|
@ -61,12 +63,17 @@ pub struct BookInstanceCreateParams {
|
||||||
)]
|
)]
|
||||||
pub async fn create_book_instance(
|
pub async fn create_book_instance(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
|
claims: Claims,
|
||||||
Json(instance_payload): Json<BookInstanceCreateParams>,
|
Json(instance_payload): Json<BookInstanceCreateParams>,
|
||||||
) -> (StatusCode, Json<Option<book_instance::Model>>) {
|
) -> (StatusCode, Json<Option<book_instance::Model>>) {
|
||||||
|
if !user_is_bal_owner(claims.user_id, instance_payload.bal_id, state.db_conn.as_ref()).await {
|
||||||
|
return (StatusCode::FORBIDDEN, Json(None));
|
||||||
|
}
|
||||||
|
|
||||||
let book_instance = book_instance::ActiveModel {
|
let book_instance = book_instance::ActiveModel {
|
||||||
book_id: Set(instance_payload.book_id),
|
book_id: Set(instance_payload.book_id),
|
||||||
owner_id: Set(instance_payload.owner_id),
|
owner_id: Set(instance_payload.owner_id),
|
||||||
|
bal_id: Set(instance_payload.bal_id),
|
||||||
price: Set(instance_payload.price),
|
price: Set(instance_payload.price),
|
||||||
status: Set(book_instance::BookStatus::Available),
|
status: Set(book_instance::BookStatus::Available),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|
@ -99,6 +106,7 @@ pub struct BookInstanceUpdateParams {
|
||||||
responses(
|
responses(
|
||||||
(status = OK, body = book_instance::Model, description = "Successfully updated book instance"),
|
(status = OK, body = book_instance::Model, description = "Successfully updated book instance"),
|
||||||
(status = NOT_FOUND, description = "No book instance with specified id was found"),
|
(status = NOT_FOUND, description = "No book instance with specified id was found"),
|
||||||
|
(status = FORBIDDEN, description = "You don't own the specified book instance"),
|
||||||
),
|
),
|
||||||
summary = "Update a book instance",
|
summary = "Update a book instance",
|
||||||
description = "Update a book instance",
|
description = "Update a book instance",
|
||||||
|
|
@ -106,9 +114,13 @@ pub struct BookInstanceUpdateParams {
|
||||||
)]
|
)]
|
||||||
pub async fn update_book_instance(
|
pub async fn update_book_instance(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
|
claims: Claims,
|
||||||
Path(id): Path<u32>,
|
Path(id): Path<u32>,
|
||||||
Json(instance_payload): Json<BookInstanceUpdateParams>,
|
Json(instance_payload): Json<BookInstanceUpdateParams>,
|
||||||
) -> (StatusCode, Json<Option<book_instance::Model>>) {
|
) -> (StatusCode, Json<Option<book_instance::Model>>) {
|
||||||
|
if !user_is_book_instance_owner(claims.user_id, id, state.db_conn.as_ref()).await {
|
||||||
|
return (StatusCode::FORBIDDEN, Json(None));
|
||||||
|
}
|
||||||
|
|
||||||
if let Ok(Some(book_instance)) = BookInstance::find_by_id(id).one(state.db_conn.as_ref()).await {
|
if let Ok(Some(book_instance)) = BookInstance::find_by_id(id).one(state.db_conn.as_ref()).await {
|
||||||
let mut book_instance: book_instance::ActiveModel = book_instance.into();
|
let mut book_instance: book_instance::ActiveModel = book_instance.into();
|
||||||
|
|
@ -162,9 +174,13 @@ pub struct BookInstanceSaleParams {
|
||||||
)]
|
)]
|
||||||
pub async fn sell_book_instance(
|
pub async fn sell_book_instance(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
|
claims: Claims,
|
||||||
Path(id): Path<u32>,
|
Path(id): Path<u32>,
|
||||||
Json(instance_payload): Json<BookInstanceSaleParams>,
|
Json(instance_payload): Json<BookInstanceSaleParams>,
|
||||||
) -> (StatusCode, Json<Option<book_instance::Model>>) {
|
) -> (StatusCode, Json<Option<book_instance::Model>>) {
|
||||||
|
if !user_is_book_instance_owner(claims.user_id, id, state.db_conn.as_ref()).await {
|
||||||
|
return (StatusCode::FORBIDDEN, Json(None));
|
||||||
|
}
|
||||||
|
|
||||||
if let Ok(Some(book_instance)) = BookInstance::find_by_id(id).one(state.db_conn.as_ref()).await {
|
if let Ok(Some(book_instance)) = BookInstance::find_by_id(id).one(state.db_conn.as_ref()).await {
|
||||||
let mut book_instance: book_instance::ActiveModel = book_instance.into();
|
let mut book_instance: book_instance::ActiveModel = book_instance.into();
|
||||||
|
|
@ -185,6 +201,7 @@ pub async fn sell_book_instance(
|
||||||
(StatusCode::NOT_FOUND, Json(None))
|
(StatusCode::NOT_FOUND, Json(None))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
post,
|
post,
|
||||||
|
|
@ -193,6 +210,7 @@ pub async fn sell_book_instance(
|
||||||
security(("jwt" = [])),
|
security(("jwt" = [])),
|
||||||
responses(
|
responses(
|
||||||
(status = OK, description = "Successfully created book instances"),
|
(status = OK, description = "Successfully created book instances"),
|
||||||
|
(status = FORBIDDEN, description = "You don't own at least one specified bal of the book instances sent"),
|
||||||
),
|
),
|
||||||
summary = "Create new book instances in bulk",
|
summary = "Create new book instances in bulk",
|
||||||
description = "Create new book instances in bulk",
|
description = "Create new book instances in bulk",
|
||||||
|
|
@ -200,17 +218,27 @@ pub async fn sell_book_instance(
|
||||||
)]
|
)]
|
||||||
pub async fn bulk_create_book_instance(
|
pub async fn bulk_create_book_instance(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
|
claims: Claims,
|
||||||
Json(instance_payload): Json<Vec<BookInstanceCreateParams>>,
|
Json(instance_payload): Json<Vec<BookInstanceCreateParams>>,
|
||||||
) -> StatusCode {
|
) -> StatusCode {
|
||||||
|
for i in &instance_payload {
|
||||||
|
if !user_is_bal_owner(claims.user_id, i.bal_id, state.db_conn.as_ref()).await {
|
||||||
|
return StatusCode::FORBIDDEN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let instances = instance_payload
|
let instances = instance_payload
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|p| book_instance::ActiveModel {
|
.map(|p| {
|
||||||
book_id: Set(p.book_id),
|
book_instance::ActiveModel {
|
||||||
owner_id: Set(p.owner_id),
|
book_id: Set(p.book_id),
|
||||||
price: Set(p.price),
|
owner_id: Set(p.owner_id),
|
||||||
status: Set(book_instance::BookStatus::Available),
|
bal_id: Set(p.bal_id),
|
||||||
..Default::default()
|
price: Set(p.price),
|
||||||
|
status: Set(book_instance::BookStatus::Available),
|
||||||
|
id: NotSet,
|
||||||
|
sold_price: NotSet
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
match BookInstance::insert_many(instances).exec(state.db_conn.as_ref()).await {
|
match BookInstance::insert_many(instances).exec(state.db_conn.as_ref()).await {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set, Unchanged}, EntityTra
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use utoipa::IntoParams;
|
use utoipa::IntoParams;
|
||||||
|
|
||||||
use crate::{entities::{owner, prelude::Owner}, utils::events::{Event, WebsocketMessage}, AppState};
|
use crate::{entities::{owner, prelude::Owner}, routes::auth::Claims, utils::events::{Event, WebsocketMessage}, AppState};
|
||||||
|
|
||||||
|
|
||||||
#[derive(IntoParams)]
|
#[derive(IntoParams)]
|
||||||
|
|
@ -61,6 +61,7 @@ pub struct OwnerCreateParams {
|
||||||
)]
|
)]
|
||||||
pub async fn create_owner(
|
pub async fn create_owner(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
|
claims: Claims,
|
||||||
Json(instance_payload): Json<OwnerCreateParams>,
|
Json(instance_payload): Json<OwnerCreateParams>,
|
||||||
) -> (StatusCode, Json<Option<owner::Model>>) {
|
) -> (StatusCode, Json<Option<owner::Model>>) {
|
||||||
|
|
||||||
|
|
@ -68,6 +69,7 @@ pub async fn create_owner(
|
||||||
first_name: Set(instance_payload.first_name),
|
first_name: Set(instance_payload.first_name),
|
||||||
last_name: Set(instance_payload.last_name),
|
last_name: Set(instance_payload.last_name),
|
||||||
contact: Set(instance_payload.contact),
|
contact: Set(instance_payload.contact),
|
||||||
|
user_id: Set(claims.user_id),
|
||||||
id: NotSet
|
id: NotSet
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -101,7 +103,8 @@ pub struct OwnerUpdateParams {
|
||||||
security(("jwt" = [])),
|
security(("jwt" = [])),
|
||||||
responses(
|
responses(
|
||||||
(status = OK, body = owner::Model, description = "Successfully updated owner"),
|
(status = OK, body = owner::Model, description = "Successfully updated owner"),
|
||||||
(status = NOT_FOUND, description = "No owner with this id exists in the database")
|
(status = NOT_FOUND, description = "No owner with this id exists in the database"),
|
||||||
|
(status = FORBIDDEN, description = "The owner doesn't belong to this account"),
|
||||||
),
|
),
|
||||||
summary = "Update an owner",
|
summary = "Update an owner",
|
||||||
description = "Update an owner",
|
description = "Update an owner",
|
||||||
|
|
@ -109,11 +112,15 @@ pub struct OwnerUpdateParams {
|
||||||
)]
|
)]
|
||||||
pub async fn update_owner(
|
pub async fn update_owner(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
|
claims: Claims,
|
||||||
Path(id): Path<u32>,
|
Path(id): Path<u32>,
|
||||||
Json(instance_payload): Json<OwnerUpdateParams>,
|
Json(instance_payload): Json<OwnerUpdateParams>,
|
||||||
) -> (StatusCode, Json<Option<owner::Model>>) {
|
) -> (StatusCode, Json<Option<owner::Model>>) {
|
||||||
|
|
||||||
if let Ok(Some(owner)) = Owner::find_by_id(id).one(state.db_conn.as_ref()).await {
|
if let Ok(Some(owner)) = Owner::find_by_id(id).one(state.db_conn.as_ref()).await {
|
||||||
|
if owner.user_id != claims.user_id {
|
||||||
|
return (StatusCode::FORBIDDEN, Json(None));
|
||||||
|
}
|
||||||
let mut owner: owner::ActiveModel = owner.into();
|
let mut owner: owner::ActiveModel = owner.into();
|
||||||
owner.first_name = match instance_payload.first_name {
|
owner.first_name = match instance_payload.first_name {
|
||||||
None => owner.first_name,
|
None => owner.first_name,
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use axum::{
|
||||||
}, response::IntoResponse
|
}, response::IntoResponse
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{utils::events, AppState};
|
use crate::{routes::auth::Claims, utils::events, AppState};
|
||||||
|
|
||||||
use futures_util::{sink::SinkExt, stream::StreamExt};
|
use futures_util::{sink::SinkExt, stream::StreamExt};
|
||||||
|
|
||||||
|
|
@ -27,14 +27,15 @@ use futures_util::{sink::SinkExt, stream::StreamExt};
|
||||||
pub async fn ws_handler(
|
pub async fn ws_handler(
|
||||||
ws: WebSocketUpgrade,
|
ws: WebSocketUpgrade,
|
||||||
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
||||||
|
claims: Claims,
|
||||||
State(state): State<Arc<AppState>>
|
State(state): State<Arc<AppState>>
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
log::debug!(target: "websocket", "{addr} connected.");
|
log::debug!(target: "websocket", "{addr} connected.");
|
||||||
ws.on_upgrade(move |socket| handle_socket(socket, addr, state))
|
ws.on_upgrade(move |socket| handle_socket(socket, addr, state, claims))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async fn handle_socket(mut socket: WebSocket, who: SocketAddr, state: Arc<AppState>) {
|
async fn handle_socket(mut socket: WebSocket, who: SocketAddr, state: Arc<AppState>, claims: Claims) {
|
||||||
if socket
|
if socket
|
||||||
.send(Message::Ping(Bytes::from_static(&[4, 2])))
|
.send(Message::Ping(Bytes::from_static(&[4, 2])))
|
||||||
.await
|
.await
|
||||||
|
|
@ -73,6 +74,9 @@ async fn handle_socket(mut socket: WebSocket, who: SocketAddr, state: Arc<AppSta
|
||||||
Ok(event) => {
|
Ok(event) => {
|
||||||
match event {
|
match event {
|
||||||
events::Event::WebsocketBroadcast(message) => {
|
events::Event::WebsocketBroadcast(message) => {
|
||||||
|
if !message.should_user_receive(claims.user_id) {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
log::debug!(target: "websocket", "Sent {message:?} to {who}");
|
log::debug!(target: "websocket", "Sent {message:?} to {who}");
|
||||||
let _ = sender.send(Message::Text(Utf8Bytes::from(message.to_json().to_string()))).await;
|
let _ = sender.send(Message::Text(Utf8Bytes::from(message.to_json().to_string()))).await;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
42
src/utils/auth.rs
Normal file
42
src/utils/auth.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
use sea_orm::ConnectionTrait;
|
||||||
|
|
||||||
|
use crate::entities::prelude::*;
|
||||||
|
|
||||||
|
pub async fn user_is_bal_owner<C>(
|
||||||
|
user_id: u32,
|
||||||
|
bal_id: u32,
|
||||||
|
db_conn: &C
|
||||||
|
) -> bool
|
||||||
|
where C: ConnectionTrait,
|
||||||
|
{
|
||||||
|
match Bal::get_by_id(db_conn, bal_id).await {
|
||||||
|
Some(bal) => user_id == bal.user_id,
|
||||||
|
None => false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn user_is_book_instance_owner<C>(
|
||||||
|
user_id: u32,
|
||||||
|
book_instance_id: u32,
|
||||||
|
db_conn: &C
|
||||||
|
) -> bool
|
||||||
|
where C: ConnectionTrait,
|
||||||
|
{
|
||||||
|
match BookInstance::get_by_id(db_conn, book_instance_id).await {
|
||||||
|
Some(instance) => user_is_bal_owner(user_id, instance.bal_id, db_conn).await,
|
||||||
|
None => false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn user_is_owner_owner<C>(
|
||||||
|
user_id: u32,
|
||||||
|
owner_id: u32,
|
||||||
|
db_conn: &C
|
||||||
|
) -> bool
|
||||||
|
where C: ConnectionTrait,
|
||||||
|
{
|
||||||
|
match Owner::get_by_id(db_conn, owner_id).await {
|
||||||
|
Some(owner) => owner.user_id == user_id,
|
||||||
|
None => false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,18 +11,28 @@ pub enum Event {
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum WebsocketMessage {
|
pub enum WebsocketMessage {
|
||||||
NewOwner(Arc<owner::Model>)
|
NewOwner(Arc<owner::Model>),
|
||||||
|
Ping
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WebsocketMessage {
|
impl WebsocketMessage {
|
||||||
pub fn to_json(self) -> Value {
|
pub fn to_json(&self) -> Value {
|
||||||
json!({
|
json!({
|
||||||
"type": match self {
|
"type": match self {
|
||||||
Self::NewOwner(_) => "new_owner",
|
Self::NewOwner(_) => "new_owner",
|
||||||
|
Self::Ping => "ping",
|
||||||
},
|
},
|
||||||
"data": match self {
|
"data": match self {
|
||||||
Self::NewOwner(owner) => json!(owner),
|
Self::NewOwner(owner) => json!(owner),
|
||||||
|
Self::Ping => json!(null),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn should_user_receive(&self, user_id: u32) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::NewOwner(owner) => owner.user_id == user_id,
|
||||||
|
_ => true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
pub mod auth;
|
||||||
pub mod cli;
|
pub mod cli;
|
||||||
pub mod events;
|
pub mod events;
|
||||||
pub mod open_library;
|
pub mod open_library;
|
||||||
|
|
|
||||||
Reference in a new issue