feat: AdminService struct

This commit is contained in:
Sebastian Pravda 2022-11-04 12:00:12 +01:00
parent 130fd1fa60
commit 702a79d1cf
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
4 changed files with 58 additions and 67 deletions

View file

@ -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)
),
}
}

View file

@ -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
}
}

View file

@ -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<String, ServiceError> {
SessionService::new_session(db, user_id, password, ip_addr).await
}
pub async fn auth(
db: &DbConn,
session_uuid: Uuid,
) -> Result<candidate::Model, ServiceError> {
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)
}
}
}

View file

@ -1,2 +1,3 @@
pub mod session_service;
pub mod candidate_service;
pub mod candidate_service;
pub mod admin_service;