dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #34728
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17806: UserStore, added filter support for inactive since, self registration, organisation unit in hql q...
------------------------------------------------------------
revno: 17806
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2014-12-27 13:32:35 +0100
message:
UserStore, added filter support for inactive since, self registration, organisation unit in hql query method
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java
dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java
dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java 2014-12-27 11:19:18 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java 2014-12-27 12:32:35 +0000
@@ -160,17 +160,39 @@
* @return a Collection of users.
*/
Collection<User> getManagedUsers( User user );
-
+
/**
* Returns all users which are managed by the given user through its managed
* groups association.
*
* @param user the user.
- * @param first the first record to return.
- * @param max the max number of records to return.
+ * @param first the first record to return, null if 0.
+ * @param max the max number of records to return, null if none.
* @return a Collection of users.
*/
Collection<User> getManagedUsersBetween( User user, int first, int max );
+
+ /**
+ * Returns all users which are managed by the given user through its managed
+ * groups association.
+ *
+ * @param searchKey the string to search by first name, surname and user name,
+ * no search if null.
+ * @param user the user.
+ * @param constrainManagedGroups constrain the result to users within managed groups.
+ * @param constrainAuthSubset constrain the result to users with a subset of
+ * authorities.
+ * @param inactiveMonths number of months since user last logged in, null if none.
+ * @param selfRegistered constrain the result to self-registered users.
+ * @param organisationUnit constrain the result to users associated with the
+ * organisation unit.
+ * @param first the first record to return, null if 0.
+ * @param max the max number of records to return, null if none.
+ * @return a List of users.
+ */
+ Collection<User> getManagedUsersBetween( String searchKey, User user,
+ boolean constrainManagedGroups, boolean constrainAuthSubset,
+ Integer inactiveMonths, boolean selfRegistered, OrganisationUnit organisationUnit, Integer first, Integer max );
/**
* Tests whether the current user is allowed to create a user associated
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java 2014-12-27 11:19:18 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java 2014-12-27 12:32:35 +0000
@@ -29,9 +29,11 @@
*/
import java.util.Collection;
+import java.util.Date;
import java.util.List;
import org.hisp.dhis.common.GenericIdentifiableObjectStore;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
/**
* @author Nguyen Hong Duc
@@ -62,10 +64,21 @@
* Returns all users which are managed by the given user through its managed
* groups association.
*
+ * @param searchKey the string to search by first name, surname and user name,
+ * no search if null.
* @param user the user.
+ * @param constrainManagedGroups constrain the result to users within managed groups.
+ * @param constrainAuthSubset constrain the result to users with a subset of
+ * authorities.
+ * @param inactiveSince date for last login.
+ * @param selfRegistered constrain the result to self-registered users.
+ * @param organisationUnit constrain the result to users associated with the
+ * organisation unit.
* @param first the first record to return, null if 0.
* @param max the max number of records to return, null if none.
* @return a List of users.
*/
- List<User> getManagedUsersBetween( User user, Integer first, Integer max );
+ List<User> getManagedUsersBetween( String searchKey, User user,
+ boolean constrainManagedGroups, boolean constrainAuthSubset,
+ Date inactiveSince, boolean selfRegistered, OrganisationUnit organisationUnit, Integer first, Integer max );
}
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java 2014-12-27 11:19:18 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java 2014-12-27 12:32:35 +0000
@@ -206,18 +206,36 @@
@Override
public Collection<User> getManagedUsers( User user )
{
- return userStore.getManagedUsersBetween( user, null, null );
+ return userStore.getManagedUsersBetween( null, user, true, true, null, false, null, null, null );
}
@Override
public Collection<User> getManagedUsersBetween( User user, int first, int max )
{
+ return userStore.getManagedUsersBetween( null, user, true, true, null, false, null, first, max );
+ }
+
+ @Override
+ public Collection<User> getManagedUsersBetween( String searchKey, User user,
+ boolean constrainManagedGroups, boolean constrainAuthSubset,
+ Integer inactiveMonths, boolean selfRegistered, OrganisationUnit organisationUnit, Integer first, Integer max )
+ {
+ Date inactiveSince = null;
+
+ if ( inactiveMonths != null )
+ {
+ Calendar cal = PeriodType.createCalendarInstance();
+ cal.add( Calendar.MONTH, ( inactiveMonths * -1 ) );
+ inactiveSince = cal.getTime();
+ }
+
if ( user != null && user.isSuper() )
{
- return getAllUsers();
+ return userStore.getManagedUsersBetween( searchKey, user, false, false, inactiveSince, selfRegistered, organisationUnit, first, max );
}
- return userStore.getManagedUsersBetween( user, first, max );
+ return userStore.getManagedUsersBetween( searchKey, user,
+ constrainManagedGroups, constrainAuthSubset, inactiveSince, selfRegistered, organisationUnit, first, max );
}
@Override
@@ -227,8 +245,7 @@
}
@Override
- public Collection<UserCredentials> getUsersByOrganisationUnitBetweenByName( OrganisationUnit unit, String userName,
- int first, int max )
+ public Collection<UserCredentials> getUsersByOrganisationUnitBetweenByName( OrganisationUnit unit, String userName, int first, int max )
{
return userCredentialsStore.getUsersByOrganisationUnitBetweenByName( unit, userName, first, max );
}
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java 2014-12-27 11:19:18 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java 2014-12-27 12:32:35 +0000
@@ -29,6 +29,7 @@
*/
import java.util.Collection;
+import java.util.Date;
import java.util.List;
import java.util.Set;
@@ -38,6 +39,8 @@
import org.hibernate.criterion.Restrictions;
import org.hisp.dhis.common.IdentifiableObjectUtils;
import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.system.util.SqlHelper;
import org.hisp.dhis.user.User;
import org.hisp.dhis.user.UserStore;
@@ -102,28 +105,89 @@
@Override
@SuppressWarnings("unchecked")
- public List<User> getManagedUsersBetween( User user, Integer first, Integer max )
+ public List<User> getManagedUsersBetween( String searchKey, User user,
+ boolean constrainManagedGroups, boolean constrainAuthSubset,
+ Date inactiveSince, boolean selfRegistered, OrganisationUnit organisationUnit, Integer first, Integer max )
{
- Collection<Integer> managedGroups = IdentifiableObjectUtils.getIdentifiers( user.getManagedGroups() );
-
- Set<String> auths = user.getUserCredentials().getAllAuthorities();
+ SqlHelper hlp = new SqlHelper();
String hql =
"select distinct u from User u " +
"inner join u.userCredentials uc " +
- "inner join u.groups g " +
- "where g.id in (:ids) " +
- "and not exists (" +
+ "left join u.groups g ";
+
+ if ( searchKey != null )
+ {
+ hql += hlp.whereAnd() + " (" +
+ "lower(u.firstName) like :key " +
+ "or lower(u.surname) like :key " +
+ "or lower(uc.username) like :key) ";
+ }
+
+ if ( constrainManagedGroups )
+ {
+ hql += hlp.whereAnd() + " g.id in (:ids) ";
+ }
+
+ if ( constrainAuthSubset )
+ {
+ hql += hlp.whereAnd() + " not exists (" +
"select uc2 from UserCredentials uc2 " +
"inner join uc2.userAuthorityGroups ag " +
"inner join ag.authorities a " +
"where uc2.id = uc.id " +
- "and a not in (:auths) ) " +
- "order by u.surname, u.firstName";
-
- Query query = sessionFactory.getCurrentSession().createQuery( hql ).
- setParameterList( "ids", managedGroups ).
- setParameterList( "auths", auths );
+ "and a not in (:auths) ) ";
+ }
+
+ //TODO constrain by own user roles
+
+ if ( inactiveSince != null )
+ {
+ hql += hlp.whereAnd() + " uc.lastLogin < :inactiveSince ";
+ }
+
+ if ( selfRegistered )
+ {
+ hql += hlp.whereAnd() + " uc.selfRegistered = true ";
+ }
+
+ if ( organisationUnit != null )
+ {
+ hql += hlp.whereAnd() + " :organisationUnit in elements(u.organisationUnits) ";
+ }
+
+ hql += "order by u.surname, u.firstName";
+
+ Query query = sessionFactory.getCurrentSession().createQuery( hql );
+
+ if ( searchKey != null )
+ {
+ query.setString( "key", "%" + searchKey.toLowerCase() + "%" );
+ }
+
+ if ( constrainManagedGroups )
+ {
+ Collection<Integer> managedGroups = IdentifiableObjectUtils.getIdentifiers( user.getManagedGroups() );
+
+ query.setParameterList( "ids", managedGroups );
+ }
+
+ if ( constrainAuthSubset )
+ {
+ Set<String> auths = user.getUserCredentials().getAllAuthorities();
+
+ query.setParameterList( "auths", auths );
+ }
+
+ if ( inactiveSince != null )
+ {
+ query.setDate( "inactiveSince", inactiveSince );
+ }
+
+ if ( organisationUnit != null )
+ {
+ query.setEntity( "organisationUnit", organisationUnit );
+ }
if ( first != null )
{
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java 2014-12-26 22:47:28 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java 2014-12-27 12:32:35 +0000
@@ -363,4 +363,110 @@
assertEquals( 0, users.size() );
}
+
+ @Test
+ public void testGetManagedGroupsSearch()
+ {
+ User userA = createUser( 'A' );
+ User userB = createUser( 'B' );
+ User userC = createUser( 'C' );
+ User userD = createUser( 'D' );
+ User userE = createUser( 'E' );
+ User userF = createUser( 'F' );
+
+ UserCredentials credentialsA = createUserCredentials( 'A', userA );
+ UserCredentials credentialsB = createUserCredentials( 'B', userB );
+ UserCredentials credentialsC = createUserCredentials( 'C', userC );
+ UserCredentials credentialsD = createUserCredentials( 'D', userD );
+ UserCredentials credentialsE = createUserCredentials( 'E', userE );
+ UserCredentials credentialsF = createUserCredentials( 'F', userF );
+
+ userService.addUser( userA );
+ userService.addUser( userB );
+ userService.addUser( userC );
+ userService.addUser( userD );
+ userService.addUser( userE );
+ userService.addUser( userF );
+
+ userService.addUserCredentials( credentialsA );
+ userService.addUserCredentials( credentialsB );
+ userService.addUserCredentials( credentialsC );
+ userService.addUserCredentials( credentialsD );
+ userService.addUserCredentials( credentialsE );
+ userService.addUserCredentials( credentialsF );
+
+ Collection<User> users = userService.getManagedUsersBetween( "rstnameA", null, false, false, null, false, null, null, null );
+
+ assertEquals( 1, users.size() );
+ assertTrue( users.contains( userA ) );
+ }
+
+ @Test
+ public void testGetManagedGroupsSelfRegistered()
+ {
+ User userA = createUser( 'A' );
+ User userB = createUser( 'B' );
+ User userC = createUser( 'C' );
+ User userD = createUser( 'D' );
+
+ UserCredentials credentialsA = createUserCredentials( 'A', userA );
+ UserCredentials credentialsB = createUserCredentials( 'B', userB );
+ UserCredentials credentialsC = createUserCredentials( 'C', userC );
+ UserCredentials credentialsD = createUserCredentials( 'D', userD );
+
+ credentialsA.setSelfRegistered( true );
+ credentialsC.setSelfRegistered( true );
+
+ userService.addUser( userA );
+ userService.addUser( userB );
+ userService.addUser( userC );
+ userService.addUser( userD );
+
+ userService.addUserCredentials( credentialsA );
+ userService.addUserCredentials( credentialsB );
+ userService.addUserCredentials( credentialsC );
+ userService.addUserCredentials( credentialsD );
+
+ Collection<User> users = userService.getManagedUsersBetween( null, null, false, false, null, true, null, null, null );
+
+ assertEquals( 2, users.size() );
+ assertTrue( users.contains( userA ) );
+ assertTrue( users.contains( userC ) );
+ }
+
+ @Test
+ public void testGetManagedGroupsOrganisationUnit()
+ {
+ User userA = createUser( 'A' );
+ User userB = createUser( 'B' );
+ User userC = createUser( 'C' );
+ User userD = createUser( 'D' );
+
+ userA.getOrganisationUnits().add( unit1 );
+ userA.getOrganisationUnits().add( unit2 );
+ userB.getOrganisationUnits().add( unit2 );
+ userC.getOrganisationUnits().add( unit1 );
+ userD.getOrganisationUnits().add( unit2 );
+
+ UserCredentials credentialsA = createUserCredentials( 'A', userA );
+ UserCredentials credentialsB = createUserCredentials( 'B', userB );
+ UserCredentials credentialsC = createUserCredentials( 'C', userC );
+ UserCredentials credentialsD = createUserCredentials( 'D', userD );
+
+ userService.addUser( userA );
+ userService.addUser( userB );
+ userService.addUser( userC );
+ userService.addUser( userD );
+
+ userService.addUserCredentials( credentialsA );
+ userService.addUserCredentials( credentialsB );
+ userService.addUserCredentials( credentialsC );
+ userService.addUserCredentials( credentialsD );
+
+ Collection<User> users = userService.getManagedUsersBetween( null, null, false, false, null, false, unit1, null, null );
+
+ assertEquals( 2, users.size() );
+ assertTrue( users.contains( userA ) );
+ assertTrue( users.contains( userC ) );
+ }
}
=== modified file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java 2014-11-11 12:58:17 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java 2014-12-27 12:32:35 +0000
@@ -1045,7 +1045,7 @@
user.setPhoneNumber( "PhoneNumber" + uniqueCharacter );
UserCredentials credentials = new UserCredentials();
- credentials.setUsername( "username" );
+ credentials.setUsername( "username" ); //TODO include uniqueCharacter
credentials.setPassword( "password" );
user.setUserCredentials( credentials );