roles:create error parity

References: https://todo.e3t.cc/~core/trifid/1
This commit is contained in:
core 2023-07-31 00:33:00 -04:00
parent 44fc07089d
commit 8111dee595
Signed by: core
GPG Key ID: FDBF740DADDCEECF
1 changed files with 48 additions and 0 deletions

View File

@ -216,6 +216,54 @@ pub async fn create_role_request(
} }
}; };
let role: Option<role::Model> = 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 { let new_role_model = role::Model {
id: random_id("role"), id: random_id("role"),
name: req.name.clone(), name: req.name.clone(),