risingwave_meta_model/
user_privilege.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use risingwave_pb::user::grant_privilege::PbAction;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

use crate::{ObjectId, PrivilegeId, UserId};

#[derive(
    Clone, Copy, Debug, Hash, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize,
)]
#[sea_orm(rs_type = "String", db_type = "string(None)")]
pub enum Action {
    #[sea_orm(string_value = "INSERT")]
    Insert,
    #[sea_orm(string_value = "SELECT")]
    Select,
    #[sea_orm(string_value = "UPDATE")]
    Update,
    #[sea_orm(string_value = "DELETE")]
    Delete,
    #[sea_orm(string_value = "USAGE")]
    Usage,
    #[sea_orm(string_value = "CREATE")]
    Create,
    #[sea_orm(string_value = "CONNECT")]
    Connect,
    #[sea_orm(string_value = "EXECUTE")]
    Execute,
}

impl From<PbAction> for Action {
    fn from(action: PbAction) -> Self {
        match action {
            PbAction::Unspecified => unreachable!("unspecified action"),
            PbAction::Insert => Self::Insert,
            PbAction::Select => Self::Select,
            PbAction::Update => Self::Update,
            PbAction::Delete => Self::Delete,
            PbAction::Usage => Self::Usage,
            PbAction::Create => Self::Create,
            PbAction::Connect => Self::Connect,
            PbAction::Execute => Self::Execute,
        }
    }
}

impl From<Action> for PbAction {
    fn from(action: Action) -> Self {
        match action {
            Action::Insert => Self::Insert,
            Action::Select => Self::Select,
            Action::Update => Self::Update,
            Action::Delete => Self::Delete,
            Action::Usage => Self::Usage,
            Action::Create => Self::Create,
            Action::Connect => Self::Connect,
            Action::Execute => Self::Execute,
        }
    }
}

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "user_privilege")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: PrivilegeId,
    pub dependent_id: Option<PrivilegeId>,
    pub user_id: UserId,
    pub oid: ObjectId,
    pub granted_by: UserId,
    pub action: Action,
    pub with_grant_option: bool,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::object::Entity",
        from = "Column::Oid",
        to = "super::object::Column::Oid",
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    Object,
    #[sea_orm(
        belongs_to = "super::user::Entity",
        from = "Column::GrantedBy",
        to = "super::user::Column::UserId",
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    User2,
    #[sea_orm(
        belongs_to = "super::user::Entity",
        from = "Column::UserId",
        to = "super::user::Column::UserId",
        on_update = "NoAction",
        on_delete = "Cascade"
    )]
    User1,
    #[sea_orm(
        belongs_to = "Entity",
        from = "Column::DependentId",
        to = "Column::Id",
        on_update = "NoAction",
        on_delete = "Cascade"
    )]
    SelfRef,
}

impl Related<super::object::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Object.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}