LookupType of Users CF

Emmanuel Manganneau <[email protected]> Wed, 20 May 2015 14:22:15 +0200
Newsgroups gmane.comp.bug-tracking.request-tracker.devel
Message-ID <[email protected]>
Hi,

new to RT, I had to write a scrip which was to compare the values of 
ticket CFs with users CFs (the CC group of the ticket being filled with 
the people having the same values of the same Cfs).

But doing LimitCustomField on a user could lead to load the wrong CF (ie 
the ticket one, not the user one). So I decided to :
- write a LimitCustomField for Users.pm,
- write a methode LimitCustomFields for Users.pm, to do as many 
LimitCustomField as needed.

I dont know if this hack will suit the requirements of RT's code : so 
please be gentle :-)


-- 
Easter-eggs                              Spécialiste GNU/Linux
44-46 rue de l'Ouest  -  75014 Paris  -  France -  Métro Gaité
Phone: +33 (0) 1 43 35 00 37    -   Fax: +33 (0) 1 43 35 00 76
[email protected]     -   http://www.easter-eggs.com
0001-Force-lookuptype-to-RT-User-when-doing-a-LimitCustom.patch (text/x-patch, 2.9 KB)
From 903292403fe3d892346a311b87cd6eb7414dcd9f Mon Sep 17 00:00:00 2001
From: "E. Manganneau" <[email protected]>
Date: Wed, 20 May 2015 13:51:42 +0200
Subject: [PATCH] Force lookuptype to RT::User when doing a LimitCustomField ;
 adds LimitCustomFields

The call to LimitCustomField in a user environment could possibly lead to the loading
of a Ticket CF (LookupType RT::Queue-RT::Ticket) when two CFs have the same name.

LimitCustomFields : given a hash of CF names associated to their values, performs
successively LimitCustomField on those CFs.
---
 lib/RT/Users.pm | 49 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 3 deletions(-)

diff --git a/lib/RT/Users.pm b/lib/RT/Users.pm
index a8e81eb..00122de 100644
--- a/lib/RT/Users.pm
+++ b/lib/RT/Users.pm
@@ -141,7 +141,7 @@ Only find items that have been deleted.
 
 sub LimitToDeleted {
     my $self = shift;
-    
+
     $self->{'handled_disabled_column'} = $self->{'find_disabled_rows'} = 1;
     $self->Limit(
         ALIAS => $self->PrincipalsAlias,
@@ -525,14 +525,14 @@ sub WhoHaveGroupRight
         }
     }
     $self->_AddSubClause( "WhichObject", "($check_objects)" );
-    
+
     my $group_members = $self->_JoinGroupMembersForGroupRights( %args, ACLAlias => $acl );
     # Find only members of groups that have the right.
     $self->Limit( ALIAS => $acl,
                   FIELD => 'PrincipalType',
                   VALUE => 'Group',
                 );
-    
+
     # no system user
     $self->Limit( ALIAS => $self->PrincipalsAlias,
                   FIELD => 'id',
@@ -672,6 +672,49 @@ sub SimpleSearch {
     return $self;
 }
 
+=head2 LimitCustomField
+
+Overloads LimitCustomField in order to add the LookupType if missing.
+Otherwise, the Custom Field could be of any kind (ie. not necesserly a RT::User type)
+
+=cut
+
+sub LimitCustomField {
+    my $self = shift;
+    my @args = @_;
+    if (! grep /^LookupType$/, @args) {
+        push @args,('LookupType' => 'RT::User');
+    }
+    return $self->SUPER::LimitCustomField(@args);
+}
+
+=head2 LimitCustomFields
+
+Quick search over severall CFs, passed by key => value.
+First arg must be a hash ref, with key being cf name and values expected values.
+
+This method load the CFs to get ids, then performs LimitCustomField successively.
+
+=cut
+
+sub LimitCustomFields {
+    my $self = shift;
+    my $cfs_values = shift;
+    my @args = @_;
+    for my $cf_name (keys %$cfs_values) {
+        my $value = $cfs_values->{$cf_name};
+        my $cf = RT::CustomField->new(RT->SystemUser);
+        my ($ok,$msg) = $cf->LoadByName(Name => $cf_name, LookupType => 'RT::User');
+        return ($ok,$msg) if ! $ok;
+
+        $self->LimitCustomField( CUSTOMFIELD => $cf->Id,
+                                                 VALUE => $value,
+                                                 @args
+            );
+    }
+    return (1,"");
+}
+
 RT::Base->_ImportOverlays();
 
 1;
-- 
2.1.4