12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- use crate::{schema,user};
- use microrm::prelude::*;
- pub fn list(realm: &str, db: microrm::DB) {
- // get realm ID
- let qi = db.query_interface();
- let realm_id = qi.get().by(schema::Realm::Shortname, realm).one().expect("couldn't query db").expect("No such realm").id();
- let users = qi.get().by(schema::User::Realm, &realm_id).all().expect("couldn't query db");
- println!("User list ({} users):", users.len());
- for user in &users {
- println!("- {:20}", user.username);
- let auth_challenges = qi.get().by(schema::AuthChallenge::User, &user.id()).all().expect("Can't get authentication challenges?");
- for ch in &auth_challenges {
- println!(" - Has {:?} authentication challenge", ch.challenge_type);
- }
- }
- }
- pub fn create(realm: &str, db: microrm::DB, username: &str) {
- // get realm ID
- let qi = db.query_interface();
- let realm_id = qi.get().by(schema::Realm::Shortname, realm).one().expect("couldn't query db").expect("No such realm").id();
- // check that the user doesn't exist already
- let existing_user = qi.get().by(schema::User::Realm, &realm_id).by(schema::User::Username, &username).one().expect("couldn't query db");
- if existing_user.is_some() {
- log::error!("Can't create user {} in {} realm as a user with that username already exists", username, realm);
- return;
- }
- qi.add(&schema::User { realm: realm_id, username: username.to_owned() }).expect("couldn't add user");
- }
- pub fn change_auth(realm: &str, db: microrm::DB, username: &str, change_password: bool) {
- // get realm ID
- let qi = db.query_interface();
- let realm_id = qi.get().by(schema::Realm::Shortname, realm).one().expect("couldn't query db").expect("No such realm").id();
- // check that the user exists
- let existing_user = qi.get().by(schema::User::Realm, &realm_id).by(schema::User::Username, &username).one().expect("couldn't query db");
- if existing_user.is_none() {
- log::error!("User {} does not exist in the {} realm!", username, realm);
- return;
- }
- let user = user::User::from_model(existing_user.unwrap());
- if change_password {
- let raw_pass = rpassword::prompt_password("Enter new user password: ").unwrap();
- user.set_new_password(&qi, raw_pass.as_bytes());
- }
- }
|