Commit e80af369 authored by MrMan's avatar MrMan

email -> username to fix SASL

- emails would be nice but would limit re-usability of users across domains
parent 744a4db0
......@@ -90,7 +90,7 @@ where
fn get_mailbox_users_by_uuids(&self, uuids: &[String]) -> Result<Vec<ModelWithUUID<MailboxUser>>, Error>;
fn remove_mailbox_user_by_email(&self, emails: String) -> Result<(), Error>;
fn remove_mailbox_user_by_username(&self, username: String) -> Result<(), Error>;
}
impl MailboxDB for DB {
......@@ -122,9 +122,9 @@ impl MailboxDB for DB {
}
}
fn remove_mailbox_user_by_email(&self, emails: String) -> Result<(), Error> {
fn remove_mailbox_user_by_username(&self, username: String) -> Result<(), Error> {
match self {
DB::SQLite(db) => db.remove_mailbox_user_by_email(emails),
DB::SQLite(db) => db.remove_mailbox_user_by_username(username),
_ => Err(Error::NotSupported)
}
}
......
......@@ -136,10 +136,10 @@ impl MailboxDB for SQLiteDB {
ModelWithUUID::<MailboxUser>::find_by_uuids(self, uuids)
}
fn remove_mailbox_user_by_email(&self, email: String) -> Result<(), Error> {
let users = ModelWithUUID::<MailboxUser>::find_by_text_field(self, "email", email)?;
if users.len() == 0 { return Err(Error::DBError(String::from("failed to find user with given email"))); }
if users.len() > 1 { return Err(Error::DBError(String::from("found multiple users with given email"))); }
fn remove_mailbox_user_by_username(&self, username: String) -> Result<(), Error> {
let users = ModelWithUUID::<MailboxUser>::find_by_text_field(self, "username", username)?;
if users.len() == 0 { return Err(Error::DBError(String::from("failed to find user with given username"))); }
if users.len() > 1 { return Err(Error::DBError(String::from("found multiple users with given username"))); }
users[0].delete(self)
}
}
......
......@@ -6,7 +6,7 @@ use rusqlite::Row;
use rusqlite::types::ToSql;
use super::super::*;
const COLUMNS: &'static [&'static str] = &["uuid", "email", "password", "quota_gb"];
const COLUMNS: &'static [&'static str] = &["uuid", "username", "password", "quota_gb"];
impl HasSQLTable<SQLiteDB> for ModelWithUUID<MailboxUser> {
fn sql_tbl_name() -> &'static str { "mailbox_users" }
......@@ -16,10 +16,10 @@ impl HasSQLTable<SQLiteDB> for ModelWithUUID<MailboxUser> {
impl<'a, 'stmt> DBEntity<SQLiteDB, Row<'a, 'stmt>> for ModelWithUUID<MailboxUser> {
fn from_row(row: &Row) -> Result<Self, Error> {
let uuid = row.get_checked("uuid")?;
let email = row.get_checked("email")?;
let username = row.get_checked("username")?;
let password = row.get_checked("password")?;
let quota_gb = row.get_checked("quota_gb")?;
let model = MailboxUser { email, password, quota_gb };
let model = MailboxUser { username, password, quota_gb };
Ok(ModelWithUUID { uuid, model })
}
......@@ -29,7 +29,7 @@ impl<'a, 'stmt> DBEntity<SQLiteDB, Row<'a, 'stmt>> for ModelWithUUID<MailboxUser
let inserted_row_count = stmt.execute(&[
&self.uuid as &ToSql,
&self.model.email,
&self.model.username,
&self.model.password,
&self.model.quota_gb
])?;
......@@ -222,7 +222,11 @@ mod tests {
let _ = entity.insert(&db).expect("test entity create failed");
// find the user by email field
let find_result = ModelWithUUID::<MailboxUser>::find_by_text_field(&db, "email", String::from(TEST_USER_EMAIL)).expect("listing failed");
let find_result = ModelWithUUID::<MailboxUser>::find_by_text_field(
&db,
"username",
String::from(TEST_USER_EMAIL),
).expect("listing failed");
assert_eq!(find_result.len(), 1, "found one entity test user by email field");
assert_eq!(find_result[0].uuid, entity.uuid, "returned entity is the right one");
}
......
......@@ -12,7 +12,7 @@ pub const MAILBOX_MIGRATIONS: &'static [Migration] = &[
sql: "
CREATE TABLE mailbox_users (
uuid TEXT PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
quota_gb INTEGER DEFAULT 0,
......
......@@ -74,13 +74,13 @@ impl Component for Postfix {
},
PostfixCmd::CreateUser(new_user) => {
debug!("creating new mailbox user with email [{}]", &new_user.email);
debug!("creating new mailbox user with email [{}]", &new_user.username);
// Attempt to add the new user
let result = self.db.add_mailbox_user(new_user);
match result {
Ok(new_user) => {
debug!("successfully created new user with email: [{}]", &new_user.model.email);
debug!("successfully created new user with email: [{}]", &new_user.model.username);
if let Some(chan) = req.response_chan {
chan.send(Box::new(new_user)).unwrap();
}
......
......@@ -109,11 +109,11 @@ pub fn parse_arguments<'a>() -> ArgMatches<'a> {
// `postmgr postfix mailbox-user-add ...`
.subcommand(SubCommand::with_name("mailbox-user-add")
.about("add a mailbox user")
.arg(Arg::with_name("email")
.arg(Arg::with_name("username")
.required(true)
.short("e")
.long("email")
.value_name("EMAIL_ADDRESS")
.long("username")
.value_name("USERNAME")
.takes_value(true))
.arg(Arg::with_name("password")
.required(true)
......@@ -129,11 +129,11 @@ pub fn parse_arguments<'a>() -> ArgMatches<'a> {
// `postmgr postfix mailbox-user-remove ...`
.subcommand(SubCommand::with_name("mailbox-user-remove")
.about("remove a mailbox user")
.arg(Arg::with_name("email")
.arg(Arg::with_name("username")
.required(true)
.short("e")
.long("email")
.value_name("EMAIL_ADDRESS")
.long("username")
.value_name("USERNAME")
.takes_value(true))))
.get_matches()
}
......
......@@ -90,13 +90,12 @@ fn main() {
// Create components
let mut postfix = Postfix::new(app_cfg.postfix.clone(), None).expect("postfix component init failed");
// TODO: Check email address validity
let email = postfix_add_mailbox_user_m.value_of("email").expect("email address missing");
let username = postfix_add_mailbox_user_m.value_of("username").expect("username address missing");
let password = postfix_add_mailbox_user_m.value_of("password").expect("password missing");
let quota_gb = postfix_add_mailbox_user_m.value_of("mailbox_quota_gb").unwrap_or("0").parse().expect("invalid quota");
let new_user = MailboxUser{
email: String::from(email),
username: String::from(username),
password: String::from(password),
quota_gb
};
......@@ -104,7 +103,7 @@ fn main() {
let _ = postfix.db.connect().expect("failed to connect to db");
postfix.db.add_mailbox_user(new_user).expect("mailbox user creation failed:");
info!("successfully created new mailbox user with email address [{}]", &email);
info!("successfully created new mailbox user with username address [{}]", &username);
}, // END `postmgr postfix mailbox-user-add ...`
......@@ -114,12 +113,12 @@ fn main() {
// Create components
let mut postfix = Postfix::new(app_cfg.postfix.clone(), None).expect("postfix component init failed");
let email = postfix_remove_mailbox_user_m.value_of("email").expect("email address missing");
let username = postfix_remove_mailbox_user_m.value_of("username").expect("username missing");
let _ = postfix.db.connect().expect("failed to connect to db");
postfix.db.remove_mailbox_user_by_email(String::from(email)).expect("mailbox user removal failed:");
postfix.db.remove_mailbox_user_by_username(String::from(username)).expect("mailbox user removal failed:");
info!("successfully removed mailbox user with email address [{}]", &email);
info!("successfully removed mailbox user with username address [{}]", &username);
}, // END `postmgr postfix mailbox-user-remove ...`
......
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MailboxUser {
pub email: String,
pub username: String,
pub password: String,
pub quota_gb: i32
}
impl MailboxUser {
pub fn new(email: String, password: String) -> MailboxUser {
pub fn new(username: String, password: String) -> MailboxUser {
MailboxUser {
email,
username,
password,
quota_gb: 0
}
......
......@@ -13,7 +13,7 @@ auxprop_plugin: sql
mech_list: PLAIN LOGIN CRAM-MD5 DIGEST-MD5 NTLM
sql_engine: sqlite
sql_database: {{ abs_db_path }}
sql_select: SELECT password FROM mailbox_users WHERE email = '%[email protected]%r'
sql_select: SELECT password FROM mailbox_users WHERE username = '%u'
{% when None %}
{% endmatch %}
{% when DBType::PostgreSQL %}
......
......@@ -8,4 +8,4 @@ auxprop_plugin: sql
mech_list: PLAIN LOGIN CRAM-MD5 DIGEST-MD5 NTLM
sql_engine: sqlite
sql_database: {{ abs_db_path }}
sql_select: SELECT password FROM mailbox_users WHERE email = '%[email protected]%r'
sql_select: SELECT password FROM mailbox_users WHERE username = '%u'
......@@ -172,14 +172,14 @@ impl Drop for DockerizedPostmgr {
fn drop(&mut self) {
// DEBUG: comment the lines below to leave dangling containers after tests
// Kill the docker process
Command::new(DOCKER_BINARY)
.arg("kill")
.arg(&self.container_name)
.stdin(Stdio::null())
.stdout(Stdio::null()) // NOTE: comment this out to get docker kill output
.output()
.expect("failed to start docker kill command");
// // Kill the docker process
// Command::new(DOCKER_BINARY)
// .arg("kill")
// .arg(&self.container_name)
// .stdin(Stdio::null())
// .stdout(Stdio::null()) // NOTE: comment this out to get docker kill output
// .output()
// .expect("failed to start docker kill command");
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment