fix auth errors and add primigenial paper trash
This commit is contained in:
@@ -2,4 +2,4 @@ pub mod file;
|
||||
pub mod folder;
|
||||
pub mod user;
|
||||
pub mod session;
|
||||
|
||||
pub mod trashed_item;
|
||||
@@ -0,0 +1,48 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum TrashedItemType {
|
||||
File,
|
||||
Folder,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TrashedItem {
|
||||
pub id: Uuid,
|
||||
pub original_id: Uuid,
|
||||
pub user_id: Uuid,
|
||||
pub item_type: TrashedItemType,
|
||||
pub name: String,
|
||||
pub original_path: String,
|
||||
pub trashed_at: DateTime<Utc>,
|
||||
pub deletion_date: DateTime<Utc>, // Fecha de eliminación permanente automática
|
||||
}
|
||||
|
||||
impl TrashedItem {
|
||||
pub fn new(
|
||||
original_id: Uuid,
|
||||
user_id: Uuid,
|
||||
item_type: TrashedItemType,
|
||||
name: String,
|
||||
original_path: String,
|
||||
retention_days: u32,
|
||||
) -> Self {
|
||||
let now = Utc::now();
|
||||
Self {
|
||||
id: Uuid::new_v4(),
|
||||
original_id,
|
||||
user_id,
|
||||
item_type,
|
||||
name,
|
||||
original_path,
|
||||
trashed_at: now,
|
||||
deletion_date: now + chrono::Duration::days(retention_days as i64),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn days_until_deletion(&self) -> i64 {
|
||||
let now = Utc::now();
|
||||
(self.deletion_date - now).num_days().max(0)
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,8 @@ pub enum UserError {
|
||||
|
||||
pub type UserResult<T> = Result<T, UserError>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, sqlx::Type)]
|
||||
#[sqlx(rename_all = "lowercase")]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
// We'll handle conversion manually for now until the type is properly set up in the database
|
||||
pub enum UserRole {
|
||||
Admin,
|
||||
User,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use async_trait::async_trait;
|
||||
use uuid::Uuid;
|
||||
use crate::domain::entities::file::File;
|
||||
use crate::domain::services::path_service::StoragePath;
|
||||
use crate::common::errors::DomainError;
|
||||
@@ -18,6 +19,9 @@ pub enum FileRepositoryError {
|
||||
#[error("Invalid file path: {0}")]
|
||||
InvalidPath(String),
|
||||
|
||||
#[error("Operation not supported: {0}")]
|
||||
OperationNotSupported(String),
|
||||
|
||||
#[error("IO Error: {0}")]
|
||||
IoError(#[from] std::io::Error),
|
||||
|
||||
@@ -90,4 +94,13 @@ pub trait FileRepository: Send + Sync + 'static {
|
||||
|
||||
/// Gets the storage path for a file
|
||||
async fn get_file_path(&self, id: &str) -> FileRepositoryResult<StoragePath>;
|
||||
|
||||
/// Moves a file to trash
|
||||
async fn move_to_trash(&self, file_id: &str) -> FileRepositoryResult<()>;
|
||||
|
||||
/// Restores a file from trash
|
||||
async fn restore_from_trash(&self, file_id: &str, original_path: &str) -> FileRepositoryResult<()>;
|
||||
|
||||
/// Permanently deletes a file (used for trash cleanup)
|
||||
async fn delete_file_permanently(&self, file_id: &str) -> FileRepositoryResult<()>;
|
||||
}
|
||||
@@ -16,6 +16,9 @@ pub enum FolderRepositoryError {
|
||||
#[error("Invalid folder path: {0}")]
|
||||
InvalidPath(String),
|
||||
|
||||
#[error("Operation not supported: {0}")]
|
||||
OperationNotSupported(String),
|
||||
|
||||
#[error("IO Error: {0}")]
|
||||
IoError(#[from] std::io::Error),
|
||||
|
||||
@@ -88,4 +91,13 @@ pub trait FolderRepository: Send + Sync + 'static {
|
||||
#[deprecated(note = "Use get_folder_by_storage_path instead")]
|
||||
#[allow(dead_code)]
|
||||
async fn get_folder_by_path(&self, path: &std::path::PathBuf) -> FolderRepositoryResult<Folder>;
|
||||
|
||||
/// Moves a folder to trash
|
||||
async fn move_to_trash(&self, folder_id: &str) -> FolderRepositoryResult<()>;
|
||||
|
||||
/// Restores a folder from trash
|
||||
async fn restore_from_trash(&self, folder_id: &str, original_path: &str) -> FolderRepositoryResult<()>;
|
||||
|
||||
/// Permanently deletes a folder (used for trash cleanup)
|
||||
async fn delete_folder_permanently(&self, folder_id: &str) -> FolderRepositoryResult<()>;
|
||||
}
|
||||
@@ -2,4 +2,4 @@ pub mod file_repository;
|
||||
pub mod folder_repository;
|
||||
pub mod user_repository;
|
||||
pub mod session_repository;
|
||||
|
||||
pub mod trash_repository;
|
||||
@@ -0,0 +1,17 @@
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::domain::entities::trashed_item::TrashedItem;
|
||||
use crate::common::errors::Result;
|
||||
|
||||
#[async_trait]
|
||||
pub trait TrashRepository: Send + Sync {
|
||||
async fn add_to_trash(&self, item: &TrashedItem) -> Result<()>;
|
||||
async fn get_trash_items(&self, user_id: &Uuid) -> Result<Vec<TrashedItem>>;
|
||||
async fn get_trash_item(&self, id: &Uuid, user_id: &Uuid) -> Result<Option<TrashedItem>>;
|
||||
async fn restore_from_trash(&self, id: &Uuid, user_id: &Uuid) -> Result<()>;
|
||||
async fn delete_permanently(&self, id: &Uuid, user_id: &Uuid) -> Result<()>;
|
||||
async fn clear_trash(&self, user_id: &Uuid) -> Result<()>;
|
||||
async fn get_expired_items(&self) -> Result<Vec<TrashedItem>>;
|
||||
}
|
||||
@@ -82,6 +82,14 @@ impl AuthService {
|
||||
pub fn generate_access_token(&self, user: &User) -> Result<String, AuthError> {
|
||||
let now = Utc::now().timestamp();
|
||||
|
||||
// Log information for debugging
|
||||
tracing::debug!(
|
||||
"Generating token for user: {}, id: {}, role: {}",
|
||||
user.username(),
|
||||
user.id(),
|
||||
user.role()
|
||||
);
|
||||
|
||||
let claims = TokenClaims {
|
||||
sub: user.id().to_string(),
|
||||
exp: now + self.access_token_expiry,
|
||||
@@ -92,12 +100,23 @@ impl AuthService {
|
||||
role: format!("{}", user.role()),
|
||||
};
|
||||
|
||||
encode(
|
||||
// Log JWT claims for debugging
|
||||
tracing::debug!("JWT claims: sub={}, exp={}, iat={}", claims.sub, claims.exp, claims.iat);
|
||||
|
||||
match encode(
|
||||
&Header::default(),
|
||||
&claims,
|
||||
&EncodingKey::from_secret(self.jwt_secret.as_bytes())
|
||||
)
|
||||
.map_err(|e| AuthError::InternalError(format!("Error al generar token: {}", e)))
|
||||
) {
|
||||
Ok(token) => {
|
||||
tracing::debug!("Token generated successfully, length: {}", token.len());
|
||||
Ok(token)
|
||||
},
|
||||
Err(e) => {
|
||||
tracing::error!("Error generating token: {}", e);
|
||||
Err(AuthError::InternalError(format!("Error al generar token: {}", e)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_refresh_token(&self) -> String {
|
||||
|
||||
Reference in New Issue
Block a user