Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Org Unit split being assigned to incorrect users [DHIS2-18423] #19239

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

david-mackessy
Copy link
Contributor

@david-mackessy david-mackessy commented Nov 20, 2024

Issue

Org Unit split feature is assigning the split org units to incorrect users (sometimes all the users in the DB in certain circumstances)

Cause

After creating the UserQueryParams with the source OrganisationUnit, that OrganisationUnit was being overridden to use the User's OrganisationUnits carrying out the split.

In cases where the User either had no organisationUnits, dataViewOrganisationUnits, or teiSearchOrganisationUnits, then all the Users in the DB would be returned from the query, resulting in all Users getting the split OrganisationUnits.

Fix avoids using:

  • UserService#handleUserQueryParams which was overriding the source OrganisationUnit already set
  • UserStore#getUsers which tries to generate a handcrafted SQL query taking into consideration a multitude of options & checks using UserQueryParams (this looks too complex and tries to accommodate too many things). This resulted in returning all Users in the DB

Fix

Use a dedicated query to fetch the Users to be actioned.
Fewer select queries are generated as a result of the fix, and is not proportionate to the number of users found (old query was triggering 1 extra query per user found - manual testing).
Warning added to UserQueryParams property.

Testing

Automated

2 integration tests added

  • 1 for Org Unit split service: checking that the correct users get assigned the split org units
  • 1 for the store: checking the generated select query count & retrieved users

@david-mackessy david-mackessy marked this pull request as ready for review November 20, 2024 14:30
Copy link

sonarcloud bot commented Nov 20, 2024

@@ -224,4 +226,15 @@ Map<String, Optional<Locale>> findNotifiableUsersWithPasswordLastUpdatedBetween(
User getUserByVerificationToken(String token);

User getUserByVerifiedEmail(String email);

/**
* Method that retrieves all {@link User}s that have an entry for the {@link OrganisationUnit} in
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps not use "Method that" in method Javadoc, as it is implicit.

* WARNING: If this field has an empty collection, it can potentially result in all users being
* returned from the database. Check the generated query to make sure you're seeing expected
* queries and results.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this phrasing is a bit misleading. This is one of many potential filters that when empty does not filter away anything. That sounds like expected behaviour to me as long as it is clear that empty means "not set" and not "match against an empty set". I think the comment we leave here should make that clear.

* @param uid {@link OrganisationUnit} {@link UID} to match on
* @return matching {@link User}s
*/
List<User> getUsersWithOrgUnit(@Nonnull String orgUnitTable, @Nonnull UID uid);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should have names of tables escape the store. That is one of the main points to have a store abstraction. Maybe use an enum?
While these 3 properties are backed by mapping tables in the DB in the object model these are properties so the wording should be accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants