initial create user
This commit is contained in:
+5
-1
@@ -370,7 +370,11 @@ pub async fn webservice(
|
||||
.service(
|
||||
web::scope("/json")
|
||||
.route("/list_links/", web::post().to(views::index_json))
|
||||
.route("/list_users/", web::post().to(views::index_users_json)),
|
||||
.route("/list_users/", web::post().to(views::index_users_json))
|
||||
.route(
|
||||
"/create_user/",
|
||||
web::post().to(views::process_create_user_json),
|
||||
),
|
||||
)
|
||||
// login to the admin area
|
||||
.route("/login/", web::get().to(views::login))
|
||||
|
||||
@@ -23,7 +23,7 @@ pub trait UserDbOperations<T> {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl UserDbOperations<User> for User {
|
||||
impl UserDbOperations<Self> for User {
|
||||
async fn get_user(id: i64, server_config: &ServerConfig) -> Result<Self, ServerError> {
|
||||
let user = sqlx::query_as!(Self, "Select * from users where id = ? ", id)
|
||||
.fetch_one(&server_config.db_pool)
|
||||
@@ -186,7 +186,7 @@ pub trait LinkDbOperations<T> {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl LinkDbOperations<Link> for Link {
|
||||
impl LinkDbOperations<Self> for Link {
|
||||
async fn get_link_by_code(
|
||||
code: &str,
|
||||
server_config: &ServerConfig,
|
||||
|
||||
+46
-1
@@ -6,7 +6,7 @@ use shared::{
|
||||
apirequests::{
|
||||
general::{Filter, Operation, Ordering},
|
||||
links::{LinkOverviewColumns, LinkRequestForm},
|
||||
users::{UserOverviewColumns, UserRequestForm},
|
||||
users::{UserDelta, UserOverviewColumns, UserRequestForm},
|
||||
},
|
||||
datatypes::{Count, FullLink, Link, User},
|
||||
};
|
||||
@@ -389,6 +389,51 @@ pub async fn create_user(
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Create a new user and save it to the database
|
||||
///
|
||||
/// # Errors
|
||||
/// Fails with [`ServerError`] if access to the database fails, this user does not have permissions or the user already exists.
|
||||
#[instrument(skip(id))]
|
||||
pub async fn create_user_json(
|
||||
id: &Identity,
|
||||
data: &web::Json<UserDelta>,
|
||||
server_config: &ServerConfig,
|
||||
) -> Result<Item<User>, ServerError> {
|
||||
info!("Creating a User: {:?}", &data);
|
||||
let auth = authenticate(id, server_config).await?;
|
||||
|
||||
// Require a password on user creation!
|
||||
let password = match &data.password {
|
||||
Some(pass) => pass,
|
||||
None => {
|
||||
return Err(ServerError::User(
|
||||
"A new users does require a password".to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
match auth {
|
||||
Role::Admin { user } => {
|
||||
let new_user = NewUser::new(
|
||||
data.username.clone(),
|
||||
data.email.clone(),
|
||||
password,
|
||||
&server_config.secret,
|
||||
)?;
|
||||
|
||||
new_user.insert_user(server_config).await?;
|
||||
|
||||
// querry the new user
|
||||
let new_user = get_user_by_name(&data.username, server_config).await?;
|
||||
Ok(Item {
|
||||
user,
|
||||
item: new_user,
|
||||
})
|
||||
}
|
||||
Role::Regular { .. } | Role::Disabled | Role::NotAuthenticated => {
|
||||
Err(ServerError::User("Permission denied!".to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Take a [`actix_web::web::Form<NewUser>`] and update the corresponding entry in the database.
|
||||
/// The password is only updated if a new password of at least 4 characters is provided.
|
||||
/// The `user_id` is never changed.
|
||||
|
||||
+20
-1
@@ -14,7 +14,11 @@ use fluent_templates::LanguageIdentifier;
|
||||
use image::{DynamicImage, ImageOutputFormat, Luma};
|
||||
use qrcode::{render::svg, QrCode};
|
||||
use queries::{authenticate, Role};
|
||||
use shared::apirequests::{links::LinkRequestForm, users::UserRequestForm};
|
||||
use shared::apirequests::{
|
||||
general::SuccessMessage,
|
||||
links::LinkRequestForm,
|
||||
users::{UserDelta, UserRequestForm},
|
||||
};
|
||||
use tera::{Context, Tera};
|
||||
use tracing::{error, info, instrument, warn};
|
||||
|
||||
@@ -346,6 +350,21 @@ pub async fn process_signup(
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(id))]
|
||||
pub async fn process_create_user_json(
|
||||
config: web::Data<crate::ServerConfig>,
|
||||
form: web::Json<UserDelta>,
|
||||
id: Identity,
|
||||
) -> Result<HttpResponse, ServerError> {
|
||||
info!("Listing Users to Json api");
|
||||
match queries::create_user_json(&id, &form, &config).await {
|
||||
Ok(item) => Ok(HttpResponse::Ok().json2(&SuccessMessage {
|
||||
message: format!("Successfully saved user: {}", item.item.username),
|
||||
})),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(id))]
|
||||
pub async fn toggle_admin(
|
||||
data: web::Path<String>,
|
||||
|
||||
Reference in New Issue
Block a user