mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-07-03 22:45:01 +00:00
Can get current admin id
This commit is contained in:
@ -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()
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user