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;
    }

# add the new group to the group list, or if the group list is empty just let the new list contain only the new group.
    my $newgrouplist;
    if ($currentgrouplist) {
        print "\tcurrent group list: $currentgrouplist\n";
        $newgrouplist = $currentgrouplist . "$group_id;";
    }
    else {
        print "\tno groups were assigned to this user.\n";
        $newgrouplist = "$group_id;";
    }

    print "\tnew group list    : $newgrouplist\n";

    # write the entry back using SetEntry
    ars_SetEntry( $ctrl, "User", $userentry[0], 0, $userfields{'Group List'},
        $newgrouplist )
      || die "ars_SetEntry(User): $ars_errstr";

}

# and of course log off nicely.
ars_Logoff($ctrl);

exit 0;



( run in 1.462 second using v1.01-cache-2.11-cpan-97f6503c9c8 )