Allow to delete aliases
This commit is contained in:
parent
1c2b4c408e
commit
237ef6250d
2 changed files with 39 additions and 1 deletions
38
src/main.rs
38
src/main.rs
|
|
@ -910,6 +910,43 @@ async fn alias_add(
|
|||
Ok(Redirect::to("/"))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(state))]
|
||||
async fn alias_delete(
|
||||
state: State<Arc<AppState>>,
|
||||
User(user): User,
|
||||
Form(alias): Form<Mail>,
|
||||
) -> Result<Redirect, Error> {
|
||||
let mut tx = state.db.begin().await?;
|
||||
|
||||
let can_use_alias = sqlx::query!(
|
||||
"SELECT COUNT(*) FROM emails WHERE id = $1 AND mail = $2",
|
||||
user,
|
||||
alias.mail
|
||||
)
|
||||
.fetch_one(&mut *tx)
|
||||
.await?
|
||||
.count
|
||||
.expect("count should not be null")
|
||||
> 0;
|
||||
|
||||
if !can_use_alias {
|
||||
tracing::error!("User is not authorized to use this alias");
|
||||
return Err(Error::InternalError);
|
||||
}
|
||||
|
||||
sqlx::query!("DELETE FROM alias WHERE alias = $1", alias.mail)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
sqlx::query!("DELETE FROM emails WHERE mail = $1", alias.mail)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(Redirect::to("/"))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Password {
|
||||
password: SecretString,
|
||||
|
|
@ -977,6 +1014,7 @@ async fn main() -> color_eyre::Result<()> {
|
|||
.route("/list/delete", post(delete_list))
|
||||
.route("/password", post(set_password))
|
||||
.route("/alias/add", post(alias_add))
|
||||
.route("/alias/delete", post(alias_delete))
|
||||
.fallback(page_not_found)
|
||||
.with_state(Arc::new(AppState {
|
||||
db,
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@
|
|||
{{ self::delete_modal(modal_id="aliasDelete" ~ alias_idx ~ loop.index,
|
||||
confirm_text="Delete alias '" ~ alias ~ "' (for '" ~ mail.primary ~ "')",
|
||||
action="/alias/delete",
|
||||
payload=["alias", alias])
|
||||
payload=["mail", alias])
|
||||
}}
|
||||
</li>
|
||||
{% endfor %}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue