MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / alter_role

Function alter_role

nodedb/src/control/server/pgwire/ddl/role.rs:77–148  ·  view source on GitHub ↗

ALTER ROLE SET INHERIT

(
    state: &SharedState,
    identity: &AuthenticatedIdentity,
    parts: &[&str],
)

Source from the content-addressed store, hash-verified

75
76/// ALTER ROLE <name> SET INHERIT <parent>
77pub fn alter_role(
78 state: &SharedState,
79 identity: &AuthenticatedIdentity,
80 parts: &[&str],
81) -> PgWireResult<Vec<Response>> {
82 require_tenant_admin(identity, "alter roles")?;
83
84 // ALTER ROLE <name> SET INHERIT <parent>
85 if parts.len() < 6 {
86 return Err(sqlstate_error(
87 "42601",
88 "syntax: ALTER ROLE <name> SET INHERIT <parent>",
89 ));
90 }
91
92 let name = parts[2];
93 if !parts[3].eq_ignore_ascii_case("SET") || !parts[4].eq_ignore_ascii_case("INHERIT") {
94 return Err(sqlstate_error(
95 "42601",
96 "expected SET INHERIT after role name",
97 ));
98 }
99 let parent = parts[5];
100
101 // Validate parent exists (built-in or custom) and the role itself exists.
102 let old_role = state
103 .roles
104 .get_role(name)
105 .ok_or_else(|| sqlstate_error("42704", &format!("role '{name}' not found")))?;
106 let parent_is_builtin = matches!(
107 parent,
108 "superuser" | "tenant_admin" | "readwrite" | "readonly" | "monitor"
109 );
110 if !parent_is_builtin && state.roles.get_role(parent).is_none() {
111 return Err(sqlstate_error(
112 "42704",
113 &format!("parent role '{parent}' does not exist"),
114 ));
115 }
116
117 let now = std::time::SystemTime::now()
118 .duration_since(std::time::UNIX_EPOCH)
119 .unwrap_or_default()
120 .as_secs();
121 let stored = crate::control::security::catalog::StoredRole {
122 name: name.to_string(),
123 tenant_id: old_role.tenant_id.as_u64(),
124 parent: parent.to_string(),
125 created_at: now,
126 };
127
128 let entry = crate::control::catalog_entry::CatalogEntry::PutRole(Box::new(stored.clone()));
129 let log_index = crate::control::metadata_proposer::propose_catalog_entry(state, &entry)
130 .map_err(|e| sqlstate_error("XX000", &format!("metadata propose: {e}")))?;
131 if log_index == 0
132 && let Some(catalog) = state.credentials.catalog()
133 {
134 catalog

Callers 1

dispatchFunction · 0.85

Calls 14

require_tenant_adminFunction · 0.85
nowFunction · 0.85
propose_catalog_entryFunction · 0.85
get_roleMethod · 0.80
duration_sinceMethod · 0.80
to_stringMethod · 0.80
put_roleMethod · 0.80
audit_recordMethod · 0.80
sqlstate_errorFunction · 0.70
lenMethod · 0.45
as_u64Method · 0.45

Tested by

no test coverage detected