Added admin users route

develop
Jef Roosens 2021-08-22 15:50:58 +02:00
parent d7333373bb
commit b45c93cdc9
Signed by untrusted user: Jef Roosens
GPG Key ID: 955C0660072F691F
6 changed files with 30 additions and 3 deletions

8
src/rb/admin.rs 100644
View File

@ -0,0 +1,8 @@
use crate::errors::RBError;
use crate::models::User;
use diesel::prelude::*;
use crate::schema::users::dsl as users;
pub fn get_users(conn: &PgConnection) -> crate::Result<Vec<User>> {
users::users.load::<User>(conn).map_err(|_| RBError::DBError)
}

View File

@ -37,7 +37,9 @@ impl<'r> Responder<'r, 'static> for RBError {
RBError::JWTCreationError | RBError::MissingJWTKey => {
(Status::InternalServerError, "Failed to create tokens.")
}
RBError::InvalidRefreshToken | RBError::DuplicateRefreshToken => (Status::Unauthorized, "Invalid refresh token."),
RBError::InvalidRefreshToken | RBError::DuplicateRefreshToken => {
(Status::Unauthorized, "Invalid refresh token.")
}
_ => (Status::InternalServerError, "Internal server error"),
};

View File

@ -3,7 +3,8 @@ extern crate diesel;
pub mod auth;
pub mod errors;
mod models;
pub mod admin;
pub mod models;
pub(crate) mod schema;
pub use errors::Result;

13
src/rbs/admin.rs 100644
View File

@ -0,0 +1,13 @@
use crate::guards::Admin;
use crate::RbDbConn;
use rb::models::User;
use rocket::serde::json::Json;
pub fn routes() -> Vec<rocket::Route> {
routes![get_users]
}
#[get("/users")]
async fn get_users(admin: Admin, conn: RbDbConn) -> rb::Result<Json<Vec<User>>> {
Ok(Json(conn.run(|c| rb::admin::get_users(c)).await?))
}

View File

@ -45,6 +45,7 @@ async fn refresh_token(
let refresh_token = refresh_token_request.into_inner().refresh_token;
Ok(Json(
conn.run(move |c| rb::auth::refresh_token(c, &refresh_token)).await?
conn.run(move |c| rb::auth::refresh_token(c, &refresh_token))
.await?,
))
}

View File

@ -11,6 +11,7 @@ use rocket::{fairing::AdHoc, Build, Rocket};
use rocket_sync_db_pools::{database, diesel};
mod auth;
mod admin;
pub(crate) mod guards;
embed_migrations!();
@ -55,4 +56,5 @@ fn rocket() -> _ {
))
.attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
.mount("/api/auth", auth::routes())
.mount("/api/admin", admin::routes())
}