RT::User problem

Hi there,

I would like to create a User object through the Load method. The
documentation writes:

“Load a user object from the database. Takes a single argument. If
the argument is numerical, load by the column ‘id’. If a user object
or its subclass passed then loads the same user by id. Otherwise,
load by the “Name” column which is the user’s textual username.”

#!/usr/bin/perl
use strict;
use lib “/opt/rt4/lib”;
use lib “/opt/rt4/local/lib”;
use RT;
use RT::User;

RT->LoadConfig;
RT->Init;

my $test_user = RT::User::Load(‘bg@buday-rd.hu’);
printf “$test_user=”, $test_user;
my @to = $test_user->EmailAddress;

for $a (0 … $#to)
{
print $to[$a], “\n”;
}

Running this script with sudo makes it say

[Mon Aug 13 11:16:47 2012] [crit]: Can’t call method “EmailAddress” on
an undefined value at ./email.pl line 13. (/opt/rt4/lib/RT.pm:341)
Can’t call method “EmailAddress” on an undefined value at ./email.pl line 13.

The username I used in the place of me@company.com is indeed a valid
user name. What is the problem then?

  • Gergely

Hi there,

I would like to create a User object through the Load method. The
documentation writes:

“Load a user object from the database. Takes a single argument. If
the argument is numerical, load by the column ‘id’. If a user object
or its subclass passed then loads the same user by id. Otherwise,
load by the “Name” column which is the user’s textual username.”

#!/usr/bin/perl
use strict;
use lib “/opt/rt4/lib”;
use lib “/opt/rt4/local/lib”;
use RT;
use RT::User;

RT->LoadConfig;
RT->Init;

my $test_user = RT::User::Load(‘bg@buday-rd.hu’);

In theory load could act as an object instance creator, but it
doesn’t. To create
an object you use ->new call first and then load record into it:

my $user = RT::User->new( RT->SystemUser );
$user->Load(‘xxxxxxx’);

About argument of Load methods. The bottom variant only accepts id. For
some objects that have unique parameters besides id it also accepts string.
For example queue name or user name (login). For such objects Load looks
at argument and if it contains any not number character then load lookups
by name not id. This the reason why RT doesn’t support user names containing
only digits.

Users have more than one unique parameter, EmailAddress is also unique.
Load in RT::User doesn’t do more checks to decide whether argument is
email address or not, so you have to use different Load* methods which exist.

A little bit about Perl’s way to call things:

  1. RT::User::Load(…) - function call in other package - call “Load”
    sub in RT::User package without emplicit arguments ignoring any
    inheritance that may exist
  2. RT::User->Load(…) - class method call - call “Load” sub starting
    from RT::User package and accounting inheritance with “RT::User” class
    name as implicit first argument
  3. $user_obj->Load(…) - instance method call - previous, but with
    object as implicit first argument

In RT you mostly use third variant. Second variant is also used, for
example: XXX->new call returns object instance of XXX class;
RT->SystemUser returns global object (one for the whole system);
RT->Config->Get(…);

First variant should be avoided, it only helpful if you do magic or in
some historical places. For example we used to write RT::Init(), but
now move towards RT->Init, likely for us Init ignores arguments and
doesn’t care how it’s called.

printf “$test_user=”, $test_user;
my @to = $test_user->EmailAddress;

for $a (0 … $#to)
{
print $to[$a], “\n”;
}

Running this script with sudo makes it say

[Mon Aug 13 11:16:47 2012] [crit]: Can’t call method “EmailAddress” on
an undefined value at ./email.pl line 13. (/opt/rt4/lib/RT.pm:341)
Can’t call method “EmailAddress” on an undefined value at ./email.pl line 13.

The username I used in the place of me@company.com is indeed a valid
user name. What is the problem then?

  • Gergely

Best regards, Ruslan.