From 702a79d1cf797fa75b2c878ac2b69599540d8a4b Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Fri, 4 Nov 2022 12:00:12 +0100 Subject: [PATCH] feat: AdminService struct --- api/src/guards/request/session_auth.rs | 15 +++--- core/src/error.rs | 72 ++++++-------------------- core/src/services/admin_service.rs | 35 +++++++++++++ core/src/services/mod.rs | 3 +- 4 files changed, 58 insertions(+), 67 deletions(-) create mode 100644 core/src/services/admin_service.rs diff --git a/api/src/guards/request/session_auth.rs b/api/src/guards/request/session_auth.rs index d7a763a..44063d4 100644 --- a/api/src/guards/request/session_auth.rs +++ b/api/src/guards/request/session_auth.rs @@ -1,5 +1,6 @@ use entity::candidate::Model as Candidate; use portfolio_core::sea_orm::prelude::Uuid; +use portfolio_core::services::admin_service::AdminService; use portfolio_core::services::candidate_service::CandidateService; use rocket::http::Status; use rocket::outcome::Outcome; @@ -57,17 +58,13 @@ impl<'r> FromRequest<'r> for AdminAuth { Err(_) => return Outcome::Failure((Status::BadRequest, None)), }; - let session = CandidateService::auth(conn, uuid).await; + let session = AdminService::auth(conn, uuid).await; match session { - Ok(model) => { - if model.is_admin { - Outcome::Success(AdminAuth(model)) - } else { - Outcome::Failure((Status::Forbidden, None)) - } - }, - Err(_) => Outcome::Failure((Status::Unauthorized, None)), + Ok(model) => Outcome::Success(AdminAuth(model)), + Err(e) => Outcome::Failure( + (Status::from_code(e.code()).unwrap_or(Status::InternalServerError), None) + ), } } diff --git a/core/src/error.rs b/core/src/error.rs index 0dace48..1923f34 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -1,45 +1,6 @@ -/* pub struct Status { - pub code: u16, -} - -pub const INVALID_CREDENTIALS_ERROR: ServiceError = ServiceError(Status { code: 401 }, - "Invalid credentials"); -pub const EXPIRED_SESSION_ERROR: ServiceError = ServiceError(Status { code: 401 }, - "Session expired, please login again"); - -pub const JWT_ERROR: ServiceError = ServiceError(Status { code: 500 }, - "Error while encoding JWT"); - -pub const USER_NOT_FOUND_ERROR: ServiceError = ServiceError(Status { code: 404 }, - "User not found"); - -pub const DB_ERROR: ServiceError = ServiceError(Status { code: 500 }, - "Database error"); - -pub const USER_NOT_FOUND_BY_JWT_ID: ServiceError = ServiceError(Status { code: 500 }, // User got somehow deleted - "User not found, please contact technical support"); // Shouldn't ever happen - -pub const USER_NOT_FOUND_BY_SESSION_ID: ServiceError = ServiceError(Status { code: 500 }, // User got somehow deleted - "User not found, please contact technical support"); // Shouldn't ever happen -pub struct ServiceError<'a>(pub Status, pub &'a str); */ - - -/* pub struct ServiceError { - pub code: u16, - pub message: String, -} - -impl ServiceError { - pub const InvalidCredentials: ServiceError = ServiceError { code: 401, message: "Invalid credentials".to_string() }; - pub const ExpiredSession: ServiceError = ServiceError { code: 401, message: "Session expired, please login again".to_string() }; - pub const JwtError: ServiceError = ServiceError { code: 500, message: "Error while encoding JWT".to_string() }; - pub const UserNotFound: ServiceError = ServiceError { code: 404, message: "User not found".to_string() }; - pub const DbError: ServiceError = ServiceError { code: 500, message: "Database error".to_string() }; - pub const UserNotFoundByJwtId: ServiceError = ServiceError { code: 500, message: "User not found, please contact technical support".to_string() }; - pub const UserNotFoundBySessionId: ServiceError = ServiceError { code: 500, message: "User not found, please contact technical support".to_string() }; -} */ pub enum ServiceError { InvalidCredentials, + Forbidden, ExpiredSession, JwtError, UserNotFound, @@ -49,28 +10,25 @@ pub enum ServiceError { } impl ServiceError { - pub fn code(&self) -> u16 { + fn code_and_message(&self) -> (u16, String) { match self { - ServiceError::InvalidCredentials => 401, - ServiceError::ExpiredSession => 401, - ServiceError::JwtError => 500, - ServiceError::UserNotFound => 404, - ServiceError::DbError => 500, - ServiceError::UserNotFoundByJwtId => 500, - ServiceError::UserNotFoundBySessionId => 500, + ServiceError::InvalidCredentials => (401, "Invalid credentials".to_string()), + ServiceError::Forbidden => (403, "Forbidden".to_string()), + ServiceError::ExpiredSession => (401, "Session expired, please login again".to_string()), + ServiceError::JwtError => (500, "Error while encoding JWT".to_string()), + ServiceError::UserNotFound => (404, "User not found".to_string()), + ServiceError::DbError => (500, "Database error".to_string()), + ServiceError::UserNotFoundByJwtId => (500, "User not found, please contact technical support".to_string()), + ServiceError::UserNotFoundBySessionId => (500, "User not found, please contact technical support".to_string()), } } + pub fn code(&self) -> u16 { + self.code_and_message().0 + } + pub fn message(&self) -> String { - match self { - ServiceError::InvalidCredentials => "Invalid credentials".to_string(), - ServiceError::ExpiredSession => "Session expired, please login again".to_string(), - ServiceError::JwtError => "Error while encoding JWT".to_string(), - ServiceError::UserNotFound => "User not found".to_string(), - ServiceError::DbError => "Database error".to_string(), - ServiceError::UserNotFoundByJwtId => "User not found, please contact technical support".to_string(), - ServiceError::UserNotFoundBySessionId => "User not found, please contact technical support".to_string(), - } + self.code_and_message().1 } } diff --git a/core/src/services/admin_service.rs b/core/src/services/admin_service.rs new file mode 100644 index 0000000..fdadbe4 --- /dev/null +++ b/core/src/services/admin_service.rs @@ -0,0 +1,35 @@ +use entity::candidate; +use sea_orm::{DbConn, prelude::Uuid}; + +use crate::error::ServiceError; + +use super::session_service::SessionService; + +pub struct AdminService; + +impl AdminService { + pub async fn login( + db: &DbConn, + user_id: i32, + password: String, + ip_addr: String + ) -> Result { + SessionService::new_session(db, user_id, password, ip_addr).await + } + + pub async fn auth( + db: &DbConn, + session_uuid: Uuid, + ) -> Result { + match SessionService::auth_user_session(db, session_uuid).await { + Ok(user) => { + if user.is_admin { + Ok(user) + } else { + Err(ServiceError::Forbidden) + } + }, + Err(e) => Err(e) + } + } +} \ No newline at end of file diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs index 37693d2..d6cd137 100644 --- a/core/src/services/mod.rs +++ b/core/src/services/mod.rs @@ -1,2 +1,3 @@ pub mod session_service; -pub mod candidate_service; \ No newline at end of file +pub mod candidate_service; +pub mod admin_service; \ No newline at end of file