You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by dl...@apache.org on 2004/09/18 21:34:36 UTC

cvs commit: jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/spi/impl DefaultGroupSecurityHandler.java DefaultCredentialHandler.java DefaultUserSecurityHandler.java DefaultRoleSecurityHandler.java CommonQueries.java

dlestrat    2004/09/18 12:34:36

  Added:       components/security/src/java/org/apache/jetspeed/security/spi/impl
                        DefaultGroupSecurityHandler.java
                        DefaultCredentialHandler.java
                        DefaultUserSecurityHandler.java
                        DefaultRoleSecurityHandler.java CommonQueries.java
  Log:
  http://nagoya.apache.org/jira/browse/JS2-114#action_53185
  
  Revision  Changes    Path
  1.1                  jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/spi/impl/DefaultGroupSecurityHandler.java
  
  Index: DefaultGroupSecurityHandler.java
  ===================================================================
  /* Copyright 2004 Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.security.spi.impl;
  
  import java.util.Collection;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
  import java.util.prefs.Preferences;
  
  import org.apache.jetspeed.security.HierarchyResolver;
  import org.apache.jetspeed.security.impl.GeneralizationHierarchyResolver;
  import org.apache.jetspeed.security.impl.GroupPrincipalImpl;
  import org.apache.jetspeed.security.om.InternalGroupPrincipal;
  import org.apache.jetspeed.security.om.InternalUserPrincipal;
  import org.apache.jetspeed.security.spi.GroupSecurityHandler;
  
  /**
   * @see org.apache.jetspeed.security.spi.GroupSecurityHandler
   * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
   */
  public class DefaultGroupSecurityHandler implements GroupSecurityHandler
  {
      /** Common queries. */
      private CommonQueries commonQueries = null;
      
      /** The role hierarchy resolver. */
      HierarchyResolver groupHierarchyResolver = new GeneralizationHierarchyResolver();
      
      /**
       * <p>Constructor providing access to the common queries.</p>
       */
      public DefaultGroupSecurityHandler(CommonQueries commonQueries)
      {
          this.commonQueries = commonQueries;
      }
      
      /**
       * <p>Constructor providing access to the common queries and specifying the group
       * hierarchy resolution strategy.</p>
       */
      public DefaultGroupSecurityHandler(CommonQueries commonQueries, HierarchyResolver groupHierarchyResolver)
      {
          this.commonQueries = commonQueries;
          this.groupHierarchyResolver = groupHierarchyResolver;
      }
      
      /**
       * @see org.apache.jetspeed.security.spi.GroupSecurityHandler#setGroupHierarchyResolver(org.apache.jetspeed.security.HierarchyResolver)
       */
      public void setGroupHierarchyResolver(HierarchyResolver groupHierarchyResolver)
      {
          this.groupHierarchyResolver = groupHierarchyResolver;
      }
      
      /**
       * @see org.apache.jetspeed.security.spi.GroupSecurityHandler#getGroupPrincipals(java.lang.String)
       */
      public Set getGroupPrincipals(String username)
      {
          Set groupPrincipals = new HashSet();
          InternalUserPrincipal internalUser = commonQueries.getInternalUserPrincipal(username);
          if (null != internalUser)
          {
              Collection internalGroups = internalUser.getGroupPrincipals();
              if (null != internalGroups)
              {
                  Iterator internalGroupsIter = internalGroups.iterator();
                  while (internalGroupsIter.hasNext())
                  {
                      InternalGroupPrincipal internalGroup = (InternalGroupPrincipal) internalGroupsIter.next();
                      Preferences preferences = Preferences.userRoot().node(internalGroup.getFullPath());
                      String [] fullPaths = groupHierarchyResolver.resolve(preferences);
                      for (int i = 0; i < fullPaths.length; i++)
                      {
                          groupPrincipals.add(new GroupPrincipalImpl(GroupPrincipalImpl.getPrincipalNameFromFullPath(fullPaths[i])));    
                      }
                  }
              }
          }
          return groupPrincipals;
      }
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/spi/impl/DefaultCredentialHandler.java
  
  Index: DefaultCredentialHandler.java
  ===================================================================
  /* Copyright 2004 Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.jetspeed.security.spi.impl;
  
  import java.sql.Timestamp;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
  
  import org.apache.jetspeed.security.PasswordCredential;
  import org.apache.jetspeed.security.SecurityException;
  import org.apache.jetspeed.security.om.InternalCredential;
  import org.apache.jetspeed.security.om.InternalUserPrincipal;
  import org.apache.jetspeed.security.om.impl.InternalCredentialImpl;
  import org.apache.jetspeed.security.spi.CredentialHandler;
  
  /**
   * @see org.apache.jetspeed.security.spi.CredentialHandler
   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
   */
  public class DefaultCredentialHandler implements CredentialHandler
  {
      /** Private credentials. */
      private static final int PRIVATE = 0;
  
      /** Public credentials. */
      private static final int PUBLIC = 1;
  
      /** Common queries. */
      private CommonQueries commonQueries = null;
  
      /**
       * <p>
       * Constructor providing access to the common queries.
       * </p>
       */
      public DefaultCredentialHandler(CommonQueries commonQueries)
      {
          this.commonQueries = commonQueries;
      }
  
      /**
       * @see org.apache.jetspeed.security.spi.CredentialHandler#getPrivateCredentials(java.lang.String)
       */
      public Set getPrivateCredentials(String username)
      {
          return getCredentials(username, PRIVATE);
      }
  
      /**
       * @see org.apache.jetspeed.security.spi.CredentialHandler#setPrivatePasswordCredential(org.apache.jetspeed.security.PasswordCredential,
       *      org.apache.jetspeed.security.PasswordCredential)
       */
      public void setPrivatePasswordCredential(PasswordCredential oldPwdCredential, PasswordCredential newPwdCredential)
              throws SecurityException
      {
          setCredential(oldPwdCredential, newPwdCredential, PRIVATE);
  
      }
  
      /**
       * @see org.apache.jetspeed.security.spi.CredentialHandler#getPublicCredentials(java.lang.String)
       */
      public Set getPublicCredentials(String username)
      {
          return getCredentials(username, PUBLIC);
      }
  
      /**
       * @see org.apache.jetspeed.security.spi.CredentialHandler#setPublicPasswordCredential(org.apache.jetspeed.security.PasswordCredential,
       *      org.apache.jetspeed.security.PasswordCredential)
       */
      public void setPublicPasswordCredential(PasswordCredential oldPwdCredential, PasswordCredential newPwdCredential)
              throws SecurityException
      {
          setCredential(oldPwdCredential, newPwdCredential, PUBLIC);
      }
  
      /**
       * <p>
       * Gets the credentials given a type.
       * </p>
       * 
       * @param username The username.
       * @param type The type.
       * @return The set.
       */
      private Set getCredentials(String username, int type)
      {
          Set internalCredentials = new HashSet();
          Set credentials = new HashSet();
          InternalUserPrincipal internalUser = commonQueries.getInternalUserPrincipal(username);
          if (null != internalUser)
          {
              internalCredentials.addAll(internalUser.getCredentials());
              Iterator iter = internalCredentials.iterator();
              while (iter.hasNext())
              {
                  InternalCredential credential = (InternalCredential) iter.next();
                  if (credential.getType() == type)
                  {
                      // PasswordCredential support.
                      if ((null != credential.getClassname())
                              && (credential.getClassname().equals((PasswordCredential.class).getName())))
                      {
                          PasswordCredential pwdCred = new PasswordCredential(username, credential.getValue()
                                  .toCharArray());
                          credentials.add(pwdCred);
                      }
                  }
              }
          }
  
          return credentials;
      }
  
      /**
       * <p>
       * Sets the password credential given a type.
       * </p>
       * 
       * @param oldPwdCredential The old {@link PasswordCredential}.
       * @param newPwdCredential The new {@link PasswordCredential}.
       * @param type The type.
       * @throws SecurityException Throws a {@link SecurityException}.
       */
      private void setCredential(PasswordCredential oldPwdCredential, PasswordCredential newPwdCredential, int type)
              throws SecurityException
      {
          InternalUserPrincipal internalUser = commonQueries.getInternalUserPrincipal(newPwdCredential.getUserName());
          if (null == internalUser)
          {
              throw new SecurityException(SecurityException.USER_DOES_NOT_EXIST + " " + newPwdCredential.getUserName());
          }
          Collection credentials = internalUser.getCredentials();
          if (null == credentials)
          {
              credentials = new ArrayList();
          }
          if (null != oldPwdCredential)
          {
              InternalCredential oldInternalCredential = new InternalCredentialImpl(internalUser.getPrincipalId(),
                      new String(oldPwdCredential.getPassword()), type, oldPwdCredential.getClass().getName());
              if (credentials.contains(oldInternalCredential))
              {
                  credentials.remove(oldInternalCredential);
              }
          }
          InternalCredential newInternalCredential = new InternalCredentialImpl(internalUser.getPrincipalId(),
                  new String(newPwdCredential.getPassword()), type, newPwdCredential.getClass().getName());
          credentials.add(newInternalCredential);
          internalUser.setModifiedDate(new Timestamp(System.currentTimeMillis()));
          internalUser.setCredentials(credentials);
          // Set the user with the new credentials.
          commonQueries.setInternalUserPrincipal(internalUser);
      }
  }
  
  
  1.1                  jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/spi/impl/DefaultUserSecurityHandler.java
  
  Index: DefaultUserSecurityHandler.java
  ===================================================================
  /* Copyright 2004 Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.security.spi.impl;
  
  import java.security.Principal;
  import java.util.Iterator;
  import java.util.LinkedList;
  import java.util.List;
  
  import org.apache.jetspeed.security.SecurityException;
  import org.apache.jetspeed.security.UserPrincipal;
  import org.apache.jetspeed.security.impl.UserPrincipalImpl;
  import org.apache.jetspeed.security.om.InternalUserPrincipal;
  import org.apache.jetspeed.security.om.impl.InternalUserPrincipalImpl;
  import org.apache.jetspeed.security.spi.UserSecurityHandler;
  
  /**
   * @see org.apache.jetspeed.security.spi.UserSecurityHandler
   * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
   */
  public class DefaultUserSecurityHandler implements UserSecurityHandler
  {
      /** Common queries. */
      private CommonQueries commonQueries = null;
      
      /**
       * <p>Constructor providing access to the common queries.</p>
       */
      public DefaultUserSecurityHandler(CommonQueries commonQueries)
      {
          this.commonQueries = commonQueries;
      }
      
      /**
       * @see org.apache.jetspeed.security.spi.UserSecurityHandler#getUserPrincipal(java.lang.String)
       */
      public Principal getUserPrincipal(String username)
      {
          UserPrincipal userPrincipal = null;
          InternalUserPrincipal internalUser = commonQueries.getInternalUserPrincipal(username);
          if (null != internalUser)
          {
              userPrincipal = new UserPrincipalImpl(UserPrincipalImpl.getPrincipalNameFromFullPath(internalUser.getFullPath()));
          }
          return userPrincipal;
      }
      
      /**
       * @see org.apache.jetspeed.security.spi.UserSecurityHandler#getUserPrincipals(java.lang.String)
       */
      public Iterator getUserPrincipals(String filter)
      {
          List userPrincipals = new LinkedList();
          Iterator result = commonQueries.getInternalUserPrincipals(filter);
          while (result.hasNext())
          {
              InternalUserPrincipal internalUser = (InternalUserPrincipal)result.next();
              String path = internalUser.getFullPath();
              if (path == null || !path.startsWith(UserPrincipal.PREFS_USER_ROOT)) // TODO: FIXME: the extend shouldn't return roles!
              {
                  continue;
              }
              userPrincipals.add(new UserPrincipalImpl(UserPrincipalImpl.getPrincipalNameFromFullPath(internalUser.getFullPath())));
          }
          return userPrincipals.iterator();
      }
  
      /**
       * @see org.apache.jetspeed.security.spi.UserSecurityHandler#setUserPrincipal(org.apache.jetspeed.security.UserPrincipal)
       */
      public void setUserPrincipal(UserPrincipal userPrincipal) throws SecurityException
      {
          String fullPath = userPrincipal.getFullPath();
          InternalUserPrincipal internalUser = new InternalUserPrincipalImpl(fullPath);
          commonQueries.setInternalUserPrincipal(internalUser);        
      }
      
      /**
       * @see org.apache.jetspeed.security.spi.UserSecurityHandler#removeUserPrincipal(org.apache.jetspeed.security.UserPrincipal)
       */
      public void removeUserPrincipal(UserPrincipal userPrincipal) throws SecurityException
      {
          InternalUserPrincipal internalUser = commonQueries.getInternalUserPrincipal(userPrincipal.getName());
          if (null != internalUser)
          {
              commonQueries.removeInternalUserPrincipal(internalUser);
          }        
      }
  
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/spi/impl/DefaultRoleSecurityHandler.java
  
  Index: DefaultRoleSecurityHandler.java
  ===================================================================
  /* Copyright 2004 Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.security.spi.impl;
  
  import java.util.Collection;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
  import java.util.prefs.Preferences;
  
  import org.apache.jetspeed.security.HierarchyResolver;
  import org.apache.jetspeed.security.impl.GeneralizationHierarchyResolver;
  import org.apache.jetspeed.security.impl.RolePrincipalImpl;
  import org.apache.jetspeed.security.om.InternalRolePrincipal;
  import org.apache.jetspeed.security.om.InternalUserPrincipal;
  import org.apache.jetspeed.security.spi.RoleSecurityHandler;
  
  /**
   * @see org.apache.jetspeed.security.spi.RoleSecurityHandler
   * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
   */
  public class DefaultRoleSecurityHandler implements RoleSecurityHandler
  {
      /** Common queries. */
      private CommonQueries commonQueries = null;
      
      /** The role hierarchy resolver. */
      HierarchyResolver roleHierarchyResolver = new GeneralizationHierarchyResolver();
      
      /**
       * <p>Constructor providing access to the common queries.</p>
       */
      public DefaultRoleSecurityHandler(CommonQueries commonQueries)
      {
          this.commonQueries = commonQueries;
      }
      
      /**
       * <p>Constructor providing access to the common queries and specifying the role
       * hierarchy resolution strategy.</p>
       */
      public DefaultRoleSecurityHandler(CommonQueries commonQueries, HierarchyResolver roleHierarchyResolver)
      {
          this.commonQueries = commonQueries;
          this.roleHierarchyResolver = roleHierarchyResolver;
      }
     
      /**
       * @see org.apache.jetspeed.security.spi.RoleSecurityHandler#setRoleHierarchyResolver(org.apache.jetspeed.security.HierarchyResolver)
       */
      public void setRoleHierarchyResolver(HierarchyResolver roleHierarchyResolver)
      {
          this.roleHierarchyResolver = roleHierarchyResolver;
      }
      
      /**
       * @see org.apache.jetspeed.security.spi.RoleSecurityHandler#getRolePrincipals(java.lang.String)
       */
      public Set getRolePrincipals(String username)
      {
          Set rolePrincipals = new HashSet();
          InternalUserPrincipal internalUser = commonQueries.getInternalUserPrincipal(username);
          if (null != internalUser)
          {
              Collection internalRoles = internalUser.getRolePrincipals();
              if (null != internalRoles)
              {
                  Iterator internalRolesIter = internalRoles.iterator();
                  while (internalRolesIter.hasNext())
                  {
                      InternalRolePrincipal internalRole = (InternalRolePrincipal) internalRolesIter.next();
                      Preferences preferences = Preferences.userRoot().node(internalRole.getFullPath());
                      String [] fullPaths = roleHierarchyResolver.resolve(preferences);
                      for (int i = 0; i < fullPaths.length; i++)
                      {
                          rolePrincipals.add(new RolePrincipalImpl(RolePrincipalImpl.getPrincipalNameFromFullPath(fullPaths[i])));    
                      }
                  }
              }
          }
          return rolePrincipals;
      }
  }
  
  
  
  1.1                  jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/spi/impl/CommonQueries.java
  
  Index: CommonQueries.java
  ===================================================================
  /* Copyright 2004 Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.security.spi.impl;
  
  import java.util.Iterator;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.jetspeed.components.persistence.store.Filter;
  import org.apache.jetspeed.components.persistence.store.PersistenceStore;
  import org.apache.jetspeed.security.SecurityException;
  import org.apache.jetspeed.security.UserPrincipal;
  import org.apache.jetspeed.security.impl.UserPrincipalImpl;
  import org.apache.jetspeed.security.om.InternalUserPrincipal;
  import org.apache.jetspeed.security.om.impl.InternalUserPrincipalImpl;
  
  /**
   * <p>
   * Provides a utility class for common SPI queries.
   * </p>
   * 
   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
   */
  public class CommonQueries
  {
      private static final Log log = LogFactory.getLog(CommonQueries.class);
  
      /** The persistence store. * */
      private PersistenceStore persistenceStore;
  
      /**
       * @param persistenceStore The persistence store.
       */
      public CommonQueries(PersistenceStore persistenceStore)
      {
          if (persistenceStore == null)
          {
              throw new IllegalArgumentException("persistenceStore cannot be null for BaseSecurityImpl");
          }
  
          this.persistenceStore = persistenceStore;
      }
  
      /**
       * <p>
       * Returns the {@link InternalUserPrincipal}from the user name.
       * </p>
       * 
       * @param username The user name.
       * @return The {@link InternalUserPrincipal}.
       */
      public InternalUserPrincipal getInternalUserPrincipal(String username)
      {
          UserPrincipal userPrincipal = new UserPrincipalImpl(username);
          String fullPath = userPrincipal.getFullPath();
          // Get user.
          Filter filter = persistenceStore.newFilter();
          filter.addEqualTo("fullPath", fullPath);
          Object query = persistenceStore.newQuery(InternalUserPrincipalImpl.class, filter);
          InternalUserPrincipal omUser = (InternalUserPrincipal) persistenceStore.getObjectByQuery(query);
          return omUser;
      }
      
      /**
       * <p>
       * Returns a collection of {@link Principal} given the filter.
       * </p>
       * 
       * @param filter The filter.
       * @return Collection of {@link InternalUserPrincipal}.
       */
      public Iterator getInternalUserPrincipals(String filter)
      {
          Iterator result = persistenceStore.getExtent(InternalUserPrincipalImpl.class).iterator();
          return result;
      }
  
      /**
       * <p>
       * Sets the given {@link InternalUserPrincipal}.
       * </p>
       * 
       * @param internalUser The {@link InternalUserPrincipal}.
       * @throws SecurityException Throws a {@link SecurityException}.
       */
      public void setInternalUserPrincipal(InternalUserPrincipal internalUser) throws SecurityException
      {
          try
          {
              persistenceStore.lockForWrite(internalUser);
              persistenceStore.getTransaction().checkpoint();
          }
          catch (Exception e)
          {
              String msg = "Unable to lock user for update.";
              log.error(msg, e);
              persistenceStore.getTransaction().rollback();
              throw new SecurityException(msg, e);
          }
      }
      
      /**
       * <p>
       * Remove the given {@link InternalUserPrincipal}.
       * </p>
       * 
       * @param internalUser The {@link InternalUserPrincipal}.
       * @throws SecurityException Throws a {@link SecurityException}.
       */
      public void removeInternalUserPrincipal(InternalUserPrincipal internalUser) throws SecurityException
      {
          try
          {
              // Remove user.
              persistenceStore.deletePersistent(internalUser);
              persistenceStore.getTransaction().checkpoint();
              if (log.isDebugEnabled())
              {
                  log.debug("Deleted user: " + internalUser.getFullPath());
              }
  
          }
          catch (Exception e)
          {
              String msg = "Unable to lock User for update.";
              log.error(msg, e);
              persistenceStore.getTransaction().rollback();
              throw new SecurityException(msg, e);
          }
      }
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org