[rt-devel] need help to write a searchbuilder query for RT 3

Michael van Elst mlelstv at dev.de.cw.net
Mon Aug 18 10:28:07 EDT 2003


On Mon, Aug 18, 2003, Dirk Pape wrote:

> >given a User, I want to have a list of all tickets, which have no owner
> >and are in a queue, where the given user has Right "OwnTicket".
> >
> >Since this needs Limit and Join, this will hopefully help me to
> >understand the systematics to invent further searches by by own.

There are two issues.

- Joining requires knowledge of the database structure as the RT Classes
  do not support joins.
- Evaluating a right requires more than a table join, I doubt that you
  can create a single database query to evaluate a right. But maybe
  Jesse knows some magic trick :)

So I suggest to do one database query to find all tickets that belong
to a given queue and that have no owner (== are owned by the Nobody user).

Then, for each ticket you check if the given user has the right to own
the ticket.

Here is an example script:

use RT;
use RT::User;
use RT::Queue;
use RT::Tickets;

# hiding in dark places
$RT::LogDir = '/tmp';
$RT::LogToFileNamed = '/dev/null';

RT::LoadConfig();
RT::Init();

# operate as superuser
my $cuser = $RT::SystemUser;

# create a user object and load it with data for the given user name
my $nuser = new RT::User($cuser);
$nuser->Load('given-user-name');

# create a queue object and load it with data for the given queue name
my $q = new RT::Queue($cuser);
$q->Load('given-queue-name');

# create tickets collection object
my $ts = new RT::Tickets($cuser);

# select tickets that belong to nobody and belong to the given queue
$ts->LimitOwner( VALUE => $RT::Nobody->Id );
$ts->LimitQueue( VALUE => $q->Id );

# fetch results
my $items = $ts->ItemsArrayRef || [];

foreach my $t (@$items) {
    # verify ticket rights
    if ($t->HasRight( Principal => $nuser,
                      Right => 'OwnTicket' )) {
        print $t->Id," YEAH\n";
    } else {
        print $t->Id," NEY\n";
    }
}


Greetings,
-- 
    ,eM""=.            a"-.                         Michael van Elst
   dWWMWM" -          :GM==;                        mlelstv at dev.de.cw.net
  :WWMWMw=--.          "W='  cable & wireless
   9WWMm==-.
    "-Wmw-"  CABLE & WIRELESS



More information about the Rt-devel mailing list