Application Techniques



Enumerating Security Providers, Users, and Groups

How to use the Server Administration API to enumerate security providers as well as the users and groups defined by providers.

About this technique

Details

Category

Administration Techniques

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on using the Server Administration API in the Administrator's Guide

Enumerating security providers   Top of page

The following user-defined loops through the objects known to the current server looking for security directories, and loads the results into a tree control. (For a description of how to get the current server, see Getting a server ).

  public void loadUsersAndGroups() 
  { 
  /** 
   *  Method: 
   *      loadUsersAndGroups() 
   *  Description: 
   *      Populate the tree control specified with users and groups from the server specified. 
   *  Parameters: 
   *      None. 
   *  Returns: 
   *      Nothing. 
   */ 
           
  AgoTreeControlNode nodeChild; 
  Boolean value = new Boolean(false); 
  String type, securityName; 
  Image treeImage=null; 
   
  // Clear the tree control 
  tcObjects.removeAll(); 
                                           
  // Get the security providers for the server. 
  try 
  {        
          // Get the security provider directory object. 
          AgiAdmDirectory dir = (AgiAdmDirectory) m_server.getElement(AgiAdmDirectory.SECURITY, AgiAdmDirectory.SECURITY, null ); 
   
          // Get the children of the directory. 
          Enumeration children = dir.getChildren(AgiAdmContainer.GET_CHILDREN_SORTED); 
   
          if (children != null) 
          { 
                  while(children.hasMoreElements()) 
                  { 
                          // Get element from the directory.  If the element is a AgiAdmDirectory 
                          // add it to the tree control.  
                          AgiAdmElement element = (AgiAdmElement)children.nextElement(); 
   
                          if (element instanceof AgiAdmDirectory) 
                          { 
                          AgiAdmDirectory child = (AgiAdmDirectory)element; 
                   
                                   
                                  // Get the server name. 
                                  securityName = ""; 
                                  type = child.getName(); 
           
                                  if (type.equals(AgiAdmDirectory.SILVER_SECURITY)) 
                                  { 
                                          // Add the SilverStream security. 
                                          securityName = "Silver Security"; 
                                          treeImage = getSilverSecurityImage(); 
                                  } 
                                  else if (type.equals(AgiAdmDirectory.NT_SECURITY)) 
                                  { 
                                          // Add the NT security. 
                                          securityName = "NT Security"; 
                                          treeImage = getNTImage(); 
                                  } 
                                  else if (type.equals(AgiAdmDirectory.LDAP_SECURITY)) 
                                  { 
                                          // Add the LDAP security. 
                                          securityName = "LDAP Security"; 
                                          treeImage = getLDAPImage(); 
                                  } 
                                  else if (type.equals(AgiAdmDirectory.NISPLUS_SECURITY)) 
                                  { 
                                          // Add the NIS+ security. 
                                          securityName =  "NIS+ Security"; 
                                          treeImage = getNISImage(); 
                                  } 
                           
                                  Hashtable info = loadUserData(type, true); 
                                  info.put("Data", element); 
                                   
                                  // Add the Security node. 
                                  nodeChild = tcObjects.add(null, AgcTreeControl.NEXT, securityName, info, treeImage); 
   
                                  if (type.equals(AgiAdmDirectory.SILVER_SECURITY)) 
                                  {        
                                           
                                          // Get the children of the directory. 
                                          Enumeration groupuser = child.getChildren(AgiAdmContainer.GET_CHILDREN_SORTED); 
   
                                          if (groupuser != null) 
                                          { 
                                                  while(groupuser.hasMoreElements()) 
                                                  { 
                                                          // Get element from the directory.  If the element is a AgiAdmGroup 
                                                          // add it to the tree control.                           
                                                          AgiAdmElement subdir = (AgiAdmElement)groupuser.nextElement(); 
                                                          String dirType = subdir.getType(); 
                                                           
                                                          if (dirType.equals(AgiAdmDirectory.SILVERGROUPS)) 
                                                          { 
                                                                  // Add the SilverGroups 
                                                                  info = loadUserData(AgiAdmDirectory.SILVERGROUPS, true); 
                                                                  info.put("Data", subdir); 
                                                                  m_agGroups = tcObjects.add(nodeChild, AgcTreeControl.CHILD, "Groups", info, getGroupsImage()); 
                                                                  tcObjects.add(m_agGroups,AgcTreeControl.CHILD, "AgEmptyGroup"); 
                                                          } 
   
                                                          if (dirType.equals(AgiAdmDirectory.SILVERUSERS)) 
                                                          { 
                                                                  // Add the SilverUsers 
                                                                  info = loadUserData(AgiAdmDirectory.SILVERUSERS, true); 
                                                                  info.put("Data", subdir); 
                                                                  m_agUsers = tcObjects.add(nodeChild, AgcTreeControl.CHILD, "Users", info, getUsersImage()); 
                                                                  tcObjects.add(m_agUsers,AgcTreeControl.CHILD, "AgEmptyUser"); 
                                                          } 
                                                  } 
                                          } 
                                  } 
                                  else 
                                          tcObjects.add(nodeChild, AgcTreeControl.CHILD, "Empty" + type); 
                           
                          } 
                  } 
          } 
  } 
  catch (Exception e) 
  { 
          agDialog.displayError(e); 
          return; 
  } 
  } 

Notes about the code

Enumerating provider servers   Top of page

The following user-defined method takes the security directory type and gets the servers known to external (non-SilverStream) security providers. The result is added to the tree control.

  public boolean getServers(String type) 
  { 
  /** 
   *  Method: 
   *      getServers() 
   *  Description: 
   *              Get a the list of server for a specified type. 
   *              (eg. NT, LDAP, or NISPLUS 
   *       
   *  Parameters: 
   *      String type 
   *  Returns: 
   *      boolean 
   */ 
  // Get the selected node and delete any children. 
  AgoTreeControlNode nodeDomains = tcObjects.getSelectedNode(); 
   
  if (nodeDomains != null) removeChildren(nodeDomains); 
                                   
  // Get the servers for the specified type. 
  try 
  {        
          // Get the directory object for the servers requested. 
          AgiAdmDirectory dir = (AgiAdmDirectory) m_server.getElement( type, type, null ); 
   
          // Get the children of the directory. 
          Enumeration children = dir.getChildren(AgiAdmContainer.GET_CHILDREN_SORTED); 
   
          if (children != null) 
          { 
                  while(children.hasMoreElements()) 
                  { 
                          // Get element from the directory.  If the element is a AgiAdmDirectory 
                          // add it to the tree control.  
                          AgiAdmElement element = (AgiAdmElement)children.nextElement(); 
   
                          if (element instanceof AgiAdmDirectory) 
                          { 
                   
                                  AgiAdmDirectory child = (AgiAdmDirectory)element; 
                   
                                  // Get the server name. 
                                  String serverName = child.getName(); 
                                   
                                  Hashtable info = new Hashtable(); 
                                  String server, group, user; 
                                   
                                  if (type.equals(AgiAdmDirectory.NT_SECURITY))  // NT Server. 
                                  { 
                                          server = AgiAdmDirectory.DOMAIN; 
                                          group = AgiAdmDirectory.NTGROUPS;  
                                          user = AgiAdmDirectory.NTUSERS; 
                                  } 
                                  else if (type.equals(AgiAdmDirectory.LDAP_SECURITY))  // LDAP Server. 
                                  { 
                                          server = AgiAdmDirectory.LDAP_SERVER; 
                                          group = AgiAdmDirectory.LDAPGROUPS; 
                                          user =  AgiAdmDirectory.LDAPUSERS; 
                                  } 
                                  else 
                                  { 
                                          server = AgiAdmDirectory.NISPLUS_SERVER; // NIS+ Server. 
                                          group = AgiAdmDirectory.NISPLUSGROUPS; 
                                          user = AgiAdmDirectory.NISPLUSUSERS; 
                                  } 
                                   
                                  // Load up the hashtable with the server info. 
                                  info.put("Type", server); 
                                  info.put("Retrieve", new Boolean(true)); 
                                  info.put("Data", element); 
                                   
                                  // Add the node 
                                  AgoTreeControlNode nodeDomain = tcObjects.add(nodeDomains,AgcTreeControl.CHILD,serverName,info,m_imgServer); 
                                   
                                  AgoTreeControlNode nodeChild; 
                                   
                                  // Get the children of the directory. 
                                  Enumeration groupuser = child.getChildren(AgiAdmContainer.GET_CHILDREN_SORTED); 
                   
                                  if (groupuser != null) 
                                  { 
                                   
                                          while(groupuser.hasMoreElements()) 
                                          { 
                                                   
                                                  // Get element from the directory.  If the element is a AgiAdmGroup 
                                                  // add it to the tree control.                           
                                                  AgiAdmElement subdir = (AgiAdmElement)groupuser.nextElement(); 
                                                  String dirType = subdir.getType(); 
                                                           
                                                  info = new Hashtable(); 
                                                  info.put("Retrieve", new Boolean(true)); 
                                                  info.put("Directory", new Boolean(false)); 
                                                  info.put("Data", subdir); 
                                                   
                                                  // Add the group node. 
                                                  if (dirType.indexOf("Groups") >= 0) 
                                                  { 
                                                          info.put("Type", group); 
                                                          nodeChild = tcObjects.add(nodeDomain,AgcTreeControl.CHILD,"Groups",info,m_imgGroups); 
                                                          tcObjects.add(nodeChild,AgcTreeControl.CHILD,serverName + "EmptyGroup"); 
                                                  } 
                                                   
                                                  // Add the user node. 
                                                  if (dirType.indexOf("Users") >= 0) 
                                                  { 
                                                          info.put("Type", user); 
                                                          nodeChild = tcObjects.add(nodeDomain,AgcTreeControl.CHILD,"Users",info,m_imgUsers); 
                                                          tcObjects.add(nodeChild,AgcTreeControl.CHILD,serverName + "EmptyUser"); 
                                                  } 
                                          } 
                                  }                
                          } 
                  } 
          } 
           
           
          // Reset the retrieve of the user data to false. 
          Hashtable userData = (Hashtable) nodeDomains.getUserData(); 
          userData.put(RETRIEVE, new Boolean(false)); 
           
          // Set the user data so we don't to retrieve next time around. 
          if (nodeDomains != null) nodeDomains.setUserData(userData);      
   
          // Select the  node.     
          if (nodeDomains != null) tcObjects.setSelectedNode(nodeDomains);         
           
          return true; 
  } 
  catch (Exception e) 
  { 
          agDialog.displayError(e); 
          return false; 
  } 
  } 

Notes about the code

Enumerating groups   Top of page

The following user-defined method takes the directory type and server and enumerates groups known to each security provider. Each group is then loaded into the tree control.

  public boolean getGroups(String type, String server) 
  { 
  /** 
   *  Method: 
   *      getGroups() 
   *  Description: 
   *              Get a the list of groups for a specific type. 
   *       
   *  Parameters: 
   *      String type 
   *              String server 
   *  Returns: 
   *      boolean 
   */ 
  // Get the selected node. 
  AgoTreeControlNode nodeGroups = tcObjects.getSelectedNode(); 
  if (nodeGroups != null) removeChildren(nodeGroups); 
   
  //Get the groups for the type that was passed in. 
  try 
  { 
          Hashtable info = new Hashtable(); 
          String childType; 
           
          if (type.equals(AgiAdmDirectory.NTGROUPS)) //NT groups. 
          { 
                 childType = AgiAdmGroup.NTGROUP; 
               info.put(AgiAdmElement.PROP_DOMAIN, server); 
          } 
          else if (type.equals(AgiAdmDirectory.LDAPGROUPS)) //LDAP groups. 
          { 
                  childType = AgiAdmGroup.LDAPGROUP; 
                  info.put(AgiAdmElement.PROP_LDAP_SERVER, server); 
          } 
          else if (type.equals(AgiAdmDirectory.NISPLUSGROUPS)) //NIS+ groups. 
          { 
                  childType = AgiAdmGroup.NISPLUSGROUP; 
                  info.put(AgiAdmElement.PROP_NISPLUS_SERVER, server); 
          } 
          else  
          { 
                 childType = AgiAdmGroup.SILVERGROUP; 
               info = null;  //SilverStream groups. 
          } 
           
          // Get the directory object for groups. 
          AgiAdmDirectory dir = (AgiAdmDirectory) m_server.getElement(type, type, info); 
   
          // Get the children of the directory. 
          Enumeration children = dir.getChildren(AgiAdmContainer.GET_CHILDREN_SORTED); 
   
          if (children != null) 
          { 
                  while(children.hasMoreElements()) 
                  { 
                          // Get element from the directory.  If the element is a AgiAdmGroup 
                          // add it to the tree control.                           
                          AgiAdmElement element = (AgiAdmElement)children.nextElement(); 
                           
                          if (element instanceof AgiAdmGroup) 
                          { 
                                  // Add the node to the tree. 
                          AgiAdmGroup child = (AgiAdmGroup)element; 
                                  String group = child.getName(); 
                                   
                                  // Load the user data hashtable. 
                                  Hashtable userInfo = loadUserData(childType, true); 
   
                                  // Put the AgiAdmUser object in the user data. 
                                  userInfo.put("Data", child); 
                           
                                  AgoTreeControlNode nodeChild =  tcObjects.add(nodeGroups,AgcTreeControl.CHILD,group, userInfo, null); 
   
                                  // Add a dummy node for group users. 
                                  tcObjects.add(nodeChild, AgcTreeControl.CHILD, "GroupUser");                     
                          } 
                  } 
          } 
           
          // Reset the retrieve of the user data to false. 
          info = (Hashtable) nodeGroups.getUserData(); 
          info.put(RETRIEVE, new Boolean(false)); 
           
          // Set the user data for groups not to retrieve next time around.        
          if (nodeGroups != null) nodeGroups.setUserData(info); 
           
          // Select the groups node. 
          if (nodeGroups != null) tcObjects.setSelectedNode(nodeGroups); 
           
          return true; 
  } 
  catch (Exception e) 
  { 
          agDialog.displayError(e); 
          return false; 
  } 
  } 

Notes about the code

Enumerating users in a group   Top of page

The following user-defined method takes the security directory type, server and group and enumerates the users known to each group. It loads the result into the tree control.

  public boolean getGroupUsers(String type, String server, String group) 
  { 
           
          /** 
   *  Method: 
   *      getGroupUsers() 
   *  Description: 
   *              Get a the list of users for a group. 
   *       
   *  Parameters: 
   *      String                          type 
   *              String                  server 
   *              String                  group 
   *  Returns: 
   *      boolean 
   */ 
  // Get the selected node and delete any children. 
  AgoTreeControlNode nodeGroupUsers = tcObjects.getSelectedNode(); 
  if (nodeGroupUsers != null) removeChildren(nodeGroupUsers); 
   
  try 
  { 
          Hashtable info = new Hashtable(); 
          String groupUser; 
          if (type.equals(AgiAdmGroup.NTGROUP)) //NT groups. 
          { 
                  groupUser = NTGROUPUSER; 
                  info.put(AgiAdmElement.PROP_DOMAIN, server); 
          } 
          else if (type.equals(AgiAdmGroup.LDAPGROUP)) //LDAP groups. 
          { 
                  groupUser = LDAPGROUPUSER; 
                  info.put(AgiAdmElement.PROP_LDAP_SERVER, server); 
          }        
          else if (type.equals(AgiAdmGroup.NISPLUSGROUP)) //NIS+ groups. 
          { 
                  groupUser = NISPLUSGROUPUSER; 
                  info.put(AgiAdmElement.PROP_NISPLUS_SERVER, server); 
          } 
          else  
          { 
                  groupUser = SILVERGROUPUSER; 
                  info = null;  //SilverStream groups. 
          } 
          // Get the SilverGroup object. 
          AgiAdmGroup groupUsers = (AgiAdmGroup) m_server.getElement(group, type, info); 
   
          // Get the children of the directory. 
          Enumeration children = groupUsers.getChildren(AgiAdmContainer.GET_CHILDREN_SORTED); 
   
          if (children != null) 
          { 
                  while(children.hasMoreElements()) 
                  { 
                          // Get element from the group object.  If the element is a AgiAdmUserReference 
                          // add it to the tree control.  
                          AgiAdmElement element = (AgiAdmElement)children.nextElement(); 
   
                          if (element instanceof AgiAdmUserReference) 
                          { 
   
                                  AgiAdmUserReference child = (AgiAdmUserReference)element; 
                                  info = loadUserData(groupUser, false); 
                                  info.put("Data", element);  
                                  tcObjects.add(nodeGroupUsers, AgcTreeControl.CHILD,child.getName(), info, null); 
           
                          } 
                  } 
          } 
   
          // Reset the retrieve of the user data to false. 
          if (nodeGroupUsers != null)  
          { 
                  info = (Hashtable) nodeGroupUsers.getUserData(); 
                  info.put(RETRIEVE, new Boolean(false)); 
                   
                  // Set the user data for SilverGroups not to retrieve next time around. 
                  nodeGroupUsers.setUserData(info); 
                   
                  // Select the SilverGroups node. 
                  tcObjects.setSelectedNode(nodeGroupUsers); 
          } 
           
          return true; 
  } 
  catch (Exception e) 
  { 
          agDialog.displayError(e); 
          return false; 
  } 
  } 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.