[rt-users] Need LDAP scrip help: using a switch statement
David Narayan
david.narayan at gmail.com
Wed Aug 22 16:34:16 EDT 2007
On 8/21/07, Tim Wilson <twilson at buffalo.k12.mn.us> wrote:
> >>> On Tue, Aug 21, 2007 at 11:07 AM, in message
> <806cdffb0708210907h4fc4912em8e3e56c4432a985e at mail.gmail.com>, "David Narayan"
> <david.narayan at gmail.com> wrote:
> > On 8/20/07, Tim Wilson <twilson at buffalo.k12.mn.us> wrote:
>
> >> I'm trying to adapt the "set custom field with LDAP query" scrip example
> >> from the RT book, and I'm stuck. I'm hoping someone has a suggestion. The
> >> main problem, I'm afraid, is a lack of perl experience.
>
> > Instead of using switch, try using a hash lookup. For example:
> >
> > my $ou = $entry- >get_value('ou');
> >
> > my %building_for = (
> > 'DISTRICT SERVICES' => 'DO',
> > 'HIGH SCHOOL' => 'HS',
> > 'JEFFERSON' => 'JES',
> > _DEFAULT_ => 'DO',
> > );
> >
> > # Lookup building. If there was no LDAP entry, use the default
> > my $building = $building_for{$ou} || $building_for{_DEFAULT_};
>
> David,
>
> Thanks for the suggestion. That approach seems very straightforward. Unfortunately, RT logs the following when the scrip is executed:
>
> Aug 21 16:08:35 support RT: About to think about scrips for transaction #7940
> Aug 21 16:08:36 support RT: Scrip 31 Commit failed: Can't call method "get_value" on an undefined value at (eval 1139) line 11. Stack: [(eval 1139):11] [/usr/local/rt3/lib/RT/ScripAction_Overlay.pm:240] [/usr/local/rt3/lib/RT/Scrip_Overlay.pm:505] [/usr/local/rt3/lib/RT/Scrips_Overlay.pm:193] [/usr/local/rt3/lib/RT/Transaction_Overlay.pm:179] [/usr/local/rt3/lib/RT/Record.pm:1444] [/usr/local/rt3/lib/RT/Ticket_Overlay.pm:744] [/usr/local/rt3/lib/RT/Interface/Email.pm:719] [/usr/local/rt3/share/html/REST/1.0/NoAuth/mail-gateway:59] (/usr/local/rt3/lib/RT/Action/UserDefined.pm:81)
>
> Here's what I have my custom action cleanup code set to now (anonymized):
>
> my $mail = ($self->TicketObj->RequestorAddresses)[0];
> my $ldap = Net::LDAP->new( 'my.ldap.server' );
> $ldap->bind;
>
> # Do the LDAP search. Note: Our eDirectory server uses "mail" for the
> # user's email address.
> my $msg = $ldap->search( base => 'o=myOrg',
> filter => '(mail=$mail)',
> );
> my $entry = $msg->entry(0);
> my $ou = $entry->get_value('ou');
>
> # Fix up the CF values based on LDAP results
> my %building_for = (
> 'DISTRICT SERVICES' => 'DO',
> 'HIGH SCHOOL' => 'HS',
> 'JEFFERSON' => 'JES',
> '_DEFAULT_' => 'DO',
> );
> my $building = $building_for{$ou} || $building_for{_DEFAULT_};
>
> my $cf = RT::CustomField->new( $RT::SystemUser );
> $cf->LoadByName( Name => 'Building' );
> $self->TicketObj->AddCustomFieldValue( Field => $cf, Value => $building );
>
> return 1;
>
> It looks like the LDAP search is failing. I get this in my eDirectory logs:
>
> (10.1.10.224:59620) (0x002b:0x63) Search request:
> base: "o=myOrg"
> scope: 2 dereference:2 sizelimit:0 timelimit:0 attrsonly:0
> filter: "(mail=$mail)"
> no attributes
>
> So it appears that the contents of the $mail variable is not getting put into that LDAP search. If I hardcode the filter in the scrip with my email address then it works perfectly. The LDAP is retrieved, and the custom field gets set properly.
>
> I took most of this code right from the RT book, and I don't understand why $mail isn't getting sent in the LDAP search. So I tried one more thing. I changed to the code to the following, creating the $filter variable in whole before passing it to the LDAP search:
>
> my $filter = "(mail=$mail)";
> my $msg = $ldap->search( base => 'o=myOrg',
> filter => $filter,
> );
>
> This worked! Now... why?
>
In the first case you're using '(mail = $mail)' -- with single quotes.
The single quotes prevent $mail from being interpolated. To make sure
$mail is interpolated correctly, use double quotes (as you're doing in
the second case when you use the separate $filter variable):
my $msg = $ldap->search( base => 'o=myOrg',
filter => "(mail=$mail)",
);
-David
More information about the rt-users
mailing list