Moved admin functions to correct module

develop
Jef Roosens 2021-08-29 20:41:03 +02:00
parent 87d8d8ff0c
commit 1378219fe5
Signed by untrusted user: Jef Roosens
GPG Key ID: B580B976584B5F30
6 changed files with 69 additions and 69 deletions

61
src/admin.rs 100644
View File

@ -0,0 +1,61 @@
use diesel::{insert_into, prelude::*, PgConnection};
use rocket::serde::json::Json;
use uuid::Uuid;
use crate::{
auth::pass::hash_password,
db,
errors::{RbError, RbResult},
guards::Admin,
schema::users::dsl as users,
RbDbConn,
};
#[get("/users")]
pub async fn get_users(_admin: Admin, conn: RbDbConn) -> RbResult<Json<Vec<db::User>>>
{
Ok(Json(conn.run(|c| db::users::all(c)).await?))
}
#[post("/users", data = "<user>")]
pub async fn create_user(_admin: Admin, conn: RbDbConn, user: Json<db::NewUser>) -> RbResult<()>
{
Ok(conn
.run(move |c| db::users::create(c, &user.into_inner()))
.await?)
}
#[get("/users/<user_id_str>")]
pub async fn get_user_info(
_admin: Admin,
conn: RbDbConn,
user_id_str: &str,
) -> RbResult<Json<db::User>>
{
let user_id = Uuid::parse_str(user_id_str).map_err(|_| RbError::UMUnknownUser)?;
match conn.run(move |c| db::users::find(c, user_id)).await {
Some(user) => Ok(Json(user)),
None => Err(RbError::UMUnknownUser),
}
}
pub fn create_admin_user(conn: &PgConnection, username: &str, password: &str) -> RbResult<bool>
{
let pass_hashed = hash_password(password)?;
let new_user = db::NewUser {
username: username.to_string(),
password: pass_hashed,
admin: true,
};
insert_into(users::users)
.values(&new_user)
.on_conflict(users::username)
.do_update()
.set(&new_user)
.execute(conn)
.map_err(|_| RbError::Custom("Couldn't create admin."))?;
Ok(true)
}

View File

@ -5,7 +5,7 @@ use self::{
jwt::{generate_jwt_token, JWTResponse},
pass::verify_user,
};
use crate::{guards::User, RbDbConn, RbResult};
use crate::{errors::RbResult, guards::User, RbDbConn};
pub mod jwt;
pub mod pass;

View File

@ -4,9 +4,8 @@ use rand::{thread_rng, Rng};
use crate::{
db::users::{NewUser, User},
errors::RbError,
errors::{RbError, RbResult},
schema::users::dsl as users,
RbResult,
};
pub fn verify_user(conn: &PgConnection, username: &str, password: &str) -> RbResult<User>
@ -39,23 +38,3 @@ pub fn hash_password(password: &str) -> RbResult<String>
argon2::hash_encoded(password.as_bytes(), &salt, &config)
.map_err(|_| RbError::Custom("Couldn't hash password."))
}
pub fn create_admin_user(conn: &PgConnection, username: &str, password: &str) -> RbResult<bool>
{
let pass_hashed = hash_password(password)?;
let new_user = NewUser {
username: username.to_string(),
password: pass_hashed,
admin: true,
};
insert_into(users::users)
.values(&new_user)
.on_conflict(users::username)
.do_update()
.set(&new_user)
.execute(conn)
.map_err(|_| RbError::Custom("Couldn't create admin."))?;
Ok(true)
}

View File

@ -11,13 +11,11 @@ extern crate diesel;
use rocket::{fairing::AdHoc, Build, Rocket};
use rocket_sync_db_pools::database;
pub use crate::errors::{RbError, RbResult};
mod admin;
pub mod auth;
pub mod db;
pub mod errors;
pub mod guards;
mod routes;
pub(crate) mod schema;
// Any import defaults are defined here
@ -54,7 +52,7 @@ async fn create_admin_user(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocke
.await
.expect("database connection");
conn.run(move |c| {
auth::pass::create_admin_user(c, &admin_user, &admin_password)
admin::create_admin_user(c, &admin_user, &admin_password)
.expect("failed to create admin user")
})
.await;
@ -76,5 +74,8 @@ fn rocket() -> _
"/api/auth",
routes![auth::already_logged_in, auth::login, auth::refresh_token,],
)
.mount("/api/admin", routes::admin::routes())
.mount(
"/api/admin",
routes![admin::get_users, admin::create_user, admin::get_user_info],
)
}

View File

@ -1,40 +0,0 @@
use rocket::serde::json::Json;
use uuid::Uuid;
use crate::{
db,
errors::{RbError, RbResult},
guards::Admin,
RbDbConn,
};
pub fn routes() -> Vec<rocket::Route>
{
routes![get_users, get_user_info, create_user]
}
#[get("/users")]
async fn get_users(_admin: Admin, conn: RbDbConn) -> RbResult<Json<Vec<db::User>>>
{
Ok(Json(conn.run(|c| db::users::all(c)).await?))
}
#[post("/users", data = "<user>")]
async fn create_user(_admin: Admin, conn: RbDbConn, user: Json<db::NewUser>) -> RbResult<()>
{
Ok(conn
.run(move |c| db::users::create(c, &user.into_inner()))
.await?)
}
#[get("/users/<user_id_str>")]
async fn get_user_info(_admin: Admin, conn: RbDbConn, user_id_str: &str)
-> RbResult<Json<db::User>>
{
let user_id = Uuid::parse_str(user_id_str).map_err(|_| RbError::UMUnknownUser)?;
match conn.run(move |c| db::users::find(c, user_id)).await {
Some(user) => Ok(Json(user)),
None => Err(RbError::UMUnknownUser),
}
}

View File

@ -1 +0,0 @@
pub mod admin;