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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use std::fmt::Debug;
use std::net::IpAddr;

use mz_adapter_types::connection::ConnectionId;
use mz_repr::role_id::RoleId;

use crate::plan::PlanContext;
use crate::session::user::{RoleMetadata, User};
use crate::session::vars::SessionVars;

pub trait SessionMetadata: Debug + Sync {
    /// Returns the session vars for this session.
    fn vars(&self) -> &SessionVars;
    /// Returns the connection ID associated with the session.
    fn conn_id(&self) -> &ConnectionId;
    /// Returns the client address associated with the session.
    fn client_ip(&self) -> Option<&IpAddr>;
    /// Returns the current transaction's PlanContext. Panics if there is not a
    /// current transaction.
    fn pcx(&self) -> &PlanContext;
    /// Returns the role metadata for this session.
    fn role_metadata(&self) -> &RoleMetadata;

    /// Returns the session's current role ID.
    ///
    /// # Panics
    /// If the session has not connected successfully.
    fn current_role_id(&self) -> &RoleId {
        &self.role_metadata().current_role
    }

    /// Returns the session's session role ID.
    ///
    /// # Panics
    /// If the session has not connected successfully.
    fn session_role_id(&self) -> &RoleId {
        &self.role_metadata().session_role
    }

    fn user(&self) -> &User {
        self.vars().user()
    }

    fn database(&self) -> &str {
        self.vars().database()
    }

    fn search_path(&self) -> &[mz_sql_parser::ast::Ident] {
        self.vars().search_path()
    }

    fn is_superuser(&self) -> bool {
        self.vars().is_superuser()
    }

    fn enable_session_rbac_checks(&self) -> bool {
        self.vars().enable_session_rbac_checks()
    }
}