use super::error::*; use async_trait::async_trait; use serde::{Deserialize, Serialize}; use std::collections::HashSet; #[derive(PartialEq, Eq, Debug, Serialize, Deserialize)] #[cfg_attr(not(target_arch = "wasm32"), derive(sqlx::FromRow))] pub struct User { pub user_id: String, pub email: String, pub display_name: String, pub first_name: String, pub last_name: String, // pub avatar: ?, pub creation_date: chrono::DateTime, } impl Default for User { fn default() -> Self { use chrono::TimeZone; User { user_id: String::new(), email: String::new(), display_name: String::new(), first_name: String::new(), last_name: String::new(), creation_date: chrono::Utc.timestamp(0, 0), } } } #[derive(PartialEq, Eq, Debug, Serialize, Deserialize)] pub struct Group { pub display_name: String, pub users: Vec, } #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)] pub struct BindRequest { pub name: String, pub password: String, } #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)] pub enum RequestFilter { And(Vec), Or(Vec), Not(Box), Equality(String, String), } #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone, Default)] pub struct CreateUserRequest { // Same fields as User, but no creation_date, and with password. pub user_id: String, pub email: String, pub display_name: Option, pub first_name: Option, pub last_name: Option, } #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone, Default)] pub struct UpdateUserRequest { // Same fields as CreateUserRequest, but no with an extra layer of Option. pub user_id: String, pub email: Option, pub display_name: Option, pub first_name: Option, pub last_name: Option, } #[async_trait] pub trait LoginHandler: Clone + Send { async fn bind(&self, request: BindRequest) -> Result<()>; } #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct GroupId(pub i32); #[async_trait] pub trait BackendHandler: Clone + Send { async fn list_users(&self, filters: Option) -> Result>; async fn list_groups(&self) -> Result>; async fn get_user_details(&self, user_id: &str) -> Result; async fn create_user(&self, request: CreateUserRequest) -> Result<()>; async fn update_user(&self, request: UpdateUserRequest) -> Result<()>; async fn delete_user(&self, user_id: &str) -> Result<()>; async fn create_group(&self, group_name: &str) -> Result; async fn add_user_to_group(&self, user_id: &str, group_id: GroupId) -> Result<()>; async fn remove_user_from_group(&self, user_id: &str, group_id: GroupId) -> Result<()>; async fn get_user_groups(&self, user: &str) -> Result>; } #[cfg(test)] mockall::mock! { pub TestBackendHandler{} impl Clone for TestBackendHandler { fn clone(&self) -> Self; } #[async_trait] impl BackendHandler for TestBackendHandler { async fn list_users(&self, filters: Option) -> Result>; async fn list_groups(&self) -> Result>; async fn get_user_details(&self, user_id: &str) -> Result; async fn create_user(&self, request: CreateUserRequest) -> Result<()>; async fn update_user(&self, request: UpdateUserRequest) -> Result<()>; async fn delete_user(&self, user_id: &str) -> Result<()>; async fn create_group(&self, group_name: &str) -> Result; async fn get_user_groups(&self, user: &str) -> Result>; async fn add_user_to_group(&self, user_id: &str, group_id: GroupId) -> Result<()>; async fn remove_user_from_group(&self, user_id: &str, group_id: GroupId) -> Result<()>; } #[async_trait] impl LoginHandler for TestBackendHandler { async fn bind(&self, request: BindRequest) -> Result<()>; } }