hopefully, finally fix ticket auto-association not getting along with RT ACLs
[freeside.git] / rt / lib / RT / Ticket_Overlay.pm
index 1916bc4..a51cd16 100644 (file)
@@ -721,13 +721,38 @@ sub Create {
     #unless we already have (a) customer(s)...
     unless ( $self->Customers->Count ) {
 
+      #first find any requestors with emails but *without* customer targets
+      my @NoCust_Requestors =
+        grep { $_->EmailAddress && ! $_->Customers->Count }
+             @{ $self->_Requestors->UserMembersObj->ItemsArrayRef };
+
+      for my $Requestor (@NoCust_Requestors) {
+
+         #perhaps the stuff in here should be in a User method??
+         my @Customers =
+           &RT::URI::freeside::email_search( email=>$Requestor->EmailAddress );
+
+         foreach my $custnum ( map $_->{'custnum'}, @Customers ) {
+
+           ## false laziness w/RT/Interface/Web_Vendor.pm
+           my @link = ( 'Type'   => 'MemberOf',
+                        'Target' => "freeside://freeside/cust_main/$custnum",
+                      );
+
+           my( $val, $msg ) = $Requestor->_AddLink(@link);
+           #XXX should do something with $msg# push @non_fatal_errors, $msg;
+
+         }
+
+      }
+
       #find any requestors with customer targets
   
       my %cust_target = ();
   
       my @Requestors =
         grep { $_->Customers->Count }
-             @{ $self->Requestors->UserMembersObj->ItemsArrayRef };
+             @{ $self->_Requestors->UserMembersObj->ItemsArrayRef };
   
       foreach my $Requestor ( @Requestors ) {
         foreach my $cust_link ( @{ $Requestor->Customers->ItemsArrayRef } ) {
@@ -744,7 +769,7 @@ sub Create {
                      'Target' => $cust_target,
                    );
   
-        my( $val, $msg ) = $self->AddLink(@link);
+        my( $val, $msg ) = $self->_AddLink(@link);
         push @non_fatal_errors, $msg;
   
       }
@@ -1786,6 +1811,25 @@ sub Requestors {
 
 # }}}
 
+# {{{ sub _Requestors
+
+=head2 _Requestors
+
+Private non-ACLed variant of Reqeustors so that we can look them up for the
+purposes of customer auto-association during create.
+
+=cut
+
+sub _Requestors {
+    my $self = shift:
+
+    my $group = RT::Group->new($RT::SystemUser);
+    $group->LoadTicketRoleGroup(Type => 'Requestor', Ticket => $self->Id);
+    return ($group);
+}
+
+% }}}
+
 # {{{ sub Cc
 
 =head2 Cc
@@ -2510,7 +2554,13 @@ sub _Links {
 
     unless ( $self->{"$field$type"} ) {
         $self->{"$field$type"} = new RT::Links( $self->CurrentUser );
-        if ( $self->CurrentUserHasRight('ShowTicket') ) {
+
+        #not sure what this ACL was supposed to do... but returning the
+        # bare (unlimited) RT::Links certainly seems wrong, it causes the
+        # $Ticket->Customers method during creation to return results for every
+        # ticket...
+        #if ( $self->CurrentUserHasRight('ShowTicket') ) {
+
             # Maybe this ticket is a merged ticket
             my $Tickets = new RT::Tickets( $self->CurrentUser );
             # at least to myself
@@ -2527,7 +2577,7 @@ sub _Links {
             $self->{"$field$type"}->Limit( FIELD => 'Type',
                                            VALUE => $type )
               if ($type);
-        }
+        #}
     }
     return ( $self->{"$field$type"} );
 }