[Bps-public-commit] RT-Extension-LDAPImport branch, useful-callbacks, created. 0.34-6-g989cd18

Ruslan Zakirov ruz at bestpractical.com
Wed Feb 13 18:41:50 EST 2013


The branch, useful-callbacks has been created
        at  989cd18f3959669e4148ac6d3339e10310d8e832 (commit)

- Log -----------------------------------------------------------------
commit 0a34f0d155d4d0b1a7a3dea09ba0fb40d0054065
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Feb 14 03:33:12 2013 +0400

    fix number of tests

diff --git a/t/group-rename.t b/t/group-rename.t
index 1cf4f07..483c66b 100644
--- a/t/group-rename.t
+++ b/t/group-rename.t
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 use lib 't/lib';
-use RT::Extension::LDAPImport::Test tests => 66;
+use RT::Extension::LDAPImport::Test tests => undef;
 eval { require Net::LDAP::Server::Test; 1; } or do {
     plan skip_all => 'Unable to test without Net::Server::LDAP::Test';
 };
@@ -114,6 +114,8 @@ ok( $importer->import_users( import => 1 ), 'imported users');
     ok get_group('Test Group 3')->FirstAttribute('LDAPImport-gid-2');
 }
 
+done_testing;
+
 sub is_member_of {
     my $uname = shift;
     my $gname = shift;

commit 989cd18f3959669e4148ac6d3339e10310d8e832
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Feb 14 03:33:58 2013 +0400

    make callbacks (subrefs) in maps useful
    
    How callbacks were called before was completly useless -
    no context and ldap attribute expected not computed value.
    It's hardly backwards incompatible change as there was no
    good reason to use them.
    
    Drop flattening nested arrays in maps. Only one level deep
    arrays are supported now.
    
    New callbacks are called with enough context to change almost
    everything and should return computed value.

diff --git a/lib/RT/Extension/LDAPImport.pm b/lib/RT/Extension/LDAPImport.pm
index 421ebe6..6d4949c 100644
--- a/lib/RT/Extension/LDAPImport.pm
+++ b/lib/RT/Extension/LDAPImport.pm
@@ -584,29 +584,13 @@ C<RT::User::Create> or C<RT::Group::Create>.
 sub _build_object {
     my $self = shift;
     my %args = @_;
-    my $mapping = $args{mapping};
-
-    my $object = {};
-    foreach my $rtfield ( keys %{$mapping} ) {
-        next if $rtfield =~ $args{skip};
-        my $ldap_attribute = $mapping->{$rtfield};
 
-        my @attributes = $self->_parse_ldap_mapping($ldap_attribute);
-        unless (@attributes) {
-            $self->_error("Invalid LDAP mapping for $rtfield ".Dumper($ldap_attribute));
-            next;
-        }
-        my @values;
-        foreach my $attribute (@attributes) {
-            #$self->_debug("fetching value for $attribute and storing it in $rtfield");
-            # otherwise we'll pull 7 alternate names out of the Name field
-            # this may want to be configurable
-            push @values, scalar $args{ldap_entry}->get_value($attribute);
-        }
-        $object->{$rtfield} = join(' ',grep {defined} @values);
+    my $res = $self->_parse_ldap_mapping( %args );
+    foreach my $value ( values %$res ) {
+        @$value = map { ref $_ eq 'ARRAY'? $_->[0] : $_ } @$value;
+        $value = join ' ', grep defined && length, @$value;
     }
-
-    return $object;
+    return $res;
 }
 
 =head3 _parse_ldap_mapping
@@ -629,18 +613,44 @@ together.
 
 sub _parse_ldap_mapping {
     my $self = shift;
-    my $mapping = shift;
-
-    if (ref $mapping eq 'ARRAY') {
-        return map { $self->_parse_ldap_mapping($_) } @$mapping;
-    } elsif (ref $mapping eq 'CODE') {
-        return map { $self->_parse_ldap_mapping($_) } $mapping->()
-    } elsif (ref $mapping) {
-        $self->_error("Invalid type of LDAPMapping [$mapping]");
-        return;
-    } else {
-        return $mapping;
+    my %args = @_;
+
+    my $mapping = $args{mapping};
+
+    my %res;
+    foreach my $rtfield ( keys %$mapping ) {
+        next if $args{'skip'} && $rtfield =~ $args{'skip'};
+        next if $args{'only'} && $rtfield !~ $args{'only'};
+
+        my $ldap_field = $mapping->{$rtfield};
+        my @list = grep defined && length, ref $ldap_field eq 'ARRAY'? @$ldap_field : ($ldap_field);
+        unless (@list) {
+            $self->_error("Invalid LDAP mapping for $rtfield, no defined fields");
+            next;
+        }
+
+        my @values;
+        foreach my $e (@list) {
+            if (ref $e eq 'CODE') {
+                push @values, $e->(
+                    %args,
+                    rt_field => $rtfield,
+                    ldap_field => $ldap_field,
+                    result => \%res,
+                );
+            } elsif (ref $e) {
+                $self->_error("Invalid type of LDAP mapping for $rtfield, value is $e");
+                next;
+            } else {
+                # XXX: get_value asref returns undef if there is no such field on
+                # the entry, should we warn?
+                push @values, grep defined, $args{'ldap_entry'}->get_value( $e, asref => 1 );
+            }
+        }
+        $res{ $rtfield } = \@values;
     }
+
+    return \%res;
 }
 
 =head2 create_rt_user
@@ -799,25 +809,18 @@ sub add_custom_field_value {
     my %args = @_;
     my $user = $args{user};
 
-    foreach my $rtfield ( keys %{$RT::LDAPMapping} ) {
+    my $data = $self->_build_object(
+        %args,
+        only => qr/^CF\.(.+)$/i,
+        mapping => $RT::LDAPMapping,
+    );
+
+    foreach my $rtfield ( keys %$data ) {
         next unless $rtfield =~ /^CF\.(.+)$/i;
         my $cf_name = $1;
-        my $ldap_attribute = $RT::LDAPMapping->{$rtfield};
 
-        my @attributes = $self->_parse_ldap_mapping($ldap_attribute);
-        unless (@attributes) {
-            $self->_error("Invalid LDAP mapping for $rtfield ".Dumper($ldap_attribute));
-            next;
-        }
-        my @values;
-        foreach my $attribute (@attributes) {
-            #$self->_debug("fetching value for $attribute and storing it in $rtfield");
-            # otherwise we'll pull 7 alternate names out of the Name field
-            # this may want to be configurable
-            push @values, scalar $args{ldap_entry}->get_value($attribute);
-        }
-        my $cfv_name = join(' ', at values); 
-        next unless $cfv_name;
+        my $cfv_name = $data->{ $rtfield }
+            or next;
 
         my $cf = RT::CustomField->new($RT::SystemUser);
         my ($status, $msg) = $cf->Load($cf_name);
@@ -866,21 +869,18 @@ sub update_object_custom_field_values {
     my %args = @_;
     my $obj  = $args{object};
 
-    foreach my $rtfield ( keys %{$RT::LDAPMapping} ) {
+    my $data = $self->_build_object(
+        %args,
+        only => qr/^UserCF\.(.+)$/i,
+        mapping => $RT::LDAPMapping,
+    );
+
+    foreach my $rtfield ( keys %$data ) {
         # XXX TODO: accept GroupCF when we call this from group_import too
         next unless $rtfield =~ /^UserCF\.(.+)$/i;
         my $cf_name = $1;
-        my $ldap_attribute = $RT::LDAPMapping->{$rtfield};
-
-        my @attributes = $self->_parse_ldap_mapping($ldap_attribute);
-        unless (@attributes) {
-            $self->_error("Invalid LDAP mapping for $rtfield ".Dumper($ldap_attribute));
-            next;
-        }
-        my $value = join ' ',
-                    grep { defined and length }
-                     map { scalar $args{ldap_entry}->get_value($_) }
-                         @attributes;
+        # XXX TODO: value can not be undefined, but empty string
+        my $value = $data->{$rtfield};
 
         my $current = $obj->FirstCustomFieldValue($cf_name);
 

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list