2021-02-07 21:39:21 +00:00
|
|
|
use super::{Command, Execution, ExecutionResult};
|
2020-11-22 21:30:24 +00:00
|
|
|
use crate::context::Context;
|
2021-05-22 14:52:32 +00:00
|
|
|
use crate::db::Users;
|
2021-05-22 23:12:17 +00:00
|
|
|
use crate::error::BotError::{AccountDoesNotExist, AuthenticationError, PasswordCreationError};
|
2021-05-24 21:32:00 +00:00
|
|
|
use crate::logic::hash_password;
|
2021-05-22 14:52:32 +00:00
|
|
|
use crate::models::User;
|
2020-11-22 21:30:24 +00:00
|
|
|
use async_trait::async_trait;
|
2021-05-21 22:33:49 +00:00
|
|
|
|
|
|
|
pub struct RegisterCommand(pub String);
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Command for RegisterCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"register user account"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_secure(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
2021-05-22 14:52:32 +00:00
|
|
|
let pw_hash = hash_password(&self.0).map_err(|e| PasswordCreationError(e))?;
|
|
|
|
let user = User {
|
|
|
|
username: ctx.username.to_owned(),
|
|
|
|
password: pw_hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.db.upsert_user(&user).await?;
|
2021-05-23 13:58:58 +00:00
|
|
|
Execution::success(format!(
|
|
|
|
"User account registered/updated. Please log in to external applications \
|
|
|
|
with username {} and the password you set.",
|
|
|
|
ctx.username
|
|
|
|
))
|
2021-05-21 22:33:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-22 22:25:00 +00:00
|
|
|
|
|
|
|
pub struct CheckCommand(pub String);
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Command for CheckCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"check user password"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_secure(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
|
|
|
let user = ctx.db.authenticate_user(&ctx.username, &self.0).await?;
|
|
|
|
|
|
|
|
match user {
|
|
|
|
Some(_) => Execution::success("Password is correct!".to_string()),
|
|
|
|
None => Err(AuthenticationError.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-22 23:12:17 +00:00
|
|
|
|
|
|
|
pub struct UnregisterCommand;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Command for UnregisterCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"unregister user account"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_secure(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
|
|
|
let user = ctx.db.get_user(&ctx.username).await?;
|
|
|
|
|
|
|
|
match user {
|
|
|
|
Some(_) => {
|
|
|
|
ctx.db.delete_user(&ctx.username).await?;
|
|
|
|
Execution::success("Your user account has been removed.".to_string())
|
|
|
|
}
|
|
|
|
None => Err(AccountDoesNotExist.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|