2023-05-15 16:09:15 +00:00
|
|
|
//! Models to export and import the accounts
|
|
|
|
|
2023-06-29 14:33:26 +00:00
|
|
|
use crate::prelude::*;
|
2023-05-05 13:45:56 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct DecryptedAccount {
|
|
|
|
pub name: String,
|
|
|
|
pub login: String,
|
|
|
|
pub password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DecryptedAccount {
|
2023-05-09 17:27:58 +00:00
|
|
|
/// Constructs DecryptedAccount by decrypting the provided account
|
2023-05-15 16:09:15 +00:00
|
|
|
#[inline]
|
2023-05-05 13:45:56 +00:00
|
|
|
pub fn from_account(account: account::Model, master_pass: &str) -> crate::Result<Self> {
|
|
|
|
let (login, password) = account.decrypt(master_pass)?;
|
|
|
|
Ok(Self {
|
2023-05-15 16:09:15 +00:00
|
|
|
name: account.name,
|
2023-05-05 13:45:56 +00:00
|
|
|
login,
|
|
|
|
password,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-09 17:27:58 +00:00
|
|
|
/// Constructs ActiveModel with eath field Set by encrypting `self`
|
2023-05-15 16:09:15 +00:00
|
|
|
#[inline]
|
2023-05-05 13:45:56 +00:00
|
|
|
pub fn into_account(
|
|
|
|
self,
|
|
|
|
user_id: u64,
|
|
|
|
master_pass: &str,
|
|
|
|
) -> crate::Result<account::ActiveModel> {
|
2023-05-15 16:09:15 +00:00
|
|
|
account::ActiveModel::from_unencrypted(
|
|
|
|
user_id,
|
|
|
|
self.name,
|
|
|
|
&self.login,
|
|
|
|
&self.password,
|
|
|
|
master_pass,
|
|
|
|
)
|
2023-06-01 11:42:35 +00:00
|
|
|
.map_err(Into::into)
|
2023-05-05 13:45:56 +00:00
|
|
|
}
|
2023-05-07 16:04:48 +00:00
|
|
|
|
2023-05-09 17:27:58 +00:00
|
|
|
/// Returns true if the account's fields are valid
|
2023-05-07 16:04:48 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn validate(&self) -> bool {
|
2023-06-23 08:45:37 +00:00
|
|
|
[
|
|
|
|
self.name.as_str(),
|
|
|
|
self.login.as_str(),
|
|
|
|
self.password.as_str(),
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.all(validate_field)
|
2023-05-07 16:04:48 +00:00
|
|
|
}
|
2023-05-05 13:45:56 +00:00
|
|
|
}
|
2023-05-05 18:29:58 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2023-06-11 12:58:41 +00:00
|
|
|
#[repr(transparent)]
|
2023-05-05 18:29:58 +00:00
|
|
|
pub struct User {
|
|
|
|
pub accounts: Vec<DecryptedAccount>,
|
|
|
|
}
|