* Consolidates a single source privilege into the list of consolidated privileges. * If a privilege with the same resource target already exists, its actions are merged with the source privilege. * Otherwise, a new privilege is created and added to the list. */
| 501 | * Otherwise, a new privilege is created and added to the list. |
| 502 | */ |
| 503 | static void |
| 504 | ConsolidatePrivilege(List **consolidatedPrivileges, const Privilege *sourcePrivilege) |
| 505 | { |
| 506 | if (sourcePrivilege == NULL || sourcePrivilege->numActions == 0) |
| 507 | { |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | ListCell *privilege; |
| 512 | ConsolidatedPrivilege *existingPrivilege = NULL; |
| 513 | |
| 514 | foreach(privilege, *consolidatedPrivileges) |
| 515 | { |
| 516 | ConsolidatedPrivilege *currentPrivilege = |
| 517 | (ConsolidatedPrivilege *) lfirst(privilege); |
| 518 | |
| 519 | if (ComparePrivileges(currentPrivilege, sourcePrivilege)) |
| 520 | { |
| 521 | existingPrivilege = currentPrivilege; |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (existingPrivilege != NULL) |
| 527 | { |
| 528 | for (size_t i = 0; i < sourcePrivilege->numActions; i++) |
| 529 | { |
| 530 | bool actionFound; |
| 531 | |
| 532 | /* The consolidated privilege does not free the actual char* in the action HTAB; |
| 533 | * therefore it is safe to pass actions[i]. */ |
| 534 | hash_search(existingPrivilege->actions, |
| 535 | &sourcePrivilege->actions[i], HASH_ENTER, |
| 536 | &actionFound); |
| 537 | } |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | ConsolidatedPrivilege *newPrivilege = palloc0( |
| 542 | sizeof(ConsolidatedPrivilege)); |
| 543 | newPrivilege->isCluster = sourcePrivilege->isCluster; |
| 544 | newPrivilege->db = pstrdup(sourcePrivilege->db); |
| 545 | newPrivilege->collection = pstrdup(sourcePrivilege->collection); |
| 546 | newPrivilege->actions = CreateStringViewHashSet(); |
| 547 | |
| 548 | for (size_t i = 0; i < sourcePrivilege->numActions; i++) |
| 549 | { |
| 550 | bool actionFound; |
| 551 | |
| 552 | hash_search(newPrivilege->actions, |
| 553 | &sourcePrivilege->actions[i], |
| 554 | HASH_ENTER, &actionFound); |
| 555 | } |
| 556 | |
| 557 | *consolidatedPrivileges = lappend(*consolidatedPrivileges, newPrivilege); |
| 558 | } |
| 559 | } |
| 560 |
no test coverage detected