risingwave_meta_model/
user.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 risingwave_pb::user::PbUserInfo;
16use sea_orm::ActiveValue::Set;
17use sea_orm::NotSet;
18use sea_orm::entity::prelude::*;
19use serde::{Deserialize, Serialize};
20
21use crate::{AuthInfo, UserId};
22
23#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
24#[sea_orm(table_name = "user")]
25pub struct Model {
26    #[sea_orm(primary_key)]
27    pub user_id: UserId,
28    #[sea_orm(unique)]
29    pub name: String,
30    pub is_super: bool,
31    pub can_create_db: bool,
32    pub can_create_user: bool,
33    pub can_login: bool,
34    pub is_admin: bool,
35    pub auth_info: Option<AuthInfo>,
36}
37
38#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
39pub enum Relation {
40    #[sea_orm(has_many = "super::object::Entity")]
41    Object,
42}
43
44impl Related<super::object::Entity> for Entity {
45    fn to() -> RelationDef {
46        Relation::Object.def()
47    }
48}
49
50impl ActiveModelBehavior for ActiveModel {}
51
52impl From<PbUserInfo> for ActiveModel {
53    fn from(user: PbUserInfo) -> Self {
54        let user_id = if user.id == 0 {
55            NotSet
56        } else {
57            Set(user.id as _)
58        };
59        Self {
60            user_id,
61            name: Set(user.name),
62            is_super: Set(user.is_super),
63            can_create_db: Set(user.can_create_db),
64            can_create_user: Set(user.can_create_user),
65            can_login: Set(user.can_login),
66            is_admin: Set(user.is_admin),
67            auth_info: Set(user.auth_info.as_ref().map(AuthInfo::from)),
68        }
69    }
70}
71
72impl From<Model> for PbUserInfo {
73    fn from(val: Model) -> Self {
74        PbUserInfo {
75            id: val.user_id as _,
76            name: val.name,
77            is_super: val.is_super,
78            can_create_db: val.can_create_db,
79            can_create_user: val.can_create_user,
80            can_login: val.can_login,
81            is_admin: val.is_admin,
82            auth_info: val.auth_info.map(|x| x.to_protobuf()),
83            grant_privileges: vec![], // fill in later
84        }
85    }
86}