Updated auth checking roles

This commit is contained in:
Benjamin Sherriff
2023-10-18 16:38:04 -04:00
parent 1db5eade4e
commit 939f8c2b90
6 changed files with 79 additions and 22 deletions

View File

@@ -3,7 +3,7 @@ use log::error;
use serde::{Serialize, Deserialize};
use siren::{GetResponse, Metadata, ServiceError};
use crate::{db::messages::{QueryMessage, QueryFilters, InsertMessage}, auth::JwtAuth};
use crate::{db::messages::{QueryMessage, QueryFilters, InsertMessage}, auth::{JwtAuth, verify_role}};
#[derive(Serialize, Deserialize)]
struct GetAllParams {
@@ -22,6 +22,10 @@ struct GetAllParams {
#[get("/messages")]
async fn get_all(req: HttpRequest, auth: JwtAuth) -> HttpResponse {
let _ = match verify_role(&auth, "admin") {
Ok(_) => {},
Err(err) => return ResponseError::error_response(&err)
};
let params = match web::Query::<GetAllParams>::from_query(req.query_string()) {
Ok(params) => params,
Err(err) => return ResponseError::error_response(&ServiceError {
@@ -65,6 +69,10 @@ async fn get_all(req: HttpRequest, auth: JwtAuth) -> HttpResponse {
#[post("/messages")]
async fn create(message: web::Json<InsertMessage>, auth: JwtAuth) -> HttpResponse {
let _ = match verify_role(&auth, "admin") {
Ok(_) => {},
Err(err) => return ResponseError::error_response(&err)
};
match InsertMessage::insert(message.into_inner()) {
Ok(message) => HttpResponse::Created().json(message),
Err(err) => {

View File

@@ -3,7 +3,7 @@ use log::error;
use serde::{Serialize, Deserialize};
use siren::{GetResponse, Metadata, ServiceError};
use crate::{db::spells::{QuerySpell, QueryFilters}, auth::JwtAuth};
use crate::{db::spells::{QuerySpell, QueryFilters}, auth::{JwtAuth, verify_role}};
use super::{Spell, InsertSpell};
@@ -135,6 +135,10 @@ async fn get_by_id(id: web::Path<String>) -> HttpResponse {
#[post("/spells")]
async fn create(spell: web::Json<Spell>, auth: JwtAuth) -> HttpResponse {
let _ = match verify_role(&auth, "admin") {
Ok(_) => {},
Err(err) => return ResponseError::error_response(&err)
};
match InsertSpell::insert(spell.into_inner().into()) {
Ok(spell) => HttpResponse::Created().json(Spell::from(spell)),
Err(err) => {
@@ -146,6 +150,10 @@ async fn create(spell: web::Json<Spell>, auth: JwtAuth) -> HttpResponse {
#[put("/spells/{id}")]
async fn update(id: web::Path<String>, spell: web::Json<Spell>, auth: JwtAuth) -> HttpResponse {
let _ = match verify_role(&auth, "admin") {
Ok(_) => {},
Err(err) => return ResponseError::error_response(&err)
};
let id = match id.parse::<i32>() {
Ok(id) => id,
Err(err) => return ResponseError::error_response(&ServiceError {
@@ -164,6 +172,10 @@ async fn update(id: web::Path<String>, spell: web::Json<Spell>, auth: JwtAuth) -
#[delete("/spells/{id}")]
async fn delete(id: web::Path<String>, auth: JwtAuth) -> HttpResponse {
let _ = match verify_role(&auth, "admin") {
Ok(_) => {},
Err(err) => return ResponseError::error_response(&err)
};
let id = match id.parse::<i32>() {
Ok(id) => id,
Err(err) => return ResponseError::error_response(&ServiceError {