diff --git a/trifid-api/src/routes/v1/roles.rs b/trifid-api/src/routes/v1/roles.rs index b1a5c0a..93d1936 100644 --- a/trifid-api/src/routes/v1/roles.rs +++ b/trifid-api/src/routes/v1/roles.rs @@ -216,6 +216,54 @@ pub async fn create_role_request( } }; + let role: Option = match role::Entity::find() + .filter(role::Column::Name.eq(&req.name)) + .one(&db.conn) + .await + { + Ok(r) => r, + Err(e) => { + error!("database error: {}", e); + return HttpResponse::InternalServerError().json(APIErrorsResponse { + errors: vec![ + APIError { + code: "ERR_DB_ERROR".to_string(), + message: "There was an error performing the database request, please try again later.".to_string(), + path: None, + } + ], + }); + } + }; + + if role.is_some() { + return HttpResponse::BadRequest().json(APIErrorsResponse { + errors: vec![ + APIError { + code: "ERR_DUPLICATE_VALUE".to_string(), + message: "value already exists".to_string(), + path: Some("name".to_string()) + } + ] + }) + } + + for (id, rule) in req.firewall_rules.iter().enumerate() { + if let Some(pr) = &rule.port_range { + if pr.from < pr.to { + return HttpResponse::BadRequest().json(APIErrorsResponse { + errors: vec![ + APIError { + code: "ERR_INVALID_VALUE".to_string(), + message: "from must be less than or equal to to".to_string(), + path: Some(format!("firewallRules[{}].portRange", id)) + } + ] + }); + } + } + } + let new_role_model = role::Model { id: random_id("role"), name: req.name.clone(),