album/src/adm/users.rs

62 lines
1.7 KiB
Rust
Raw Normal View History

use adm::result::Error;
use diesel::{insert, update};
use diesel::pg::PgConnection;
use diesel::prelude::*;
use djangohashers::make_password;
use rand::distributions::IndependentSample;
use rand::distributions::range::Range;
use rand::os::OsRng;
use std::iter::Iterator;
pub fn list(db: &PgConnection) -> Result<(), Error> {
use schema::users::dsl::*;
println!(
"Existing users: {:?}.",
2017-11-09 03:06:12 +04:00
users.select(username).load::<String>(db)?,
);
Ok(())
}
pub fn passwd(db: &PgConnection, uname: &str) -> Result<(), Error> {
let pword = random_password(14);
let hashword = make_password(&pword);
use schema::users::dsl::*;
2017-05-09 00:01:59 +04:00
match update(users.filter(username.eq(&uname)))
.set(password.eq(&hashword))
.execute(db)?
{
1 => {
println!("Updated password for {:?} to {:?}", uname, pword);
}
0 => {
use models::NewUser;
insert(&NewUser { username: uname, password: &hashword })
.into(users)
2017-05-09 00:01:59 +04:00
.execute(db)?;
println!("Created user {:?} with password {:?}", uname, pword);
}
n => {
println!(
"Strange, updated {} passwords for {:?} to {:?}",
n,
uname,
2017-11-09 03:06:12 +04:00
pword,
);
}
};
Ok(())
}
fn random_password(len: usize) -> String {
let rng = &mut OsRng::new().expect("Init rng");
let nlc = b'z' - b'a' + 1;
let x = Range::new(0, 6 * nlc + 4 * 10 + 1);
(0..len)
2017-02-04 21:12:13 +04:00
.map(|_| match x.ind_sample(rng) {
n if n < (1 * nlc) => (b'A' + (n % nlc)) as char,
n if n < (6 * nlc) => (b'a' + (n % nlc)) as char,
n => (b'0' + n % 10) as char,
})
.collect()
}