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

Can update admin general settings

This commit is contained in:
Pierre HUBERT 2021-05-13 17:17:31 +02:00
parent 830c6d0700
commit 158d35171d
4 changed files with 40 additions and 1 deletions

View File

@ -6,6 +6,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_auth_success::AdminAuthSuccess;
use crate::api_data::admin::admin_id_api::AdminIDAPI; use crate::api_data::admin::admin_id_api::AdminIDAPI;
use crate::api_data::admin::admin_info_api::AdminInfoAPI; use crate::api_data::admin::admin_info_api::AdminInfoAPI;
use crate::data::admin::NewAdminGeneralSettings;
use crate::data::base_request_handler::BaseRequestHandler; use crate::data::base_request_handler::BaseRequestHandler;
use crate::data::http_request_handler::HttpRequestHandler; use crate::data::http_request_handler::HttpRequestHandler;
use crate::helpers::{admin_access_token_helper, admin_account_helper}; use crate::helpers::{admin_access_token_helper, admin_account_helper};
@ -61,10 +62,31 @@ pub fn get_admin_info(r: &mut HttpRequestHandler) -> RequestResult {
if admin_id == r.admin_id()? { if admin_id == r.admin_id()? {
admin_account_helper::find_admin_by_id(admin_id)? admin_account_helper::find_admin_by_id(admin_id)?
} else { } else {
// TODO
unimplemented!(); unimplemented!();
} }
} }
}; };
r.set_response(AdminInfoAPI::new(&admin)) r.set_response(AdminInfoAPI::new(&admin))
}
/// Update general settings
pub fn update_general_settings(r: &mut HttpRequestHandler) -> RequestResult {
let admin_id = r.post_admin_id("id")?;
let new_name = r.post_string("name")?;
let new_email = r.post_email("email")?;
if admin_id != r.admin_id()? {
// TODO
unimplemented!();
}
admin_account_helper::set_general_settings(NewAdminGeneralSettings {
id: admin_id,
name: new_name,
email: new_email,
})?;
r.ok()
} }

View File

@ -48,4 +48,11 @@ pub struct AdminAccessToken {
pub token: String, pub token: String,
pub id: AdminID, pub id: AdminID,
pub last_refresh: u64 pub last_refresh: u64
}
/// New admin general settings
pub struct NewAdminGeneralSettings {
pub id: AdminID,
pub name: String,
pub email: String
} }

View File

@ -4,7 +4,7 @@
use crate::constants::{ADMIN_RESET_TOKEN_LENGTH, ADMIN_RESET_TOKEN_LIFETIME}; use crate::constants::{ADMIN_RESET_TOKEN_LENGTH, ADMIN_RESET_TOKEN_LIFETIME};
use crate::constants::database_tables_names::ADMIN_LIST_TABLE; use crate::constants::database_tables_names::ADMIN_LIST_TABLE;
use crate::data::admin::{Admin, AdminID, AdminResetToken, NewAdmin}; use crate::data::admin::{Admin, AdminID, AdminResetToken, NewAdmin, NewAdminGeneralSettings};
use crate::data::error::{ExecError, Res}; use crate::data::error::{ExecError, Res};
use crate::helpers::database; use crate::helpers::database;
use crate::utils::crypt_utils::rand_str; use crate::utils::crypt_utils::rand_str;
@ -62,6 +62,15 @@ pub fn create_new_reset_token(id: AdminID) -> Res<AdminResetToken> {
Ok(token) Ok(token)
} }
/// Update admin general settings
pub fn set_general_settings(settings: NewAdminGeneralSettings) -> Res {
database::UpdateInfo::new(ADMIN_LIST_TABLE)
.cond_admin_id("id", settings.id)
.set_str("name", &settings.name)
.set_str("email", &settings.email)
.exec()
}
/// Turn a database entry into an admin structure /// Turn a database entry into an admin structure
fn db_to_admin(row: &database::RowResult) -> Res<Admin> { fn db_to_admin(row: &database::RowResult) -> Res<Admin> {
let reset_token_expire = row.get_optional_u64("reset_token_expire")? let reset_token_expire = row.get_optional_u64("reset_token_expire")?

View File

@ -352,5 +352,6 @@ pub fn get_routes() -> Vec<Route> {
Route::admin_post("/admin/accounts/sign_out", Box::new(admin_account_controller::sign_out)), Route::admin_post("/admin/accounts/sign_out", Box::new(admin_account_controller::sign_out)),
Route::admin_post("/admin/accounts/id", Box::new(admin_account_controller::get_admin_id)), Route::admin_post("/admin/accounts/id", Box::new(admin_account_controller::get_admin_id)),
Route::admin_post("/admin/accounts/info", Box::new(admin_account_controller::get_admin_info)), Route::admin_post("/admin/accounts/info", Box::new(admin_account_controller::get_admin_info)),
Route::admin_post("/admin/accounts/update_general_settings", Box::new(admin_account_controller::update_general_settings)),
] ]
} }