Users loading, noot noot!

This commit is contained in:
Gregor Reitzenstein 2020-12-16 13:30:04 +01:00
parent a279a2ed48
commit 78e7d45614
3 changed files with 39 additions and 7 deletions

11
examples/users.toml Normal file
View File

@ -0,0 +1,11 @@
[Testuser]
# Define them in roles.toml as well
roles = []
# If two or more users want to use the same machine at once the higher prio
# wins
priority = 0
# You can add whatever random data you want.
# It will get stored in the `kv` field in UserData.
noot = "noot!"

View File

@ -3,9 +3,14 @@
//! 2. "I have this here user, what are their roles (and other associated data)"
use serde::{Serialize, Deserialize};
use std::fmt;
use std::fs;
use std::iter::FromIterator;
use std::path::Path;
use crate::db::access::RoleIdentifier;
use std::collections::HashMap;
use crate::error::Result;
mod internal;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
@ -80,8 +85,8 @@ pub struct UserData {
pub priority: u64,
/// Additional data storage
#[serde(flatten)]
kv: HashMap<Box<[u8]>, Box<[u8]>>,
#[serde(flatten, skip_serializing_if = "HashMap::is_empty")]
kv: HashMap<String, String>,
}
impl UserData {
@ -101,6 +106,17 @@ const fn default_priority() -> u64 {
0
}
pub fn load_file<P: AsRef<Path>>(path: P) -> Result<HashMap<String, User>> {
let f = fs::read(path)?;
let mut map: HashMap<String, UserData> = toml::from_slice(&f)?;
Ok(HashMap::from_iter(map.drain().map(|(uid, user_data)|
( uid.clone()
, User::new(UserId::new(uid, None, None), user_data)
)
)))
}
#[cfg(test_DISABLED)]
mod tests {
use super::*;

View File

@ -122,18 +122,23 @@ fn main() {
// Returning a `Result` from `main` allows us to use the `?` shorthand.
// In the case of an Err it will be printed using `fmt::Debug`
fn maybe(matches: clap::ArgMatches, log: Arc<Logger>) -> Result<(), Error> {
// If no `config` option is given use a preset default.
let configpath = matches.value_of("config").unwrap_or("/etc/bffh/config.toml");
let config = config::read(&PathBuf::from_str(configpath).unwrap())?;
debug!(log, "Loaded Config: {:?}", config);
if matches.is_present("dump") {
error!(log, "Dumping is currently not implemented");
Ok(())
} else if matches.is_present("load") {
error!(log, "Loading is currently not implemented");
let mut dir = PathBuf::from(matches.value_of_os("load").unwrap());
dir.push("users.toml");
let map = db::user::load_file(&dir);
debug!(log, "Loaded users: {:?}", map);
dir.pop();
Ok(())
} else {
// If no `config` option is given use a preset default.
let configpath = matches.value_of("config").unwrap_or("/etc/bffh/config.toml");
let config = config::read(&PathBuf::from_str(configpath).unwrap())?;
debug!(log, "Loaded Config: {:?}", config);
let ex = Executor::new();
let mqtt = AsyncClient::new(config.mqtt_url.clone())?;