mz_expr/scalar/func/impls/
mz_acl_item.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::str::FromStr;
11
12use crate::EvalError;
13use mz_ore::str::StrExt;
14use mz_repr::ArrayRustType;
15use mz_repr::adt::mz_acl_item::{AclItem, AclMode, MzAclItem};
16use mz_repr::adt::system::Oid;
17
18sqlfunc!(
19    #[sqlname = "mz_aclitem_grantor"]
20    fn mz_acl_item_grantor(mz_acl_item: MzAclItem) -> String {
21        mz_acl_item.grantor.to_string()
22    }
23);
24
25sqlfunc!(
26    #[sqlname = "aclitem_grantor"]
27    fn acl_item_grantor(acl_item: AclItem) -> Oid {
28        acl_item.grantor
29    }
30);
31
32sqlfunc!(
33    #[sqlname = "mz_aclitem_grantee"]
34    fn mz_acl_item_grantee(mz_acl_item: MzAclItem) -> String {
35        mz_acl_item.grantee.to_string()
36    }
37);
38
39sqlfunc!(
40    #[sqlname = "aclitem_grantee"]
41    fn acl_item_grantee(acl_item: AclItem) -> Oid {
42        acl_item.grantee
43    }
44);
45
46sqlfunc!(
47    #[sqlname = "mz_aclitem_privileges"]
48    fn mz_acl_item_privileges(mz_acl_item: MzAclItem) -> String {
49        mz_acl_item.acl_mode.to_string()
50    }
51);
52
53sqlfunc!(
54    #[sqlname = "aclitem_privileges"]
55    fn acl_item_privileges(acl_item: AclItem) -> String {
56        acl_item.acl_mode.to_string()
57    }
58);
59
60sqlfunc!(
61    #[sqlname = "mz_format_privileges"]
62    fn mz_format_privileges(privileges: String) -> Result<ArrayRustType<String>, EvalError> {
63        AclMode::from_str(&privileges)
64            .map(|acl_mode| {
65                ArrayRustType(
66                    acl_mode
67                        .explode()
68                        .into_iter()
69                        .map(|privilege| privilege.to_string())
70                        .collect(),
71                )
72            })
73            .map_err(|e: anyhow::Error| EvalError::InvalidPrivileges(e.to_string().into()))
74    }
75);
76
77sqlfunc!(
78    #[sqlname = "mz_validate_privileges"]
79    fn mz_validate_privileges(privileges: String) -> Result<bool, EvalError> {
80        AclMode::parse_multiple_privileges(&privileges)
81            .map(|_| true)
82            .map_err(|e: anyhow::Error| EvalError::InvalidPrivileges(e.to_string().into()))
83    }
84);
85
86sqlfunc!(
87    #[sqlname = "mz_validate_role_privilege"]
88    fn mz_validate_role_privilege(privilege: String) -> Result<bool, EvalError> {
89        let privilege_upper = privilege.to_uppercase();
90        if privilege_upper != "MEMBER" && privilege_upper != "USAGE" {
91            Err(EvalError::InvalidPrivileges(
92                format!("{}", privilege.quoted()).into(),
93            ))
94        } else {
95            Ok(true)
96        }
97    }
98);