admin/roles: update label
This commit is contained in:
parent
d778380d8b
commit
8dbeffddc9
6 changed files with 80 additions and 2 deletions
5
crates/database/queries/roles/set_label.sql
Normal file
5
crates/database/queries/roles/set_label.sql
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
update roles
|
||||
|
||||
set label = ?
|
||||
|
||||
where name is ?
|
||||
|
|
@ -612,6 +612,16 @@
|
|||
},
|
||||
"query": "update users\n\nset paper_key = ?\n\nwhere id is ?"
|
||||
},
|
||||
"693c8c3de266cb3a8fc98bdea8e3b87de0dcb78316628d48f0e6efc0f8103040": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Right": 2
|
||||
}
|
||||
},
|
||||
"query": "update roles\n\nset label = ?\n\nwhere name is ?"
|
||||
},
|
||||
"6ff12f357d884a50035d708577a7f3109a07a1ca193cb3082d13687af65c6de0": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
|
|
|
|||
|
|
@ -44,11 +44,25 @@ impl Roles {
|
|||
|
||||
pub async fn set_archive_status(
|
||||
conn: impl SqliteExecutor<'_>,
|
||||
id: &str,
|
||||
name: &str,
|
||||
value: bool,
|
||||
) -> Result<Option<()>, Error> {
|
||||
let query: SqliteQueryResult =
|
||||
sqlx::query_file!("queries/roles/set_archive_status.sql", value, id)
|
||||
sqlx::query_file!("queries/roles/set_archive_status.sql", value, name)
|
||||
.execute(conn)
|
||||
.await
|
||||
.map_err(handle_error)?;
|
||||
|
||||
Ok((query.rows_affected() == 1).then_some(()))
|
||||
}
|
||||
|
||||
pub async fn set_label(
|
||||
conn: impl SqliteExecutor<'_>,
|
||||
name: &str,
|
||||
label: &str,
|
||||
) -> Result<Option<()>, Error> {
|
||||
let query: SqliteQueryResult =
|
||||
sqlx::query_file!("queries/roles/set_label.sql", label, name)
|
||||
.execute(conn)
|
||||
.await
|
||||
.map_err(handle_error)?;
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ pub fn routes() -> Vec<Route> {
|
|||
admin_roles_new_form,
|
||||
admin_roles_view,
|
||||
admin_roles_archive,
|
||||
admin_roles_info_update,
|
||||
]
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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."),
|
||||
))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,4 +60,10 @@ impl Role {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_label(&self, conn: impl SqliteExecutor<'_>, label: &str) -> Result<(), Error> {
|
||||
DatabaseRoles::set_label(conn, self.name.as_ref(), label).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue