mirror of
https://gitlab.com/fabinfra/fabaccess/bffh.git
synced 2025-06-11 19:03:21 +02:00
Api framework impl
This commit is contained in:
@ -1,11 +1,27 @@
|
||||
use crate::db::{AllocAdapter, Environment, RawDB, Result, DB};
|
||||
use crate::db::{DatabaseFlags, LMDBorrow, RoTransaction, WriteFlags};
|
||||
use lmdb::{RwTransaction, Transaction};
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use lmdb::{RwTransaction, Transaction};
|
||||
use crate::db::{RawDB, DB, AllocAdapter, Environment, Result};
|
||||
use crate::db::{DatabaseFlags, LMDBorrow, RoTransaction, WriteFlags, };
|
||||
use super::User;
|
||||
|
||||
use rkyv::{Deserialize, Archived};
|
||||
use rkyv::{Archived, Deserialize};
|
||||
|
||||
#[derive(
|
||||
Clone,
|
||||
PartialEq,
|
||||
Eq,
|
||||
Debug,
|
||||
rkyv::Archive,
|
||||
rkyv::Serialize,
|
||||
rkyv::Deserialize,
|
||||
serde::Serialize,
|
||||
serde::Deserialize,
|
||||
)]
|
||||
pub struct User {
|
||||
id: u128,
|
||||
username: String,
|
||||
roles: Vec<String>,
|
||||
}
|
||||
|
||||
type Adapter = AllocAdapter<User>;
|
||||
#[derive(Clone, Debug)]
|
||||
@ -64,59 +80,3 @@ impl UserDB {
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UserIndex {
|
||||
env: Arc<Environment>,
|
||||
usernames: RawDB,
|
||||
roles: RawDB,
|
||||
}
|
||||
|
||||
impl UserIndex {
|
||||
pub fn update(&self, old: &User, new: &User) -> Result<()> {
|
||||
assert_eq!(old.id, new.id);
|
||||
let mut txn = self.env.begin_rw_txn()?;
|
||||
if old.username != new.username {
|
||||
self.update_username(&mut txn, new.id, &old.username, &new.username)?;
|
||||
}
|
||||
|
||||
let mut to_remove: HashSet<&String> = old.roles.iter().collect();
|
||||
let mut to_add: HashSet<&String> = HashSet::new();
|
||||
for role in new.roles.iter() {
|
||||
// If a role wasn't found in the old ones it's a new one that's being added
|
||||
if !to_remove.remove(role) {
|
||||
to_add.insert(role);
|
||||
}
|
||||
// Otherwise it's in both sets so we just ignore it.
|
||||
}
|
||||
|
||||
self.update_roles(&mut txn, new.id, to_remove, to_add)?;
|
||||
txn.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn update_username(&self, txn: &mut RwTransaction, uid: u128, old: &String, new: &String)
|
||||
-> Result<()>
|
||||
{
|
||||
let flags = WriteFlags::empty();
|
||||
self.usernames.del(txn, &old.as_bytes(), Some(&uid.to_ne_bytes()))?;
|
||||
self.usernames.put(txn, &new.as_bytes(), &uid.to_ne_bytes(), flags)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn update_roles(&self,
|
||||
txn: &mut RwTransaction,
|
||||
uid: u128,
|
||||
remove: HashSet<&String>,
|
||||
add: HashSet<&String>
|
||||
) -> Result<()>
|
||||
{
|
||||
let flags = WriteFlags::empty();
|
||||
for role in remove.iter() {
|
||||
self.roles.del(txn, &role.as_bytes(), Some(&uid.to_ne_bytes()))?;
|
||||
}
|
||||
for role in add.iter() {
|
||||
self.roles.put(txn, &role.as_bytes(), &uid.to_ne_bytes(), flags)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -14,24 +14,43 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use rkyv::{Archive, Deserialize, Infallible, Serialize};
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
use rkyv::{Archive, Serialize, Deserialize, Infallible};
|
||||
|
||||
mod db;
|
||||
pub mod db;
|
||||
|
||||
pub use db::UserDB;
|
||||
pub use crate::authentication::db::PassDB;
|
||||
use crate::authorization::roles::Role;
|
||||
|
||||
#[derive(Debug, Clone, Archive, Serialize, Deserialize, serde::Serialize, serde::Deserialize)]
|
||||
#[derive(
|
||||
Copy,
|
||||
Clone,
|
||||
PartialEq,
|
||||
Eq,
|
||||
Debug,
|
||||
Archive,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
serde::Serialize,
|
||||
serde::Deserialize,
|
||||
)]
|
||||
#[archive_attr(derive(Debug, PartialEq, serde::Serialize, serde::Deserialize))]
|
||||
pub struct User {
|
||||
id: u128,
|
||||
username: String,
|
||||
roles: Vec<String>,
|
||||
id: u64
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn new(id: u128, username: String, roles: Vec<String>) -> Self {
|
||||
User { id, username, roles }
|
||||
pub fn new(id: u64) -> Self {
|
||||
User { id }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_username(&self) -> &str {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub fn get_roles(&self) -> impl IntoIterator<Item=Role> {
|
||||
unimplemented!();
|
||||
[]
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user