Change permission creation to return permissions even if they exist

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 4 years ago
parent 3a2ea2a03d
commit 04de5a4e4f
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -42,8 +42,8 @@ impl Permissions {
let _: Vec<DatabaseResult<()>> = permissions
.iter()
.map(|CreatePermissionsEntry { name, description }| {
let exists = transaction
.query_opt("SELECT id FROM permissions WHERE name = $1", &[&name])?;
let exists =
transaction.query_opt("SELECT * FROM permissions WHERE name = $1", &[&name])?;
if exists.is_none() {
let row = transaction.query_one(
@ -52,12 +52,15 @@ impl Permissions {
)?;
created_permissions.push(serde_postgres::from_row(&row)?);
} else {
created_permissions.push(serde_postgres::from_row(&exists.unwrap())?);
}
Ok(())
})
.collect();
transaction.commit()?;
Ok(created_permissions)
}
}

@ -62,17 +62,31 @@ impl Roles {
&[&role.id, &permission],
)?;
}
if let Err(e) = transaction.execute(
"INSERT INTO user_roles (user_id, role_id) VALUES ($1, $2)",
&[&1, &role.id],
) {
log::debug!("Failed to add role to admin user: {}", e);
}
Ok(role)
};
if let Err(_) = result {
log::trace!("Rollback");
transaction.rollback()?;
} else {
log::trace!("Commit");
transaction.commit()?;
}
match result {
Err(e) => {
log::warn!("Failed to create role {}: {}", name, e);
log::trace!("Rolling back...");
transaction.rollback()?;
log::trace!("Rolled back!");
Err(e)
}
Ok(role) => {
log::debug!("Successfully created role {} with id {}", name, role.id);
log::trace!("Committing...");
transaction.commit()?;
log::trace!("Committed!");
result
Ok(role)
}
}
}
}

Loading…
Cancel
Save