WebGUI
      Click here to register.
      
View Cart (0)
Sprechen Sie WebGUI? Parlez vous WebGUI? Se habla WebGUI? Spreekt u WebGUI?

Do you speak WebGUI? Please help us translate WebGUI into your language.


How to add a webgui user via a url

So we had a problem - we wanted to create webgui users from an external application.  In our case an old database application which we managed all of our customers in.  Now you could do this via sql queries I guess, but you really don't want to mess around with the WebGUI database willy nilly and have some upgrade break the process, so instead the better way to approach it is to utilise the extremely powerful WebGUI API.

So using the userimport.pl script as a reference, I then wrote a new script called adduser.pl which I'm sharing below.

The basic idea of this script being that i could use a url like:

http://mysite.com/adduser.pl?username=xxx&password=xxx

and it should return a bit of html indicating success or failure.
The script will only create new users and each parameter is given through the url.  The only required parameters are username and password, but you can also add firstname, lastname, companyname and email to the url if you wish.

ie. 

http://mysite.com/adduser.pl?username=xxx&password=xxx&email=xxx&firstname=xxx&lastname=xxx&companyname=xxx

Please note that you'll need to tweak the script for your webgui setup slightly, but you can use it prettymuch as is.  The line you WILL need to change is the one referring to your webgui config file:

my $session = WebGUI::Session->open("/data/WebGUI","webgui.conf");

Please find the full script below:


#!/data/wre/prereqs/perl/bin/perl
use strict;
use lib '/data/WebGUI/lib';
use CGI;
use WebGUI::Session;
use WebGUI::User;
use WebGUI::Auth::WebGUI;
use Digest::MD5;

my $cgi = CGI->new;
print $cgi->header("text/html");
print "<HTML>", "\n";
print "<HEAD><TITLE>Add User Script</TITLE></HEAD>", "\n";
print "<BODY>", "\n";
print "<H1>", "Add User Script", "</H1>", "<HR>", "\n";

my $username = $cgi->param("username");
my $password = $cgi->param("password");
my $firstname = $cgi->param("firstname");
my $lastname = $cgi->param("lastname");
my $companyname = $cgi->param("companyname");
my $email = $cgi->param("email");

my $error = 0;

if ($username eq "")
{
  $error = 1;
  print "ERROR: username not specified\n\n<BR><BR>";
}

if ($password eq "")
{
  $error = 1;
  print "ERROR: password not specified\n\n<BR><BR>";
}
my $session = WebGUI::Session->open("/data/WebGUI","webgui.conf");
# test if the user exists as visitor first
my $auth = WebGUI::Auth::WebGUI->new($session, 'WebGUI','1');
if (!$auth->validUsername($username))
{
  $error = 1;
  print "ERROR: username already exists\n\n<BR><BR>";
}
if ($error > 0)
{
  print "<b>Script Aborted!</b>\n<BR>";
  print "<BR><h3 style='color:red'>Failure!</h3>";
}
else
{
  print " A WebGUI User has been created with the following details:\n<BR><UL>";
  print "<LI>Username = $username</LI>\n";
  print "<LI>Password = $password</LI>\n";
  print "<LI>First Name = $firstname</LI>\n";
  print "<LI>Last Name = $lastname</LI>\n";
  print "<LI>Company Name = $companyname</LI>\n";
  print "<LI>Email = $email</LI></UL>\n";
  my $user = WebGUI::User->new($session, "new");
  $session->user({userId=>3});
  $user->username($username);
  $auth = WebGUI::Auth->new($session, 'WebGUI', $user->userId);
  my $hashRef = { identifier => Digest::MD5::md5_base64($password),
                  changePassword => 1,
                  changeUsername => 0 };
  $auth->saveParams($user->userId, 'WebGUI', $hashRef);
  $user->authMethod("WebGUI");
  $user->status("Active");
  # set user profile fields based on inputs
  $user->profileField("firstName", $firstname);
  $user->profileField("lastName", $lastname);
  $user->profileField("workName", $companyname);
  $user->profileField("email", $email);

  if ($user->username eq $username)
{
    print "<BR><h3 style='color:green'>Success!</h3>";
  }
else
{
    print "<BR><h3 style='color:red'>Failure!</h3>";
  }
  $session->var->end;
  $session->close;
}
print "</BODY></HTML>", "\n"; 

You can download the full script above from the contributions section

Hopefully the above script will prove useful for someone else wanting to do something similar.  Please note that there are security concerns with this script - especially if you add privileged users through this method.  As we were adding normal users and added privileges through a separate sql table, this wasn't an issue for us.

If you have any further questions about the above script - you can contact me, Radix on the webgui IRC channel.

Keywords: howto