How to list all enabled users in Perl script?

Greetings:

RT 4.0.17. In the web UI, when I list all enabled users by selecting
’%’ I get a list of about 152 users (approximately). Now, I have
written a Perl script that is supposed to list all enabled RT users.
When I run it, it finds 147 users and lists them in alphabetical
order. But approximately 5 users at the end of the alphabet ('Name’
starting with ‘v’, ‘w’, ‘x’, ‘y’, or ‘z’) are missing. For the life of
me I can’t figure out why. Any ideas?

use strict;
use warnings;

use RT;
use RT::User;
use RT::Users;

BEGIN {
RT->LoadConfig;
RT->Init;
}

my $RT_enabled_users = RT::Users->new($RT::SystemUser);
$RT_enabled_users->LimitToEnabled;

my $loop_count = 0;
while (my $current_user = $RT_enabled_users->Next) {

$loop_count += 1;

my $uid = $current_user->Name;
my $real_name = $current_user->RealName;
my $rt_email = $current_user->EmailAddress;

print “$loop_count: ‘$uid’ – $real_name – $rt_email\n”;

}

print “Loop count: $loop_count\n”;

Thanks alot,
Nathan

Call print $RT_enabled_users->BuildSelectQuery, "\n";, check SQL manually
and it is correct then move to Next function.On Mon, Sep 2, 2013 at 6:04 PM, Nathan Cutler presnypreklad@gmail.comwrote:

Greetings:

RT 4.0.17. In the web UI, when I list all enabled users by selecting
‘%’ I get a list of about 152 users (approximately). Now, I have
written a Perl script that is supposed to list all enabled RT users.
When I run it, it finds 147 users and lists them in alphabetical
order. But approximately 5 users at the end of the alphabet (‘Name’
starting with ‘v’, ‘w’, ‘x’, ‘y’, or ‘z’) are missing. For the life of
me I can’t figure out why. Any ideas?

use strict;
use warnings;

use RT;
use RT::User;
use RT::Users;

BEGIN {
RT->LoadConfig;
RT->Init;
}

my $RT_enabled_users = RT::Users->new($RT::SystemUser);
$RT_enabled_users->LimitToEnabled;

my $loop_count = 0;
while (my $current_user = $RT_enabled_users->Next) {

$loop_count += 1;

my $uid = $current_user->Name;
my $real_name = $current_user->RealName;
my $rt_email = $current_user->EmailAddress;

print “$loop_count: ‘$uid’ – $real_name – $rt_email\n”;

}

print “Loop count: $loop_count\n”;

Thanks alot,
Nathan

Best regards, Ruslan.

Call print $RT_enabled_users->BuildSelectQuery, "\n";, check SQL manually
and it is correct then move to Next function.

Thanks, Ruslan. I tried your suggestion. It outputs the following
SELECT statement:

SELECT DISTINCT main.* FROM Users main JOIN Principals Principals_1
ON ( Principals_1.id = main.id ) WHERE (Principals_1.Disabled = ‘0’)
AND (Principals_1.PrincipalType = ‘User’) ORDER BY main.Name ASC

which I cut-and-pasted into the mysql prompt. This gives me 167 rows
in alphabetical order. The Perl script’s output, however, shows only
151 rows.

I changed the SELECT statement to output just main.Name (into a file).
Then, I modified my script to output just the Name of each enabled
user (also to a file). And then I diffed the two resulting files. The
SELECT statement yields 16 more users than the Perl script does.

Here’s my guess at what’s happening: I’m using the MergeUsers
extension. The SELECT statement ignores the fact that some of the
users have been merged, and simply grabs them all. The Perl script, by
contrast, attempts to get only those users that have not been merged
into other users. But for some reason it fails to do this correctly,
leaving out more users than it should. Significantly, it leaves out 8
users at the end of the list. This would indicate that it’s getting a
wrong count of the number of non-merged users. Once it reaches the end
of the count, it simply ignores the remaining users, even though they
are non-merged.

It’s quite possible that I have merged 8 users. These 8 are not in the
list generated by the Perl script. In addition to these 8, another 8
(at the end of the alphabet) are also missing. It’s very weird and it
definitely smells like a bug.

Any ideas appreciated.

Nathan

Find sub Next in lib/RT/Extension/MergeUsers.pm, put logging or printing
debug statements there to check what is going on.On Tue, Sep 3, 2013 at 2:29 PM, Nathan Cutler presnypreklad@gmail.comwrote:

Call print $RT_enabled_users->BuildSelectQuery, "\n";, check SQL
manually
and it is correct then move to Next function.

Thanks, Ruslan. I tried your suggestion. It outputs the following
SELECT statement:

SELECT DISTINCT main.* FROM Users main JOIN Principals Principals_1
ON ( Principals_1.id = main.id ) WHERE (Principals_1.Disabled = ‘0’)
AND (Principals_1.PrincipalType = ‘User’) ORDER BY main.Name ASC

which I cut-and-pasted into the mysql prompt. This gives me 167 rows
in alphabetical order. The Perl script’s output, however, shows only
151 rows.

I changed the SELECT statement to output just main.Name (into a file).
Then, I modified my script to output just the Name of each enabled
user (also to a file). And then I diffed the two resulting files. The
SELECT statement yields 16 more users than the Perl script does.

Here’s my guess at what’s happening: I’m using the MergeUsers
extension. The SELECT statement ignores the fact that some of the
users have been merged, and simply grabs them all. The Perl script, by
contrast, attempts to get only those users that have not been merged
into other users. But for some reason it fails to do this correctly,
leaving out more users than it should. Significantly, it leaves out 8
users at the end of the list. This would indicate that it’s getting a
wrong count of the number of non-merged users. Once it reaches the end
of the count, it simply ignores the remaining users, even though they
are non-merged.

It’s quite possible that I have merged 8 users. These 8 are not in the
list generated by the Perl script. In addition to these 8, another 8
(at the end of the alphabet) are also missing. It’s very weird and it
definitely smells like a bug.

Any ideas appreciated.

Nathan

Best regards, Ruslan.

Find sub Next in lib/RT/Extension/MergeUsers.pm, put logging or printing
debug statements there to check what is going on.

OK, I’ll try that. I ran some more tests on my testing RT instance.
Here’s what I just found out:

When no users are merged, the script produces the same output (same
users, same number of users) as the SELECT statement. When I then go
and merge a single user into another user, the number of users
produced by the script decreases by two – the merged user plus
the alphabetically last user.

For example: with no users merged, the SELECT and the script both
return 63 rows (users). After user X is merged into user Y, the script
returns 61 rows (users), instead of 62 that would be expected. The two
missing users are user X and user Z (the last user in the alphabetical
listing produced by SELECT).

In my production instance, I have 8 merged users and the number of
users produced by the script is 16 less than the SELECT listing. In
addition to the 8 merged users, the 8 users at the end of the alphabet
are missing.

So it looks like an “off-by-one” bug.

Nathan

Hi,

Nathan Cutler wrote:

Find sub Next in lib/RT/Extension/MergeUsers.pm, put logging or printing
debug statements there to check what is going on.

OK, I’ll try that. I ran some more tests on my testing RT instance.
Here’s what I just found out:

When no users are merged, the script produces the same output (same
users, same number of users) as the SELECT statement. When I then go
and merge a single user into another user, the number of users
produced by the script decreases by two – the merged user plus
the alphabetically last user.

I can reproduce this on our installation (RT 4.0.17). I suspect it’s also the cause for the problem I reported earlier last week - while logged in as a “merged” user, my user wouldn’t show up as potential owner when modi fying a ticket. It’s not a rights problem in my case since this user can “take” any ticket.

I did try to trace the problem in the code, but I got lost in the vast wilderness of DBIx::SearchBuilder subclasses :wink:

MfG,
Thomas Bätzler
BRINGE Informationstechnik GmbH
Zur Seeplatte 12
D-76228 Karlsruhe
Germany

Fon: +49 721 94246-0
Fon: +49 171 5438457
Fax: +49 721 94246-66
Web: http://www.bringe.de/

Geschäftsführer: Dipl.-Ing. (FH) Martin Bringe
Ust.Id: DE812936645, HRB 108943 Mannheim

Hi,

Nathan Cutler wrote:

Find sub Next in lib/RT/Extension/MergeUsers.pm, put logging or
printing
debug statements there to check what is going on.

OK, I’ll try that. I ran some more tests on my testing RT instance.
Here’s what I just found out:

When no users are merged, the script produces the same output (same
users, same number of users) as the SELECT statement. When I then go
and merge a single user into another user, the number of users
produced by the script decreases by two – the merged user plus
the alphabetically last user.

I can reproduce this on our installation (RT 4.0.17). I suspect it’s also
the cause for the problem I reported earlier last week - while logged in as
a “merged” user, my user wouldn’t show up as potential owner when modi
fying a ticket. It’s not a rights problem in my case since this user can
“take” any ticket.

agree.

I did try to trace the problem in the code, but I got lost in the vast
wilderness of DBIx::SearchBuilder subclasses :wink:

I’m planning to look at it later tonight or tomorrow.

Guys, please send version of DBIx::SearchBuilder, just in case it’s not the
latest.

MfG,
Thomas Bätzler

BRINGE Informationstechnik GmbH
Zur Seeplatte 12
D-76228 Karlsruhe
Germany

Fon: +49 721 94246-0
Fon: +49 171 5438457
Fax: +49 721 94246-66
Web: http://www.bringe.de/

Geschäftsführer: Dipl.-Ing. (FH) Martin Bringe
Ust.Id: DE812936645, HRB 108943 Mannheim

Best regards, Ruslan.

I’m planning to look at it later tonight or tomorrow.

Thanks, Ruslan.

Guys, please send version of DBIx::SearchBuilder, just in case it’s not the
latest.

I’m running 1.63.

Hi guys,

Can you test the following patch?On Thu, Sep 5, 2013 at 12:21 PM, Nathan Cutler presnypreklad@gmail.comwrote:

I’m planning to look at it later tonight or tomorrow.

Thanks, Ruslan.

Guys, please send version of DBIx::SearchBuilder, just in case it’s not
the
latest.

I’m running 1.63.

Best regards, Ruslan.

MergeUsers-dont-skip-records-in-collection.patch (512 Bytes)

Hello Ruslan,

Can you test the following patch?

This seems to fix the problem with Nathan’s script aswell as mine where my user didn’t show up in “modify ticket”.

Thank you for your help!

Cheers,
Thomas

Can you test the following patch?

I’ve applied the patch on both our testing and production instances.
It seems to work! Will keep an eye on it to see if there are any
undesired consequences.

Thanks alot!

Nathan

Hi RT users,

A new version of MergeUsers extension has been released to the CPAN.On Thu, Sep 5, 2013 at 6:54 PM, Nathan Cutler presnypreklad@gmail.comwrote:

Can you test the following patch?

I’ve applied the patch on both our testing and production instances.
It seems to work! Will keep an eye on it to see if there are any
undesired consequences.

Thanks alot!

Best regards, Ruslan.