ARSperl

 view release on metacpan or  search on metacpan

example/AddUsersToGroup.pl  view on Meta::CPAN

# This loop will process all users one by one, see if they are already a member of the group specified,
# if neccesary we add them to the group by changing the Group List and writing it back.
foreach (@users) {
    print "Adding $_ to $group .. \n";

    # Create a qualifier to retrieve the Entry ID for this user
    ( my $userqualifier =
          ars_LoadQualifier( $ctrl, "User", "'Login Name' = \"$_\"" ) )
      || die "ars_LoadQualifier: $ars_errstr";

# Fetch the EID for this user; if there is no such user, say so and continue with next user
# ars_GetListEntry provides a list with Entry-Id, Short description pairs
# In this case only one pair. That means $userentry[0] will contain the actual Entry ID.
    my @userentry = ars_GetListEntry( $ctrl, "User", $userqualifier, 0, 0, );

    # If there is no record for this user, say so and conitue with the next one
    if ( !@userentry ) { print "No user $_\n"; next; }

# Get the value of the Group List field. Syntax = ars_GetEntry(ctrl, schema, eid [field ID...n])
# so in this case we only get the value returned for one field ID, the Group List
# If you do not specify field ID's, you will get all values for the whole entry.
    my %uservalues =
      ars_GetEntry( $ctrl, "User", $userentry[0], $userfields{'Group List'} );

    # Get the field values for this entry
    # set $currentgrouplist to the contents of the Group List field
    my $currentgrouplist = $uservalues{ $userfields{'Group List'} };

#if the Group List already contains the group, say so and continue with  next user
    if (
        (
               ( $currentgrouplist =~ /^$group_id;/ )
            || ( $currentgrouplist =~ /;$group_id;/ )
        )
      )
    {
        print "\talready a member of $group\n";
        next;
    }



( run in 0.614 second using v1.01-cache-2.11-cpan-b85c58fdc1d )