Package javax.naming.ldap

Examples of javax.naming.ldap.InitialLdapContext


      }     
   }
  
   public boolean createUser(String username, String password, String firstname, String lastname)
   {
      InitialLdapContext ctx = null;     
      try
      {
         ctx = initialiseContext();
        
         Attributes userAttribs = new BasicAttributes();
        
         BasicAttribute userClass = new BasicAttribute(getObjectClassAttribute());
         for (String objectClass : getUserObjectClasses())
         {
            userClass.add(objectClass);
         }
        
         userAttribs.put(userClass);
         userAttribs.put(new BasicAttribute(getUserNameAttribute(), username));
         userAttribs.put(new BasicAttribute(getUserPasswordAttribute(), password));
        
         if (getFirstNameAttribute() != null && firstname != null)
         {
            userAttribs.put(new BasicAttribute(getFirstNameAttribute(), firstname));
         }
        
         if (getLastNameAttribute() != null && lastname != null)
         {
            userAttribs.put(new BasicAttribute(getLastNameAttribute(), lastname));
         }
        
         if (getFullNameAttribute() != null && firstname != null && lastname != null)
         {
            userAttribs.put(new BasicAttribute(getFullNameAttribute(), firstname + " " + lastname));
         }
        
         if (getEnabledAttribute() != null)
         {
            userAttribs.put(new BasicAttribute(getEnabledAttribute(), LDAP_BOOLEAN_TRUE));
         }
        
         String userDN = String.format("%s=%s,%s", getUserNameAttribute(), username, getUserContextDN() );         
         ctx.createSubcontext(userDN, userAttribs);
        
         return true;
      }
      catch (NamingException ex)
      {
         throw new IdentityManagementException("Failed to create user", ex);
      }     
      finally
      {
         if (ctx != null)
         {
            try
            {
               ctx.close();
            }
            catch (NamingException ex) {}
         }
      }     
   }
View Full Code Here


      return createUser(username, password, null, null);
   }

   public boolean deleteRole(String role)
   {
      InitialLdapContext ctx = null;     
      try
      {
         ctx = initialiseContext();
                
         String roleDN = String.format("%s=%s,%s", getRoleNameAttribute(), role, getRoleContextDN() );         
         ctx.destroySubcontext(roleDN);        
         return true;
      }
      catch (NamingException ex)
      {
         throw new IdentityManagementException("Failed to delete role", ex);
      }
      finally
      {
         if (ctx != null)
         {
            try
            {
               ctx.close();
            }
            catch (NamingException ex) {}
         }
      }     
   }
View Full Code Here

      }     
   }
  
   public boolean roleExists(String role)
   {     
      InitialLdapContext ctx = null;     
      try
      {
         ctx = initialiseContext();             
        
         int searchScope = SearchControls.SUBTREE_SCOPE;
         int searchTimeLimit = 10000;
        
         String[] roleAttr = { getRoleNameAttribute() };
                          
         SearchControls controls = new SearchControls();
         controls.setSearchScope(searchScope);
         controls.setReturningAttributes(roleAttr);
         controls.setTimeLimit(searchTimeLimit);
        
         String roleFilter = "(&(" + getObjectClassAttribute() + "={0})(" + getRoleNameAttribute() + "={1}))";
         Object[] filterArgs = { getRoleObjectClasses(), role};
        
         NamingEnumeration answer = ctx.search(getRoleContextDN(), roleFilter, filterArgs, controls);
         while (answer.hasMore())
         {
            SearchResult sr = (SearchResult) answer.next();
            Attributes attrs = sr.getAttributes();
            Attribute user = attrs.get( getRoleNameAttribute() );
           
            for (int i = 0; i < user.size(); i++)
            {
               Object value = user.get(i);
               if (role.equals(value)) return true;
            }           
         }
         answer.close();

         return false;
      }
      catch (NamingException ex)
      {
         throw new IdentityManagementException("Error getting roles", ex);
      }
      finally
      {
         if (ctx != null)
         {
            try
            {
               ctx.close();
            }
            catch (NamingException ex) {}
         }
      }
   }  
View Full Code Here

      }
   }  

   public boolean deleteUser(String name)
   {
      InitialLdapContext ctx = null;     
      try
      {
         ctx = initialiseContext();
                
         String userDN = getUserDN(name);         
         ctx.destroySubcontext(userDN);        
         return true;
      }
      catch (NamingException ex)
      {
         throw new IdentityManagementException("Failed to delete user", ex);
      }
      finally
      {
         if (ctx != null)
         {
            try
            {
               ctx.close();
            }
            catch (NamingException ex) {}
         }
      }     
   }
View Full Code Here

  
   public boolean isUserEnabled(String name)
   {
      if (getEnabledAttribute() == null) return true;     

      InitialLdapContext ctx = null;     
      try
      {
         ctx = initialiseContext();
        
         String userDN = getUserDN(name);         
         Attributes attribs = ctx.getAttributes(userDN, new String[] { getEnabledAttribute() });
         Attribute enabledAttrib = attribs.get( getEnabledAttribute() );
         if (enabledAttrib != null)
         {
            for (int r = 0; r < enabledAttrib.size(); r++)
            {
               Object value = enabledAttrib.get(r);
               if (LDAP_BOOLEAN_TRUE.equals(value)) return true;
            }
         }        

         return false;
      }
      catch (NamingException ex)
      {
         throw new IdentityManagementException("Failed to delete user", ex);
      }
      finally
      {
         if (ctx != null)
         {
            try
            {
               ctx.close();
            }
            catch (NamingException ex) {}
         }
      }     
   }  
View Full Code Here

   public boolean disableUser(String name)
   {
      if (getEnabledAttribute() == null) return false;
     
      InitialLdapContext ctx = null;
      try
      {
         ctx = initialiseContext();
        
         String userDN = getUserDN(name);         
         BasicAttribute enabledAttrib = new BasicAttribute(getEnabledAttribute(), LDAP_BOOLEAN_FALSE);
         ModificationItem mod = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, enabledAttrib);
        
         ctx.modifyAttributes(userDN, new ModificationItem[] { mod });
         return true;
      }
      catch (NamingException ex)
      {
         throw new IdentityManagementException("Failed to disable user", ex);
      }     
      finally
      {
         if (ctx != null)
         {
            try
            {
               ctx.close();
            }
            catch (NamingException ex) {}
         }
      }
   }
View Full Code Here

   public boolean enableUser(String name)
   {
      if (getEnabledAttribute() == null) return false;
     
      InitialLdapContext ctx = null;
      try
      {
         ctx = initialiseContext();
        
         String userDN = getUserDN(name);         
         BasicAttribute enabledAttrib = new BasicAttribute(getEnabledAttribute(), LDAP_BOOLEAN_TRUE);
         ModificationItem mod = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, enabledAttrib);
        
         ctx.modifyAttributes(userDN, new ModificationItem[] { mod });
         return true;
      }
      catch (NamingException ex)
      {
         throw new IdentityManagementException("Failed to disable user", ex);
      }     
      finally
      {
         if (ctx != null)
         {
            try
            {
               ctx.close();
            }
            catch (NamingException ex) {}
         }
      }
   }
View Full Code Here

   public List<String> getGrantedRoles(String name)
   {
      Set<String> userRoles = new HashSet<String>();
     
      InitialLdapContext ctx = null;     
      try
      {
         ctx = initialiseContext();
                 
         String userFilter = "(" + getUserNameAttribute() + "={0})";        
         String[] roleAttr = { getUserRoleAttribute() };
                 
         SearchControls controls = new SearchControls();
         controls.setSearchScope(searchScope);
         controls.setReturningAttributes(roleAttr);
         controls.setTimeLimit(getSearchTimeLimit());
         Object[] filterArgs = {name};
        
         NamingEnumeration answer = ctx.search(getUserContextDN(), userFilter, filterArgs, controls);
         while (answer.hasMore())
         {
            SearchResult sr = (SearchResult) answer.next();
            Attributes attrs = sr.getAttributes();
            Attribute roles = attrs.get( getUserRoleAttribute() );
            if (roles != null)
            {
               for (int r = 0; r < roles.size(); r++)
               {
                  Object value = roles.get(r);
                  String roleName = null;
                  if (getRoleAttributeIsDN() == true)
                  {
                     String roleDN = value.toString();
                     String[] returnAttribute = {getRoleNameAttribute()};
                     try
                     {
                        Attributes result2 = ctx.getAttributes(roleDN, returnAttribute);
                        Attribute roles2 = result2.get(getRoleNameAttribute());
                        if( roles2 != null )
                        {
                           for(int m = 0; m < roles2.size(); m ++)
                           {
                              roleName = (String) roles2.get(m);
                              userRoles.add(roleName);
                           }
                        }
                     }
                     catch (NamingException ex)
                     {
                        throw new IdentityManagementException("Failed to query roles", ex);
                     }
                  }
                  else
                  {
                     // The role attribute value is the role name
                     roleName = value.toString();
                     userRoles.add(roleName);
                  }
               }
            }
         }
         answer.close();                    
        
         return new ArrayList<String>(userRoles);        
      }
      catch (NamingException ex)
      {
         throw new IdentityManagementException("Error getting roles", ex);
      }
      finally
      {
         if (ctx != null)
         {
            try
            {
               ctx.close();
            }
            catch (NamingException ex) {}
         }
      }
   }
View Full Code Here

      return getGrantedRoles(name);
   }

   public boolean grantRole(String name, String role)
   {     
      InitialLdapContext ctx = null;
      try
      {
         ctx = initialiseContext();
        
         String userDN = getUserDN(name);
                 
         BasicAttribute roleAttrib = new BasicAttribute(getUserRoleAttribute(),
               getRoleAttributeIsDN() ? getRoleDN(role) : role);
         ModificationItem mod = new ModificationItem(DirContext.ADD_ATTRIBUTE, roleAttrib);
        
         ctx.modifyAttributes(userDN, new ModificationItem[] { mod });
         return true;
      }
      catch (NamingException ex)
      {
         throw new IdentityManagementException("Failed to grant role", ex);
      }     
      finally
      {
         if (ctx != null)
         {
            try
            {
               ctx.close();
            }
            catch (NamingException ex) {}
         }
      }
   }
View Full Code Here

      }
   }
  
   public boolean revokeRole(String name, String role)
   {     
      InitialLdapContext ctx = null;     
      try
      {
         ctx = initialiseContext();  
         String userDN = getUserDN(name);
        
         Attributes roleAttribs = ctx.getAttributes(userDN, new String[] { getUserRoleAttribute() });
         Attribute roleAttrib = roleAttribs.get( getUserRoleAttribute() );
         if (roleAttrib != null)
         {  
            boolean modified = false;           
            for (int i = roleAttrib.size() - 1; i >= 0; i--)
            {              
               if (getRoleAttributeIsDN())
               {
                  Attributes attribs = ctx.getAttributes((String) roleAttrib.get(i),
                        new String[] { getRoleNameAttribute() });
                  Attribute roleNameAttrib = attribs.get( getRoleNameAttribute() );
                  for (int j = 0; j < roleNameAttrib.size(); j++)
                  {
                     if (role.equals(roleNameAttrib.get(j)))
                     {
                        modified = true;
                        roleAttrib.remove(i);
                     }
                  }
               }
               else if (role.equals(roleAttrib.get(i)))
               {
                  modified = true;
                  roleAttrib.remove(i);
               }
            }
           
            if (modified)
            {
               ModificationItem mod = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, roleAttrib);
               ctx.modifyAttributes(userDN, new ModificationItem[] { mod });
            }           
         }
      }
      catch (NamingException ex)
      {
         throw new IdentityManagementException("Failed to grant role", ex);
      }     
      finally
      {
         if (ctx != null)
         {
            try
            {
               ctx.close();
            }
            catch (NamingException ex) {}
         }
      }     
     
View Full Code Here

TOP

Related Classes of javax.naming.ldap.InitialLdapContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.