1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Can get current admin id

This commit is contained in:
Pierre HUBERT 2021-05-11 17:17:52 +02:00
parent d8ec093786
commit ef0845f075
8 changed files with 70 additions and 4 deletions

View File

@ -0,0 +1,18 @@
//! # Admin ID API
//!
//! @author Pierre Hubert
use serde::Serialize;
use crate::data::admin::AdminID;
#[derive(Serialize)]
pub struct AdminIDAPI {
id: u64,
}
impl AdminIDAPI {
pub fn new(id: AdminID) -> Self {
Self { id: id.id() }
}
}

View File

@ -4,3 +4,4 @@
pub mod admin_auth_options;
pub mod admin_auth_success;
pub mod admin_id_api;

View File

@ -4,6 +4,7 @@
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::data::base_request_handler::BaseRequestHandler;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::helpers::{admin_access_token_helper, admin_account_helper};
@ -36,3 +37,8 @@ pub fn auth_with_reset_token(r: &mut HttpRequestHandler) -> RequestResult {
r.set_response(AdminAuthSuccess::new(token))
}
/// Get current admin ID
pub fn get_admin_id(r: &mut HttpRequestHandler) -> RequestResult {
r.set_response(AdminIDAPI::new(r.admin_id()?))
}

View File

@ -33,6 +33,7 @@ use crate::utils::string_utils::{check_emoji_code, check_html_color, check_url,
use crate::utils::user_data_utils::{generate_new_user_data_file_name, prepare_file_creation, user_data_path};
use crate::utils::virtual_directories_utils;
use crate::utils::zip_utils::is_valid_zip;
use crate::data::admin::AdminID;
#[derive(Serialize)]
struct SuccessMessage {
@ -73,6 +74,8 @@ pub trait BaseRequestHandler {
self.user_access_token().map(|u| &u.user_id)
}
/// Get an admin ID, if available
fn admin_id_opt(&self) -> Option<AdminID>;
/// Success message
fn success(&mut self, message: &str) -> RequestResult {
@ -200,6 +203,12 @@ pub trait BaseRequestHandler {
}
/// Get current admin ID, returning an error in case of error
fn admin_id(&self) -> Res<AdminID> {
self.admin_id_opt().ok_or(ExecError::boxed_new("Could not get required admin ID!"))
}
/// Check if a POST parameter was present in the request or not
fn has_post_parameter(&self, name: &str) -> bool {
self.post_parameter_opt(name).is_some()

View File

@ -7,12 +7,13 @@ use actix_web::http::{HeaderName, HeaderValue, StatusCode};
use serde::Serialize;
use crate::api_data::http_error::HttpError;
use crate::data::admin::{AdminAccessToken, AdminID};
use crate::data::api_client::APIClient;
use crate::data::base_request_handler::{BaseRequestHandler, RequestValue};
use crate::data::config::conf;
use crate::data::error::{Res, ResultBoxError};
use crate::data::user_token::UserAccessToken;
use crate::helpers::{account_helper, api_helper};
use crate::helpers::{account_helper, api_helper, admin_access_token_helper};
use crate::routes::RequestResult;
/// Http request handler
@ -26,6 +27,7 @@ pub struct HttpRequestHandler {
headers: HashMap<String, String>,
client: Option<APIClient>,
curr_user_token: Option<UserAccessToken>,
curr_admin_token: Option<AdminAccessToken>,
}
impl HttpRequestHandler {
@ -38,6 +40,7 @@ impl HttpRequestHandler {
headers: HashMap::new(),
client: None,
curr_user_token: None,
curr_admin_token: None,
}
}
@ -140,6 +143,15 @@ impl HttpRequestHandler {
self.bad_request("Invalid origin for admin requests!".to_string())
}
/// Check admin access token
pub fn check_admin_access_token(&mut self) -> Res {
let token = self.post_string("token")?;
self.curr_admin_token = Some(admin_access_token_helper::find_by_token(&token)?);
Ok(())
}
}
impl BaseRequestHandler for HttpRequestHandler {
@ -189,4 +201,8 @@ impl BaseRequestHandler for HttpRequestHandler {
fn user_access_token(&self) -> Option<&UserAccessToken> {
self.curr_user_token.as_ref()
}
fn admin_id_opt(&self) -> Option<AdminID> {
self.curr_admin_token.as_ref().map(|p| p.id)
}
}

View File

@ -11,6 +11,7 @@ use crate::data::error::{Res, ResultBoxError};
use crate::data::user_token::UserAccessToken;
use crate::data::user_ws_connection::UserWsConnection;
use crate::routes::RequestResult;
use crate::data::admin::AdminID;
pub enum UserWsResponseType {
SUCCESS,
@ -103,4 +104,8 @@ impl BaseRequestHandler for UserWsRequestHandler {
fn user_access_token(&self) -> Option<&UserAccessToken> {
Some(&self.connection.user_token)
}
fn admin_id_opt(&self) -> Option<AdminID> {
None
}
}

View File

@ -149,6 +149,17 @@ impl Route {
limit_policy,
}
}
pub fn admin_post(uri: &'static str, func: RequestProcess) -> Route {
Route {
scope: RouteScope::ADMIN,
method: POST,
need_login: true,
uri,
func,
limit_policy: LimitPolicy::NONE,
}
}
}
/// Get the list of routes available
@ -338,5 +349,6 @@ pub fn get_routes() -> Vec<Route> {
// Admin accounts controller
Route::limited_admin_post_without_login("/admin/accounts/auth_options", Box::new(admin_account_controller::get_auth_options), LimitPolicy::FAILURE(5)),
Route::limited_admin_post_without_login("/admin/accounts/auth_with_reset_token", Box::new(admin_account_controller::auth_with_reset_token), LimitPolicy::FAILURE(5)),
Route::admin_post("/admin/accounts/id", Box::new(admin_account_controller::get_admin_id)),
]
}

View File

@ -228,8 +228,7 @@ fn process_simple_route(route: &Route, req: &mut HttpRequestHandler) -> RequestR
req.check_admin_origin()?;
if route.need_login {
// TODO : implement
unimplemented!();
req.check_admin_access_token()?;
}
}
}