[rt-users] Need LDAP scrip help: using a switch statement

David Narayan david.narayan at gmail.com
Tue Aug 21 12:07:27 EDT 2007


On 8/20/07, Tim Wilson <twilson at buffalo.k12.mn.us> wrote:
> Hey everyone,
>
> 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.
>
> I've created a new scrip called "Set Building CF from LDAP on Create" with Condition "On Create" and Action "User Defined". Here's my custom action preparation code:
>
> my $email = ($self->TicketObj->RequestorAddresses)[0];
> my $ldap = Net::LDAP->new( 'my.ldap.server );
> $ldap->bind;
> my $msg = $ldap->search( base   => 'ou=Staff,o=My-ldap-org',
>                          filter => '(email=$email)',
>                        );
> my $entry = $msg->entry(0);
> my $ou = $entry->get_value('ou');
>
> # Fix up the CF names based on LDAP results
> switch ($ou) {
>     case "DISTRICT SERVICES"  { $building = "DO" }
>     case "HIGH SCHOOL"        { $building = "HS" }
>     case "JEFFERSON"          { $building = "JES" }
>     else                      { $building = "DO" }
> };
>
> my $cf = RT::CustomField->new( $RT::SystemUser );
> $cf->LoadByName( Name => 'Building' );
> $self->TicketObj->AddCustomFieldValue( Field => $cf, Value => $building );
>
> return 1;
>
> I don't have anything in the Custom action cleanup code box.
>
> I'm trying to use a switch because the field in our LDAP records doesn't match the custom field text in RT. Rather than changing everything in the LDAP, I was hoping to make the translation here in the scrip action.
>
> Anyone see a problem with this scrip? It doesn't seem to break anything, but neither does the custom field get set when a new ticket comes in.
>
> Running RT 3.6.3 on Ubuntu with MySQL 5.0.22 and apache-ssl 1.3.34
>
> -Tim
>
>

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_};

This essentially does the same thing as your switch statement but it
doesn't require an additional module.

-David



More information about the rt-users mailing list