Skip to content

fix(contexts): remove the navigation settings of a deleted receiver - #3204

Open
rubenvdlinde wants to merge 5 commits into
mainfrom
fix/noid/navigation-of-deleted-receiver
Open

rubenvdlinde wants to merge 5 commits into
mainfrom
fix/noid/navigation-of-deleted-receiver

Conversation

@rubenvdlinde

Copy link
Copy Markdown
Contributor

On main, independent of the grid and menu stack. When a user, group or team is deleted, ReceiverCleanupListener removes the shares made to them. The application navigation settings in tables_contexts_navigation that belonged to those shares, and the settings a deleted user made for themselves, stayed behind. They're now removed too.

Size: 169 lines: 54 code and 115 tests.

What changes

  • Settings of the deleted shares: before the listener deletes the shares to a receiver, it removes the navigation rows of those shares: the default row (empty user_id) and every user's own choice. This applies to users, groups and teams.
  • A deleted user's own settings: on UserDeletedEvent the listener also removes every row with that user_id, including the ones on shares made to a group the user was in.
  • New mapper methods: ShareMapper::findIdsByReceiver(), ContextNavigationMapper::deleteByShareIds() (in chunks of 1000) and ContextNavigationMapper::deleteByUserId(). Failures are logged as a warning, like the existing share cleanup, so deleting a user never fails because of Tables.

How it was tested

On Nextcloud 34 with PostgreSQL, new tests/unit/Listener/ReceiverCleanupListenerTest.php against the database:

  • deleting a user removes the settings of the share made to them and their own setting on a group share, and keeps the settings of other shares and other users
  • deleting a group removes the settings of the share made to the group and keeps a share to a user with the same name

Without the change both tests fail: all five and all three rows are still there. The full unit suite passes on PostgreSQL and SQLite (424 tests). composer cs:check, psalm and rector are all clean.

🖼️ Screenshots

No UI changes.

🏁 Checklist

  • ⛑️ Tests (unit and/or integration) are included or not needed
  • 🔙 Backport requests are created or not needed: /backport to stableX.X
  • 📅 Milestone is set
  • 🌸 PR title is meaningful (if it should be in the changelog: is it meaningful to users?)

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/navigation-of-deleted-receiver branch from 912a4c3 to 946d003 Compare November 3, 2026 14:51
@rubenvdlinde
rubenvdlinde marked this pull request as ready for review November 6, 2026 08:25

@AndyScherzinger AndyScherzinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review focused on performance, security, clean code and architecture (backend only). The checks ran on a dev instance (MariaDB) with this PR on main, deleting throwaway users, groups and their shares with occ.

The cleanup itself is right. For an application shared with a user, deleting that user removed the share's default row, the sharer's override and the user's own row, plus the user's override on a group share; deleting the group removed the group share's rows. Other rows stayed. The main problem is the order inside the existing try (comment on line 42).

CI: green.


private function cleanupByParticipant(string $type, string $participant): void {
try {
$this->contextNavigationMapper->deleteByShareIds($this->shareMapper->findIdsByReceiver($participant, $type));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The navigation cleanup now runs before deleteByReceiver() inside the same try/catch(\Throwable), so any database error in findIdsByReceiver() or deleteByShareIds() (a deadlock, a lock-wait timeout, a lost connection) now silently skips the share deletion this listener exists for. Before this PR the try held only deleteByReceiver(). The event fires once, so nothing retries. If the uid or gid is reused later, the new account or group gets the old shares, because access is matched by the receiver string. Reading the ids first, then deleteByReceiver(), then deleteByShareIds($ids) in its own guard keeps the share deletion independent; ShareService::delete() and deleteForShareReview() already use that order. Wrapping both in atomic() wouldn't help, since the rollback would keep the shares too.

Validated: reproduced on a deployed instance (MariaDB 10.6, S-main restored and this PR at 946d003e, with throwaway users and groups deleted through occ: an application shared with a user, its navigation row held FOR UPDATE by a second session, occ user:delete logged "cleanup table shares for deleted receiver has failed: ... 1205 Lock wait timeout exceeded" and the share stayed; after occ user:add with the same uid, the new account's GET /api/2/contexts listed the application).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I had put the navigation cleanup in front of the share deletion inside the same try. The listener now reads the application share ids first, deletes the shares as before, and removes the navigation rows afterwards in their own try with their own warning. There is a test where the navigation delete throws and the shares are still gone.

cc @blizzz

$user->method('getUID')->willReturn('gone');
$this->listener->handle(new UserDeletedEvent($user));

$this->assertSame([[$toStays, ''], [$toGroup, 'stays']], $this->storedNavigation());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test never asserts that the shares were deleted, although the class docblock says so, and the listener's logger is a plain mock. Removing deleteByReceiver() from the listener, or making it throw, keeps both tests green: storedNavigation() reads only tables_contexts_navigation, and the warning goes to the mock. No other test covers this listener, and the CircleDestroyedEvent branch isn't tested. Asserting that the receiver's tables_shares rows are gone and the others are kept, expects($this->never())->method('warning'), and a case where the navigation mapper throws and the shares are still deleted would cover it (the last one also pins the order from the comment on the listener's line 42). Smaller: navigation() re-implements ContextNavigationMapper::setDisplayModeByShareId() with a literal 1 instead of Application::NAV_ENTRY_MODE_RECIPIENTS, and share() sets five permission flags the listener never reads (permissionRead on a context share is a row shape production doesn't write).

Validated: code check only.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the tests only looked at the navigation table. They now assert that the receiver's shares are deleted and the others kept, expect no warning on the normal path, and cover a deleted team and a navigation delete that throws. The fixture uses NAV_ENTRY_MODE_RECIPIENTS and no longer sets permission flags the listener does not read.

cc @blizzz

/**
* @return list<array{int, string}> share id and user id of every stored navigation setting
*/
private function storedNavigation(): array {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

storedNavigation() asserts on the whole tables_contexts_navigation table, and tearDown() wipes every tables_* table. Run as AGENTS.md documents (vendor/bin/phpunit --bootstrap tests/unit/bootstrap.php <file>) without TEST_MODE=local, DatabaseTestCase uses the instance's database: any existing context share has a navigation row, so the assertion fails, and tearDown() then deletes all Tables data of that instance. CI is safe because it starts from an empty database, and the wipe is the base class's behaviour, but this PR adds the unscoped whole-table assertion and another test that triggers it. Restricting storedNavigation() to the fixture's share ids and deleting only the fixture's rows avoids both.

Validated: code check only.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. The test now keeps the ids of the shares it creates, storedNavigation() and the new share assertion only look at those, and tearDown() deletes only those shares and their navigation rows instead of wiping every Tables table.

cc @blizzz

Comment thread lib/Db/ShareMapper.php Outdated
* @return int[]
* @throws Exception
*/
public function findIdsByReceiver(string $receiver, string $receiverType): array {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findIdsByReceiver() has no node_type = 'context' condition, so table and view share ids, which never have navigation rows, go into chunked DELETEs that can't match: a receiver with 2,500 table shares and no context share costs 3 DELETE ... IN (1000 ids) that delete nothing. More generally, "deleting a context share deletes its navigation rows" is now written in five places: ShareService::delete(), deleteForShareReview(), deleteAllForContext(), transferSharesForContext() and this listener. The new copy sits at the mapper layer and already lacks the context filter the other four apply. A ShareService::deleteAllForReceiver() that the listener calls (context share ids only, shares first, navigation rows in their own guard, without the activity, audit and federation side effects of delete()) keeps one rule. The open #2476 also rewrites this listener and ShareMapper at the same place, and would then run a second SELECT for the same receiver.

Validated: code check only, plus EXPLAIN on the dev MariaDB (the filtered SELECT still uses shares_receiver_idx).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, only application shares have navigation rows. The query is now findContextShareIdsByReceiver() and filters on node_type = 'context', and a test checks that a table share id never reaches the navigation delete. I kept the sequence in the listener for this fix and would rather pull it and the other places onto one shared helper in a follow-up.

cc @blizzz

Comment thread lib/Db/ShareMapper.php Outdated
->from($this->table)
->where($qb->expr()->eq('receiver', $qb->createNamedParameter($receiver, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq('receiver_type', $qb->createNamedParameter($receiverType, IQueryBuilder::PARAM_STR)));
return array_map(intval(...), $qb->executeQuery()->fetchFirstColumn());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IResult::fetchFirstColumn() is @since 33.0.0. The backport box is open, and the listener was backported to stable1.0 (NC 30-32) before (35326a0). Cherry-picked there, every user, group and team deletion would throw "Call to undefined method" before deleteByReceiver() runs, the catch (\Throwable) would swallow it, and share cleanup would stop entirely (made worse by the order in the comment on the listener's line 42). Psalm against the stable30 stubs should catch it, but fetchAll() with array_column() works on every supported version.

Validated: code check only (lib/public/DB/IResult.php on server master: @since 33.0.0).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I had not checked the since tag against stable1.0. It no longer uses fetchFirstColumn(). On main it reads with fetchAllAssociative(), because main requires NC 33 and the rector check of the strict job asks for it there; the backport to stable1.0 will use fetchAll() instead, and I'll adapt that by hand when the backport comes up.

cc @blizzz

*
* @throws Exception
*/
public function deleteByUserId(string $userId): int {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleteByUserId() filters on user_id, the second column of the primary key (share_id, user_id), so every user deletion scans the whole tables_contexts_navigation table (EXPLAIN type ALL), also for users without any override. The table is small today (one row per context share plus per-user overrides, which NavigationContextItem.vue writes for every share of an application when a user toggles it), but under READ COMMITTED the scanning DELETE waits on any locked row of the table. Accepting it is reasonable; for bulk deprovisioning an index on user_id (migration plus AddMissingIndicesListener) avoids it.

Validated: {INSTANCE}: EXPLAIN gives type ALL; with an unrelated navigation row locked, the user's DELETE waited until the lock was released).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. The navigation table stays small, so I accept the scan on user_id here and leave the table without an extra index.

cc @blizzz

public function handle(Event $event): void {
if ($event instanceof UserDeletedEvent) {
$this->cleanupByParticipant(ShareReceiverType::USER, $event->getUser()->getUID());
$this->cleanupNavigationOfUser($event->getUser()->getUID());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only covers future deletions. Per-user overrides left by users deleted before this release (no version since 0.8 removed them) stay: a re-created bob added to the group of a share where the old bob had set display mode 0 inherits that override, and the app is hidden from his navigation although the manager's default is to show it (a 2 shows it against a hidden default). It grants no access, so it's low, but a one-time repair step deleting navigation rows whose share no longer exists or whose user_id isn't an existing user would close it. Smaller: $event->getUser()->getUID() is resolved twice here (lines 31 and 32); one $userId keeps the next cleanup from copying the chain a third time.

Validated: code check only.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The uid is now read once. You are right that rows left by earlier deletions stay behind; since they grant no access I would rather not grow this fix, so I drafted a follow-up issue for a repair step that removes navigation rows whose share or user no longer exists.

cc @blizzz

Comment thread lib/Db/ContextNavigationMapper.php Outdated
$qb = $this->db->getQueryBuilder();
$qb->delete($this->table)
->where($qb->expr()->in('share_id', $qb->createParameter('shareIds')));
foreach (array_chunk($shareIds, 1000) as $chunk) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array_chunk($shareIds, 1000) uses a literal where AGENTS.md asks for a named DB_CHUNK_SIZE = 1_000 constant ("IN clauses must be chunked to at most 1 000 items ... Use a named constant ... rather than a magic number"), which ContextMapper, Row2Mapper, RowSleeveMapper, TableMapper and ViewMapper already declare.

Validated: code check only.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, ContextNavigationMapper now declares DB_CHUNK_SIZE like the other mappers and chunks by it.

cc @blizzz

@github-actions

Copy link
Copy Markdown
Contributor

Hello there,
Thank you so much for taking the time and effort to create a pull request to our Nextcloud project.

We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process.

Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6

Thank you for contributing to Nextcloud and we hope to hear from you soon!

(If you believe you should not receive this message, you can add yourself to the blocklist.)

Deleting a user, group or team removed the shares made to them but left
the application navigation settings of those shares in
tables_contexts_navigation, and deleting a user also left the settings
they made for themselves on shares to others. Both are now removed with
the shares.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
…stant

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
Filter on node_type 'context', since only application shares have
navigation rows, and read the ids with fetchAll() so the query also
works on Nextcloud 30 to 32.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
…avigation

Read the application share ids first, delete the shares as before and
remove the navigation rows afterwards in their own guard, so a failing
navigation delete no longer keeps the shares. Read the uid once.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
…ope it to the fixture

Assert that the receiver's shares are deleted and others kept, expect no
warning on the normal path, cover a deleted team and a failing navigation
delete, and read and clean up only the rows the test created.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/navigation-of-deleted-receiver branch from 15fb338 to 816fcc6 Compare November 21, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants