Search tickets for current user

Hi. I’d like to write a portlet to display all tickets the logged-in user can see, either as the owner/ requestor or CC. The user might be a member of a group with right to see the tickets in other queues that the user can’t see.
I use the following code but it always returns the whole queues:

use RT::Interface::CLI qw( GetCurrentUser );

my $Qs = RT::Queues->new($RT::SystemUser);
my $user = GetCurrentUser();
$Qs->FindAllRows;
$Qs->UnLimit;

my $Query;
my @QNames;

while ( my $Q = $Qs->Next ) {
if ($Q->HasRight(‘ShowTicket’, $user->Id) && $Q->HasRight(‘SeeQueue’, $user->Id )) {
push @QNames, $Q->Name;
}
}
Can you please help me?

Is this portlet in the RT interface? You could use a saved search with the ‘CurrentUser’ value for the search parameters if so

Yes it is in the web interface.

I want the portlet to be used for specific users, so that when they click “RT at a glance” see not only tickets they own or requested but the tickets they are CC.

Is it possible to use saved search for this purpose?

Yes you can use ‘CurrentUser’ in the search

You mean instead of “$RT::SystemUser”?

If you go to the RT search interface and use the advanced tab you can search Owner = '__CurrentUser__' and then save that search as a saved search and put that saved search on your RT at a glance page as a portlet

Thanks! How can I share the saved search with other users? I have created a saved search for an RT admin but I can’t see it when I log in as another user.

You can add your user to a group and then save the saved search the the groups privacy

Thank you so much. I did what you suggested with some changes and it worked, however it’s too slow when the number queues is big.
In fact I used:
Cc.Name =‘CurrentUser’ OR QueueCc.Name =‘CurrentUser
because I wanted to search for tickets whose CC or QueueCc is the current user.

Is there any way to make it faster? Would it run faster if I create a portlet as a script rather than a saved search?

Thank you so much. I did what you suggested with some changes and it worked, however it’s too slow when the number queues is big.

Do you mean you are searching across many queues in the query or you have a lot of queues in general?

Cc.Name =‘ CurrentUser ’ OR QueueCc.Name =‘ CurrentUser
because I wanted to search for tickets whose CC or QueueCc is the current user.
Is there any way to make it faster? Would it run faster if I create a portlet as a script rather than a saved search?

The best way to speed up the queries is most likely to tighten them down more, If you write your own portlet it would still end up doing the same searches right?

Cc.Name =‘ CurrentUser ’ OR QueueCc.Name =‘ CurrentUser

If you do a search for Cc at the queue level won’t that just return all tickets in that queue?

I assume that the query builder does search all tickets the current user has access to, so they might be in the queues which the user can see or in other queues that is potentially all queues in general. right?

It would depend of how I do the search, correct? For example I can use FromSQL or Limit. Aren’t they different?

As I mentioned above my assumption is the query is run against multiple queues. In fact the result would be different if I use Cc.Name = ‘CurrentUser

You can try and make a more efficient query but I believe the SQL will end up being about the same as whatever the query builder spits out. You could try adding more limits to the number of tickets coming back by only showing tickets with “Active” statuses

The problem is if I add “Active” status then I won’t see some of the tickets I’m looking for although they are “open”! It’s so weird.

Many thanks any way.

If you can share the query and change any names you don’t want to show we can get a better idea of any optimizations that can be made.

Thanks a lot. I found out my mistake. I had a wrong assumption and now it’s resolved.