diff --git a/src/main/java/org/olat/basesecurity/BaseSecurity.java b/src/main/java/org/olat/basesecurity/BaseSecurity.java index c04e3cca9eb629a1215d0261e0e1186e89b26af2..7d5bbecd8bdd083aff567d5b0f7fa13d95432b56 100644 --- a/src/main/java/org/olat/basesecurity/BaseSecurity.java +++ b/src/main/java/org/olat/basesecurity/BaseSecurity.java @@ -418,6 +418,8 @@ public interface BaseSecurity { public Policy createAndPersistPolicyWithResource(SecurityGroup secGroup, String permission, Date from, Date to, OLATResource olatResource); + + public Policy findPolicy(SecurityGroup secGroup, String permission, OLATResource olatResource); /** * Create and persist an invitation with its security group and security token. diff --git a/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java b/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java index 1adfa1cefd9050036faaa5d4020fc9d8eca07f44..7624c414a1d210cadc4634221ca2de4da0d55a9b 100644 --- a/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java +++ b/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java @@ -440,10 +440,8 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD } //9. copy relations if(copyRelations) { - List<OLATResource> resources = businessGroupRelationDAO.findResources(Collections.singletonList(sourceBusinessGroup), 0, -1); - for(OLATResource resource:resources) { - businessGroupRelationDAO.addRelationToResource(newGroup, resource); - } + List<RepositoryEntry> resources = businessGroupRelationDAO.findRepositoryEntries(Collections.singletonList(sourceBusinessGroup), 0, -1); + addResourcesTo(Collections.singletonList(newGroup), resources); } return newGroup; } diff --git a/src/main/java/org/olat/upgrade/OLATUpgrade_8_4_5.java b/src/main/java/org/olat/upgrade/OLATUpgrade_8_4_5.java new file mode 100644 index 0000000000000000000000000000000000000000..fe4717c59f3851f5856dd8659fd785aceeaef190 --- /dev/null +++ b/src/main/java/org/olat/upgrade/OLATUpgrade_8_4_5.java @@ -0,0 +1,146 @@ +/** + * <a href="http://www.openolat.org"> + * OpenOLAT - Online Learning and Training</a><br> + * <p> + * Licensed under the Apache License, Version 2.0 (the "License"); <br> + * you may not use this file except in compliance with the License.<br> + * You may obtain a copy of the License at the + * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a> + * <p> + * Unless required by applicable law or agreed to in writing,<br> + * software distributed under the License is distributed on an "AS IS" BASIS, <br> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br> + * See the License for the specific language governing permissions and <br> + * limitations under the License. + * <p> + * Initial code contributed and copyrighted by<br> + * frentix GmbH, http://www.frentix.com + * <p> + */ +package org.olat.upgrade; + +import java.util.Collections; +import java.util.List; + +import org.olat.basesecurity.BaseSecurity; +import org.olat.basesecurity.Constants; +import org.olat.basesecurity.Policy; +import org.olat.core.commons.persistence.DB; +import org.olat.group.BusinessGroup; +import org.olat.group.BusinessGroupOrder; +import org.olat.group.BusinessGroupService; +import org.olat.group.model.SearchBusinessGroupParams; +import org.olat.repository.RepositoryEntry; +import org.olat.repository.RepositoryManager; +import org.olat.resource.OLATResourceManager; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Description:<br> + * upgrade code for OpenOLAT 8.3.x -> OpenOLAT 8.4.0 + * - Upgrade bookmarks to new database structure + * - Recalculate small user avatar images to new size + * + * <P> + * Initial Date: 24.03.2011 <br> + * + * @author Roman Haag, roman.haag@frentix.com, www.frentix.com + */ +public class OLATUpgrade_8_4_5 extends OLATUpgrade { + + private static final String TASK_COURSE_TO_GROUP_PERMISSIONS = "Upgrade course to group permissions"; + private static final int BATCH_SIZE = 20; + private static final String VERSION = "OLAT_8.4.5"; + + @Autowired + private DB dbInstance; + @Autowired + private BaseSecurity securityManager; + @Autowired + private OLATResourceManager resourceManager; + @Autowired + private RepositoryManager repositoryManager; + @Autowired + private BusinessGroupService businessGroupService; + + + public OLATUpgrade_8_4_5() { + super(); + } + + @Override + public String getVersion() { + return VERSION; + } + + @Override + public boolean doPreSystemInitUpgrade(UpgradeManager upgradeManager) { + return false; + } + + @Override + public boolean doPostSystemInitUpgrade(UpgradeManager upgradeManager) { + UpgradeHistoryData uhd = upgradeManager.getUpgradesHistory(VERSION); + if (uhd == null) { + // has never been called, initialize + uhd = new UpgradeHistoryData(); + } else { + if (uhd.isInstallationComplete()) { + return false; + } + } + + boolean allOk = upgradeCourseToGroup(upgradeManager, uhd); + + uhd.setInstallationComplete(allOk); + upgradeManager.setUpgradesHistory(uhd, VERSION); + if(allOk) { + log.audit("Finished OLATUpgrade_8_4_5 successfully!"); + } else { + log.audit("OLATUpgrade_8_4_5 not finished, try to restart OpenOLAT!"); + } + return allOk; + } + + private boolean upgradeCourseToGroup(UpgradeManager upgradeManager, UpgradeHistoryData uhd) { + if (!uhd.getBooleanDataValue(TASK_COURSE_TO_GROUP_PERMISSIONS)) { + SearchBusinessGroupParams params = new SearchBusinessGroupParams(); + + int counter = 0; + List<BusinessGroup> groups; + do { + groups = businessGroupService.findBusinessGroups(params, null, counter, BATCH_SIZE, BusinessGroupOrder.nameAsc); + for(BusinessGroup group:groups) { + processBusinessGroup(group); + } + counter += groups.size(); + log.audit("Business group processed: " + groups.size()); + dbInstance.intermediateCommit(); + } while(groups.size() == BATCH_SIZE); + + uhd.setBooleanDataValue(TASK_COURSE_TO_GROUP_PERMISSIONS, true); + upgradeManager.setUpgradesHistory(uhd, VERSION); + } + return true; + } + + private void processBusinessGroup(BusinessGroup group) { + List<RepositoryEntry> relations = businessGroupService.findRepositoryEntries(Collections.singletonList(group), 0, -1); + for(RepositoryEntry re:relations) { + //add author permission if needed + Policy accessPolicy = securityManager.findPolicy(re.getOwnerGroup(), Constants.PERMISSION_ACCESS, group.getResource()); + if(accessPolicy == null) { + securityManager.createAndPersistPolicyWithResource(re.getOwnerGroup(), Constants.PERMISSION_ACCESS, group.getResource()); + } + //add coach and participant permission if needed + Policy coachPolicy = securityManager.findPolicy(re.getOwnerGroup(), Constants.PERMISSION_ACCESS, group.getResource()); + if(coachPolicy == null) { + securityManager.createAndPersistPolicyWithResource(group.getOwnerGroup(), Constants.PERMISSION_COACH, re.getOlatResource()); + } + Policy participantPolicy = securityManager.findPolicy(re.getOwnerGroup(), Constants.PERMISSION_ACCESS, group.getResource()); + if(participantPolicy == null) { + securityManager.createAndPersistPolicyWithResource(group.getPartipiciantGroup(), Constants.PERMISSION_PARTI, re.getOlatResource()); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml b/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml index 1682390259fd003d2fa4ff3bbcdd9019b40815a0..4ca6bc5bce96eedb8f2855fd0b52ff1052b7354f 100644 --- a/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml +++ b/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml @@ -40,6 +40,7 @@ <bean id="upgrade_8_2_0" class="org.olat.upgrade.OLATUpgrade_8_2_0"/> <bean id="upgrade_8_3_0" class="org.olat.upgrade.OLATUpgrade_8_3_0"/> <bean id="upgrade_8_4_0" class="org.olat.upgrade.OLATUpgrade_8_4_0"/> + <bean id="upgrade_8_4_5" class="org.olat.upgrade.OLATUpgrade_8_4_5"/> </list> </property> </bean>