Skip to content
Snippets Groups Projects
Commit 72682c43 authored by srosse's avatar srosse
Browse files

OO-4004: set user role to identity without org. and not deleted

parent a7cebc62
No related branches found
No related tags found
No related merge requests found
......@@ -34,6 +34,7 @@ import java.util.stream.Collectors;
import javax.persistence.TypedQuery;
import org.olat.basesecurity.GroupMembershipInheritance;
import org.olat.basesecurity.IdentityImpl;
import org.olat.basesecurity.IdentityRef;
import org.olat.basesecurity.OrganisationService;
import org.olat.basesecurity.OrganisationStatus;
......@@ -277,6 +278,24 @@ public class OrganisationDAO {
.getResultList();
}
/**
* The method search identities, which are not deleted,
* which a not part of an organization.
*
* @return A list of identities
*/
public List<Identity> getIdentitiesWithoutOrganisations() {
StringBuilder sb = new StringBuilder(256);
sb.append("select ident from ").append(IdentityImpl.class.getCanonicalName()).append(" as ident")
.append(" where ident.status<199 and not exists (select membership.key from bgroupmember as membership")
.append(" inner join organisation as org on (org.group.key=membership.group.key)")
.append(" where membership.identity.key=ident.key")
.append(" )");
return dbInstance.getCurrentEntityManager()
.createQuery(sb.toString(), Identity.class)
.getResultList();
}
public List<Organisation> getOrganisations(IdentityRef identity, List<String> roleList) {
QueryBuilder sb = new QueryBuilder(256);
sb.append("select org from organisation org")
......
......@@ -55,6 +55,7 @@ details=Infoseite
details.copy=Kopieren
details.delete=L\u00F6schen
edit.icon=<i class\="o_icon o_icon-lg o_icon_edit" title\="Editieren"> </i>
error.select.target.level=Sie m\u00FCssen ein Ziel w\u00E4hlen.
filter.active=$\:status.active
filter.deleted=$\:status.deleted
filter.inactive=$\:status.inactive
......
......@@ -55,6 +55,7 @@ details=Info page
details.copy=Copy
details.delete=Delete
edit.icon=<i class\="o_icon o_icon-lg o_icon_edit" title\="Edit"> </i>
error.select.target.level=You must choose a target.
filter.active=$\:status.active
filter.deleted=$\:status.deleted
filter.inactive=$\:status.inactive
......
/**
* <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.List;
import org.olat.basesecurity.OrganisationRoles;
import org.olat.basesecurity.OrganisationService;
import org.olat.basesecurity.manager.OrganisationDAO;
import org.olat.core.commons.persistence.DB;
import org.olat.core.id.Identity;
import org.springframework.beans.factory.annotation.Autowired;
/**
*
* Initial date: 3 avr. 2019<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public class OLATUpgrade_13_2_4 extends OLATUpgrade {
private static final String VERSION = "OLAT_13.2.4";
private static final String ORPHAN_IDENTITIES = "ORPHAN IDENTITIES";
@Autowired
private DB dbInstance;
@Autowired
private OrganisationDAO organisationDao;
@Autowired
private OrganisationService organisationService;
public OLATUpgrade_13_2_4() {
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 = true;
allOk &= updateCurriculumElementChildren(upgradeManager, uhd);
uhd.setInstallationComplete(allOk);
upgradeManager.setUpgradesHistory(uhd, VERSION);
if(allOk) {
log.audit("Finished OLATUpgrade_13_2_4 successfully!");
} else {
log.audit("OLATUpgrade_13_2_4 not finished, try to restart OpenOLAT!");
}
return allOk;
}
/**
* Find identities without organizations but not deleted.
*
* @param upgradeManager The upgrade manager
* @param uhd The upgrade history
* @return true if successful
*/
private boolean updateCurriculumElementChildren(UpgradeManager upgradeManager, UpgradeHistoryData uhd) {
boolean allOk = true;
if (!uhd.getBooleanDataValue(ORPHAN_IDENTITIES)) {
List<Identity> orphans = organisationDao.getIdentitiesWithoutOrganisations();
if(orphans.isEmpty()) {
for(Identity orphan:orphans) {
organisationService.addMember(orphan, OrganisationRoles.user);
}
log.info("Add " + orphans.size() + " identities without organisations to the default org (wasn't in users sec. group)");
}
dbInstance.commitAndCloseSession();
uhd.setBooleanDataValue(ORPHAN_IDENTITIES, allOk);
upgradeManager.setUpgradesHistory(uhd, VERSION);
}
return allOk;
}
}
......@@ -22,8 +22,7 @@
<bean id="olatupgrades" class="org.olat.upgrade.UpgradesDefinitions" >
<property name="upgrades">
<list>
<!-- add upgrade bean here, if you need to run DB upgrades add the property "alterDbStatements" with the
filename from the /database/[dialect]/ directory -->
<!-- add upgrade bean here -->
<bean id="upgrade_10_0_0" class="org.olat.upgrade.OLATUpgrade_10_0_0"/>
<bean id="upgrade_10_0_3" class="org.olat.upgrade.OLATUpgrade_10_0_3"/>
<bean id="upgrade_10_1_0" class="org.olat.upgrade.OLATUpgrade_10_1_0"/>
......@@ -48,6 +47,7 @@
<bean id="upgrade_13_0_0_beta9" class="org.olat.upgrade.OLATUpgrade_13_0_0_beta9"/>
<bean id="upgrade_13_1_0" class="org.olat.upgrade.OLATUpgrade_13_1_0"/>
<bean id="upgrade_13_2_0" class="org.olat.upgrade.OLATUpgrade_13_2_0"/>
<bean id="upgrade_13_2_4" class="org.olat.upgrade.OLATUpgrade_13_2_4"/>
</list>
</property>
</bean>
......
......@@ -512,4 +512,24 @@ public class OrganisationDAOTest extends OlatTestCase {
boolean has = organisationDao.hasAnyRole(member, OrganisationRoles.usermanager.name());
Assert.assertTrue(has);
}
@Test
public void getIdentitiesWithoutOrganisations() {
Identity member = JunitTestHelper.createAndPersistIdentityAsRndUser("Member-9");
Identity notMember = JunitTestHelper.createAndPersistIdentityAsRndUser("Member-10");
String identifier = UUID.randomUUID().toString();
Organisation organisation1 = organisationDao.createAndPersistOrganisation("Org 12", identifier, null, null, null);
dbInstance.commit();
// remove def. organisation and make some noise
Organisation defOrganisation = organisationService.getDefaultOrganisation();
organisationService.removeMember(defOrganisation, notMember);
organisationService.addMember(organisation1, member, OrganisationRoles.user);
dbInstance.commitAndCloseSession();
// check
List<Identity> identities = organisationDao.getIdentitiesWithoutOrganisations();
Assert.assertFalse(identities.isEmpty());
Assert.assertFalse(identities.contains(member));
Assert.assertTrue(identities.contains(notMember));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment