Posts Tagged ‘web services’

Adding Users via Web Services in Sakai
Tuesday, January 29th, 2008

We got ourselves into a little bind and had to add a few hundred users to our portfolio system in a short period of time. The alternatives were to figure out a way to script the addition of users or manually enter the students and add them to the appropriate worksite. Since I’m always up for a challenge, I decided to tackle the script…believe it or not I’ve never had the opportunity to work with perl and/or web services, so this was a great opportunity to get my hands dirty.

A quick search led me to Will Trillich’s helpful documentation over at Serensoft, which enabled me to get started pretty quickly. There was also some documentation over in the Sakai Confluence wiki that helped with the configuration. In order to use web services in sakai, you need to turn it on in the sakai.properties configuration file as follows:

# Enable web services login
webservices.allowlogin=true

If that line doesn’t exist, go ahead and add it.

Here’s the perl script I ended up with to add users from a csv file to sakai and enroll them in a specific course…play nice, it’s my first:


#!/usr/bin/perl

#
# Perl script to add a list of users to sakai and enroll
# them in a given course.  Users are read from a csv formatted
# file with lines like the following:
#
# SUID;DOB;LastName;FirstName;NETID
#

use SOAP::Lite;

# Sakai service location constants
use constant {
    LOGIN_WS => "/sakai-axis/SakaiLogin.jws?wsdl",
    SCRIPT_WS => "/sakai-axis/SakaiScript.jws?wsdl",
    SYR_ADDY => "\@syr.edu",
    DEFAULT_ROLE => "access",
};

# Initialize variables from command line parameters
my $userlist = shift;
my $host = shift;
my $id = shift;
my $password = shift;
my $siteId = shift;
my $roleId = shift;

# Routine to display some usage information
sub usage {
    print "\nUsage: useradd.pl filename host username password [siteId] [roleId]\n\n";
    print "e.g.  useradd.pl userlist.csv http://localhost:8080 admin admin_pass\n\n";
}

# Routine to connect to sakai login service
sub sakaiLogin {
    return $host.LOGIN_WS;
}

# Routine to connect to
sub sakaiScript {
    return $host.SCRIPT_WS;
}

# Routine to create an email address
sub makeEmail {
    my $username = shift;
    return $username.SYR_ADDY;
}

# Create password
sub makePassword {
    my $username = shift;
    my $dob = shift;
    ($month, $day, $year) = split(/\//, $dob);
    $month = ($month < 10) ? "0".$month : $month;
    $day = ($day < 10) ? "0".$day : $day;
    return $username.$month.$day;
}

# If either are undefined, display usage and exit
if (!$userlist ||
    !$host ||
    !$id ||
    !$password) {
    &usage;
    exit;
}

# Open the file containing the user list
open(USERLIST, $userlist) || die "Unable to open $userlist!\n";

# Initialize SOAP client
$loginClient = SOAP::Lite->new(proxy => &sakaiLogin($host));
$sessionId = $loginClient->login($id, $password)->result;

print "SESSION_ID: ".$sessionId."\n";

$scriptClient = SOAP::Lite->new(proxy => &sakaiScript($host));

while (<USERLIST>) {
    chop;
    ($suid, $dob, $lastName, $firstName, $netId) = split(/;/);
    my $email = &makeEmail($netId);
    my $password = &makePassword($netId, $dob);

    print "Attempting to add user:\n";
    print "\tEID:\t\t".$netId."\n";
    print "\tFIRST_NAME:\t".$firstName."\n";
    print "\tLAST_NAME:\t".$lastName."\n";
    print "\tEMAIL:\t\t".$email."\n";
    print "\tPASSWORD:\t".$password."\n";

    my $result = $scriptClient->addNewUser($sessionId,
					   $netId,
					   $firstName,
					   $lastName,
					   $email,
					   '',
					   $password)->result;

    print "Result: ".$result."\n";

    if ($siteId) {
	if (!$roleId) {
	    $roleId = DEFAULT_ROLE;
	}
	print "Attempting to add ".$netId." to site ".$siteId." with ".$roleId." role...";

	$result = $scriptClient->addMemberToSiteWithRole($sessionId,
							 $siteId,
							 $netId,
							 $roleId)->result;

	print $result."\n";
    }

    print "\n";
}

$loginClient->logout($sessionId);

close(USERLIST);

I was blown away how easy it was to accomplish so much with so little! In hindsight, there were several cases where I used bash scripts to do too much, whereas a simple perl script would have saved me a great deal of effort. I notice that there’s a pretty big dispute going on over which is better, perl or python…maybe next time I’ll give python a shot and let you know what I think.

Powered by Laughing Squid