|
16 | 16 | *
|
17 | 17 | */
|
18 | 18 |
|
19 |
| -use crate::option::CONFIG; |
20 | 19 | use crate::rbac::user::User;
|
21 |
| -use std::collections::HashMap; |
| 20 | +use crate::{option::CONFIG, storage::StorageMetadata}; |
| 21 | +use std::{collections::HashMap, sync::Mutex}; |
22 | 22 |
|
23 | 23 | use super::{
|
24 | 24 | role::{model::DefaultPrivilege, Action, Permission, RoleBuilder},
|
25 | 25 | user,
|
26 | 26 | };
|
27 | 27 | use chrono::{DateTime, Utc};
|
28 |
| -use once_cell::sync::OnceCell; |
| 28 | +use once_cell::sync::{Lazy, OnceCell}; |
29 | 29 | use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
30 | 30 |
|
31 | 31 | pub type Roles = HashMap<String, Vec<DefaultPrivilege>>;
|
32 | 32 |
|
33 | 33 | pub static USERS: OnceCell<RwLock<Users>> = OnceCell::new();
|
34 | 34 | pub static ROLES: OnceCell<RwLock<Roles>> = OnceCell::new();
|
| 35 | +pub static DEFAULT_ROLE: Lazy<Mutex<Option<String>>> = Lazy::new(|| Mutex::new(None)); |
35 | 36 | pub static SESSIONS: OnceCell<RwLock<Sessions>> = OnceCell::new();
|
36 | 37 |
|
37 | 38 | pub fn users() -> RwLockReadGuard<'static, Users> {
|
@@ -86,7 +87,12 @@ pub fn mut_sessions() -> RwLockWriteGuard<'static, Sessions> {
|
86 | 87 | // the user_map is initialized from the config file and has a list of all users
|
87 | 88 | // the auth_map is initialized with admin user only and then gets lazily populated
|
88 | 89 | // as users authenticate
|
89 |
| -pub fn init(users: Vec<User>, mut roles: Roles) { |
| 90 | +pub fn init(metadata: &StorageMetadata) { |
| 91 | + let users = metadata.users.clone(); |
| 92 | + let mut roles = metadata.roles.clone(); |
| 93 | + |
| 94 | + *DEFAULT_ROLE.lock().unwrap() = metadata.default_role.clone(); |
| 95 | + |
90 | 96 | let admin_privilege = DefaultPrivilege::Admin;
|
91 | 97 | let admin_permissions = RoleBuilder::from(&admin_privilege).build();
|
92 | 98 | roles.insert("admin".to_string(), vec![admin_privilege]);
|
|
0 commit comments