Script create user in RT system with validation LDAP

Hi all!

I want create user with perl script, but don’t working.

Can you help me?

#!/usr/bin/perl
use lib ("/usr/local/rt-3.8.2/lib/");
use RT::Interface::CLI qw(CleanEnv);
use RT;
RT::LoadConfig();
RT::Init();
use RT::Ticket;
use RT::CurrentUser;
use RT::CustomField;
use RT::Action::Generic;
use RT::Transaction;
use MIME::Entity;
package RT::Action::MyAction;
use base qw(RT::Action::Generic);
use RT::Authen::ExternalAuth::LDAP;
use RT::Authen::ExternalAuth::DBI;

my $user = ‘username’;
my $mail = ‘username@domain.com’;
RT->Config->Get(‘WebExternalAuto’);

$session->{‘CurrentUser’} = RT::CurrentUser->new();
$session->{‘CurrentUser’}->Load($user);

my $UserObj = RT::User->new($RT::SystemUser);
print $UserObj."\n";
my ($val, $msg) = $UserObj->SetName($user);

my ($val, $msg) =

$UserObj->Create(
%{ref($RT::AutoCreate) ? $RT::AutoCreate : {}},
Name => $user,
Gecos => $user,
_RecordTransaction => 1,
EmailAddress => $mail,
RealName => ‘User USer’,
Comments => ‘user LDAP’,
WorkPhone => 'phone,
Address1 => ‘streetAddress’,
City => ‘l’,
State => ‘st’,
Zip => ‘postalCode’,
Country => ‘co’
);
$RT::Logger->info(“Autocreated authenticated user " . $UserObj->Name .
” (" . $UserObj->Id . “)\n”);

$session{‘CurrentUser’}->Load($user) if $UserObj->Id;
$RT::Logger->error( “No puedo crear el usuario $user: $msg” );

if(RT::Authen::ExternalAuth::UserExists($user)){ print “EXISTS”;} else
{print “NO EXISTS”;}

$RT::Logger->info( “Autocreated external user”,
$UserObj->Name,
"(",
$UserObj->Id,
")");

         $RT::Logger->debug("Loading new user (",
                             $username,
                             ") into current session");
         $session->{'CurrentUser'}->Load($username);

I want create user with perl script, but don’t working.
Can you help me?

How doesn’t it work?
Please describe the failure and provide the logs.

-kevin

Hi Kevin.

Mi log file is this:

[Thu Mar 24 16:20:36 2011] [debug]:
RT::Authen::ExternalAuth::CanonicalizeUserInfo called by RT::User
/usr/local/rt-3.8.2_pruebas/lib//RT/User_Vendor.pm 20 with: Disabled: 0,
EmailAddress: , Gecos: pruebas, Name: pruebas, Privileged: 0
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:450)
[Thu Mar 24 16:20:36 2011] [debug]: Attempting to get user info using
this external service:
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:458)
[Thu Mar 24 16:20:36 2011] [info]:
RT::Authen::ExternalAuth::CanonicalizeUserInfo returning Disabled: 0,
EmailAddress: , Gecos: pruebas, Name: pruebas, Privileged: 0
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:536)
[Thu Mar 24 16:20:36 2011] [error]: No puedo crear el usuario pruebas:
Could not set user info (./scripts/crea_user2.pl:39)

Thanks.

El 24/03/2011 16:58, Kevin Falcone escribi�:> On Thu, Mar 24, 2011 at 03:20:12PM +0100, Sergio Cubero wrote:

I want create user with perl script, but don’t working.
Can you help me?
How doesn’t it work?
Please describe the failure and provide the logs.

-kevin

#!/usr/bin/perl
use lib ("/usr/local/rt-3.8.2/lib/");
use RT::Interface::CLI qw(CleanEnv);
use RT;
RT::LoadConfig();
RT::Init();
use RT::Ticket;
use RT::CurrentUser;
use RT::CustomField;
use RT::Action::Generic;
use RT::Transaction;
use MIME::Entity;
package RT::Action::MyAction;
use base qw(RT::Action::Generic);
use RT::Authen::ExternalAuth::LDAP;
use RT::Authen::ExternalAuth::DBI;

my $user = ‘username’;
my $mail = ‘username@domain.com’;
RT->Config->Get(‘WebExternalAuto’);

$session->{‘CurrentUser’} = RT::CurrentUser->new();
$session->{‘CurrentUser’}->Load($user);

my $UserObj = RT::User->new($RT::SystemUser);
print $UserObj."\n";
my ($val, $msg) = $UserObj->SetName($user);

my ($val, $msg) =

$UserObj->Create(
%{ref($RT::AutoCreate) ? $RT::AutoCreate : {}},
Name => $user,
Gecos => $user,
_RecordTransaction => 1,
EmailAddress => $mail,
RealName => ‘User USer’,
Comments => ‘user LDAP’,
WorkPhone => 'phone,
Address1 => ‘streetAddress’,
City => ‘l’,
State => ‘st’,
Zip => ‘postalCode’,
Country => ‘co’
);
$RT::Logger->info(“Autocreated authenticated user " . $UserObj->Name .
” (" . $UserObj->Id . “)\n”);

$session{‘CurrentUser’}->Load($user) if $UserObj->Id;
$RT::Logger->error( “No puedo crear el usuario $user: $msg” );

if(RT::Authen::ExternalAuth::UserExists($user)){ print “EXISTS”;}
else {print “NO EXISTS”;}

$RT::Logger->info( “Autocreated external user”,
$UserObj->Name,
"(",
$UserObj->Id,
")");

         $RT::Logger->debug("Loading new user (",
                             $username,
                             ") into current session");
         $session->{'CurrentUser'}->Load($username);

Mi log file is this:

I don’t see most of your log messages showing up there, especially the
part that tells you if this is an external or internal user. Are they
getting cut out?

You’re also not returning an email address for the user, which makes
me think you might already have a user in the system with a blank
email address and RT won’t allow you to make two like that.

-kevin

Hi Kevin.

I have change the script. I running the script and local users or LDAP
users, but don’t create user.

This is my new script:

#!/usr/bin/perl
use lib ("/usr/local/rt-3.8.2_pruebas/lib/");

use RT::Interface::CLI qw(CleanEnv);
use RT;
use RT::Util;
RT::LoadConfig();
RT::Init();
use RT::Ticket;
use RT::CurrentUser;
use RT::CustomField;
use RT::Action::Generic;
use RT::Transaction;
use MIME::Entity;
package RT::Action::MyAction;
use base qw(RT::Action::Generic);
use RT::Authen::ExternalAuth::LDAP;
use RT::Authen::ExternalAuth::DBI;

my $user = ‘pruebas’;
my $mail = ‘pruebas@dominio.com’;

$session{‘CurrentUser’} = RT::CurrentUser->new();
my $load_method = RT->Config->Get(‘WebExternalGecos’) ? ‘LoadByGecos’ :
‘Load’;
$session{‘CurrentUser’} = RT::CurrentUser->new();
$session{‘CurrentUser’}->$load_method($user);
my $UserObj = RT::User->new(RT::CurrentUser->new(‘RT_System’));
my ($val, $msg) = $UserObj->Create(
%{ref($RT::AutoCreate) ?
$RT::AutoCreate : {}},
Name => $user,
Gecos => $user,
);

$session{‘CurrentUser’}->Load($user) if $UserObj->Id;
$RT::Logger->error( “No puedo crear el usuario $user: $msg” );

This is my ouput log file:

[Thu Mar 24 17:40:34 2011] [debug]:
RT::Authen::ExternalAuth::CanonicalizeUserInfo called by RT::User
/usr/local/rt-3.8.2_pruebas/lib//RT/User_Vendor.pm 20 with: Disabled: 0,
EmailAddress: , Gecos: pruebas, Name: pruebas, Privileged: 0
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:450)
[Thu Mar 24 17:40:34 2011] [debug]: Attempting to get user info using
this external service:
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:458)
[Thu Mar 24 17:40:34 2011] [info]:
RT::Authen::ExternalAuth::CanonicalizeUserInfo returning Disabled: 0,
EmailAddress: , Gecos: pruebas, Name: pruebas, Privileged: 0
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:536)
[Thu Mar 24 17:40:34 2011] [error]: No puedo crear el usuario pruebas:
Could not set user info (./crea_user2.pl:39)

thanks.

El 24/03/2011 18:33, Kevin Falcone escribi�:> On Thu, Mar 24, 2011 at 05:22:05PM +0100, sercuto wrote:

Mi log file is this:
I don’t see most of your log messages showing up there, especially the
part that tells you if this is an external or internal user. Are they
getting cut out?

You’re also not returning an email address for the user, which makes
me think you might already have a user in the system with a blank
email address and RT won’t allow you to make two like that.

-kevin

[Thu Mar 24 16:20:36 2011] [debug]:
RT::Authen::ExternalAuth::CanonicalizeUserInfo called by RT::User
/usr/local/rt-3.8.2_pruebas/lib//RT/User_Vendor.pm 20 with:
Disabled: 0, EmailAddress: , Gecos: pruebas, Name: pruebas,
Privileged: 0
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:450)
[Thu Mar 24 16:20:36 2011] [debug]: Attempting to get user info
using this external service:
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:458)
[Thu Mar 24 16:20:36 2011] [info]:
RT::Authen::ExternalAuth::CanonicalizeUserInfo returning Disabled:
0, EmailAddress: , Gecos: pruebas, Name: pruebas, Privileged: 0
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:536)
[Thu Mar 24 16:20:36 2011] [error]: No puedo crear el usuario
pruebas: Could not set user info (./scripts/crea_user2.pl:39)

Thanks.

El 24/03/2011 16:58, Kevin Falcone escribi�:

On Thu, Mar 24, 2011 at 03:20:12PM +0100, Sergio Cubero wrote:

I want create user with perl script, but don’t working.
Can you help me?
How doesn’t it work?
Please describe the failure and provide the logs.

-kevin

#!/usr/bin/perl
use lib ("/usr/local/rt-3.8.2/lib/");
use RT::Interface::CLI qw(CleanEnv);
use RT;
RT::LoadConfig();
RT::Init();
use RT::Ticket;
use RT::CurrentUser;
use RT::CustomField;
use RT::Action::Generic;
use RT::Transaction;
use MIME::Entity;
package RT::Action::MyAction;
use base qw(RT::Action::Generic);
use RT::Authen::ExternalAuth::LDAP;
use RT::Authen::ExternalAuth::DBI;

my $user = ‘username’;
my $mail = ‘username@domain.com’;
RT->Config->Get(‘WebExternalAuto’);

$session->{‘CurrentUser’} = RT::CurrentUser->new();
$session->{‘CurrentUser’}->Load($user);

my $UserObj = RT::User->new($RT::SystemUser);
print $UserObj."\n";
my ($val, $msg) = $UserObj->SetName($user);

my ($val, $msg) =

$UserObj->Create(
%{ref($RT::AutoCreate) ? $RT::AutoCreate : {}},
Name => $user,
Gecos => $user,
_RecordTransaction => 1,
EmailAddress => $mail,
RealName => ‘User USer’,
Comments => ‘user LDAP’,
WorkPhone => 'phone,
Address1 => ‘streetAddress’,
City => ‘l’,
State => ‘st’,
Zip => ‘postalCode’,
Country => ‘co’
);
$RT::Logger->info(“Autocreated authenticated user " . $UserObj->Name .
” (" . $UserObj->Id . “)\n”);

$session{‘CurrentUser’}->Load($user) if $UserObj->Id;
$RT::Logger->error( “No puedo crear el usuario $user: $msg” );

if(RT::Authen::ExternalAuth::UserExists($user)){ print “EXISTS”;}
else {print “NO EXISTS”;}

$RT::Logger->info( “Autocreated external user”,
$UserObj->Name,
"(",
$UserObj->Id,
")");

         $RT::Logger->debug("Loading new user (",
                             $username,
                             ") into current session");
         $session->{'CurrentUser'}->Load($username);

I have change the script. I running the script and local users or
LDAP users, but don’t create user.

Did you check if you already have a user with a blank EmailAddress
like I suggested?

select count(*) from Users where EmailAddress = ‘’;
your logs say you’re not pulling email back from LDAP

Maybe you should explain what you’re trying to do. It is possible
that there are better ways to accomplish your goal.
If all you want to do is require that new users exist in LDAP, then
make sure that AutoCreateNonExternalUsers is set to 0.

-kevin

Hi Kevin.

I don’t have any users with a blank EmailAddress.

My target is create a simple script in perl for create users. This script be called inside RT server.

./create_user

I could do it by inserting data directly in the database RT3, but I wanted to please do itusing APIs RT.

rt3_pruebas=# select count(*) from Users where emailaddress = ‘’;
count
0
(1 row)

Thanks.

El 24/03/2011 21:48, Kevin Falcone escribi�:> On Thu, Mar 24, 2011 at 06:46:19PM +0100, sercuto wrote:

I have change the script. I running the script and local users or
LDAP users, but don’t create user.
Did you check if you already have a user with a blank EmailAddress
like I suggested?

select count(*) from Users where EmailAddress = ‘’;
your logs say you’re not pulling email back from LDAP

Maybe you should explain what you’re trying to do. It is possible
that there are better ways to accomplish your goal.
If all you want to do is require that new users exist in LDAP, then
make sure that AutoCreateNonExternalUsers is set to 0.

-kevin

This is my new script:

#!/usr/bin/perl
use lib ("/usr/local/rt-3.8.2_pruebas/lib/");

use RT::Interface::CLI qw(CleanEnv);
use RT;
use RT::Util;
RT::LoadConfig();
RT::Init();
use RT::Ticket;
use RT::CurrentUser;
use RT::CustomField;
use RT::Action::Generic;
use RT::Transaction;
use MIME::Entity;
package RT::Action::MyAction;
use base qw(RT::Action::Generic);
use RT::Authen::ExternalAuth::LDAP;
use RT::Authen::ExternalAuth::DBI;

my $user = ‘pruebas’;
my $mail = ‘pruebas@dominio.com’;

$session{‘CurrentUser’} = RT::CurrentUser->new();
my $load_method = RT->Config->Get(‘WebExternalGecos’) ?
‘LoadByGecos’ : ‘Load’;
$session{‘CurrentUser’} = RT::CurrentUser->new();
$session{‘CurrentUser’}->$load_method($user);
my $UserObj = RT::User->new(RT::CurrentUser->new(‘RT_System’));
my ($val, $msg) = $UserObj->Create(
%{ref($RT::AutoCreate)
? $RT::AutoCreate : {}},
Name => $user,
Gecos => $user,
);

$session{‘CurrentUser’}->Load($user) if $UserObj->Id;
$RT::Logger->error( “No puedo crear el usuario $user: $msg” );


This is my ouput log file:

[Thu Mar 24 17:40:34 2011] [debug]:
RT::Authen::ExternalAuth::CanonicalizeUserInfo called by RT::User
/usr/local/rt-3.8.2_pruebas/lib//RT/User_Vendor.pm 20 with:
Disabled: 0, EmailAddress: , Gecos: pruebas, Name: pruebas,
Privileged: 0
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:450)
[Thu Mar 24 17:40:34 2011] [debug]: Attempting to get user info
using this external service:
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:458)
[Thu Mar 24 17:40:34 2011] [info]:
RT::Authen::ExternalAuth::CanonicalizeUserInfo returning Disabled:
0, EmailAddress: , Gecos: pruebas, Name: pruebas, Privileged: 0
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:536)
[Thu Mar 24 17:40:34 2011] [error]: No puedo crear el usuario
pruebas: Could not set user info (./crea_user2.pl:39)

thanks.

El 24/03/2011 18:33, Kevin Falcone escribi�:

On Thu, Mar 24, 2011 at 05:22:05PM +0100, sercuto wrote:

Mi log file is this:
I don’t see most of your log messages showing up there, especially the
part that tells you if this is an external or internal user. Are they
getting cut out?

You’re also not returning an email address for the user, which makes
me think you might already have a user in the system with a blank
email address and RT won’t allow you to make two like that.

-kevin

[Thu Mar 24 16:20:36 2011] [debug]:
RT::Authen::ExternalAuth::CanonicalizeUserInfo called by RT::User
/usr/local/rt-3.8.2_pruebas/lib//RT/User_Vendor.pm 20 with:
Disabled: 0, EmailAddress: , Gecos: pruebas, Name: pruebas,
Privileged: 0
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:450)
[Thu Mar 24 16:20:36 2011] [debug]: Attempting to get user info
using this external service:
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:458)
[Thu Mar 24 16:20:36 2011] [info]:
RT::Authen::ExternalAuth::CanonicalizeUserInfo returning Disabled:
0, EmailAddress: , Gecos: pruebas, Name: pruebas, Privileged: 0
(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:536)
[Thu Mar 24 16:20:36 2011] [error]: No puedo crear el usuario
pruebas: Could not set user info (./scripts/crea_user2.pl:39)

Thanks.

El 24/03/2011 16:58, Kevin Falcone escribi�:

On Thu, Mar 24, 2011 at 03:20:12PM +0100, Sergio Cubero wrote:

I want create user with perl script, but don’t working.
Can you help me?
How doesn’t it work?
Please describe the failure and provide the logs.

-kevin

#!/usr/bin/perl
use lib ("/usr/local/rt-3.8.2/lib/");
use RT::Interface::CLI qw(CleanEnv);
use RT;
RT::LoadConfig();
RT::Init();
use RT::Ticket;
use RT::CurrentUser;
use RT::CustomField;
use RT::Action::Generic;
use RT::Transaction;
use MIME::Entity;
package RT::Action::MyAction;
use base qw(RT::Action::Generic);
use RT::Authen::ExternalAuth::LDAP;
use RT::Authen::ExternalAuth::DBI;

my $user = ‘username’;
my $mail = ‘username@domain.com’;
RT->Config->Get(‘WebExternalAuto’);

$session->{‘CurrentUser’} = RT::CurrentUser->new();
$session->{‘CurrentUser’}->Load($user);

my $UserObj = RT::User->new($RT::SystemUser);
print $UserObj."\n";
my ($val, $msg) = $UserObj->SetName($user);

my ($val, $msg) =

$UserObj->Create(
%{ref($RT::AutoCreate) ? $RT::AutoCreate : {}},
Name => $user,
Gecos => $user,
_RecordTransaction => 1,
EmailAddress => $mail,
RealName => ‘User USer’,
Comments => ‘user LDAP’,
WorkPhone => 'phone,
Address1 => ‘streetAddress’,
City => ‘l’,
State => ‘st’,
Zip => ‘postalCode’,
Country => ‘co’
);
$RT::Logger->info(“Autocreated authenticated user " . $UserObj->Name .
” (" . $UserObj->Id . “)\n”);

$session{‘CurrentUser’}->Load($user) if $UserObj->Id;
$RT::Logger->error( “No puedo crear el usuario $user: $msg” );

if(RT::Authen::ExternalAuth::UserExists($user)){ print “EXISTS”;}
else {print “NO EXISTS”;}

$RT::Logger->info( “Autocreated external user”,
$UserObj->Name,
"(",
$UserObj->Id,
")");

         $RT::Logger->debug("Loading new user (",
                             $username,
                             ") into current session");
         $session->{'CurrentUser'}->Load($username);

My target is create a simple script in perl for create users. This script be called inside RT server.

Inside RT server? I don’t understand, this looks like a command line
script.

I could do it by inserting data directly in the database RT3, but&n bsp;I wanted to please do
itusing APIs RT.

I guess I don’t understand why you’re loading the extension instead of
just calling Create()

You seem to be loading a lot of unused code and setting up $session
for no particular reason which makes your script hard to understand.

(/usr/local/rt-3.8.2_pruebas/lib//RT/Authen/ExternalAuth.pm:450)
[Thu Mar 24 17:40:34 2011] [debug]: Attempting to get user info
using this external service:

I don’t see any service name here, do you have one defined?
If RT::Authen::ExternalAuth can’t canonicalize the user info from
something in ExternalInfoPriority it isn’t going to allow the user
creation unless you’ve set AutoCreateNonExternalUsers properly.

You should warn those out in your script to confirm they’re set to
what you think they are.

-kevin