Author: ilya
Date: 2007-03-22 13:20:53 -0500 (Thu, 22 Mar 2007)
New Revision: 74825

Modified:
   
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/ChangeLog
   
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/IPumaServicesProvider.cs
   
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/PumaServicesProvider.cs
   
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/WPAuthenticationModule.cs
   
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/WPGroupsRoleProvider.cs
Log:
1. Ability to use AuthenticationModule also in servlet context, when the 
current user doesn't have security grants for accessing PUMA API.
2. Namespace - '.' bug fixed.



Modified: 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/ChangeLog
===================================================================
--- 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/ChangeLog
  2007-03-22 17:38:12 UTC (rev 74824)
+++ 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/ChangeLog
  2007-03-22 18:20:53 UTC (rev 74825)
@@ -1,8 +1,15 @@
-03-23-2007      Ilya Kharmatsky    <ilyak -at- mainsoft.com>
+03-22-2007      Ilya Kharmatsky    <ilyak -at- mainsoft.com>
        
+       * WPAuthenticationModule.cs, WPGroupsRoleProvider.cs,
+       IPumaServicesProvider.cs, PumaServicesProvider.cs:
+       added ability to perform auth checking also in context of 
+       servlets and provided fix for 'namespace' bug.
+
+03-13-2007      Ilya Kharmatsky    <ilyak -at- mainsoft.com>
+       
        * WPMembershipProvider.cs: bug fixes 
 
-03-23-2007      Ilya Kharmatsky    <ilyak -at- mainsoft.com>
+03-13-2007      Ilya Kharmatsky    <ilyak -at- mainsoft.com>
        * WPAuthenticationModule.cs: fixed in WPPrincipal class method -
        IsInRole - we are leaving this for RoleBasedPrincipal class to handle
        this method.

Modified: 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/IPumaServicesProvider.cs
===================================================================
--- 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/IPumaServicesProvider.cs
   2007-03-22 17:38:12 UTC (rev 74824)
+++ 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/IPumaServicesProvider.cs
   2007-03-22 18:20:53 UTC (rev 74825)
@@ -145,6 +145,14 @@
         /// <returns>null in case no configuration attribute defined with 
given name, otherwise
         /// string representation of value</returns>
         string GetConfigAttribute(string attribName);
+
+        /// <summary>
+        /// Returns the array of the names of groups defined in Portal Server, 
+        /// where the given user is a member.
+        /// </summary>
+        /// <param name="user">com.ibm.portal.um.User instance - to be checked 
for membership</param>
+        /// <returns>string array of groups name, defined in portal server for 
given user</returns>
+        string[] GetRolesForUser(com.ibm.portal.um.User user);
         
     }
 }

Modified: 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/PumaServicesProvider.cs
===================================================================
--- 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/PumaServicesProvider.cs
    2007-03-22 17:38:12 UTC (rev 74824)
+++ 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/PumaServicesProvider.cs
    2007-03-22 18:20:53 UTC (rev 74825)
@@ -178,6 +178,15 @@
             }
         }
 
+        public string[] GetRolesForUser(com.ibm.portal.um.User user)
+        {
+            if (user == null)
+                return new string[0];
+            
+            java.util.List groups = PumaLocator.findGroupsByPrincipal(user, 
true);
+            return WPGroupsRoleProvider.GroupsToStringArray(this, groups);
+        }
+
         public void AddAttribute(com.ibm.portal.um.Principal p, string 
attributeName, string attributeValue)
         {
             HashMap map = new HashMap();

Modified: 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/WPAuthenticationModule.cs
===================================================================
--- 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/WPAuthenticationModule.cs
  2007-03-22 17:38:12 UTC (rev 74824)
+++ 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/WPAuthenticationModule.cs
  2007-03-22 18:20:53 UTC (rev 74825)
@@ -56,26 +56,62 @@
 
         void OnAuthenticateRequest(object sender, EventArgs args)
         {
+            
             HttpApplication app = (HttpApplication)sender;
             PortletRequest req = vmw.portlet.PortletUtils.getPortletRequest();
             if (req != null)
+            {
                 app.Context.User = new WPPrincipal(req);
+            }
+            else
+            {
+                javax.servlet.http.HttpServletRequest servletRequest =
+                    vmw.j2ee.J2EEUtils.getHttpServletRequest();
+                if (servletRequest == null)
+                    return;
+                javax.servlet.http.HttpSession session = 
servletRequest.getSession(true);
+                string uName = (string) session.getAttribute("VMW_CURR_USER_");
+                if (uName != null)
+                {
+                    IIdentity iident = new GenericIdentity(uName);
+                    app.Context.User = new GenericPrincipal(iident, new 
string[0]);
+                }
+            }
         }
     }
 
     internal class WPPrincipal : IPrincipal
     {
+        internal static readonly string VMW_CURR_USER = "VMW_CURR_USER_";
+
         private IIdentity _identity;
         private string _username;
+        private PortletRequest _request;
 
         public WPPrincipal(PortletRequest req)
         {
+            _request = req;
             string authType = req.getAuthType();
             if (authType == null)
                 authType = "";
+
             IPumaServicesProvider provider = 
PumaServicesProviderFactory.CreateProvider();
-            _username = provider.CurrentUserName;
+            try
+            {
+                _username = provider.CurrentUserName;
+            }
+            catch 
+            {
+                _username = req.getRemoteUser();
+            }
             _identity = new GenericIdentity(_username, authType);
+            string[] roles = provider.GetRolesForUser(provider.CurrentUser);
+            req.getPortletSession(true).setAttribute(VMW_CURR_USER,
+                    _username,
+                    PortletSession__Finals.APPLICATION_SCOPE);
+            req.getPortletSession().setAttribute("VMW_CURR_USER_ROLES_", 
+                roles, 
+                PortletSession__Finals.APPLICATION_SCOPE);
         }
 
         public bool IsInRole(string role)
@@ -86,20 +122,7 @@
             if (_username == null)
                 return false;
 
-            return false;
-
-//            try
-//            {
-//                return Roles.IsUserInRole(_username, role);
-//            }
-//            catch (Exception e)
-//            {
-//#if DEBUG
-//                Console.WriteLine(e);
-//#endif
-//                return false;
-//            }
-
+            return _request.isUserInRole(role);
         }
 
         public IIdentity Identity { get { return _identity; } }

Modified: 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/WPGroupsRoleProvider.cs
===================================================================
--- 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/WPGroupsRoleProvider.cs
    2007-03-22 17:38:12 UTC (rev 74824)
+++ 
trunk/mcs/class/Mainsoft.Web/Mainsoft.Web.J2EE.WAS/Mainsoft.Web.Security/WPGroupsRoleProvider.cs
    2007-03-22 18:20:53 UTC (rev 74825)
@@ -37,6 +37,12 @@
 using System.Text;
 using System.Configuration.Provider;
 
+
+using javax.portlet;
+using javax.servlet.http;
+using vmw.portlet;
+using vmw.j2ee;
+
 namespace Mainsoft.Web.Security
 {
     public class WPGroupsRoleProvider : RoleProvider 
@@ -46,6 +52,8 @@
         private static readonly string PROVIDER_NAME = "WPGroupsRoleProvider";
         private static readonly string DESCRIPTION = "WebSphere Groups Role 
provider";
 
+        private static readonly string VMW_STORED_IN_SESSION_ROLES = 
"VMW_CURR_USER_ROLES_";
+
         private string applicationName;
 
         public override void Initialize(string name, NameValueCollection 
config)
@@ -168,9 +176,24 @@
 
             com.ibm.portal.um.User user = (com.ibm.portal.um.User) 
users.get(0);
 
-            java.util.List groups = 
pumaServices.PumaLocator.findGroupsByPrincipal(user, true);
-            return GroupsToStringArray(pumaServices, groups);
+            try
+            {
+                java.util.List groups = 
pumaServices.PumaLocator.findGroupsByPrincipal(user, true);
+                return GroupsToStringArray(pumaServices, groups);
+            }
+            catch 
+            {
+                if (username != HttpContext.Current.User.Identity.Name)
+                    return new string[0];
 
+                HttpSession session = HttpSession;
+                string[] result = null;
+
+                if (session != null)
+                    result = 
(string[])session.getAttribute(VMW_STORED_IN_SESSION_ROLES);
+
+                return (result == null) ? new string[0] : result;
+            }
         }
 
         internal protected static string[] 
GetRolesForUser(com.ibm.portal.um.User user)
@@ -289,7 +312,7 @@
 
         private string NormalizeRoleName(IPumaServicesProvider provider, 
string roleName)
         {
-            string groupNamespace = 
provider.GetConfigAttribute(GROUP_NAMESPACE_ATTRIBUTE);
+            string groupNamespace = GetGroupsNamespaceName(provider);
             if (groupNamespace == null)
                 return roleName;
 
@@ -298,7 +321,7 @@
 
         private string DeNormalizeRoleName(IPumaServicesProvider provider, 
string groupName)
         {
-            string groupNamespace = 
provider.GetConfigAttribute(GROUP_NAMESPACE_ATTRIBUTE);
+            string groupNamespace = GetGroupsNamespaceName(provider);
             if (groupNamespace == null)
                 return groupName;
             if (groupName.StartsWith(groupNamespace))
@@ -307,10 +330,10 @@
                 return groupName;
         }
 
-        private static string[] GroupsToStringArray(IPumaServicesProvider 
pumaServices, java.util.List groups)
+        internal static string[] GroupsToStringArray(IPumaServicesProvider 
pumaServices, java.util.List groups)
         {
             java.util.ArrayList res = new java.util.ArrayList(groups.size());
-            string groupNamespace = 
pumaServices.GetConfigAttribute(GROUP_NAMESPACE_ATTRIBUTE);
+            string groupNamespace = GetGroupsNamespaceName(pumaServices);
 
             java.util.ArrayList groupAttribs = new java.util.ArrayList();
             groupAttribs.add("cn");
@@ -330,6 +353,37 @@
             }
             return (string[])res.toArray(new string[res.size()]);
         }
+
+        private PortletSession PortletSession
+        {
+            get
+            {
+                PortletRequest pr = PortletUtils.getPortletRequest();
+                if (pr == null)
+                    return null;
+                return pr.getPortletSession(true);
+            }
+        }
+
+        private HttpSession HttpSession
+        {
+            get
+            {
+                HttpServletRequest req = J2EEUtils.getHttpServletRequest();
+                if (req == null)
+                    return null;
+                return req.getSession(true);
+            }
+        }
+
+        private static string GetGroupsNamespaceName(IPumaServicesProvider 
provider)
+        {
+            string res = 
provider.GetConfigAttribute(GROUP_NAMESPACE_ATTRIBUTE);
+            if (res != null)
+                return res + '.';
+
+            return null;
+        }
         #endregion
     }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to