mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-03-14 18:02:37 +00:00
70 lines
2.3 KiB
Rust
70 lines
2.3 KiB
Rust
//! # Admin account controller
|
|
//!
|
|
//! @author Pierre Hubert
|
|
|
|
use crate::api_data::admin::admin_auth_options::AdminAuthOptions;
|
|
use crate::api_data::admin::admin_auth_success::AdminAuthSuccess;
|
|
use crate::api_data::admin::admin_id_api::AdminIDAPI;
|
|
use crate::api_data::admin::admin_info_api::AdminInfoAPI;
|
|
use crate::data::base_request_handler::BaseRequestHandler;
|
|
use crate::data::http_request_handler::HttpRequestHandler;
|
|
use crate::helpers::{admin_access_token_helper, admin_account_helper};
|
|
use crate::routes::RequestResult;
|
|
use crate::utils::date_utils::time;
|
|
|
|
/// Get admin auth options
|
|
pub fn get_auth_options(r: &mut HttpRequestHandler) -> RequestResult {
|
|
let mail = r.post_email("mail")?;
|
|
let admin = admin_account_helper::find_admin_by_email(&mail)?;
|
|
|
|
r.set_response(AdminAuthOptions::new(&admin))
|
|
}
|
|
|
|
/// Login admin using a reset token
|
|
pub fn auth_with_reset_token(r: &mut HttpRequestHandler) -> RequestResult {
|
|
let reset_token = r.post_string("token")?;
|
|
let admin = admin_account_helper::find_admin_by_email(&r.post_email("mail")?)?;
|
|
|
|
let token = r.some_or_internal_error(
|
|
admin.reset_token,
|
|
"Specified user has not valid reset token for now!",
|
|
)?;
|
|
|
|
if !token.token.eq(&reset_token) || time() > token.expire {
|
|
return r.forbidden("Specified reset token is invalid!".to_string());
|
|
}
|
|
|
|
let token = admin_access_token_helper::create(admin.id)?;
|
|
|
|
r.set_response(AdminAuthSuccess::new(token))
|
|
}
|
|
|
|
/// Sign out current admin
|
|
pub fn sign_out(r: &mut HttpRequestHandler) -> RequestResult {
|
|
admin_access_token_helper::destroy(r.admin_id()?)?;
|
|
|
|
r.ok()
|
|
}
|
|
|
|
/// Get current admin ID
|
|
pub fn get_admin_id(r: &mut HttpRequestHandler) -> RequestResult {
|
|
r.set_response(AdminIDAPI::new(r.admin_id()?))
|
|
}
|
|
|
|
/// Get current admin information
|
|
pub fn get_admin_info(r: &mut HttpRequestHandler) -> RequestResult {
|
|
let admin = match r.has_post_parameter("id") {
|
|
false => admin_account_helper::find_admin_by_id(r.admin_id()?)?,
|
|
true => {
|
|
let admin_id = r.post_admin_id("id")?;
|
|
|
|
if admin_id == r.admin_id()? {
|
|
admin_account_helper::find_admin_by_id(admin_id)?
|
|
} else {
|
|
unimplemented!();
|
|
}
|
|
}
|
|
};
|
|
|
|
r.set_response(AdminInfoAPI::new(&admin))
|
|
} |