O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
BlackBerry Hacks
By Dave Mabe
October 2005
More Info

HACK
#82
Create a Web Interface for User Administration
Using the BES User Admin service, create a web interface for adding and removing users
The Code
[Discuss (0) | Link to this hack]

The Code

	#!perl -w

	use strict;
	use CGI;
	use Text::CSV;

	my $BES_USER_ADMIN_CLIENT =
	  "besuseradminclient.exe"; # change to your specific path

	my %BES_SERVERS = ( 
		BES_NAME => "COMPUTER_NAME", # this maps your BES name
							# to the computer name
	);
	my $PASSWORD = "password"; # change to the password for BESUserAdmin
	my $TEMP_DIR = "c:\\temp";

	my $cgi = CGI->new;
	print $cgi->header;
	print $cgi->start_html("BES User Administration");

	print <<"END";
	<style type="text/css">
	body {
	  font-size: 9pt;
	  font-family: arial;
	}
	</style>
	END

	print $cgi->h1("BES User Administration");

	my $op = $cgi->param("op");

	if ($op eq 'remove') {
		my $bes = $cgi->param("bes");
		my $user = $cgi->param("user");
		my $error_file = "$TEMP_DIR\\error.$bes.remove.csv";
		my $system_command = "\"$BES_USER_ADMIN_CLIENT\" " .
		   "-n $BES_SERVERS{$bes} " .
		   "-b $bes -p $PASSWORD -delete -u $user 2> $error_file";
		if (system $system_command == 0) {
			print "<p>Successfully removed $user from $bes</p>\n";
		} else {
			print "<p>ERROR removing $user from $bes.</p>\n";
		}

	} elsif ($op eq 'add') {
		my $bes = $cgi->param("bes");
		my $user = $cgi->param("user");
		my $activation_password = $cgi->param("act_password");
		my $error_file = "$TEMP_DIR\\error.$bes.add.csv";
		my $system_command = "\"$BES_USER_ADMIN_CLIENT\" -n $BES_SERVERS{$bes} " .
		   "-b $bes -p $PASSWORD -add -u $user " .
		   "-w $activation_password 2> $error_file";
		if (system $system_command == 0) {
			print "<p>Successfully added $user from $bes</p>\n";
		} else {
			print "<p>ERROR adding $user from $bes.</p>\n";
		}
	}

	my $csv = Text::CSV->new;
	foreach my $bes (sort keys %BES_SERVERS) {

		my $output_file = "$TEMP_DIR\\bes.$bes.users.csv";
		my $error_file = "$TEMP_DIR\\error.$bes.users.csv";
		my $system_command = "\"$BES_USER_ADMIN_CLIENT\" " .
		  "-n $BES_SERVERS{$bes} " .
		  "-b $bes -p $PASSWORD -stats -users > $output_file 2> $error_file";
		my $return_code = system $system_command;
		open OUTPUT, $output_file || die "Can't open output: $!\n";
		print "<table border=\"1\">\n";
		my $count = 0;
		while (<OUTPUT>) {
			chomp;
			$csv->parse($_);
			my @fields = $csv->fields;
			splice @fields, 1, 2; # remove the really long fields
			if ($count == 0) {
				print
				  "<tr><td><strong>Remove</strong></td><td><strong>",
				  join("</strong></td><td><strong>",@fields),
				  "</strong></td></tr>\n";
			} else {
				print
				  "<tr><td><a href=\"./besadmin.pl?op=remove" .
				  "&amp;user=$fields[0]&amp;bes=$bes\">Remove</a></td><td>",
				  join("</td><td>",@fields),
				  "</td></tr>\n";
			}
			$count++;
		}
		print "</table>\n";
		close OUTPUT;
	}
	print <<"END";
	<p>
	<form action="./besadmin.pl" method="post">
	<input type="text" name="user" value="Username" />
	<input type="text" name="act_password"
	value="Activation Password" />
	<input type="hidden" name="op" value="add" />
	<select name="bes">
	END

	foreach my $bes (sort keys %BES_SERVERS) {
		print "\t<option value=\"$bes\">$bes</option>\n";
	}

	print <<"END";
	</select>
	<input type="submit" value="Add" value="Add" />
	</form>
	</p>
	END

	print $cgi->end_html;


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.