modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/LoginView.java | 129 ++++++++-- modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/UserSessionManager.java | 9 modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/gwt/LdapGWTService.java | 5 modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/server/gwt/LdapGWTServiceImpl.java | 14 + modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/group/LdapGroupManagerBean.java | 23 + modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/group/LdapGroupManagerLocal.java | 2 6 files changed, 151 insertions(+), 31 deletions(-)
New commits: commit 34f9d85692a50c7b5c578591562ee6e623f3e16d Author: Simeon Pinder spinder@redhat.com Date: Fri Oct 15 12:05:27 2010 -0400
i)ldap user registration prepopulation ii)numerous ldap fixes a)F5 refresh username b)last name required c)stale session checks.
diff --git a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/LoginView.java b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/LoginView.java index da1ee4c..66c37c6 100644 --- a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/LoginView.java +++ b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/LoginView.java @@ -32,6 +32,7 @@ import com.google.gwt.http.client.Request; import com.google.gwt.http.client.RequestBuilder; import com.google.gwt.http.client.RequestCallback; import com.google.gwt.http.client.Response; +import com.google.gwt.user.client.Cookies; import com.google.gwt.user.client.rpc.AsyncCallback; import com.smartgwt.client.types.Alignment; import com.smartgwt.client.types.FormErrorOrientation; @@ -185,6 +186,10 @@ public class LoginView extends Canvas { public void showRegistrationDialog(String user, final String sessionId, final String password, final AsyncCallback<Subject> callback) { if (!loginShowing) { + if ((user != null) && (!user.trim().isEmpty())) { + Cookies.setCookie(USERNAME, user); + Cookies.setCookie(PASSWORD, password); + } loginShowing = true;
forms = new ArrayList<DynamicForm>(); @@ -194,6 +199,7 @@ public class LoginView extends Canvas { form.setAutoFocus(true); form.setShowErrorText(true); form.setErrorOrientation(FormErrorOrientation.BOTTOM); + int fieldWidth = 120;
VLayout column = new VLayout(); HeaderItem header = new HeaderItem(); @@ -206,28 +212,29 @@ public class LoginView extends Canvas { { first.setRequired(true); first.setWrapTitle(false); - first.setWidth(100); + first.setWidth(fieldWidth); } last = new TextItem(LAST, "Last Name"); { last.setWrapTitle(false); - last.setWidth(100); + last.setWidth(fieldWidth); + last.setRequired(true); } final TextItem username = new TextItem(USERNAME, "Username"); { - username.setRequired(true); - username.setValue(user); + username.setValue(Cookies.getCookie(USERNAME)); + username.setDisabled(true); - username.setWidth(100); + username.setWidth(fieldWidth); column.addMember(wrapInDynamicForm(6, first, last, username)); } email = new TextItem(EMAIL, "Email"); email.setRequired(true); - email.setWidth(100); + email.setWidth(fieldWidth); phone = new TextItem(PHONE, "Phone"); - phone.setWidth(100); + phone.setWidth(fieldWidth); department = new TextItem(DEPARTMENT, "Department"); - department.setWidth(100); + department.setWidth(fieldWidth); SpacerItem space = new SpacerItem(); space.setColSpan(1); column.addMember(wrapInDynamicForm(6, email, phone, department)); @@ -242,6 +249,11 @@ public class LoginView extends Canvas { IButton okButton = new IButton("OK"); okButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { + //check for session timeout + if (isSessionStale()) { + resetLogin(); + } + //validation if (validateForms(forms)) { Log.trace("Successfully validated all data for user registration."); @@ -253,31 +265,61 @@ public class LoginView extends Canvas { form.setValue(PHONE, String.valueOf(phone.getValue())); form.setValue(DEPARTMENT, String.valueOf(department.getValue())); form.setValue(SESSIONID, sessionId); - form.setValue(PASSWORD, password); + form.setValue(PASSWORD, Cookies.getCookie(PASSWORD)); registerLdapUser(form, callback); } }
- /** Iterates through the dynamic forms populated then calls validate(). - * - * @param forms - * @return - */ - private boolean validateForms(ArrayList<DynamicForm> forms) { - boolean allValid = true; - for (DynamicForm form : forms) { - if (!form.validate()) { - allValid = false; + }); + row.addMember(okButton); + //send request to LDAP server to grab user details for this user. Already sure ldap user exists + GWTServiceLookup.getLdapService().getLdapDetailsFor(user, new AsyncCallback<Map<String, String>>() { + public void onSuccess(final Map<String, String> ldapUserDetails) { + //now prepopulate UI fields if they exist + for (String key : ldapUserDetails.keySet()) { + String value; + if (key.equalsIgnoreCase("givenName")) {//aka first name + value = ldapUserDetails.get(key); + first.setValue(value); + } else if (key.equalsIgnoreCase("sn")) {//aka Surname + value = ldapUserDetails.get(key); + if ((value != null) && (!value.isEmpty())) { + last.setValue(value); + } + } else if (key.equalsIgnoreCase("telephoneNumber")) { + value = ldapUserDetails.get(key); + if ((value != null) && (!value.isEmpty())) { + phone.setValue(value); + } + } else if (key.equalsIgnoreCase("mail")) { + value = ldapUserDetails.get(key); + if ((value != null) && (!value.isEmpty())) { + email.setValue(value); + } } } - return allValid; + } + + public void onFailure(Throwable caught) { + Log.debug("Optional LDAP detail retrieval did not succeed. Registration prepopulation will occur."); } }); - row.addMember(okButton);
IButton resetButton = new IButton("Reset"); resetButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { + if (isSessionStale()) { + resetLogin(); + } + + //clear out all validation messages. + { + String empty = " "; + first.setValue(empty); + last.setValue(empty); + email.setValue("test@test.com"); + validateForms(forms); + } first.clearValue(); last.clearValue(); email.clearValue(); @@ -290,10 +332,7 @@ public class LoginView extends Canvas { IButton logout = new IButton("Logout"); logout.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { - UserSessionManager.invalidateSession(); - window.destroy(); - loginShowing = false; - new LoginView().showLoginDialog(); + resetLogin(); } }); row.addMember(logout); @@ -322,6 +361,46 @@ public class LoginView extends Canvas { } }
+ /** Iterates through the dynamic forms populated then calls validate(). + * + * @param forms + * @return + */ + private boolean validateForms(ArrayList<DynamicForm> forms) { + boolean allValid = true; + for (DynamicForm form : forms) { + if (!form.validate()) { + allValid = false; + } + } + return allValid; + } + + /** Go through steps of invalidating this login and piping them back to CoreGUI Login. + */ + private void resetLogin() { + UserSessionManager.invalidateSession(); + window.destroy(); + loginShowing = false; + new LoginView().showLoginDialog(); + } + + /** Check to see whether session has timed out while user has been waiting on this form. + * @return + */ + private boolean isSessionStale() { + boolean staleSession = false; + String lastAccess = UserSessionManager.getLastAccessTime(); + if ((lastAccess != null) && (!lastAccess.trim().isEmpty())) { + long expiryTime = Long.valueOf(lastAccess) + UserSessionManager.SESSION_TIMEOUT; + long expiryMillis = expiryTime - System.currentTimeMillis(); + if (expiryMillis < 0) { + staleSession = true; + } + } + return staleSession; + } + /**Uses the information from the populated form to create the Subject for the new LDAP user. * * @param populatedForm - validated data diff --git a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/UserSessionManager.java b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/UserSessionManager.java index 2d283a3..71c62b8 100644 --- a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/UserSessionManager.java +++ b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/UserSessionManager.java @@ -54,10 +54,12 @@ import org.rhq.enterprise.gui.coregui.client.util.preferences.UserPreferences; * @author Joseph Marques */ public class UserSessionManager { - private static int SESSION_TIMEOUT = 29 * 60 * 1000; // 29 mins, just shorter than the 30-min web session timeout + public static int SESSION_TIMEOUT = 29 * 60 * 1000; // 29 mins, just shorter than the 30-min web session timeout private static int LOGOUT_DELAY = 5 * 1000; // wait 5 seconds for in-flight requests to complete before logout
public static final String SESSION_NAME = "RHQ_Sesssion"; + public static final String SESSION_LAST_ACCESS = SESSION_NAME + ".LAST_ACCESS"; + private static Subject sessionSubject; private static UserPreferences userPreferences;
@@ -99,6 +101,7 @@ public class UserSessionManager { final int subjectId = Integer.parseInt(parts[0]); final String sessionId = parts[1]; // not null final long lastAccess = Long.parseLong(parts[2]); + Cookies.setCookie(SESSION_LAST_ACCESS, String.valueOf(lastAccess)); Log.info("sessionAccess-subjectId: " + subjectId); Log.info("sessionAccess-sessionId: " + sessionId); Log.info("sessionAccess-lastAccess: " + lastAccess); @@ -363,4 +366,8 @@ public class UserSessionManager { public static UserPreferences getUserPreferences() { return userPreferences; } + + public static String getLastAccessTime() { + return Cookies.getCookie(SESSION_LAST_ACCESS); + } } diff --git a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/gwt/LdapGWTService.java b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/gwt/LdapGWTService.java index 0209e37..7915b80 100644 --- a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/gwt/LdapGWTService.java +++ b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/gwt/LdapGWTService.java @@ -41,6 +41,11 @@ public interface LdapGWTService extends RemoteService { Set<Map<String, String>> findAvailableGroups();
/** + * @return Map with LDAP details for user passed. + */ + Map<String, String> getLdapDetailsFor(String user); + + /** * Updates the role with the ldap groups selected. */ void addLdapGroupsToRole(int roleId, List<String> groupIds); diff --git a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/server/gwt/LdapGWTServiceImpl.java b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/server/gwt/LdapGWTServiceImpl.java index d03c53c..fe68250 100644 --- a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/server/gwt/LdapGWTServiceImpl.java +++ b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/server/gwt/LdapGWTServiceImpl.java @@ -294,4 +294,18 @@ public class LdapGWTServiceImpl extends AbstractGWTServiceImpl implements LdapGW } return ldapEnabled; } + + /**Returns all LDAP details for a given user, using the configured ldap details of server. + * + */ + @Override + public Map<String, String> getLdapDetailsFor(String user) { + Map<String, String> ldapDetails = new HashMap<String, String>(); + try { + ldapDetails = ldapManager.findLdapUserDetails(user); + } catch (Exception e) { + throw new RuntimeException(ThrowableUtil.getAllMessages(e)); + } + return ldapDetails; + } } \ No newline at end of file diff --git a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/group/LdapGroupManagerBean.java b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/group/LdapGroupManagerBean.java index 8c75852..2cff69b 100644 --- a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/group/LdapGroupManagerBean.java +++ b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/group/LdapGroupManagerBean.java @@ -211,6 +211,14 @@ public class LdapGroupManagerBean implements LdapGroupManagerLocal { }
private String getUserDN(Properties options, String userName) { + Map<String, String> details = findLdapUserDetails(userName); + String userDN = details.get("dn"); + return userDN; + } + + public Map<String, String> findLdapUserDetails(String userName) { + Properties options = systemManager.getSystemConfiguration(); + HashMap<String, String> userDetails = new HashMap<String, String>(); // Load our LDAP specific properties Properties env = getProperties(options);
@@ -265,12 +273,17 @@ public class LdapGroupManagerBean implements LdapGroupManagerLocal { SearchResult si = (SearchResult) answer.next();
// Construct the UserDN - String userDN = si.getName() + "," + baseDNs[x]; - return userDN; + NamingEnumeration<String> keys = si.getAttributes().getIDs(); + while (keys.hasMore()) { + String key = keys.next(); + Attribute value = si.getAttributes().get(key); + if (value != null) { + userDetails.put(key, value.get() + ""); + } + } + return userDetails; } - - // If we try all the BaseDN's and have not found a match, return false - return ""; + return userDetails; } catch (NamingException e) { throw new RuntimeException(e); } diff --git a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/group/LdapGroupManagerLocal.java b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/group/LdapGroupManagerLocal.java index 6709b32..17a008a 100644 --- a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/group/LdapGroupManagerLocal.java +++ b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/group/LdapGroupManagerLocal.java @@ -50,4 +50,6 @@ public interface LdapGroupManagerLocal { Set<Map<String, String>> findAvailableGroups();
Set<String> findAvailableGroupsFor(String userName); + + Map<String, String> findLdapUserDetails(String userName); } \ No newline at end of file