[jboss-cvs] JBossBlog SVN: r239 - in trunk: src/portal/org/jboss/blog/session/security/external and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Mar 5 04:08:12 EST 2008


Author: adamw
Date: 2008-03-05 04:08:12 -0500 (Wed, 05 Mar 2008)
New Revision: 239

Added:
   trunk/src/portal/org/jboss/blog/session/security/external/PortalRole.java
   trunk/src/portal/org/jboss/blog/session/security/external/PortalSecurityService.java
   trunk/src/portal/org/jboss/blog/session/security/external/PortalSecurityServiceImpl.java
   trunk/src/portal/org/jboss/blog/session/security/external/PortalUser.java
Modified:
   trunk/resources/components.properties
   trunk/src/portal/org/jboss/blog/session/security/external/PortalExternalSecurityService.java
Log:


Modified: trunk/resources/components.properties
===================================================================
--- trunk/resources/components.properties	2008-03-04 16:47:15 UTC (rev 238)
+++ trunk/resources/components.properties	2008-03-05 09:08:12 UTC (rev 239)
@@ -1,2 +1,2 @@
 jndiPattern \#{ejbName}/local
-debug false
+debug true

Modified: trunk/src/portal/org/jboss/blog/session/security/external/PortalExternalSecurityService.java
===================================================================
--- trunk/src/portal/org/jboss/blog/session/security/external/PortalExternalSecurityService.java	2008-03-04 16:47:15 UTC (rev 238)
+++ trunk/src/portal/org/jboss/blog/session/security/external/PortalExternalSecurityService.java	2008-03-05 09:08:12 UTC (rev 239)
@@ -8,15 +8,10 @@
 import org.jboss.blog.model.security.RestrictedSecurityGroup;
 import org.jboss.blog.model.security.RestrictedSecurityUser;
 import org.jboss.blog.session.security.InvalidLoginException;
-import org.jboss.portal.identity.*;
-import org.jboss.portal.identity.Role;
 
 import javax.persistence.EntityManager;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
 import java.util.List;
 import java.util.ArrayList;
-import java.util.Set;
 
 /**
  * @author <a href="mailto:adam at warski.org">Adam Warski</a>
@@ -28,6 +23,9 @@
     @In
     private EntityManager entityManager;
 
+    @In
+    private PortalSecurityService portalSecurityService;
+
     protected EntityManager getEntityManager() {
         return entityManager;
     }
@@ -35,38 +33,11 @@
     @Logger
     private Log log;
 
-    private UserModule getUserModule() throws PortalSecurityException {
-        try {
-            return (UserModule) new InitialContext().lookup("java:portal/UserModule");
-        } catch (NamingException e) {
-            log.error(e);
-            throw new PortalSecurityException(e);
-        }
-    }
-
-    private RoleModule getRoleModule() throws PortalSecurityException {
-        try {
-            return (RoleModule) new InitialContext().lookup("java:portal/RoleModule");
-        } catch (NamingException e) {
-            log.error(e);
-            throw new PortalSecurityException(e);
-        }
-    }
-
-    private MembershipModule getMembershipModule() throws PortalSecurityException {
-        try {
-            return (MembershipModule) new InitialContext().lookup("java:portal/MembershipModule");
-        } catch (NamingException e) {
-            log.error(e);
-            throw new PortalSecurityException(e);
-        }
-    }
-
-    private List<SecurityGroup> convertRoles(Set<Role> roles, boolean getUnrestricted) {
+    private List<SecurityGroup> convertRoles(List<PortalRole> roles, boolean getUnrestricted) {
         List<SecurityGroup> securityGroups = new ArrayList<SecurityGroup>();
 
-        for (Role role : roles) {
-            SecurityGroup securityGroup = new SecurityGroup(role, role.getId().toString());
+        for (PortalRole role : roles) {
+            SecurityGroup securityGroup = new SecurityGroup(role, role.getId());
             if (getUnrestricted) {
                 securityGroups.add(getUnrestrictedSecurityGroup(securityGroup));
             } else {
@@ -77,11 +48,11 @@
         return securityGroups;
     }
 
-    private List<SecurityUser> convertUsers(Set<User> users, boolean getUnrestricted) {
+    private List<SecurityUser> convertUsers(List<PortalUser> users, boolean getUnrestricted) {
         List<SecurityUser> securityUsers = new ArrayList<SecurityUser>();
 
-        for (User user : users) {
-            SecurityUser securityUser = new SecurityUser(user, user.getId().toString());
+        for (PortalUser user : users) {
+            SecurityUser securityUser = new SecurityUser(user, user.getId());
             if (getUnrestricted) {
                 securityUsers.add(getUnrestrictedSecurityUser(securityUser));
             } else {
@@ -92,56 +63,37 @@
         return securityUsers;
     }
 
-    private User getRealUser(RestrictedSecurityUser securityUser) throws PortalSecurityException, NoSuchUserException {
-        if (securityUser.getRealUser() == null) {
-            try {
-                User user = getUserModule().findUserById(securityUser.getExternalId());
-                securityUser.setRealUser(user);
+    private PortalRole getRealRole(RestrictedSecurityGroup securityGroup) throws PortalSecurityException {
+        if (securityGroup.getRealGroup() == null) {
+            PortalRole role = portalSecurityService.getRoleById(securityGroup.getExternalId());
+            securityGroup.setRealGroup(role);
 
-                return user;
-            } catch (NoSuchUserException e) {
-                throw e;
-            } catch (IdentityException e) {
-                throw new PortalSecurityException(e);
-            }
+            return role;
         } else {
-            return (User) securityUser.getRealUser();
+            return (PortalRole) securityGroup.getRealGroup();
         }
     }
 
-    private Role getRealRole(RestrictedSecurityGroup securityGroup) throws PortalSecurityException {
-        if (securityGroup.getRealGroup() == null) {
-            try {
-                Role role = getRoleModule().findRoleById(securityGroup.getExternalId());
-                securityGroup.setRealGroup(role);
+    private PortalUser getRealUser(RestrictedSecurityUser securityUser) throws PortalSecurityException {
+        if (securityUser.getRealUser() == null) {
+            PortalUser user = portalSecurityService.getUserById(securityUser.getExternalId());
+            securityUser.setRealUser(user);
 
-                return role;
-            } catch (IdentityException e) {
-                throw new PortalSecurityException(e);
-            }
+            return user;
         } else {
-            return (Role) securityGroup.getRealGroup();
+            return (PortalUser) securityUser.getRealUser();
         }
     }
 
     public SecurityUser authenticate(String username, String password) throws InvalidLoginException {
         try {
-            User user = getUserModule().findUserByUserName(username);
+            PortalUser portalUser = portalSecurityService.authenticate(username, password);
 
-            if (!user.validatePassword(password)) {
-                throw new InvalidLoginException();
-            }
-
             SecurityUser securityUser = new SecurityUser();
-            securityUser.setExternalId(user.getId().toString());
-            securityUser.setRealUser(user);
+            securityUser.setExternalId(portalUser.getId());
+            securityUser.setRealUser(portalUser);
 
             return getUnrestrictedSecurityUser(securityUser);
-        } catch (NoSuchUserException e) {
-            throw new InvalidLoginException();
-        } catch (IdentityException e) {
-            log.error(e);
-            throw new InvalidLoginException();
         } catch (PortalSecurityException e) {
             log.error(e);
             throw new InvalidLoginException();
@@ -151,10 +103,7 @@
     public List<? extends RestrictedSecurityGroup> getAllGroups() {
         try {
             //noinspection unchecked
-            return convertRoles((Set<Role>) getRoleModule().findRoles(), false);
-        } catch (IdentityException e) {
-            log.error(e);
-            return new ArrayList<RestrictedSecurityGroup>();
+            return convertRoles(portalSecurityService.getAllRoles(), false);
         } catch (PortalSecurityException e) {
             log.error(e);
             return new ArrayList<RestrictedSecurityGroup>();
@@ -163,14 +112,9 @@
 
     public List<? extends RestrictedSecurityUser> getAllUsers() {
         try {
-            // TODO - users count
+
             //noinspection unchecked
-            return convertUsers((Set<User>) getUserModule().findUsers(0, 100000), false);
-        } catch (NoSuchUserException e) {
-            return new ArrayList<RestrictedSecurityUser>();
-        } catch (IdentityException e) {
-            log.error(e);
-            return new ArrayList<RestrictedSecurityUser>();
+            return convertUsers(portalSecurityService.getAllUsers(), false);
         } catch (PortalSecurityException e) {
             log.error(e);
             return new ArrayList<RestrictedSecurityUser>();
@@ -180,12 +124,7 @@
     public List<SecurityGroup> getGroupsOfUser(SecurityUser securityUser) {
         try {
             //noinspection unchecked
-            return convertRoles((Set<Role>) getMembershipModule().getRoles(getRealUser(securityUser)), true);
-        } catch (NoSuchUserException e) {
-            return new ArrayList<SecurityGroup>();
-        } catch (IdentityException e) {
-            log.error(e);
-            return new ArrayList<SecurityGroup>();
+            return convertRoles(portalSecurityService.getRolesOfUser(securityUser.getExternalId()), true);
         } catch (PortalSecurityException e) {
             log.error(e);
             return new ArrayList<SecurityGroup>();
@@ -194,9 +133,7 @@
 
     public String getDisplayName(RestrictedSecurityUser securityUser) {
         try {
-            return getRealUser(securityUser).getUserName();
-        } catch (NoSuchUserException e) {
-            return "<deleted (id = " + securityUser.getExternalId() + ">";
+            return getRealUser(securityUser).getDisplayName();
         } catch (PortalSecurityException e) {
             return "<? " + e.getMessage() + ">";
         }
@@ -212,15 +149,9 @@
 
     public RestrictedSecurityGroup getAdminGroup() {
         try {
-            Role adminRole = getRoleModule().findRoleByName("Admin");
+            PortalRole portalRole = portalSecurityService.getAdminRole();
 
-            if (adminRole == null) {
-                return null;
-            }
-
-            return new SecurityGroup(adminRole, adminRole.getId().toString());
-        } catch (IdentityException e) {
-            return null;
+            return new SecurityGroup(portalRole, portalRole.getId());
         } catch (PortalSecurityException e) {
             return null;
         }

Added: trunk/src/portal/org/jboss/blog/session/security/external/PortalRole.java
===================================================================
--- trunk/src/portal/org/jboss/blog/session/security/external/PortalRole.java	                        (rev 0)
+++ trunk/src/portal/org/jboss/blog/session/security/external/PortalRole.java	2008-03-05 09:08:12 UTC (rev 239)
@@ -0,0 +1,30 @@
+package org.jboss.blog.session.security.external;
+
+/**
+ * @author <a href="mailto:adam at warski.org">Adam Warski</a>
+ */
+public class PortalRole {
+    private String id;
+    private String displayName;
+
+    public PortalRole(String id, String displayName) {
+        this.id = id;
+        this.displayName = displayName;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getDisplayName() {
+        return displayName;
+    }
+
+    public void setDisplayName(String displayName) {
+        this.displayName = displayName;
+    }
+}

Added: trunk/src/portal/org/jboss/blog/session/security/external/PortalSecurityService.java
===================================================================
--- trunk/src/portal/org/jboss/blog/session/security/external/PortalSecurityService.java	                        (rev 0)
+++ trunk/src/portal/org/jboss/blog/session/security/external/PortalSecurityService.java	2008-03-05 09:08:12 UTC (rev 239)
@@ -0,0 +1,28 @@
+package org.jboss.blog.session.security.external;
+
+import org.jboss.blog.session.security.InvalidLoginException;
+
+import javax.ejb.Local;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:adam at warski.org">Adam Warski</a>
+ */
+ at Local
+public interface PortalSecurityService {
+    public PortalUser authenticate(String username, String password) throws PortalSecurityException, InvalidLoginException;
+
+    public List<PortalRole> getAllRoles() throws PortalSecurityException;
+
+    public List<PortalUser> getAllUsers() throws PortalSecurityException;
+
+    public List<PortalRole> getRolesOfUser(String id) throws PortalSecurityException;
+
+    public PortalRole getAdminRole() throws PortalSecurityException;
+
+    public PortalRole getRoleById(String id) throws PortalSecurityException;
+
+    public PortalUser getUserById(String id) throws PortalSecurityException; 
+
+    public void remove();
+}

Added: trunk/src/portal/org/jboss/blog/session/security/external/PortalSecurityServiceImpl.java
===================================================================
--- trunk/src/portal/org/jboss/blog/session/security/external/PortalSecurityServiceImpl.java	                        (rev 0)
+++ trunk/src/portal/org/jboss/blog/session/security/external/PortalSecurityServiceImpl.java	2008-03-05 09:08:12 UTC (rev 239)
@@ -0,0 +1,160 @@
+package org.jboss.blog.session.security.external;
+
+import org.jboss.seam.annotations.*;
+import org.jboss.seam.log.Log;
+import org.jboss.portal.identity.*;
+import org.jboss.portal.identity.Role;
+import org.jboss.blog.session.security.InvalidLoginException;
+
+import javax.ejb.Stateless;
+import javax.ejb.Remove;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * @author <a href="mailto:adam at warski.org">Adam Warski</a>
+ */
+ at Stateless
+ at Name("portalSecurityService")
+ at AutoCreate
+public class PortalSecurityServiceImpl implements PortalSecurityService {
+    @Logger
+    private Log log;
+
+    private UserModule getUserModule() throws PortalSecurityException {
+        try {
+            return (UserModule) new InitialContext().lookup("java:portal/UserModule");
+        } catch (NamingException e) {
+            log.error(e);
+            throw new PortalSecurityException(e);
+        }
+    }
+
+    private RoleModule getRoleModule() throws PortalSecurityException {
+        try {
+            return (RoleModule) new InitialContext().lookup("java:portal/RoleModule");
+        } catch (NamingException e) {
+            log.error(e);
+            throw new PortalSecurityException(e);
+        }
+    }
+
+    private MembershipModule getMembershipModule() throws PortalSecurityException {
+        try {
+            return (MembershipModule) new InitialContext().lookup("java:portal/MembershipModule");
+        } catch (NamingException e) {
+            log.error(e);
+            throw new PortalSecurityException(e);
+        }
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+    public PortalUser authenticate(String username, String password)
+            throws InvalidLoginException, PortalSecurityException {
+        User user;
+        try {
+            user = getUserModule().findUserByUserName(username);
+        } catch (NoSuchUserException e) {
+            throw new InvalidLoginException();
+        } catch (IdentityException e) {
+            throw new PortalSecurityException(e);
+        }
+
+        if (!user.validatePassword(password)) {
+            throw new InvalidLoginException();
+        }
+
+        return new PortalUser(user.getId().toString(), user.getUserName());
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+    public List<PortalRole> getAllRoles() throws PortalSecurityException {
+        try {
+            List<PortalRole> roles = new ArrayList<PortalRole>();
+
+            for (Object roleObj : getRoleModule().findRoles()) {
+                Role role = (Role) roleObj;
+                roles.add(new PortalRole(role.getId().toString(), role.getDisplayName()));
+            }
+
+            return roles;
+        } catch (IdentityException e) {
+            throw new PortalSecurityException(e);
+        }
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+    public List<PortalUser> getAllUsers() throws PortalSecurityException {
+        try {
+            List<PortalUser> users = new ArrayList<PortalUser>();
+
+            // TODO - users count
+            for (Object userObj : getUserModule().findUsers(0, 100000)) {
+                User user = (User) userObj;
+                users.add(new PortalUser(user.getId().toString(), user.getUserName()));
+            }
+
+            return users;
+        } catch (IdentityException e) {
+            throw new PortalSecurityException(e);
+        }
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+    public List<PortalRole> getRolesOfUser(String id) throws PortalSecurityException {
+        try {
+            List<PortalRole> roles = new ArrayList<PortalRole>();
+
+            for (Object roleObj : getMembershipModule().getRoles(getUserModule().findUserById(id)))  {
+                Role role = (Role) roleObj;
+                roles.add(new PortalRole(role.getId().toString(), role.getDisplayName()));
+            }
+
+            return roles;
+        } catch (IdentityException e) {
+            throw new PortalSecurityException(e);
+        }
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+    public PortalRole getAdminRole() throws PortalSecurityException {
+        try {
+            Role adminRole = getRoleModule().findRoleByName("Admin");
+
+            if (adminRole == null) {
+                return null;
+            }
+
+            return new PortalRole(adminRole.getId().toString(), adminRole.getDisplayName());
+        } catch (IdentityException e) {
+            throw new PortalSecurityException(e);
+        }
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+    public PortalRole getRoleById(String id) throws PortalSecurityException {
+        try {
+            Role role = getRoleModule().findRoleById(id);
+            return new PortalRole(role.getId().toString(), role.getDisplayName());
+        } catch (IdentityException e) {
+            throw new PortalSecurityException(e);
+        }
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+    public PortalUser getUserById(String id) throws PortalSecurityException {
+        try {
+            User user = getUserModule().findUserById(id);
+            return new PortalUser(user.getId().toString(), user.getUserName());
+        } catch (IdentityException e) {
+            throw new PortalSecurityException(e);
+        }
+    }
+
+    @Remove
+    public void remove() {  }
+}

Added: trunk/src/portal/org/jboss/blog/session/security/external/PortalUser.java
===================================================================
--- trunk/src/portal/org/jboss/blog/session/security/external/PortalUser.java	                        (rev 0)
+++ trunk/src/portal/org/jboss/blog/session/security/external/PortalUser.java	2008-03-05 09:08:12 UTC (rev 239)
@@ -0,0 +1,30 @@
+package org.jboss.blog.session.security.external;
+
+/**
+ * @author <a href="mailto:adam at warski.org">Adam Warski</a>
+ */
+public class PortalUser {
+    private String id;
+    private String displayName;
+
+    public PortalUser(String id, String displayName) {
+        this.id = id;
+        this.displayName = displayName;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getDisplayName() {
+        return displayName;
+    }
+
+    public void setDisplayName(String displayName) {
+        this.displayName = displayName;
+    }
+}




More information about the jboss-cvs-commits mailing list