All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.security.mapping.providers.OptionsRoleMappingProvider Maven / Gradle / Ivy

There is a newer version: 5.1.0.Final
Show newest version
/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */ 
package org.jboss.security.mapping.providers;
  
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.jboss.security.identity.Role;
import org.jboss.security.identity.RoleGroup;
import org.jboss.security.identity.plugins.SimpleRole;
import org.jboss.security.mapping.MappingProvider;
import org.jboss.security.mapping.MappingResult;


/**
 *  Role Mapping Provider that picks up the roles from the
 *  options and then appends them to the passed Group
 *  @author Anil Saldhana
 *  @version $Revision$
 *  @since  Aug 24, 2006
 */
public class OptionsRoleMappingProvider implements MappingProvider 
{   
   //Standard Strings
   private static final String REPLACE_ROLES_STRING = "replaceRoles";
   private static final String ROLES_MAP = "rolesMap";
   
   private MappingResult result;

   private Map options = null;
   
   private Properties roleMapProperties = new Properties();
   
   /**
    * Specifies
    */
   private boolean REPLACE_ROLES = false;
   
   public void init(Map opt)
   {
     this.options = opt;
     if(options != null)
     {
        if(options.containsKey(REPLACE_ROLES_STRING))
        {
           REPLACE_ROLES = "true".equalsIgnoreCase((String)options.get(REPLACE_ROLES_STRING)); 
        }
        if(options.containsKey(ROLES_MAP))
        {
           roleMapProperties = (Properties)options.get(ROLES_MAP);
        } 
     } 
   }
   
   public void setMappingResult(MappingResult res)
   { 
      result = res;
   }
   
   public void performMapping(Map contextMap, RoleGroup mappedObject)
   {   
      ArrayList removeMembers = new ArrayList();
      ArrayList addMembers = new ArrayList(); 

      List rolesList = mappedObject.getRoles();
      if(rolesList != null)
      {
         for(Role r: rolesList)
         {
            String commaSeparatedRoles = roleMapProperties.getProperty(r.getRoleName());
            if(commaSeparatedRoles != null)
            {
               String[] tokens = MappingProviderUtil.getRolesFromCommaSeparatedString(commaSeparatedRoles);
               int len = tokens != null ? tokens.length : 0;
               for(int i = 0; i < len; i++)
               {
                  if(this.REPLACE_ROLES)
                     removeMembers.add(r); 
                  addMembers.add(new SimpleRole(tokens[i])); 
               }
            }  
         }
      } 
      
      //Go through  the remove list
      for(Role p:removeMembers)
      {
         mappedObject.removeRole(p);
      }
      //Go through the add list
      for(Role p:addMembers)
      {
         mappedObject.addRole(p);
      }
      
      result.setMappedObject(mappedObject);
   }   
   
   /**
    * @see MappingProvider#supports(Class)
    */
   public boolean supports(Class p)
   {
      if(RoleGroup.class.isAssignableFrom(p))
         return true;

      return false;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy