diff --git a/src/main/java/org/olat/course/groupsandrights/CourseGroupManager.java b/src/main/java/org/olat/course/groupsandrights/CourseGroupManager.java index 58e5ccac03142df896dfe39d801451d3f0dbd299..989511677ae2fdefcfb0e9b6ced8000b9cdc528a 100644 --- a/src/main/java/org/olat/course/groupsandrights/CourseGroupManager.java +++ b/src/main/java/org/olat/course/groupsandrights/CourseGroupManager.java @@ -77,6 +77,8 @@ public interface CourseGroupManager { * @return true if user is in learning group, false otherwhise */ public boolean isIdentityInGroup(Identity identity, String groupName); + + public boolean isIdentityInGroup(Identity identity, Long groupKey); /** * Checks whether a set of learning groups with an identical name are full or not. @@ -97,6 +99,8 @@ public interface CourseGroupManager { * @return true if user is in such an area, false otherwhise */ public boolean isIdentityInLearningArea(Identity identity, String areaName); + + public boolean isIdentityInLearningArea(Identity identity, Long areaKey); /** * Checks if user is coach in any of the courses learning groups @@ -122,14 +126,6 @@ public interface CourseGroupManager { */ public boolean isIdentityCourseParticipant(Identity identity); - /** - * Checks if user is participant in any learning group of this course - * - * @param identity - * @return boolean - */ - public boolean isIdentityParticipantInAnyGroup(Identity identity); - /** * @return A list of all learning group from all learning group contexts of * this course diff --git a/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java b/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java index 916bbbc1c9c5ada7dc8daff0155ed26f680505d2..9dd60c5dd57ca0863aa2d499a5a34cb49f6a096e 100644 --- a/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java +++ b/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java @@ -130,7 +130,12 @@ public class PersistingCourseGroupManager extends BasicManager implements Course public boolean isIdentityInGroup(Identity identity, String groupName) { return businessGroupService.isIdentityInBusinessGroup(identity, groupName, true, true, courseResource); } - + + @Override + public boolean isIdentityInGroup(Identity identity, Long groupKey) { + return businessGroupService.isIdentityInBusinessGroup(identity, groupKey, true, true, courseResource); + } + /** * @see org.olat.course.groupsandrights.CourseGroupManager#isLearningGroupFull(java.lang.String) */ @@ -177,7 +182,12 @@ public class PersistingCourseGroupManager extends BasicManager implements Course * java.lang.String) */ public boolean isIdentityInLearningArea(Identity identity, String areaName) { - return areaManager.isIdentityInBGArea(identity, areaName, courseResource); + return areaManager.isIdentityInBGArea(identity, areaName, null, courseResource); + } + + @Override + public boolean isIdentityInLearningArea(Identity identity, Long areaKey) { + return areaManager.isIdentityInBGArea(identity, null, areaKey, courseResource); } /** @@ -287,7 +297,7 @@ public class PersistingCourseGroupManager extends BasicManager implements Course } boolean isParticipant = secManager.isIdentityPermittedOnResourceable(identity, Constants.PERMISSION_COACH, courseResource) - || businessGroupService.isIdentityInBusinessGroup(identity, null, true, false, courseResource); + || businessGroupService.isIdentityInBusinessGroup(identity, (String)null, true, false, courseResource); return isParticipant; } @@ -306,7 +316,7 @@ public class PersistingCourseGroupManager extends BasicManager implements Course } boolean isParticipant = secManager.isIdentityPermittedOnResourceable(identity, Constants.PERMISSION_PARTI, courseResource) - || businessGroupService.isIdentityInBusinessGroup(identity, null, false, true, courseResource); + || businessGroupService.isIdentityInBusinessGroup(identity, (String)null, false, true, courseResource); return isParticipant; } @@ -320,13 +330,6 @@ public class PersistingCourseGroupManager extends BasicManager implements Course return secMgr.isIdentityPermittedOnResourceable(identity, Constants.PERMISSION_ADMIN, courseResource); } - /** - * @see org.olat.course.groupsandrights.CourseGroupManager#isIdentityParticipantInAnyLearningGroup(org.olat.core.id.Identity) - */ - public boolean isIdentityParticipantInAnyGroup(Identity identity) { - return businessGroupService.isIdentityInBusinessGroup(identity, null, false, true, courseResource); - } - /** * @see org.olat.course.groupsandrights.CourseGroupManager#deleteCourseGroupmanagement() */ diff --git a/src/main/java/org/olat/course/run/preview/PreviewCourseGroupManager.java b/src/main/java/org/olat/course/run/preview/PreviewCourseGroupManager.java index d40ccff1248265c5d0fc8290c33df0e551576e2b..9ffc57d317db693951657d26b67f641b1e800843 100644 --- a/src/main/java/org/olat/course/run/preview/PreviewCourseGroupManager.java +++ b/src/main/java/org/olat/course/run/preview/PreviewCourseGroupManager.java @@ -90,6 +90,16 @@ final class PreviewCourseGroupManager extends BasicManager implements CourseGrou return groups.contains(groupName); } + @Override + public boolean isIdentityInGroup(Identity identity, Long groupKey) { + for(BusinessGroup group:groups) { + if(groupKey.equals(group.getKey())) { + return true; + } + } + return false; + } + /** * @see org.olat.course.groupsandrights.CourseGroupManager#isLearningGroupFull(java.lang.String) */ @@ -104,6 +114,16 @@ final class PreviewCourseGroupManager extends BasicManager implements CourseGrou return areas.contains(areaName); } + @Override + public boolean isIdentityInLearningArea(Identity identity, Long areaKey) { + for(BGArea area:areas) { + if(areaKey.equals(area.getKey())) { + return true; + } + } + return false; + } + /** * @see org.olat.course.groupsandrights.CourseGroupManager#isIdentityCourseCoach(org.olat.core.id.Identity) */ @@ -123,13 +143,6 @@ final class PreviewCourseGroupManager extends BasicManager implements CourseGrou return isCourseAdmin; } - /** - * @see org.olat.course.groupsandrights.CourseGroupManager#isIdentityParticipantInAnyLearningGroup(org.olat.core.id.Identity) - */ - public boolean isIdentityParticipantInAnyGroup(Identity identity) { - throw new AssertException("unsupported"); - } - /** * @see org.olat.course.groupsandrights.CourseGroupManager#getAllLearningGroupsFromAllContexts() */ diff --git a/src/main/java/org/olat/group/BusinessGroupService.java b/src/main/java/org/olat/group/BusinessGroupService.java index e00e8f76a82c4123e4e902f80f541991a4e001b4..13252de114106659cc6cf0f1796fba6662ba0c95 100644 --- a/src/main/java/org/olat/group/BusinessGroupService.java +++ b/src/main/java/org/olat/group/BusinessGroupService.java @@ -454,6 +454,19 @@ public interface BusinessGroupService { */ public boolean isIdentityInBusinessGroup(Identity identity, String groupName, boolean ownedById, boolean attendedById, OLATResource resource); + /** + * Checks if an identity is in a business group with a specific key, either as owner or + * as participant + * @param identity + * @param groupKey + * @param ownedById + * @param attendedById + * @param resource + * @return + */ + public boolean isIdentityInBusinessGroup(Identity identity, Long groupKey, boolean ownedById, boolean attendedById, OLATResource resource); + + //export - import /** * Export group definitions to file. diff --git a/src/main/java/org/olat/group/area/BGAreaManager.java b/src/main/java/org/olat/group/area/BGAreaManager.java index 8185afd0f742823c92f97fa69a83b19097b6088d..823a89f31c79632ce05d2728c2bef761aedbfcca 100644 --- a/src/main/java/org/olat/group/area/BGAreaManager.java +++ b/src/main/java/org/olat/group/area/BGAreaManager.java @@ -176,7 +176,7 @@ public interface BGAreaManager { * @param resource * @return true if identity is in such an area, false otherwise */ - public boolean isIdentityInBGArea(Identity identity, String areaName, OLATResource resource); + public boolean isIdentityInBGArea(Identity identity, String areaName, Long groupKey, OLATResource resource); /** * Reloads the business group area from the database or the hibernate second diff --git a/src/main/java/org/olat/group/area/BGAreaManagerImpl.java b/src/main/java/org/olat/group/area/BGAreaManagerImpl.java index b26d98e640bf5a1187396888e324fbf15df48228..60de407a03820f7feaf24834ee39a66a473a9b46 100644 --- a/src/main/java/org/olat/group/area/BGAreaManagerImpl.java +++ b/src/main/java/org/olat/group/area/BGAreaManagerImpl.java @@ -327,19 +327,30 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager { * @see org.olat.group.area.BGAreaManager#isIdentityInBGArea(org.olat.core.id.Identity, * java.lang.String, org.olat.group.context.BGContext) */ - public boolean isIdentityInBGArea(Identity identity, String areaName, OLATResource resource) { + public boolean isIdentityInBGArea(Identity identity, String areaName, Long areaKey, OLATResource resource) { StringBuilder sb = new StringBuilder(); sb.append("select count(grp) from ").append(BusinessGroupImpl.class.getName()).append(" as grp") .append(", org.olat.group.area.BGAreaImpl as area, org.olat.group.area.BGtoAreaRelationImpl bgarel, org.olat.basesecurity.SecurityGroupMembershipImpl as secgmemb") - .append(" where bgarel.groupArea = area and bgarel.businessGroup = grp") - .append(" and area.name=:name and area.resource.key=:resourceKey ") + .append(" where bgarel.groupArea = area and bgarel.businessGroup = grp"); + if(StringHelper.containsNonWhitespace(areaName)) { + sb.append(" and area.name=:name "); + } + if(areaKey != null) { + sb.append(" and area.key=:areaKey "); + } + sb.append(" and area.resource.key=:resourceKey ") .append(" and ((grp.partipiciantGroup = secgmemb.securityGroup and secgmemb.identity.key=:identityKey) or (grp.ownerGroup = secgmemb.securityGroup and secgmemb.identity=:identityKey))"); - Number count = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), Number.class) + TypedQuery<Number> query = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), Number.class) .setParameter("identityKey", identity.getKey()) - .setParameter("resourceKey", resource.getKey()) - .setParameter("name", areaName) - .getSingleResult(); + .setParameter("resourceKey", resource.getKey()); + if(StringHelper.containsNonWhitespace(areaName)) { + query.setParameter("name", areaName); + } + if(areaKey != null) { + query.setParameter("areaKey", areaKey); + } + Number count = query.getSingleResult(); return count.intValue() > 0; } diff --git a/src/main/java/org/olat/group/manager/BusinessGroupRelationDAO.java b/src/main/java/org/olat/group/manager/BusinessGroupRelationDAO.java index c52e155b4e5f2be8e2a0fd32b69741d4992afa30..ea6ca96146519f346d2e97489741453ee5efa2c0 100644 --- a/src/main/java/org/olat/group/manager/BusinessGroupRelationDAO.java +++ b/src/main/java/org/olat/group/manager/BusinessGroupRelationDAO.java @@ -31,6 +31,7 @@ import javax.persistence.TypedQuery; import org.olat.basesecurity.SecurityGroupMembershipImpl; import org.olat.core.commons.persistence.DB; import org.olat.core.id.Identity; +import org.olat.core.util.StringHelper; import org.olat.group.BusinessGroup; import org.olat.group.BusinessGroupImpl; import org.olat.group.model.BGRepositoryEntryRelation; @@ -88,13 +89,21 @@ public class BusinessGroupRelationDAO { em.remove(relation); } } - - public boolean isIdentityInBusinessGroup(Identity identity, String name, OLATResource resource) { + public boolean isIdentityInBusinessGroup(Identity identity, String name, Long groupKey, OLATResource resource) { StringBuilder sb = new StringBuilder(); - sb.append("select count(bgi) from ").append(BusinessGroupImpl.class.getName()).append(" bgi where") - .append(" bgi.name=:name") - .append(" and (") + sb.append("select count(bgi) from ").append(BusinessGroupImpl.class.getName()).append(" bgi"); + boolean and = false; + if(StringHelper.containsNonWhitespace(name)) { + and = and(sb, and); + sb.append(" bgi.name=:name"); + } + if(groupKey != null) { + and = and(sb, and); + sb.append(" bgi.key=:groupKey"); + } + and(sb, and); + sb.append(" (") .append(" bgi.partipiciantGroup in (") .append(" select participantMemberShip.securityGroup from ").append(SecurityGroupMembershipImpl.class.getName()).append(" participantMemberShip ") .append(" where participantMemberShip.identity.key=:identityKey") @@ -109,14 +118,20 @@ public class BusinessGroupRelationDAO { .append(" select relation.group from ").append(BGResourceRelation.class.getName()).append(" relation where relation.resource.key=:resourceKey") .append(" )"); - Number count = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), Number.class) + TypedQuery<Number> query = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), Number.class) .setParameter("identityKey", identity.getKey()) - .setParameter("name", name) - .setParameter("resourceKey", resource.getKey()) - .getSingleResult(); + .setParameter("resourceKey", resource.getKey()); + + if(StringHelper.containsNonWhitespace(name)) { + query.setParameter("name", name); + } + if(groupKey != null) { + query.setParameter("groupKey", groupKey); + } + Number count = query.getSingleResult(); return count.intValue() > 0; } - + public int countMembersOf(OLATResource resource, boolean owner, boolean attendee) { if(!owner && !attendee) return 0; @@ -284,4 +299,10 @@ public class BusinessGroupRelationDAO { query.setParameter("groupKeys", groupKeys); return query.getResultList(); } + + private boolean and(StringBuilder sb, boolean and) { + if(and) sb.append(" and "); + else sb.append(" where "); + return true; + } } diff --git a/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java b/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java index acd487be3d38e1d8515044ba721278c1ff6a756c..15136683616d79ff794440edd9368a39261b989d 100644 --- a/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java +++ b/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java @@ -1200,7 +1200,14 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD @Transactional(readOnly=true) public boolean isIdentityInBusinessGroup(Identity identity, String groupName, boolean ownedById, boolean attendedById, OLATResource resource) { - return businessGroupRelationDAO.isIdentityInBusinessGroup(identity, groupName, resource); + return businessGroupRelationDAO.isIdentityInBusinessGroup(identity, groupName, null, resource); + } + + @Override + @Transactional(readOnly=true) + public boolean isIdentityInBusinessGroup(Identity identity, Long groupKey, + boolean ownedById, boolean attendedById, OLATResource resource) { + return businessGroupRelationDAO.isIdentityInBusinessGroup(identity, null, groupKey, resource); } @Override diff --git a/src/test/java/org/olat/group/test/BGAreaManagerTest.java b/src/test/java/org/olat/group/test/BGAreaManagerTest.java index e184400d225188b7c5bfc11fe9bc7e35e32a675c..2dc3c852cfc8ce6be3b694c27363e08f6289ca69 100644 --- a/src/test/java/org/olat/group/test/BGAreaManagerTest.java +++ b/src/test/java/org/olat/group/test/BGAreaManagerTest.java @@ -555,14 +555,25 @@ public class BGAreaManagerTest extends OlatTestCase { dbInstance.commitAndCloseSession(); //check in area 1 - boolean testArea1 = areaManager.isIdentityInBGArea(id1, "area-1-" + areaName, resource); + boolean testArea1 = areaManager.isIdentityInBGArea(id1, "area-1-" + areaName, null, resource); Assert.assertTrue(testArea1); //check in area 1 - boolean testArea2 = areaManager.isIdentityInBGArea(id1, "area-2-" + areaName, resource); + boolean testArea2 = areaManager.isIdentityInBGArea(id1, "area-2-" + areaName, null, resource); Assert.assertTrue(testArea2); //check in area 1 - boolean testArea3 = areaManager.isIdentityInBGArea(id1, "area-3-" + areaName, resource); + boolean testArea3 = areaManager.isIdentityInBGArea(id1, "area-3-" + areaName, null, resource); Assert.assertFalse(testArea3); + + //check with keys + //check in area 1 + boolean testArea4 = areaManager.isIdentityInBGArea(id1, null, area1.getKey(), resource); + Assert.assertTrue(testArea4); + //check in area 1 + boolean testArea5 = areaManager.isIdentityInBGArea(id1, null, area2.getKey(), resource); + Assert.assertTrue(testArea5); + //check in area 1 + boolean testArea6 = areaManager.isIdentityInBGArea(id1, null, area3.getKey(), resource); + Assert.assertFalse(testArea6); } /** diff --git a/src/test/java/org/olat/group/test/BusinessGroupRelationDAOTest.java b/src/test/java/org/olat/group/test/BusinessGroupRelationDAOTest.java index 84ca20439608e3d642177caa95bb4dd4ce683e45..dc689f4964dbdcd55d26fd305931094d31cac86d 100644 --- a/src/test/java/org/olat/group/test/BusinessGroupRelationDAOTest.java +++ b/src/test/java/org/olat/group/test/BusinessGroupRelationDAOTest.java @@ -235,17 +235,47 @@ public class BusinessGroupRelationDAOTest extends OlatTestCase { dbInstance.commitAndCloseSession(); //check - boolean test1 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bgis-1", resource1); + boolean test1 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bgis-1", null, resource1); Assert.assertTrue(test1); //name doesn't exist - boolean test2 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bgis-2", resource1); + boolean test2 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bgis-2", null, resource1); Assert.assertFalse(test2); //case insensitive (different between mysql and postgresql) //boolean test3 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bgis-1".toUpperCase(), resource1); //Assert.assertTrue(test3); //wrong resource - boolean test4 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bgis-1", resource3); + boolean test4 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bgis-1", null, resource3); Assert.assertFalse(test4); + //check null + boolean test5 = businessGroupRelationDao.isIdentityInBusinessGroup(id, null, null, resource1); + Assert.assertTrue(test5); + } + + @Test + public void isIdentityInBusinessGroupKeyOwner() { + //create relations + Identity id = JunitTestHelper.createAndPersistIdentityAsUser(UUID.randomUUID().toString()); + OLATResource resource1 = JunitTestHelper.createRandomResource(); + OLATResource resource2 = JunitTestHelper.createRandomResource(); + OLATResource resource3 = JunitTestHelper.createRandomResource(); + BusinessGroup group1 = businessGroupDao.createAndPersist(null, "rel-bgiskey-1", "rel-bgiskey-1-desc", -1, -1, false, false, false, false, false); + businessGroupRelationDao.addRelationToResource(group1, resource1); + businessGroupRelationDao.addRelationToResource(group1, resource2); + securityManager.addIdentityToSecurityGroup(id, group1.getOwnerGroup()); + + dbInstance.commitAndCloseSession(); + + //check + boolean test1 = businessGroupRelationDao.isIdentityInBusinessGroup(id, null, group1.getKey(), resource1); + Assert.assertTrue(test1); + //name doesn't exist + boolean test2 = businessGroupRelationDao.isIdentityInBusinessGroup(id, null, 1l, resource1); + Assert.assertFalse(test2); + boolean test3 = businessGroupRelationDao.isIdentityInBusinessGroup(id, null, group1.getKey(), resource3); + Assert.assertFalse(test3); + //check null + boolean test5 = businessGroupRelationDao.isIdentityInBusinessGroup(id, null, null, resource1); + Assert.assertTrue(test5); } @Test @@ -263,16 +293,16 @@ public class BusinessGroupRelationDAOTest extends OlatTestCase { dbInstance.commitAndCloseSession(); //check - boolean test1 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bg-part-1", resource1); + boolean test1 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bg-part-1", null, resource1); Assert.assertTrue(test1); //name doesn't exist - boolean test2 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bg-part-2", resource1); + boolean test2 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bg-part-2", null, resource1); Assert.assertFalse(test2); //case insensitive (different between mysql and postgresql) //boolean test3 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bg-part-1".toUpperCase(), resource1); //Assert.assertTrue(test3); //wrong resource - boolean test4 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bg-part-1", resource3); + boolean test4 = businessGroupRelationDao.isIdentityInBusinessGroup(id, "rel-bg-part-1", null, resource3); Assert.assertFalse(test4); }