- # if we're looking for a queue by name, make it a number
- if ( defined $args{'Queue'} && ($args{'Queue'} =~ /\D/ || !$self->ContextObject) ) {
- my $QueueObj = RT::Queue->new( $self->CurrentUser );
- $QueueObj->Load( $args{'Queue'} );
- $args{'Queue'} = $QueueObj->Id;
- $self->SetContextObject( $QueueObj )
- unless $self->ContextObject;
+ if ( defined $args{'Queue'} ) {
+ # Set a LookupType for backcompat, otherwise we'll calculate
+ # one of RT::Queue from your ContextObj. Older code was relying
+ # on us defaulting to RT::Queue-RT::Ticket in old LimitToQueue call.
+ $args{LookupType} ||= 'RT::Queue-RT::Ticket';
+ $args{ObjectId} //= delete $args{Queue};
+ }
+
+ # Default the ObjectType to the top category of the LookupType; it's
+ # what the CFs are assigned on.
+ $args{ObjectType} ||= $1 if $args{LookupType} and $args{LookupType} =~ /^([^-]+)/;
+
+ # Resolve the ObjectId/ObjectType; this is necessary to properly
+ # limit ObjectId, and also possibly useful to set a ContextObj if we
+ # are currently lacking one. It is not strictly necessary if we
+ # have a context object and were passed a numeric ObjectId, but it
+ # cannot hurt to verify its sanity. Skip if we have a false
+ # ObjectId, which means "global", or if we lack an ObjectType
+ if ($args{ObjectId} and $args{ObjectType}) {
+ my ($obj, $ok, $msg);
+ eval {
+ $obj = $args{ObjectType}->new( $self->CurrentUser );
+ ($ok, $msg) = $obj->Load( $args{ObjectId} );
+ };
+
+ if ($ok) {
+ $args{ObjectId} = $obj->id;
+ $self->SetContextObject( $obj )
+ unless $self->ContextObject;
+ } else {
+ $RT::Logger->warning("Failed to load $args{ObjectType} '$args{ObjectId}'");
+ if ($args{IncludeGlobal}) {
+ # Fall back to acting like we were only asked about the
+ # global case
+ $args{ObjectId} = 0;
+ } else {
+ # If they didn't also want global results, there's no
+ # point in searching; abort
+ return wantarray ? (0, $self->loc("Not found")) : 0;
+ }
+ }
+ } elsif (not $args{ObjectType} and $args{ObjectId}) {
+ # If we skipped out on the above due to lack of ObjectType, make
+ # sure we clear out ObjectId of anything lingering
+ $RT::Logger->warning("No LookupType or ObjectType passed; ignoring ObjectId");
+ delete $args{ObjectId};