Skip to content

Instantly share code, notes, and snippets.

@kevinoconnor7
Created August 14, 2013 20:44
Show Gist options
  • Save kevinoconnor7/6235387 to your computer and use it in GitHub Desktop.
Save kevinoconnor7/6235387 to your computer and use it in GitHub Desktop.
Map a user's OU to RT queue
# LDAP address
my $ldap_address = ""
# LDAP base DN path
my $ldap_base_dn = ""
# LDAP user to connect with
my $ldap_user_path = ""
# LDAP user password to connect with
my $ldap_user_password = ""
# What OU level to look at
my $OU_level = 1
# Map first OU to queue name
my %ou_map = (
'Accounting' => "NY Support",
'Marketing' => "NY Support",
'Engineering' => "CA Support",
);
# DO NOT EDIT BELOW THIS LINE
# Get the e-mail address of the requester
my $email = ($self->TicketObj->RequestorAddresses)[0];
# Make a connection to LDAP and bind
my $ldap = Net::LDAP->new( $ldap_address );
my $ldapMsg = $ldap->bind(
$ldap_user_path,
password => $ldap_user_password
);
# Stop if there's an error
if($ldapMsg->is_error)
{
$RT::Logger->error("Error connecting to LDAP.");
return 0;
}
# Setup what attributes we want and do the search
my $attrs = ['cn', 'mail'];
my $ldapResults = $ldap->search (
base => $ldap_base_dn,
scope => "sub",
filter => "mail=$email",
attrs => $attrs
);
# Stop if there's an error
if($ldapResults->code)
{
$RT::Logger->error("Error searching for user");
return 0;
}
# Stop if we can't find the user
if( (!$ldapResults) || ($ldapResults->count == 0) )
{
$RT::Logger->info("No results found for user.");
return 0;
}
# Break out resulting data
my $entry = $ldapResults->entry(0);
# Get the dn of the user and then find the highest (most near base) OU the user is in
# plus the level we want to look at
my $dn = $entry->dn;
my @dnparts = split(',', $dn);
my $part = scalar(@dnparts)-scalar(split(',', $ldap_base_dn)) + $OU_level;
my $ou = substr($dnparts[$part], 3);
# Loop and try to match the user's OU to the map
foreach my $ouKey (keys %ou_map ){
if($ou eq $ouKey) {
# OU matches - move to the right queue
$RT::Logger->info("Matched user OU of $ou to queue $ou_map{$ouKey}");
my ($status, $msg) = $self->TicketObj->SetQueue($ou_map{$ouKey});
unless ( $status ) {
$RT::Logger->error("Coudln't change queue: $msg");
return 0;
}
return 1;
}
}
$RT::Logger->info("No matching queue found for user.");
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment