return false;
}
}
try {
acquireSyncLock();
InitialDirContext ctx;
List<Attributes> ldapUserList;
List<Attributes> newLdapUserList;
Map<Identity, Map<String, String>> changedMapIdenityMap;
List<Identity> deletedUserList;
String user;
LDAPLoginManager ldapMan = LDAPLoginManager.getInstance();
ctx = ldapMan.bindSystem();
if (ctx == null) {
errors.insert("LDAP connection ERROR");
log.error("Error in LDAP batch sync: LDAP connection empty");
freeSyncLock();
return false;
}
// Get time before sync to have a save sync time when sync is successful
Date timeBeforeSync = new Date();
String sinceSentence = (lastSyncDate == null ? " (full sync)" : " since last sync from " + lastSyncDate);
// create User to Delete List
deletedUserList = ldapMan.getIdentitysDeletedInLdap(ctx);
// delete old users
if (deletedUserList == null || deletedUserList.size() == 0) {
log.info("LDAP batch sync: no users to delete" + sinceSentence);
} else {
if (LDAPLoginModule.isDeleteRemovedLDAPUsersOnSync()) {
// check if more not more than the defined percentages of
// users managed in LDAP should be deleted
// if they are over the percentage, they will not be deleted
// by the sync job
Manager securityManager = ManagerFactory.getManager();
SecurityGroup ldapGroup = securityManager
.findSecurityGroupByName(LDAPConstants.SECURITY_GROUP_LDAP);
List<Identity> olatListIdentity = securityManager
.getIdentitiesOfSecurityGroup(ldapGroup);
if (olatListIdentity.size() == 0)
log
.info("No users managed by LDAP, can't delete users");
else {
int prozente = (int) (((float)deletedUserList.size() / (float) olatListIdentity.size())*100);
if (prozente >= LDAPLoginModule.getDeleteRemovedLDAPUsersPercentage()) {
log
.info("LDAP batch sync: more than "
+ LDAPLoginModule
.getDeleteRemovedLDAPUsersPercentage()
+ "% of LDAP managed users should be deleted. Please use Admin Deletion Job. Or increase deleteRemovedLDAPUsersPercentage. "
+ prozente
+ "% tried to delete.");
} else {
// delete users
ldapMan.deletIdentities(deletedUserList);
log.info("LDAP batch sync: "
+ deletedUserList.size() + " users deleted"
+ sinceSentence);
}
}
} else {
// Do nothing, only log users to logfile
StringBuffer users = new StringBuffer();
for (Iterator iterator = deletedUserList.iterator(); iterator
.hasNext();) {
Identity toBeDeleted = (Identity) iterator.next();
users.append(toBeDeleted.getName());
if (iterator.hasNext())
users.append(",");
}
log
.info("LDAP batch sync: "
+ deletedUserList.size()
+ " users detected as to be deleted"
+ sinceSentence
+ ". Automatic deleting is disabled in LDAPLoginModule, delete these users manually::["
+ users.toString() + "]");
}
}
// Get new and modified users from LDAP
ldapUserList = ldapMan.getUserAttributesModifiedSince(lastSyncDate, ctx);
Identity identity;
// Check for new and modified users
Map<String, String> changedAttrMap;
newLdapUserList = new LinkedList<Attributes>();
changedMapIdenityMap = new HashMap<Identity, Map<String, String>>();
for (int i = 0; i < ldapUserList.size(); i++) {
user = LDAPHelper.getAttributeValue(ldapUserList.get(i).get(
LDAPHelper.mapOlatPropertyToLdapAttribute(LDAPConstants.LDAP_USER_IDENTIFYER)));
identity = ldapMan.findIdentyByLdapAuthentication(user, errors);
if (identity != null) {
changedAttrMap = ldapMan.prepareUserPropertyForSync(ldapUserList.get(i), identity);
if (changedAttrMap != null) changedMapIdenityMap.put(identity, changedAttrMap);
} else {
if (errors.isEmpty()) {
String[] reqAttrs = LDAPHelper.checkReqAttr(ldapUserList.get(i));
if (reqAttrs == null) newLdapUserList.add(ldapUserList.get(i));
else log.warn("Error in LDAP batch sync: can't create user with username::" + user + " : missing required attributes::"
+ ArrayUtils.toString(reqAttrs));
} else {
log.warn(errors.get());
}
}
}
// sync existing users
if (changedMapIdenityMap == null || changedMapIdenityMap.size() == 0) {
log.info("LDAP batch sync: no users to sync" + sinceSentence);
} else {
Iterator<Identity> itrIdent = changedMapIdenityMap.keySet().iterator();
while (itrIdent.hasNext()) {
Identity ident = itrIdent.next();
ldapMan.syncUser(changedMapIdenityMap.get(ident), ident);
}
log.info("LDAP batch sync: " + changedMapIdenityMap.size() + " users synced" + sinceSentence);
}
// create new users
if (newLdapUserList.size() == 0) {
log.info("LDAP batch sync: no users to create" + sinceSentence);
} else {
for (int i = 0; i < newLdapUserList.size(); i++) {
ldapMan.createAndPersistUser(newLdapUserList.get(i));
// commit new users after 100 users
DBFactory.getInstance().intermediateCommit();
}
log.info("LDAP batch sync: " + newLdapUserList.size() + " users created" + sinceSentence);
}
// update sync time and set running flag
lastSyncDate = timeBeforeSync;
freeSyncLock();
ctx.close();
return true;
} catch (Exception e) {
// don't block following retries
freeSyncLock();
errors.insert("Unknown error");