| | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | public static final String LDAP_PASSWORD_KEY = "StoredInLDAP"; |
| | | |
| | | private IStoredSettings settings; |
| | | private long lastLdapUserSyncTs = 0L; |
| | | private long ldapSyncCachePeriod; |
| | | |
| | | public LdapUserService() { |
| | | super(); |
| | | } |
| | | |
| | | private void initializeLdapCaches() { |
| | | final String cacheDuration = settings.getString(Keys.realm.ldap.ldapCachePeriod, "2 MINUTES"); |
| | | final long duration; |
| | | final TimeUnit timeUnit; |
| | | try { |
| | | final String[] s = cacheDuration.split(" ", 2); |
| | | duration = Long.parseLong(s[0]); |
| | | timeUnit = TimeUnit.valueOf(s[1]); |
| | | ldapSyncCachePeriod = timeUnit.toMillis(duration); |
| | | } catch (RuntimeException ex) { |
| | | throw new IllegalArgumentException(Keys.realm.ldap.ldapCachePeriod + " must have format '<long> <TimeUnit>' where <TimeUnit> is one of 'MILLISECONDS', 'SECONDS', 'MINUTES', 'HOURS', 'DAYS'"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | String file = settings.getString(Keys.realm.ldap.backingUserService, "users.conf"); |
| | | File realmFile = GitBlit.getFileOrFolder(file); |
| | | |
| | | initializeLdapCaches(); |
| | | |
| | | serviceImpl = createUserService(realmFile); |
| | | logger.info("LDAP User Service backed by " + serviceImpl.toString()); |
| | | |
| | | synchronizeLdapUsers(); |
| | | } |
| | | |
| | | protected void synchronizeLdapUsers() { |
| | | protected synchronized void synchronizeLdapUsers() { |
| | | final boolean enabled = settings.getBoolean(Keys.realm.ldap.synchronizeUsers.enable, false); |
| | | if (!enabled) { |
| | | return; |
| | | } |
| | | if (enabled) { |
| | | if (lastLdapUserSyncTs + ldapSyncCachePeriod < System.currentTimeMillis()) { |
| | | final boolean deleteRemovedLdapUsers = settings.getBoolean(Keys.realm.ldap.synchronizeUsers.removeDeleted, true); |
| | | LDAPConnection ldapConnection = getLdapConnection(); |
| | | if (ldapConnection != null) { |
| | |
| | | updateTeamModels(userTeams.values()); |
| | | } |
| | | } |
| | | lastLdapUserSyncTs = System.currentTimeMillis(); |
| | | } finally { |
| | | ldapConnection.close(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private LDAPConnection getLdapConnection() { |
| | | try { |