* ParseResourceDocument parses the "resource" field from a privilege entry. * Extracts the database and collection names as StringViews. * Both 'db' and 'collection' are required fields. */
| 517 | * Both 'db' and 'collection' are required fields. |
| 518 | */ |
| 519 | static void |
| 520 | ParseResourceDocument(bson_iter_t *privilegeDocIter, |
| 521 | StringView *dbName, |
| 522 | StringView *collectionName) |
| 523 | { |
| 524 | if (bson_iter_type(privilegeDocIter) != BSON_TYPE_DOCUMENT) |
| 525 | { |
| 526 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), |
| 527 | errmsg("'resource' must be a document."))); |
| 528 | } |
| 529 | |
| 530 | bson_iter_t resourceIter; |
| 531 | bson_iter_recurse(privilegeDocIter, &resourceIter); |
| 532 | |
| 533 | bool dbFound = false; |
| 534 | bool collectionFound = false; |
| 535 | |
| 536 | while (bson_iter_next(&resourceIter)) |
| 537 | { |
| 538 | const char *resourceKey = bson_iter_key(&resourceIter); |
| 539 | |
| 540 | if (strcmp(resourceKey, "db") == 0) |
| 541 | { |
| 542 | if (bson_iter_type(&resourceIter) != BSON_TYPE_UTF8) |
| 543 | { |
| 544 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), |
| 545 | errmsg("'db' in resource must be a string."))); |
| 546 | } |
| 547 | |
| 548 | uint32_t strLength = 0; |
| 549 | const char *strValue = bson_iter_utf8(&resourceIter, &strLength); |
| 550 | if (strValue == NULL || strLength == 0) |
| 551 | { |
| 552 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), |
| 553 | errmsg("'db' in resource must not be empty."))); |
| 554 | } |
| 555 | |
| 556 | *dbName = CreateStringViewFromStringWithLength(strValue, strLength); |
| 557 | dbFound = true; |
| 558 | } |
| 559 | else if (strcmp(resourceKey, "collection") == 0) |
| 560 | { |
| 561 | if (bson_iter_type(&resourceIter) != BSON_TYPE_UTF8) |
| 562 | { |
| 563 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), |
| 564 | errmsg( |
| 565 | "'collection' in resource must be a string."))); |
| 566 | } |
| 567 | |
| 568 | uint32_t strLength = 0; |
| 569 | const char *strValue = bson_iter_utf8(&resourceIter, &strLength); |
| 570 | if (strValue == NULL || strLength == 0) |
| 571 | { |
| 572 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), |
| 573 | errmsg("'collection' in resource must not be empty."))); |
| 574 | } |
| 575 | |
| 576 | *collectionName = CreateStringViewFromStringWithLength(strValue, strLength); |
no test coverage detected