From 5a30d4b71694904cab6028c9490cfaf23f09904d Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Tue, 16 Apr 2019 17:06:39 +0200
Subject: [PATCH] OO-4025: send mail to leave group to its own administrators

---
 .../manager/BusinessGroupServiceImpl.java     | 28 ++++++-----
 .../group/test/BusinessGroupServiceTest.java  | 48 +++++++++++++++++++
 2 files changed, 65 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java b/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java
index 5ae5c4a5df6..d66116f4397 100644
--- a/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java
+++ b/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java
@@ -41,6 +41,7 @@ import org.olat.basesecurity.Group;
 import org.olat.basesecurity.GroupRoles;
 import org.olat.basesecurity.IdentityRef;
 import org.olat.basesecurity.OrganisationRoles;
+import org.olat.basesecurity.SearchIdentityParams;
 import org.olat.collaboration.CollaborationTools;
 import org.olat.collaboration.CollaborationToolsFactory;
 import org.olat.commons.info.InfoMessageFrontendManager;
@@ -48,6 +49,7 @@ import org.olat.core.CoreSpringFactory;
 import org.olat.core.commons.persistence.DB;
 import org.olat.core.commons.services.notifications.NotificationsManager;
 import org.olat.core.id.Identity;
+import org.olat.core.id.OrganisationRef;
 import org.olat.core.id.Roles;
 import org.olat.core.logging.DBRuntimeException;
 import org.olat.core.logging.KnownIssueException;
@@ -1036,13 +1038,13 @@ public class BusinessGroupServiceImpl implements BusinessGroupService {
 			if(group.isAllowToLeave()) {
 				opt = new LeaveOption();
 			} else {
-				ContactList list = getAdminContactList(group);
+				ContactList list = getAdminContactList(identity, group);
 				opt = new LeaveOption(false, list);
 			}
 		} else if(groupModule.isAllowLeavingGroupCreatedByAuthors() && groupModule.isAllowLeavingGroupCreatedByLearners()) {
 			opt = new LeaveOption();
 		} else if(!groupModule.isAllowLeavingGroupCreatedByAuthors() && !groupModule.isAllowLeavingGroupCreatedByLearners()) {
-			ContactList list = getAdminContactList(group);
+			ContactList list = getAdminContactList(identity, group);
 			opt = new LeaveOption(false, list);
 		} else {
 			int numOfCoaches = countMembers(group, GroupRoles.coach.name());
@@ -1053,7 +1055,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService {
 					if(groupModule.isAllowLeavingGroupCreatedByAuthors()) {
 						opt = new LeaveOption();
 					} else {
-						ContactList list = getAdminContactList(group);
+						ContactList list = getAdminContactList(identity, group);
 						opt = new LeaveOption(false, list);
 					}
 
@@ -1061,7 +1063,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService {
 				} else if(groupModule.isAllowLeavingGroupCreatedByLearners()) {
 					opt = new LeaveOption();
 				} else {
-					ContactList list = getAdminContactList(group);
+					ContactList list = getAdminContactList(identity, group);
 					opt = new LeaveOption(false, list);
 				}
 			} else {
@@ -1070,13 +1072,13 @@ public class BusinessGroupServiceImpl implements BusinessGroupService {
 					if(groupModule.isAllowLeavingGroupCreatedByAuthors()) {
 						opt = new LeaveOption();
 					} else {
-						ContactList list = getAdminContactList(group);
+						ContactList list = getAdminContactList(identity, group);
 						opt = new LeaveOption(false, list);
 					}
 				} else if(groupModule.isAllowLeavingGroupCreatedByLearners()) {
 					opt = new LeaveOption();
 				} else {
-					ContactList list = getAdminContactList(group);
+					ContactList list = getAdminContactList(identity, group);
 					opt = new LeaveOption(false, list);
 				}	
 			}
@@ -1084,7 +1086,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService {
 		return opt;
 	}
 	
-	public ContactList getAdminContactList(BusinessGroup group) {
+	private ContactList getAdminContactList(Identity identity, BusinessGroup group) {
 		ContactList list = new ContactList("Contact");
 		List<Identity> coaches = getMembers(group, GroupRoles.coach.name());
 		if(coaches.isEmpty()) {
@@ -1093,10 +1095,14 @@ public class BusinessGroupServiceImpl implements BusinessGroupService {
 			coaches.addAll(repositoryService.getMembers(entries, RepositoryEntryRelationType.all, GroupRoles.coach.name()));
 			
 			if(coaches.isEmpty()) {
-				//get system administrators
-				OrganisationRoles[] adminRoles = { OrganisationRoles.administrator };
-				List<Identity> admins = securityManager.getIdentitiesByPowerSearch(null, null, false, adminRoles,
-						null, null, null, null, null, Identity.STATUS_VISIBLE_LIMIT);
+				//get system administrators of the user's organisations
+				Roles roles = securityManager.getRoles(identity);
+				List<OrganisationRef> identityOrgs = roles.getOrganisationsWithRole(OrganisationRoles.user);
+				SearchIdentityParams identityParams = new SearchIdentityParams();
+				identityParams.setOrganisations(identityOrgs);
+				identityParams.setRoles(new OrganisationRoles[]{ OrganisationRoles.administrator });
+				identityParams.setStatus(Identity.STATUS_VISIBLE_LIMIT);
+				List<Identity> admins = securityManager.getIdentitiesByPowerSearch(identityParams, 0, -1);
 				list.addAllIdentites(admins);
 			}
 		}
diff --git a/src/test/java/org/olat/group/test/BusinessGroupServiceTest.java b/src/test/java/org/olat/group/test/BusinessGroupServiceTest.java
index 0e15e3437b2..0a5f25e3dc3 100644
--- a/src/test/java/org/olat/group/test/BusinessGroupServiceTest.java
+++ b/src/test/java/org/olat/group/test/BusinessGroupServiceTest.java
@@ -42,12 +42,15 @@ import org.olat.basesecurity.BaseSecurity;
 import org.olat.basesecurity.Group;
 import org.olat.basesecurity.GroupRoles;
 import org.olat.basesecurity.OrganisationRoles;
+import org.olat.basesecurity.OrganisationService;
 import org.olat.basesecurity.manager.GroupDAO;
 import org.olat.collaboration.CollaborationTools;
 import org.olat.collaboration.CollaborationToolsFactory;
 import org.olat.core.commons.persistence.DB;
 import org.olat.core.id.Identity;
+import org.olat.core.id.Organisation;
 import org.olat.core.id.Roles;
+import org.olat.core.id.User;
 import org.olat.core.logging.OLog;
 import org.olat.core.logging.Tracing;
 import org.olat.core.util.mail.ContactList;
@@ -67,6 +70,7 @@ import org.olat.resource.accesscontrol.ResourceReservation;
 import org.olat.resource.accesscontrol.manager.ACReservationDAO;
 import org.olat.test.JunitTestHelper;
 import org.olat.test.OlatTestCase;
+import org.olat.user.UserManager;
 import org.springframework.beans.factory.annotation.Autowired;
 
 /**
@@ -86,6 +90,8 @@ public class BusinessGroupServiceTest extends OlatTestCase {
 	@Autowired
 	private ACService acService;
 	@Autowired
+	private UserManager userManager;
+	@Autowired
 	private ACReservationDAO reservationDao;
 	@Autowired
 	private BaseSecurity securityManager;
@@ -96,6 +102,8 @@ public class BusinessGroupServiceTest extends OlatTestCase {
 	@Autowired
 	private BusinessGroupService businessGroupService;
 	@Autowired
+	private OrganisationService organisationService;
+	@Autowired
 	private MailModule mailModule;
 	
 	// Identities for tests
@@ -1145,6 +1153,46 @@ public class BusinessGroupServiceTest extends OlatTestCase {
 		}
 	}
 	
+	@Test
+	public void allowToLeavingBusinessGroup_subOrganisation() {
+		// a special
+		String uuid = UUID.randomUUID().toString();
+		Organisation organisation = organisationService.getDefaultOrganisation();
+		Organisation subOrganisation = organisationService
+				.createOrganisation("Sub-organisation", uuid, "", organisation, null);
+		
+		// create an administrator
+		String adminName = "admin" + uuid;
+		User adminUser = userManager.createUser("Admin", "Istrator", uuid + "admin@openolat.org");
+		Identity adminIdentity = securityManager.createAndPersistIdentityAndUser(adminName, null, adminUser, null, adminName);
+		organisationService.addMember(subOrganisation, adminIdentity, OrganisationRoles.user);
+		organisationService.addMember(subOrganisation, adminIdentity, OrganisationRoles.administrator);
+		//create a user
+		String userName = "user" + uuid;
+		User user = userManager.createUser("Us", "er", uuid + "user@openolat.org");
+		Identity userIdentity = securityManager.createAndPersistIdentityAndUser(userName, null, user, null, userName);
+		organisationService.addMember(subOrganisation, userIdentity, OrganisationRoles.user);
+		dbInstance.commitAndCloseSession();
+		
+		BusinessGroup group = businessGroupService.createBusinessGroup(null, "Leaving group", "But you cannot leave :-(", new Integer(0), new Integer(2), false, false, null);
+		businessGroupRelationDao.addRole(userIdentity, group, GroupRoles.participant.name());
+		dbInstance.commitAndCloseSession();
+		
+		//set to not allowed to leave
+		group = businessGroupService.updateAllowToLeaveBusinessGroup(group, false);
+		dbInstance.commitAndCloseSession();
+
+		//check the authors group leaving option
+		LeaveOption optionToLeave = businessGroupService.isAllowToLeaveBusinessGroup(userIdentity, group);
+		Assert.assertNotNull(optionToLeave);
+		Assert.assertFalse(optionToLeave.isAllowToLeave());
+		ContactList contacts = optionToLeave.getContacts();
+		Collection<Identity> contactList = contacts.getIdentiEmails().values();
+		Assert.assertNotNull(contactList);
+		Assert.assertFalse(contactList.isEmpty());
+		Assert.assertTrue(contactList.contains(adminIdentity));
+	}
+	
 	@Ignore @Test
 	public void parallelRemoveParticipants() {
 		mailModule.setInterSystem(true);
-- 
GitLab