2025-03-20 09:22:31 +01:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
use crate::domain::entities::user::{User, UserRole};
|
|
|
|
|
use crate::common::errors::DomainError;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum UserRepositoryError {
|
|
|
|
|
#[error("Usuario no encontrado: {0}")]
|
|
|
|
|
NotFound(String),
|
|
|
|
|
|
|
|
|
|
#[error("Usuario ya existe: {0}")]
|
|
|
|
|
AlreadyExists(String),
|
|
|
|
|
|
|
|
|
|
#[error("Error de base de datos: {0}")]
|
|
|
|
|
DatabaseError(String),
|
|
|
|
|
|
|
|
|
|
#[error("Error de validación: {0}")]
|
|
|
|
|
ValidationError(String),
|
|
|
|
|
|
|
|
|
|
#[error("Error de tiempo de espera: {0}")]
|
|
|
|
|
Timeout(String),
|
|
|
|
|
|
|
|
|
|
#[error("Operación no permitida: {0}")]
|
|
|
|
|
OperationNotAllowed(String),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub type UserRepositoryResult<T> = Result<T, UserRepositoryError>;
|
|
|
|
|
|
|
|
|
|
// Conversión de UserRepositoryError a DomainError
|
|
|
|
|
impl From<UserRepositoryError> for DomainError {
|
|
|
|
|
fn from(err: UserRepositoryError) -> Self {
|
|
|
|
|
match err {
|
|
|
|
|
UserRepositoryError::NotFound(msg) => {
|
|
|
|
|
DomainError::not_found("User", msg)
|
|
|
|
|
},
|
|
|
|
|
UserRepositoryError::AlreadyExists(msg) => {
|
|
|
|
|
DomainError::already_exists("User", msg)
|
|
|
|
|
},
|
|
|
|
|
UserRepositoryError::DatabaseError(msg) => {
|
|
|
|
|
DomainError::internal_error("Database", msg)
|
|
|
|
|
},
|
|
|
|
|
UserRepositoryError::ValidationError(msg) => {
|
2025-04-13 01:04:04 +02:00
|
|
|
DomainError::validation_error(msg)
|
2025-03-20 09:22:31 +01:00
|
|
|
},
|
|
|
|
|
UserRepositoryError::Timeout(msg) => {
|
|
|
|
|
DomainError::timeout("Database", msg)
|
|
|
|
|
},
|
|
|
|
|
UserRepositoryError::OperationNotAllowed(msg) => {
|
|
|
|
|
DomainError::access_denied("User", msg)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
pub trait UserRepository: Send + Sync + 'static {
|
|
|
|
|
/// Crea un nuevo usuario
|
|
|
|
|
async fn create_user(&self, user: User) -> UserRepositoryResult<User>;
|
|
|
|
|
|
|
|
|
|
/// Obtiene un usuario por ID
|
|
|
|
|
async fn get_user_by_id(&self, id: &str) -> UserRepositoryResult<User>;
|
|
|
|
|
|
|
|
|
|
/// Obtiene un usuario por nombre de usuario
|
|
|
|
|
async fn get_user_by_username(&self, username: &str) -> UserRepositoryResult<User>;
|
|
|
|
|
|
|
|
|
|
/// Obtiene un usuario por correo electrónico
|
|
|
|
|
async fn get_user_by_email(&self, email: &str) -> UserRepositoryResult<User>;
|
|
|
|
|
|
|
|
|
|
/// Actualiza un usuario existente
|
|
|
|
|
async fn update_user(&self, user: User) -> UserRepositoryResult<User>;
|
|
|
|
|
|
|
|
|
|
/// Actualiza solo el uso de almacenamiento de un usuario
|
|
|
|
|
async fn update_storage_usage(&self, user_id: &str, usage_bytes: i64) -> UserRepositoryResult<()>;
|
|
|
|
|
|
|
|
|
|
/// Actualiza la fecha de último inicio de sesión
|
|
|
|
|
async fn update_last_login(&self, user_id: &str) -> UserRepositoryResult<()>;
|
|
|
|
|
|
|
|
|
|
/// Lista usuarios con paginación
|
|
|
|
|
async fn list_users(&self, limit: i64, offset: i64) -> UserRepositoryResult<Vec<User>>;
|
|
|
|
|
|
|
|
|
|
/// Activa o desactiva un usuario
|
|
|
|
|
async fn set_user_active_status(&self, user_id: &str, active: bool) -> UserRepositoryResult<()>;
|
|
|
|
|
|
|
|
|
|
/// Cambia la contraseña de un usuario
|
|
|
|
|
async fn change_password(&self, user_id: &str, password_hash: &str) -> UserRepositoryResult<()>;
|
|
|
|
|
|
|
|
|
|
/// Cambia el rol de un usuario
|
|
|
|
|
async fn change_role(&self, user_id: &str, role: UserRole) -> UserRepositoryResult<()>;
|
|
|
|
|
|
2025-04-12 18:58:21 +02:00
|
|
|
/// Lista usuarios por rol (admin o user)
|
|
|
|
|
async fn list_users_by_role(&self, role: &str) -> UserRepositoryResult<Vec<User>>;
|
|
|
|
|
|
2025-03-20 09:22:31 +01:00
|
|
|
/// Elimina un usuario
|
|
|
|
|
async fn delete_user(&self, user_id: &str) -> UserRepositoryResult<()>;
|
2026-02-10 20:32:32 +01:00
|
|
|
|
|
|
|
|
/// Finds a user by OIDC provider + subject pair
|
|
|
|
|
async fn get_user_by_oidc_subject(&self, provider: &str, subject: &str) -> UserRepositoryResult<User>;
|
2025-03-20 09:22:31 +01:00
|
|
|
}
|