72 lines
1.9 KiB
Rust
72 lines
1.9 KiB
Rust
use std::collections::LinkedList;
|
|
use std::sync::RwLock;
|
|
#[cfg(not(test))]
|
|
use std::fs;
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
const ALLOW_PATH: &str = "/opt/mini_admin_bot/allow_users";
|
|
#[cfg(not(debug_assertions))]
|
|
const POLE_PATH: &str = "/opt/mini_admin_bot/allow_pole";
|
|
|
|
#[cfg(debug_assertions)]
|
|
const ALLOW_PATH: &str = "allow_users";
|
|
#[cfg(debug_assertions)]
|
|
const POLE_PATH: &str = "allow_pole";
|
|
|
|
#[cfg(not(test))]
|
|
fn read_ids<'a>(path: &str) -> LinkedList<String> {
|
|
let content = fs::read_to_string(path).expect("Something went wrong reading the file");
|
|
content
|
|
.split('\n')
|
|
.into_iter()
|
|
.map(|item| format!("{}", item))
|
|
.collect::<LinkedList<String>>()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
fn read_ids<'a>(path: &str) -> LinkedList<String> {
|
|
let mut ret = LinkedList::new();
|
|
ret.push_front("id_1".to_string());
|
|
ret.push_front("id_2".to_string());
|
|
if !path.eq(POLE_PATH) {
|
|
ret.push_front("id_3".to_string());
|
|
}
|
|
ret
|
|
}
|
|
|
|
pub struct GroupPermissions {
|
|
aproved_groups: RwLock<LinkedList<String>>,
|
|
party_groups: RwLock<LinkedList<String>>,
|
|
}
|
|
|
|
impl GroupPermissions {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
aproved_groups: RwLock::new(read_ids(ALLOW_PATH)),
|
|
party_groups: RwLock::new(read_ids(POLE_PATH)),
|
|
}
|
|
}
|
|
|
|
pub fn compare(&self, id: &String) -> bool {
|
|
self.aproved_groups.read().unwrap().contains(id)
|
|
}
|
|
|
|
pub fn compar_party(&self, id: &String) -> bool {
|
|
self.party_groups.read().unwrap().contains(id)
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
pub fn test_compare() {
|
|
let gp = GroupPermissions::new();
|
|
assert_eq!(true, gp.compare(&"id_3".to_string()));
|
|
assert_eq!(false, gp.compare(&"id_4".to_string()));
|
|
}
|
|
|
|
#[test]
|
|
pub fn test_compare_pole() {
|
|
let gp = GroupPermissions::new();
|
|
assert_eq!(true, gp.compar_party(&"id_2".to_string()));
|
|
assert_eq!(false, gp.compar_party(&"id_3".to_string()));
|
|
}
|