Skip to content

Commit 6e90af8

Browse files
committed
refactor: move default_role to user option
1 parent 8d897e8 commit 6e90af8

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

common/meta/types/src/user_info.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ pub struct UserInfo {
4040

4141
pub quota: UserQuota,
4242

43-
pub default_role: Option<String>,
44-
4543
pub option: UserOption,
4644
}
4745

@@ -55,7 +53,6 @@ impl UserInfo {
5553
UserInfo {
5654
name: name.to_string(),
5755
hostname: hostname.to_string(),
58-
default_role: None,
5956
auth_info,
6057
grants,
6158
quota,
@@ -97,17 +94,45 @@ impl TryFrom<Vec<u8>> for UserInfo {
9794
#[serde(default)]
9895
pub struct UserOption {
9996
flags: BitFlags<UserOptionFlag>,
97+
98+
default_role: Option<String>,
10099
}
101100

102101
impl UserOption {
103102
pub fn new(flags: BitFlags<UserOptionFlag>) -> Self {
104-
Self { flags }
103+
Self {
104+
flags,
105+
default_role: None,
106+
}
107+
}
108+
109+
pub fn empty() -> Self {
110+
Default::default()
111+
}
112+
113+
pub fn with_flags(mut self, flags: BitFlags<UserOptionFlag>) -> Self {
114+
self.flags = flags;
115+
self
116+
}
117+
118+
pub fn with_default_role(mut self, default_role: Option<String>) -> Self {
119+
self.default_role = default_role;
120+
self
121+
}
122+
123+
pub fn with_set_flag(mut self, flag: UserOptionFlag) -> Self {
124+
self.flags.insert(flag);
125+
self
105126
}
106127

107128
pub fn flags(&self) -> &BitFlags<UserOptionFlag> {
108129
&self.flags
109130
}
110131

132+
pub fn default_role(&self) -> &Option<String> {
133+
&self.default_role
134+
}
135+
111136
pub fn set_all_flag(&mut self) {
112137
self.flags = BitFlags::all();
113138
}

common/proto-conv/src/user_from_to_protobuf_impl.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,22 @@ impl FromToProto<pb::UserOption> for mt::UserOption {
8181
where Self: Sized {
8282
check_ver(p.ver, p.min_compatible)?;
8383

84-
let flags = BitFlags::<mt::UserOptionFlag, u64>::from_bits(p.flags);
85-
match flags {
86-
Ok(flags) => Ok(mt::UserOption::new(flags)),
87-
Err(e) => Err(Incompatible {
84+
let flags =
85+
BitFlags::<mt::UserOptionFlag, u64>::from_bits(p.flags).map_err(|e| Incompatible {
8886
reason: format!("UserOptionFlag error: {}", e),
89-
}),
90-
}
87+
})?;
88+
89+
Ok(mt::UserOption::default()
90+
.with_flags(flags)
91+
.with_default_role(p.default_role))
9192
}
9293

9394
fn to_pb(&self) -> Result<pb::UserOption, Incompatible> {
9495
Ok(pb::UserOption {
9596
ver: VER,
9697
min_compatible: MIN_COMPATIBLE_VER,
9798
flags: self.flags().bits(),
99+
default_role: self.default_role().clone(),
98100
})
99101
}
100102
}
@@ -246,7 +248,6 @@ impl FromToProto<pb::UserInfo> for mt::UserInfo {
246248
Ok(mt::UserInfo {
247249
name: p.name.clone(),
248250
hostname: p.hostname.clone(),
249-
default_role: p.default_role.clone(),
250251
auth_info: mt::AuthInfo::from_pb(p.auth_info.ok_or_else(|| Incompatible {
251252
reason: "UserInfo.auth_info cannot be None".to_string(),
252253
})?)?,
@@ -268,7 +269,6 @@ impl FromToProto<pb::UserInfo> for mt::UserInfo {
268269
min_compatible: MIN_COMPATIBLE_VER,
269270
name: self.name.clone(),
270271
hostname: self.hostname.clone(),
271-
default_role: self.default_role.clone(),
272272
auth_info: Some(mt::AuthInfo::to_pb(&self.auth_info)?),
273273
grants: Some(mt::UserGrantSet::to_pb(&self.grants)?),
274274
quota: Some(mt::UserQuota::to_pb(&self.quota)?),

common/proto-conv/tests/it/user_proto_conv.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ fn s(ss: impl ToString) -> String {
3030
}
3131

3232
fn test_user_info() -> UserInfo {
33-
let mut option = mt::UserOption::default();
34-
option.set_option_flag(mt::UserOptionFlag::TenantSetting);
33+
let option = mt::UserOption::default()
34+
.with_set_flag(mt::UserOptionFlag::TenantSetting)
35+
.with_default_role(Some("role1".to_string()));
3536

3637
mt::UserInfo {
3738
name: "test_user".to_string(),
3839
hostname: "localhost".to_string(),
39-
default_role: None,
4040
auth_info: mt::AuthInfo::Password {
4141
hash_value: [
4242
116, 101, 115, 116, 95, 112, 97, 115, 115, 119, 111, 114, 100,

common/protos/proto/user.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ message UserOption {
9696
uint64 min_compatible = 101;
9797

9898
uint64 flags = 1;
99+
optional string default_role = 2;
99100
}
100101

101102
message UserInfo {
@@ -108,7 +109,6 @@ message UserInfo {
108109
UserGrantSet grants = 4;
109110
UserQuota quota = 5;
110111
UserOption option = 6;
111-
optional string default_role = 7;
112112
}
113113

114114
message UserIdentity {

query/src/interpreters/interpreter_user_create.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ impl Interpreter for CreateUserInterpreter {
6363
grants: UserGrantSet::empty(),
6464
quota: UserQuota::no_limit(),
6565
option: plan.user_option,
66-
default_role: None,
6766
};
6867
user_mgr
6968
.add_user(&tenant, user_info, plan.if_not_exists)

0 commit comments

Comments
 (0)