Filtered "privileged" LDAP Import

[email protected] Wed, 22 Jun 2016 15:10:49 +0200
Newsgroups gmane.comp.bug-tracking.request-tracker.devel
Message-ID <20160622151049.2f8d0638@deepblue>
--MP_/sZW83g++kgpXShTPIsD03jB
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi,

I wanted to import several users via LDAP import. But I only wanted some users to have privileged access.
I came across Joachim Thuau's modifications [1] and ported them to RT 4.4.

Would it be possible to check the attached patch and propably merge it into future releases of RT? 

[1] http://lists.bestpractical.com/pipermail/rt-users/2012-May/076336.html

Thanks a lot!
Kind regards,
Martin
--MP_/sZW83g++kgpXShTPIsD03jB
Content-Type: text/x-patch
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename=0001-Add-support-for-filtered-privileged-LDAP-Import.patch

From 64f73da6ca45cf882b4ce3e89edd476f2534a5c8 Mon Sep 17 00:00:00 2001
From: Martin <[email protected]>
Date: Wed, 22 Jun 2016 13:50:26 +0100
Subject: [PATCH] Add support for filtered privileged LDAP Import

Allows to set only certain users as "privileged" via LDAP filter.
This has been originally implemented by Joachim Thuau:
http://lists.bestpractical.com/pipermail/rt-users/2012-May/076336.html
This patch only adjusts his code changes so that it works with with
RT 4.4.
---
 lib/RT/LDAPImport.pm  | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++
 sbin/rt-ldapimport.in |  4 ++++
 2 files changed, 56 insertions(+)

diff --git a/lib/RT/LDAPImport.pm b/lib/RT/LDAPImport.pm
index 1b00928..b995bf1 100644
--- a/lib/RT/LDAPImport.pm
+++ b/lib/RT/LDAPImport.pm
@@ -1545,6 +1545,58 @@ sub disconnect_ldap {
     return;
 }
 
+sub set_user_as_privileged {
+    my $self = shift;
+    my %args = @_;
+
+    my $user_obj = $self->_load_rt_user(%args);
+    if ($user_obj->Id) {
+        $RT::Logger->debug( "Found user $args{user}{Name} to make privileged." );
+         if ( $args{import} ) {
+             $user_obj->SetPrivileged(1);
+         } else {
+             $RT::Logger->debug( "Would have set user $args{user}{Name} privileged." );
+         }
+    } else {
+        $RT::Logger->debug( "Failed to load user $args{user}{Name}. Skipping.\n" );
+    }
+}
+
+=head2 update_users_to_privileged user import => 0|1
+
+Update users that match the LDAP filter to privileged.
+
+=cut
+
+sub update_users_to_privileged {
+    my $self = shift;
+    my %args = @_;
+
+    my @results = $self->_run_search(
+        base => $RT::LDAPBase,
+        filter => $RT::LDAPPrivilegedFilter
+    );
+
+    my $count = scalar @results;
+    unless ( @results && $count ) {
+        $RT::Logger->debug("No results found, no group import.");
+        $self->disconnect_ldap;
+        return;
+    }
+
+    my $mapping = $RT::LDAPMapping;
+    return unless $self->_check_ldap_mapping( mapping => $mapping );
+
+    my $done = 0;
+    while (my $entry = shift @results) {
+        my $user = $self->_build_object( ldap_entry => $entry, skip => qr/(?i)^CF\./, mapping => $mapping );
+        $self->set_user_as_privileged( user => $user, import => $args{import} );
+        $done++;
+        $RT::Logger->debug("Setup $done/$count users as privileged");
+    }
+    return 1;
+}
+
 RT::Base->_ImportOverlays();
 
 1;
diff --git a/sbin/rt-ldapimport.in b/sbin/rt-ldapimport.in
index 449e7f0..04a5829 100644
--- a/sbin/rt-ldapimport.in
+++ b/sbin/rt-ldapimport.in
@@ -120,6 +120,8 @@ if ($OPT{import}) {
         print "Starting group import\n";
         $importer->import_groups(import => 1);
     }
+    print "Privileged user processing\n";
+    $importer->update_users_to_privileged(import => 1);
     print "Finished import\n";
 } else {
     print <<TESTING;
@@ -132,5 +134,7 @@ TESTING
         print "Testing group import\n";
         $importer->import_groups();
     }
+    print "Testing privileged user selection\n";
+    $importer->update_users_to_privileged();
     print "Finished test\n";
 }
-- 
1.8.3.1


--MP_/sZW83g++kgpXShTPIsD03jB
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

---------
RT 4.4 and RTIR Training Sessions https://bestpractical.com/training
* Los Angeles - September, 2016
--MP_/sZW83g++kgpXShTPIsD03jB--