public UserModel authenticate(String username, char[] password) {\r
String simpleUsername = getSimpleUsername(username);\r
\r
- LDAPConnection ldapConnection = getLdapConnection(); \r
+ LDAPConnection ldapConnection = getLdapConnection();\r
if (ldapConnection != null) {\r
- // Find the logging in user's DN\r
- String accountBase = settings.getString(Keys.realm.ldap.accountBase, "");\r
- String accountPattern = settings.getString(Keys.realm.ldap.accountPattern, "(&(objectClass=person)(sAMAccountName=${username}))");\r
- accountPattern = StringUtils.replace(accountPattern, "${username}", escapeLDAPSearchFilter(simpleUsername));\r
+ try {\r
+ // Find the logging in user's DN\r
+ String accountBase = settings.getString(Keys.realm.ldap.accountBase, "");\r
+ String accountPattern = settings.getString(Keys.realm.ldap.accountPattern, "(&(objectClass=person)(sAMAccountName=${username}))");\r
+ accountPattern = StringUtils.replace(accountPattern, "${username}", escapeLDAPSearchFilter(simpleUsername));\r
\r
- SearchResult result = doSearch(ldapConnection, accountBase, accountPattern);\r
- if (result != null && result.getEntryCount() == 1) {\r
- SearchResultEntry loggingInUser = result.getSearchEntries().get(0);\r
- String loggingInUserDN = loggingInUser.getDN();\r
- \r
- if (isAuthenticated(ldapConnection, loggingInUserDN, new String(password))) {\r
- logger.debug("LDAP authenticated: " + username);\r
- \r
- UserModel user = getUserModel(simpleUsername);\r
- if (user == null) // create user object for new authenticated user\r
- user = new UserModel(simpleUsername);\r
+ SearchResult result = doSearch(ldapConnection, accountBase, accountPattern);\r
+ if (result != null && result.getEntryCount() == 1) {\r
+ SearchResultEntry loggingInUser = result.getSearchEntries().get(0);\r
+ String loggingInUserDN = loggingInUser.getDN();\r
\r
- // create a user cookie\r
- if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {\r
- user.cookie = StringUtils.getSHA1(user.username + new String(password));\r
- }\r
- \r
- if (!supportsTeamMembershipChanges())\r
- getTeamsFromLdap(ldapConnection, simpleUsername, loggingInUser, user);\r
- \r
- // Get User Attributes\r
- setUserAttributes(user, loggingInUser);\r
+ if (isAuthenticated(ldapConnection, loggingInUserDN, new String(password))) {\r
+ logger.debug("LDAP authenticated: " + username);\r
+\r
+ UserModel user = getUserModel(simpleUsername);\r
+ if (user == null) // create user object for new authenticated user\r
+ user = new UserModel(simpleUsername);\r
\r
- // Push the ldap looked up values to backing file\r
- super.updateUserModel(user);\r
- if (!supportsTeamMembershipChanges()) {\r
- for (TeamModel userTeam : user.teams)\r
- updateTeamModel(userTeam);\r
+ // create a user cookie\r
+ if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {\r
+ user.cookie = StringUtils.getSHA1(user.username + new String(password));\r
+ }\r
+\r
+ if (!supportsTeamMembershipChanges())\r
+ getTeamsFromLdap(ldapConnection, simpleUsername, loggingInUser, user);\r
+\r
+ // Get User Attributes\r
+ setUserAttributes(user, loggingInUser);\r
+\r
+ // Push the ldap looked up values to backing file\r
+ super.updateUserModel(user);\r
+ if (!supportsTeamMembershipChanges()) {\r
+ for (TeamModel userTeam : user.teams)\r
+ updateTeamModel(userTeam);\r
+ }\r
+\r
+ return user;\r
}\r
- \r
- return user;\r
}\r
+ } finally {\r
+ ldapConnection.close();\r
}\r
}\r
- \r
return null; \r
}\r
\r