admin/roles: update label

This commit is contained in:
Philippe Loctaux 2023-05-07 18:59:00 +02:00
parent d778380d8b
commit 8dbeffddc9
6 changed files with 80 additions and 2 deletions

View file

@ -42,6 +42,7 @@ pub fn routes() -> Vec<Route> {
admin_roles_new_form,
admin_roles_view,
admin_roles_archive,
admin_roles_info_update,
]
}

View file

@ -176,3 +176,45 @@ pub async fn admin_roles_archive(
Ok(Flash::new(redirect, flash_kind, flash_message))
}
#[derive(Debug, FromForm)]
pub struct UpdateRoleForm<'r> {
pub label: &'r str,
}
#[post("/admin/roles/<id>/info", data = "<form>")]
pub async fn admin_roles_info_update(
_admin: JwtAdmin,
mut db: Connection<Database>,
id: RocketRoleID,
form: Form<UpdateRoleForm<'_>>,
) -> Result<Flash<Redirect>> {
let mut transaction = db.begin().await?;
let role = Role::get_by_name(&mut transaction, &id.0)
.await?
.ok_or_else(|| Error::not_found("Could not find role"))?;
if role.is_archived() {
return Err(Error::forbidden("Role is archived"));
}
// Update label
if role.label() != form.label {
if let Err(e) = role.set_label(&mut transaction, form.label).await {
return Ok(Flash::new(
Redirect::to(uri!(admin_roles_view(id))),
FlashKind::Danger,
e.to_string(),
));
}
}
transaction.commit().await?;
Ok(Flash::new(
Redirect::to(uri!(admin_roles_view(id))),
FlashKind::Success,
format!("Role has been updated."),
))
}