* connection_status implements the * core logic for connectionStatus command */
| 1125 | * core logic for connectionStatus command |
| 1126 | */ |
| 1127 | Datum |
| 1128 | connection_status(pgbson *showPrivilegesSpec) |
| 1129 | { |
| 1130 | ReportFeatureUsage(FEATURE_CONNECTION_STATUS); |
| 1131 | |
| 1132 | bool showPrivileges = false; |
| 1133 | if (showPrivilegesSpec != NULL) |
| 1134 | { |
| 1135 | showPrivileges = ParseConnectionStatusSpec(showPrivilegesSpec); |
| 1136 | } |
| 1137 | |
| 1138 | bool noError = true; |
| 1139 | const char *currentUser = GetUserNameFromId(GetUserId(), noError); |
| 1140 | |
| 1141 | bool returnDocuments = false; |
| 1142 | Datum userInfoDatum = GetSingleUserInfo(currentUser, returnDocuments); |
| 1143 | |
| 1144 | if (userInfoDatum == (Datum) 0) |
| 1145 | { |
| 1146 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR), |
| 1147 | errmsg( |
| 1148 | "Cannot find logged-in user"))); |
| 1149 | } |
| 1150 | |
| 1151 | const char *parentRole = text_to_cstring(DatumGetTextP(userInfoDatum)); |
| 1152 | if (parentRole == NULL) |
| 1153 | { |
| 1154 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR), |
| 1155 | errmsg( |
| 1156 | "Unable to locate appropriate role for the specified user"))); |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * Example output structure: |
| 1161 | * { |
| 1162 | * authInfo: { |
| 1163 | * authenticatedUsers: [ { user: ..., db: ... } ], // always 1 element |
| 1164 | * authenticatedUserRoles: [ { role: ..., db: ... }, ... ], |
| 1165 | * authenticatedUserPrivileges: [ { privilege }, ... ] // if showPrivileges |
| 1166 | * }, |
| 1167 | * ok: 1 |
| 1168 | * } |
| 1169 | * |
| 1170 | * privilege: { resource: { db:, collection: }, actions: [...] } |
| 1171 | */ |
| 1172 | pgbson_writer finalWriter; |
| 1173 | PgbsonWriterInit(&finalWriter); |
| 1174 | pgbson_writer authInfoWriter; |
| 1175 | PgbsonWriterStartDocument(&finalWriter, "authInfo", 8, |
| 1176 | &authInfoWriter); |
| 1177 | |
| 1178 | pgbson_array_writer usersArrayWriter; |
| 1179 | PgbsonWriterStartArray(&authInfoWriter, "authenticatedUsers", 18, &usersArrayWriter); |
| 1180 | pgbson_writer userWriter; |
| 1181 | PgbsonArrayWriterStartDocument(&usersArrayWriter, &userWriter); |
| 1182 | PgbsonWriterAppendUtf8(&userWriter, "user", 4, currentUser); |
| 1183 | PgbsonWriterAppendUtf8(&userWriter, "db", 2, "admin"); |
| 1184 | PgbsonArrayWriterEndDocument(&usersArrayWriter, &userWriter); |
no test coverage detected