MCPcopy Create free account
hub / github.com/documentdb/documentdb / WriteRoleResponse

Function WriteRoleResponse

pg_documentdb/src/commands/roles.c:1305–1419  ·  view source on GitHub ↗

* Primitive type properties include _id, role, db, isBuiltin. * privileges: supported privilege actions of this role if defined. * roles: 1st level directly inherited roles if defined. * allInheritedRoles: all recursively inherited roles if defined. * inheritedPrivileges: consolidated privileges of current role and all recursively inherited roles if defined. */

Source from the content-addressed store, hash-verified

1303 * inheritedPrivileges: consolidated privileges of current role and all recursively inherited roles if defined.
1304 */
1305static void
1306WriteRoleResponse(const char *internalRoleName,
1307 pgbson_array_writer *rolesArrayWriter,
1308 RolesInfoSpec rolesInfoSpec,
1309 HTAB *roleInheritanceTable)
1310{
1311 bool foundEntry = false;
1312 RoleParentEntry *entry = (RoleParentEntry *) hash_search(
1313 roleInheritanceTable, internalRoleName, HASH_FIND, &foundEntry);
1314 if (!foundEntry)
1315 {
1316 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR),
1317 errmsg("Role '%s' not found.",
1318 internalRoleName)));
1319 }
1320
1321 pgbson_writer roleDocumentWriter;
1322 PgbsonArrayWriterStartDocument(rolesArrayWriter, &roleDocumentWriter);
1323
1324 const char *nativeRoleName = entry->nativeRoleName;
1325 char *roleId = psprintf("admin.%s", nativeRoleName);
1326 PgbsonWriterAppendUtf8(&roleDocumentWriter, "_id", 3, roleId);
1327 pfree(roleId);
1328
1329 PgbsonWriterAppendUtf8(&roleDocumentWriter, "role", 4, nativeRoleName);
1330 PgbsonWriterAppendUtf8(&roleDocumentWriter, "db", 2, "admin");
1331 PgbsonWriterAppendBool(&roleDocumentWriter, "isBuiltIn", 9,
1332 IS_NATIVE_BUILTIN_ROLE(nativeRoleName));
1333
1334 if (rolesInfoSpec.showPrivileges)
1335 {
1336 pgbson_array_writer privilegesArrayWriter;
1337 PgbsonWriterStartArray(&roleDocumentWriter, "privileges", 10,
1338 &privilegesArrayWriter);
1339 WritePrivileges(entry->internalRoleName, &privilegesArrayWriter);
1340 PgbsonWriterEndArray(&roleDocumentWriter, &privilegesArrayWriter);
1341 }
1342
1343 /* Write direct roles - parent roles are stored as internal names, convert to native names */
1344 pgbson_array_writer parentRolesArrayWriter;
1345 PgbsonWriterStartArray(&roleDocumentWriter, "roles", 5, &parentRolesArrayWriter);
1346 WriteRoles(entry->parentRoles, &parentRolesArrayWriter, roleInheritanceTable,
1347 internalRoleName);
1348 PgbsonWriterEndArray(&roleDocumentWriter, &parentRolesArrayWriter);
1349
1350 List *allInheritedRoles = LookupAllInheritedRoles(internalRoleName,
1351 roleInheritanceTable);
1352 pgbson_array_writer inheritedRolesArrayWriter;
1353 PgbsonWriterStartArray(&roleDocumentWriter, "allInheritedRoles", 17,
1354 &inheritedRolesArrayWriter);
1355 ListCell *roleCell;
1356 foreach(roleCell, allInheritedRoles)
1357 {
1358 const char *inheritedInternalName = (const char *) lfirst(roleCell);
1359
1360 /* Look up inherited role in HTAB to get its native name */
1361 bool inheritedFound = false;
1362 RoleParentEntry *inheritedEntry = (RoleParentEntry *) hash_search(

Callers 2

Calls 12

PgbsonWriterAppendUtf8Function · 0.85
PgbsonWriterAppendBoolFunction · 0.85
PgbsonWriterStartArrayFunction · 0.85
WritePrivilegesFunction · 0.85
PgbsonWriterEndArrayFunction · 0.85
LookupAllInheritedRolesFunction · 0.85
CreateStringViewHashSetFunction · 0.85
WriteRolesFunction · 0.70
foreachFunction · 0.50

Tested by

no test coverage detected