7.4 Managing Users

Use a script to perform the following user access functions:

  • Create, find, or delete users

  • Create or find groups

The following sections describe user and group script functions and changing group membership:

7.4.1 Understanding Script Functions

The following sample script shows how to perform these functions. Read the comments to understand the purpose of each function.

// @debug off

// Locate the groups and users elements.
var groups = formula.Root.findElement( 'groups=Groups/security=Security root=Administration' )
var users = formula.Root.findElement( 'users=Users/security=Security/root=Administration' )

// Do we have the testGroup?  If not, create the new group.
try 
{
   formula.log.info( 'Finding group testGroup' )
   formula.Root.findElement( 'group=testGroup/groups=Groups/security=Security/root=Administration' )
}
catch( missing )
{
   formula.log.info( 'Group testGroup missing; creating...' )
   groups.perform( session, 'LifeCycle|Create', [], [ 
                   'testGroup',                 // Group name
                   'Test group description',    // Group description
                   '' ] )                       // Group membership (comma-delimited list)
}

// Delete some users.
var userNames = ['jim', 'anne', 'lisa', 'neil', 'john' ]
for( var i = 0; i < userNames.length; ++i )
{
   try
   {
      // Find the user.
      formula.log.info( 'Finding user ' + userNames[i] )
      var user = formula.Root.findElement( 'user=' + formula.util.encodeURL( userNames[i] ) + '/users=Users/security=Security/root=Administration' )
      formula.log.info( 'Deleting user' )
      user.perform( session, 'LifeCycle|Delete', [], [] )
   }
   catch( missing )
   {
   }
}

// Create some users.
for( var i = 0; i < userNames.length; ++i )
{
   try
   {
      // Find the user.
      formula.log.info( 'Finding user ' + userNames[i] )
      formula.Root.findElement( 'user=' + formula.util.encodeURL( userNames[i] ) + '/users=Users/security=Security/root=Administration' )
      formula.log.info( 'Found existing user ' + userNames[i] )
   }
   catch( missing )
   {
      // Create the user.
      var memberOf = 'testGroup' + ( ( ( i % 2 ) == 1 ) ? ',users' : '' ) // Test for group membership
      formula.log.info( 'User ' + userNames[i] + ' missing; creating with groups: ' + memberOf + '...' )
      users.perform( session, 'LifeCycle|Create', [], [
                     userNames[i], // User name
                     'password',   // Password
                     '',           // Full name
                     '',           // email
                     '',           // phone
                     '',           // fax
                     '',           // pager
                     memberOf      // Group membership (comma-delimited list)
                     ] )
   }
}

// Now, set the user's profile to one we know (guest).
var guest = formula.Root.findElement( 'user=guest/users=Users/security=Security/root=Administration' )
if( guest )
{
   var guestProfile = guest.profile
   formula.log.info( 'Setting profile to guest profile: ' + guestProfile )

   for( var i = 0; i < userNames.length; ++i )
   {
      formula.log.info( 'Updating profile for user ' + userNames[i] )
      var user = formula.Root.findElement( 'user=' + formula.util.encodeURL( userNames[i] ) + '/users=Users/security=Security/root=Administration' )
      user.profile = guestProfile
   }
}

formula.log.info( 'Done' )

7.4.2 Changing a User’s Group Membership

To change a user’s group membership, use the following function and replace ‘group1, group2’ with actual group names:

user.perform( session, 'LifeCycle|SetGroupNames', [], ['group1,group2'] ) 

For example:

  var tech = 'jim'
     var user = formula.Root.findElement( 'user=' + formula.util.encodeURL( tech ) + '/users=Users/security=Security/root=Administration' )
     user.perform( session, 'LifeCycle|SetGroupNames', [], ['group1,group2'] )