Author: channa
Date: Mon Dec  3 07:30:54 2007
New Revision: 10451

Log:

Added mechanism to associate an infocard with the current user's profile. WIP.

Modified:
   
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/InfoCardHandler.java
   trunk/mashup/java/modules/www/org/infocardaccept.jsp
   trunk/mashup/java/modules/www/org/user.jsp

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/InfoCardHandler.java
==============================================================================
--- 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/InfoCardHandler.java
     (original)
+++ 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/InfoCardHandler.java
     Mon Dec  3 07:30:54 2007
@@ -18,6 +18,8 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.wso2.mashup.MashupConstants;
+import org.wso2.mashup.webapp.utils.RegistryUtils;
+import org.wso2.registry.Registry;
 import org.wso2.registry.RegistryConstants;
 import org.wso2.registry.RegistryException;
 import org.wso2.registry.jdbc.JDBCRegistry;
@@ -26,16 +28,19 @@
 import org.wso2.solutions.identity.relyingparty.TokenVerifierConstants;
 import org.wso2.usermanager.Realm;
 import org.wso2.usermanager.UserManagerException;
+import org.wso2.usermanager.UserStoreAdmin;
 import org.wso2.usermanager.UserStoreReader;
 
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
+import java.util.Map;
 
 /**
  * Encapsulates the infocard based sign-in process logic.
  */
 public class InfoCardHandler {
     private static final Log log = LogFactory.getLog(InfoCardHandler.class);
+    private static final String PPID_PROPERTY_KEY = "ppid";
 
     /**
      * Retrieves the infocard parameters and uses the ppid to validate the 
user.
@@ -68,7 +73,8 @@
                 Realm realm = (Realm) 
context.getAttribute(RegistryConstants.REGISTRY_REALM);
                 try {
                     UserStoreReader storeReader = realm.getUserStoreReader();
-                    String[] userNames = 
storeReader.getUserNamesWithPropertyValue("ppid", ppid);
+                    String[] userNames = 
storeReader.getUserNamesWithPropertyValue(
+                            PPID_PROPERTY_KEY, ppid);
 
                     // Only one person with PPID in the infocard, so get 
secure registry for user.
                     if (userNames.length == 1) {
@@ -93,4 +99,41 @@
         }
         return success;
     }
+
+    /**
+     * Associates the ppid of a given infocard with the current user.
+     *
+     * @param registry The current instance of the registry.
+     * @param request  Servlet request object, contains attributes provided by 
the info card.
+     */
+    public static boolean associateCardWithUser(Registry registry, 
HttpServletRequest request) {
+        boolean cardAdded = false;
+        try {
+            // Get the available user properties.
+            String currentUser = RegistryUtils.getCurrentUser(registry);
+            ServletContext context = request.getSession().getServletContext();
+            Realm realm = (Realm) 
context.getAttribute(RegistryConstants.REGISTRY_REALM);
+            UserStoreAdmin userStoreAdmin = realm.getUserStoreAdmin();
+            Map userProps = 
realm.getUserStoreAdmin().getUserProperties(currentUser);
+
+            // If infocard has been successfully used, add the card's ppid to 
user's properties.
+            String auth = (String) 
request.getAttribute(TokenVerifierConstants.SERVLET_ATTR_STATE);
+            if (TokenVerifierConstants.STATE_SUCCESS.equals(auth)) {
+                String ppid = (String) 
request.getAttribute(IdentityConstants.CLAIM_PPID);
+
+                // todo: Figure out how best to handle a user with multiple 
ppid's.
+                // Add if ppid has not been associated with a user and user 
has no associaed ppid.
+                if (!userProps.containsValue(ppid) && 
!userProps.containsKey(PPID_PROPERTY_KEY)) {
+                    userProps.put(PPID_PROPERTY_KEY, ppid);
+                    userStoreAdmin.setUserProperties(currentUser, userProps);
+                    cardAdded = true;
+                }
+            }
+        } catch (RegistryException e) {
+            log.error("Error retrieving current user", e);
+        } catch (UserManagerException e) {
+            log.error("Error adding info card to profile", e);
+        }
+        return cardAdded;
+    }
 }

Modified: trunk/mashup/java/modules/www/org/infocardaccept.jsp
==============================================================================
--- trunk/mashup/java/modules/www/org/infocardaccept.jsp        (original)
+++ trunk/mashup/java/modules/www/org/infocardaccept.jsp        Mon Dec  3 
07:30:54 2007
@@ -21,7 +21,7 @@
 <%
 
     Registry registry = RegistryUtils.getRegistry(request);
-
+    boolean isLoggedIn = RegistryUtils.isLoggedIn(registry);
 %>
 <html>
 <head>
@@ -29,43 +29,55 @@
     <!-- Required CSS -->
     <link href="css/styles.css" rel="stylesheet" type="text/css"/>
     <script language="javascript" src="js/common.js" 
type="text/javascript"></script>
-    <script language="javascript" type="text/javascript">userLoggedOn = 
<%=RegistryUtils.isLoggedIn(registry) %>;</script>
+    <script language="javascript" type="text/javascript">userLoggedOn = 
<%=isLoggedIn %>;</script>
 </head>
 <body>
 <%
     String thisPage = "infocardaccept.jsp";
     String bounceback = request.getParameter("bounceback");
+    String title;
+    String message;
     if (bounceback == null) {
         bounceback = "index.jsp";
     } else {
         bounceback = URLDecoder.decode(bounceback, "UTF-8");
     }
 
-    boolean success;
-    // If infocard authentication has been used, call bean method.
-    success = InfoCardHandler.signIn(request);
+    // Check if the user is validated already. If so, this is to associate the 
user with the card.
+    if (isLoggedIn) {
+        title = "Associate InfoCard with user profile";
+       if (InfoCardHandler.associateCardWithUser(registry, request)) {
+           message = "InfoCard successfully added to user profile.";
+       } else {
+            message = "Could not add InfoCard to user profile.";
+       }
+    } else {
+        // Unauthenticated users are trying to sign in.
+         title = "Sign-in to Mashups.WSO2.org";
+        if (InfoCardHandler.signIn(request)) {
+               response.sendRedirect(bounceback);
+            return;
+        } else {
+            message = "Infocard based login failed.";
+        }
+    }
 %>
-<% if (success) {
-    response.sendRedirect(bounceback);
-} else { %>
 <div id="page">
-
     <%@ include file="header.jsp" %>
 
     <div id="content">
         <table width="100%" height="400" border="0" cellspacing="0" 
cellpadding="5">
             <tr>
-                <th><label>Sign-in to Mashups.WSO2.org</label></th>
+                <th><label><%= title %></label></th>
             </tr>
             <tr>
                 <td align="center" height="175">
-                    <div class="login-error">Infocard based login failed.</div>
+                    <div class="login-error"><%= message %></div>
                 </td>
             </tr>
         </table>
     </div>
     <%@ include file="footer.jsp" %>
 </div>
-<% } %>
 </body>
 </html>
\ No newline at end of file

Modified: trunk/mashup/java/modules/www/org/user.jsp
==============================================================================
--- trunk/mashup/java/modules/www/org/user.jsp  (original)
+++ trunk/mashup/java/modules/www/org/user.jsp  Mon Dec  3 07:30:54 2007
@@ -56,7 +56,7 @@
             getAttribute(RegistryConstants.REGISTRY_REALM);
 
     String memberName = request.getParameter("name");
-    
+
     String mashupPath = request.getParameter("path");
     String thisPage = "user.jsp?name=" + memberName;
 
@@ -214,7 +214,16 @@
                         </div>
                     </td>
                 </tr>
-            </table>
+                <tr>
+                    <td class="profile_label">
+                        <nobr>info card:</nobr>
+                    </td>
+                    <td>
+                        <div class="profile_username">Register your <a 
href="infocard.html">infocard</a>
+                        </div>
+                    </td>
+                </tr>
+             </table>
         </div>
     </td>
     <td width="210" valign="top"><br/>

_______________________________________________
Mashup-dev mailing list
[email protected]
http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to