dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #34706
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17798: UserService, methods for getting managed users
------------------------------------------------------------
revno: 17798
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-12-25 16:05:06 +0100
message:
UserService, methods for getting managed users
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
--
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-25 14:26:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java 2014-12-25 15:05:06 +0000
@@ -164,6 +164,26 @@
Collection<String> getUsernames( String query, Integer max );
+ /**
+ * Returns all users which are managed by the given user through its managed
+ * groups association.
+ *
+ * @param user the user.
+ * @return a List of users.
+ */
+ List<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.
+ * @return a List of users.
+ */
+ List<User> getManagedUsers( User user, int first, int max );
+
int countDataSetUserAuthorityGroups( DataSet dataSet );
/**
=== 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-25 14:26:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java 2014-12-25 15:05:06 +0000
@@ -29,6 +29,7 @@
*/
import java.util.Collection;
+import java.util.List;
import org.hisp.dhis.common.GenericIdentifiableObjectStore;
@@ -64,4 +65,24 @@
* @return a Collection of Users.
*/
Collection<User> getUsersByName( String name );
+
+ /**
+ * Returns all users which are managed by the given user through its managed
+ * groups association.
+ *
+ * @param user the user.
+ * @return a List of users.
+ */
+ List<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.
+ * @return a List of users.
+ */
+ List<User> getManagedUsers( User user, int first, int 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-25 14:26:57 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java 2014-12-25 15:05:06 +0000
@@ -204,6 +204,18 @@
}
@Override
+ public List<User> getManagedUsers( User user )
+ {
+ return userStore.getManagedUsers( user );
+ }
+
+ @Override
+ public List<User> getManagedUsers( User user, int first, int max )
+ {
+ return userStore.getManagedUsers( user, first, max );
+ }
+
+ @Override
public Collection<UserCredentials> getUsersByOrganisationUnitBetween( OrganisationUnit unit, int first, int max )
{
return userCredentialsStore.getUsersByOrganisationUnitBetween( unit, first, max );
@@ -340,7 +352,7 @@
{
return userAuthorityGroupStore.countDataSetUserAuthorityGroups( dataSet );
}
-
+
@Override
public boolean isSuperUser( UserCredentials userCredentials )
{
=== 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-25 14:26:57 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java 2014-12-25 15:05:06 +0000
@@ -28,6 +28,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -35,6 +36,7 @@
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.common.IdentifiableObjectUtils;
import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
import org.hisp.dhis.user.User;
import org.hisp.dhis.user.UserStore;
@@ -115,4 +117,29 @@
return criteria.list();
}
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public List<User> getManagedUsers( User user )
+ {
+ Collection<Integer> managedGroups = IdentifiableObjectUtils.getIdentifiers( user.getManagedGroups() );
+
+ String hql = "select distinct u from User u join u.groups g where g.id in (:ids) order by u.surname, u.firstName";
+
+ return sessionFactory.getCurrentSession().createQuery( hql ).setParameterList( "ids", managedGroups ).list();
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public List<User> getManagedUsers( User user, int first, int max )
+ {
+ Collection<Integer> managedGroups = IdentifiableObjectUtils.getIdentifiers( user.getManagedGroups() );
+
+ String hql = "select distinct u from User u join u.groups g where g.id in (:ids) order by u.surname, u.firstName";
+
+ return sessionFactory.getCurrentSession().createQuery( hql ).
+ setParameterList( "ids", managedGroups ).
+ setFirstResult( first ).
+ setMaxResults( max ).list();
+ }
}
=== 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-25 14:10:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java 2014-12-25 15:05:06 +0000
@@ -35,6 +35,7 @@
import static org.junit.Assert.assertFalse;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import org.hisp.dhis.DhisSpringTest;
@@ -190,4 +191,51 @@
assertFalse( userA.isManagedBy( userC ) );
assertFalse( userA.isManagedBy( userD ) );
}
+
+ @Test
+ public void testGetManagedGroups()
+ {
+ User userA = createUser( 'A' );
+ User userB = createUser( 'B' );
+ User userC = createUser( 'C' );
+ User userD = createUser( 'D' );
+
+ userService.addUser( userA );
+ userService.addUser( userB );
+ userService.addUser( userC );
+ userService.addUser( userD );
+
+ UserGroup userGroup1 = createUserGroup( 'A', Sets.newHashSet( userA, userB ) );
+ UserGroup userGroup2 = createUserGroup( 'B', Sets.newHashSet( userC, userD ) );
+ userA.getGroups().add( userGroup1 );
+ userB.getGroups().add( userGroup1 );
+ userC.getGroups().add( userGroup2 );
+ userD.getGroups().add( userGroup2 );
+
+ userGroup1.setManagedGroups( Sets.newHashSet( userGroup2 ) );
+ userGroup2.setManagedByGroups( Sets.newHashSet( userGroup1 ) );
+
+ userGroupService.addUserGroup( userGroup1 );
+ userGroupService.addUserGroup( userGroup2 );
+
+ List<User> users = userService.getManagedUsers( userA );
+
+ assertEquals( 2, users.size() );
+ assertTrue( users.contains( userC ) );
+ assertTrue( users.contains( userD ) );
+
+ users = userService.getManagedUsers( userA, 0, 1 );
+
+ assertEquals( 1, users.size() );
+
+ users = userService.getManagedUsers( userB );
+
+ assertEquals( 2, users.size() );
+ assertTrue( users.contains( userC ) );
+ assertTrue( users.contains( userD ) );
+
+ users = userService.getManagedUsers( userC );
+
+ assertEquals( 0, users.size() );
+ }
}