mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-22 05:19:21 +00:00
Can register new API clients from command line
This commit is contained in:
parent
36bfe8e24e
commit
9cc7797443
@ -2,12 +2,34 @@ use crate::constants::database_tables_names::CLIENTS_TABLE;
|
||||
use crate::data::api_client::APIClient;
|
||||
use crate::data::error::{Res, ResultBoxError};
|
||||
use crate::helpers::database;
|
||||
use crate::helpers::database::QueryInfo;
|
||||
use crate::helpers::database::{QueryInfo, InsertQuery};
|
||||
|
||||
/// API helper
|
||||
///
|
||||
/// @author Pierre Hubert
|
||||
|
||||
/// Get the entire list of clients
|
||||
pub fn get_clients() -> Res<Vec<APIClient>> {
|
||||
QueryInfo::new(CLIENTS_TABLE)
|
||||
.exec(db_to_client)
|
||||
}
|
||||
|
||||
/// Register a new client
|
||||
pub fn register_client(client: &APIClient) -> Res<u64> {
|
||||
let mut query = InsertQuery::new(CLIENTS_TABLE)
|
||||
.add_str("name", &client.name)
|
||||
.add_opt_str("domain", client.domain.as_ref())
|
||||
.add_opt_str("comment", client.comment.as_ref())
|
||||
.add_opt_str("firebase_project_name", client.firebase_project_name.as_ref())
|
||||
.add_opt_str("firebase_service_account_file", client.firebase_service_account_file.as_ref());
|
||||
|
||||
if client.default_expiration_time > 0 {
|
||||
query = query.add_u64("default_expiration_time", client.default_expiration_time);
|
||||
}
|
||||
|
||||
query.insert_expect_result()
|
||||
}
|
||||
|
||||
/// Get information about a client, based on its token
|
||||
pub fn get_client(name: &str) -> ResultBoxError<APIClient> {
|
||||
QueryInfo::new(CLIENTS_TABLE)
|
||||
@ -15,7 +37,6 @@ pub fn get_client(name: &str) -> ResultBoxError<APIClient> {
|
||||
.query_row(db_to_client)
|
||||
}
|
||||
|
||||
|
||||
/// Get information about a client, based on its origin
|
||||
pub fn get_by_origin(name: &str) -> ResultBoxError<APIClient> {
|
||||
QueryInfo::new(CLIENTS_TABLE)
|
||||
|
59
src/main.rs
59
src/main.rs
@ -1,10 +1,11 @@
|
||||
use comunic_server::{cleanup_thread, server};
|
||||
use comunic_server::constants::admin::{ADMIN_ROLES_LIST, AdminRole};
|
||||
use comunic_server::data::admin::NewAdmin;
|
||||
use comunic_server::data::api_client::APIClient;
|
||||
use comunic_server::data::config::{conf, Config};
|
||||
use comunic_server::data::error::Res;
|
||||
use comunic_server::data::user::UserID;
|
||||
use comunic_server::helpers::{account_helper, admin_account_helper, admin_roles_helper, database};
|
||||
use comunic_server::helpers::{account_helper, admin_account_helper, admin_roles_helper, api_helper, database};
|
||||
use comunic_server::utils::date_utils::current_year;
|
||||
|
||||
type MainActionFunction = Res;
|
||||
@ -34,7 +35,23 @@ fn get_actions() -> Vec<Action> {
|
||||
function: Box::new(help),
|
||||
},
|
||||
|
||||
// Reset password
|
||||
// Get the list of registered clients
|
||||
Action {
|
||||
name: "clients_list",
|
||||
description: "Get the list of registered clients",
|
||||
arguments: vec![],
|
||||
function: Box::new(list_clients),
|
||||
},
|
||||
|
||||
// Register a new API client
|
||||
Action {
|
||||
name: "register_client",
|
||||
description: "Register a new API client",
|
||||
arguments: vec!["name", "origin", "comment"],
|
||||
function: Box::new(register_client),
|
||||
},
|
||||
|
||||
// Reset a user password
|
||||
Action {
|
||||
name: "reset_password",
|
||||
description: "Create a password reset URL for a user",
|
||||
@ -170,6 +187,44 @@ fn help(_a: Vec<String>) -> Res {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn list_clients(_args: Vec<String>) -> Res {
|
||||
for client in api_helper::get_clients()? {
|
||||
println!(
|
||||
"Client {}\n* Name: {}\n* Domain: {}\n* Comment: {}\n* Default tokens expiration time: {}\n* Firebase project: {}",
|
||||
client.id,
|
||||
client.name,
|
||||
client.domain.unwrap_or("None".to_string()),
|
||||
client.comment.unwrap_or("None".to_string()),
|
||||
client.default_expiration_time,
|
||||
client.firebase_project_name.unwrap_or("None".to_string())
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn register_client(args: Vec<String>) -> Res {
|
||||
let client = APIClient {
|
||||
id: 0,
|
||||
name: args[0].to_string(),
|
||||
domain: match args[1].is_empty() {
|
||||
true => None,
|
||||
false => Some(args[1].to_string())
|
||||
},
|
||||
comment: match args[2].is_empty() {
|
||||
true => None,
|
||||
false => Some(args[2].to_string())
|
||||
},
|
||||
default_expiration_time: 0,
|
||||
firebase_project_name: None,
|
||||
firebase_service_account_file: None,
|
||||
};
|
||||
|
||||
let id = api_helper::register_client(&client)?;
|
||||
println!("Registered client #{}", id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_password(args: Vec<String>) -> Res {
|
||||
let user_id = UserID::new(args[0].parse::<u64>()?);
|
||||
let token = account_helper::generate_password_reset_token(&user_id)?;
|
||||
|
Loading…
Reference in New Issue
Block a user