risingwave_frontend/user/
user_manager.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::HashMap;
16
17use itertools::Itertools;
18use risingwave_pb::user::{GrantPrivilege, UserInfo};
19
20use crate::user::UserId;
21use crate::user::user_catalog::UserCatalog;
22
23/// `UserInfoManager` is responsible for managing users.
24pub struct UserInfoManager {
25    user_by_name: HashMap<String, UserCatalog>,
26    user_name_by_id: HashMap<UserId, String>,
27}
28
29#[expect(clippy::derivable_impls)]
30impl Default for UserInfoManager {
31    fn default() -> Self {
32        UserInfoManager {
33            user_by_name: HashMap::new(),
34            user_name_by_id: HashMap::new(),
35        }
36    }
37}
38
39impl UserInfoManager {
40    pub fn get_user_mut(&mut self, id: UserId) -> Option<&mut UserCatalog> {
41        let name = self.user_name_by_id.get(&id)?;
42        self.user_by_name.get_mut(name)
43    }
44
45    pub fn get_all_users(&self) -> Vec<UserCatalog> {
46        self.user_by_name.values().cloned().collect_vec()
47    }
48
49    pub fn get_user_by_name(&self, user_name: &str) -> Option<&UserCatalog> {
50        self.user_by_name.get(user_name)
51    }
52
53    pub fn get_user_name_by_id(&self, id: UserId) -> Option<String> {
54        self.user_name_by_id.get(&id).cloned()
55    }
56
57    pub fn get_user_name_map(&self) -> &HashMap<UserId, String> {
58        &self.user_name_by_id
59    }
60
61    pub fn create_user(&mut self, user_info: UserInfo) {
62        let id = user_info.id;
63        let name = user_info.name.clone();
64        self.user_by_name
65            .try_insert(name.clone(), user_info.into())
66            .unwrap();
67        self.user_name_by_id.try_insert(id, name).unwrap();
68    }
69
70    pub fn drop_user(&mut self, id: UserId) {
71        let name = self.user_name_by_id.remove(&id).unwrap();
72        self.user_by_name.remove(&name).unwrap();
73    }
74
75    pub fn update_user(&mut self, user_info: UserInfo) {
76        let id = user_info.id;
77        let name = user_info.name.clone();
78        if let Some(old_name) = self.get_user_name_by_id(id) {
79            self.user_by_name.remove(&old_name);
80            self.user_by_name.insert(name.clone(), user_info.into());
81        } else {
82            self.user_by_name
83                .insert(name.clone(), user_info.into())
84                .unwrap();
85        }
86        self.user_name_by_id.insert(id, name).unwrap();
87    }
88
89    pub fn authorize(&mut self, _user_name: &str, _password: &str) -> bool {
90        todo!()
91    }
92
93    pub fn verify(&self, _user_name: &str, _privileges: &[GrantPrivilege]) -> bool {
94        todo!()
95    }
96
97    pub fn clear(&mut self) {
98        self.user_by_name.clear();
99        self.user_name_by_id.clear();
100    }
101}