[LON-CAPA-cvs] cvs: modules /msu localenroll.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Fri, 16 May 2008 23:41:51 -0000


This is a MIME encoded message

--raeburn1210981311
Content-Type: text/plain

raeburn		Fri May 16 19:41:51 2008 EDT

  Modified files:              
    /modules/msu	localenroll.pm 
  Log:
  Bug 5598.
  - User search at MSU now queries LDAP server maintained by ATS.
  - Will find Visting Instructors, Lifelong Learners etc. who are absent from Faculty, Staff, Student and Affiliate database tables maintained by AIS/HR. 
  
  
--raeburn1210981311
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20080516194151.txt"

Index: modules/msu/localenroll.pm
diff -u modules/msu/localenroll.pm:1.35 modules/msu/localenroll.pm:1.36
--- modules/msu/localenroll.pm:1.35	Thu May 15 13:50:16 2008
+++ modules/msu/localenroll.pm	Fri May 16 19:41:49 2008
@@ -1,6 +1,6 @@
 # functions to glue school database system into Lon-CAPA for
 # automated enrollment
-# $Id: localenroll.pm,v 1.35 2008/05/15 17:50:16 raeburn Exp $
+# $Id: localenroll.pm,v 1.36 2008/05/16 23:41:49 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -31,6 +31,7 @@
 use LONCAPA::Configuration;
 use Apache::lonnet;
 use localstudentphoto;
+use Net::LDAP;
 
 sub run () {
     my $dom = shift;
@@ -489,7 +490,7 @@
     if ($dbflag) {
         my @srchtables = ('FACULTY_VU','STAFF_VU','STUDENT','AFFILIATE',
                           'ASSISTANT','STUDENT_AFFILIATE');
-        &query_user_tables($dbh,\@srchtables,$instusers,$instids);
+        &query_user_tables($dbflag,$dbh,\@srchtables,$instusers,$instids);
         $outcome = 'ok';
         &disconnect_DB($dbh);
     }
@@ -508,158 +509,242 @@
                   StaffAff => 'AFFILIATE',
                   StuAff => 'STUDENT_AFFILIATE');
     my ($dbh,$dbflag) = &connect_DB('HR');
-    if ($dbflag) {
-        $outcome = 'ok';
-        foreach my $type (@{$types}) {
-            if (exists($tables{$type})) {
-                push(@srchtables,$tables{$type});           
-            }
-        }
-        if (@srchtables == 0) {
-            foreach my $type (keys(%tables)) {
-                push(@srchtables,$tables{$type});
-            }
+    foreach my $type (@{$types}) {
+        if (exists($tables{$type})) {
+            push(@srchtables,$tables{$type});           
         }
-        if ($srchby eq '' && $srchterm eq '') {
-            if ($uname ne '') {
-                $srchby = 'uname';
-                $srchterm = $uname;
-            } elsif ($id ne '') {
-                $srchby = 'id';
-                $srchterm = $id;
-            }
+    }
+    if (@srchtables == 0) {
+        foreach my $type (keys(%tables)) {
+            push(@srchtables,$tables{$type});
         }
-        if ($srchterm ne '') {
-            &query_user_tables($dbh,\@srchtables,$instusers,$instids,
-                               $srchby,$srchterm,$srchtype);
+    }
+    if ($srchby eq '' && $srchterm eq '') {
+        if ($uname ne '') {
+            $srchby = 'uname';
+            $srchterm = $uname;
+        } elsif ($id ne '') {
+            $srchby = 'id';
+            $srchterm = $id;
         }
+    }
+    if ($srchterm ne '') {
+        $outcome = &query_user_tables($dbflag,$dbh,\@srchtables,$instusers,$instids,
+                                      $srchby,$srchterm,$srchtype,$types);
+    }
+    if ($dbflag) {
         &disconnect_DB($dbh);
     }
-    return $outcome; 
+    return $outcome;
 }
 
 sub query_user_tables {
-    my ($dbh,$srchtables,$instusers,$instids,$srchby,$srchterm,$srchtype) = @_;
-    my ($condition,%multipids);
+    my ($dbflag,$dbh,$srchtables,$instusers,$instids,$srchby,$srchterm,$srchtype,$types) = @_;
+    my ($outcome,$condition,%multipids,$ldapfilter);
     if ($srchby eq 'uname') {
         if ($srchterm =~ /^\w{2,8}$/) {
             if ($srchtype eq 'contains') {
                 $condition = "WHERE MSUNetID LIKE '%$srchterm%'";
+                $ldapfilter = '(uid=*'.$srchterm.'*)';
             } elsif ($srchtype eq 'begins') {
                 $condition = "WHERE MSUNetID LIKE '$srchterm%'";
+                $ldapfilter = '(uid='.$srchterm.'*)';
             } else {
                 $condition = "WHERE MSUNetID = '$srchterm'";
+                $ldapfilter = '(uid='.$srchterm.')';
             }
         }
     } elsif ($srchby eq 'lastname') {
-        if ($srchterm =~ /[A-Za-z\-\.']+/) {
+        if ($srchterm =~ /[A-Za-z\-\.'\s]+/) {
             if ($srchtype eq 'contains') {
-                my $quoted_last = $dbh->quote('%'.$srchterm.'%');
-                $condition = "WHERE LastName LIKE $quoted_last";
+                if ($dbflag) {
+                    my $quoted_last = $dbh->quote('%'.$srchterm.'%');
+                    $condition = "WHERE LastName LIKE $quoted_last";
+                }
+                $ldapfilter = '(sn=*'.$srchterm.'*)';
             } elsif ($srchtype eq 'begins') {
-                my $quoted_last = $dbh->quote($srchterm.'%');
-                $condition = "WHERE LastName LIKE $quoted_last";
+                if ($dbflag) {
+                    my $quoted_last = $dbh->quote($srchterm.'%');
+                    $condition = "WHERE LastName LIKE $quoted_last";
+                }
+                $ldapfilter = '(sn='.$srchterm.'*)';
             } else {
-                my $quoted_last = $dbh->quote($srchterm);
-                $condition = "WHERE LastName = $quoted_last";
+                if ($dbflag) { 
+                    my $quoted_last = $dbh->quote($srchterm);
+                    $condition = "WHERE LastName = $quoted_last";
+                }
+                $ldapfilter = '(sn='.$srchterm.')';
             }
         }
     } elsif ($srchby eq 'lastfirst') {
         my ($srchlast,$srchfirst) = split(/,/,$srchterm);
         $srchlast =~ s/\s+$//;
         $srchfirst =~ s/^\s+//;
-        if (($srchlast =~ /[A-Za-z\-\.']+/) && 
-            ($srchfirst  =~ /[A-Za-z\-\.']+/)) { 
+        if (($srchlast =~ /[A-Za-z\-\.'\s]+/) &&
+            ($srchfirst  =~ /[A-Za-z\-\.'\s]+/)) {
             my ($quoted_first,$quoted_last);
             if ($srchtype eq 'contains') {
-                $quoted_last = $dbh->quote('%'.$srchlast.'%');
-                $quoted_first = $dbh->quote('%'.$srchfirst.'%');
-                $condition = "WHERE ( LastName LIKE $quoted_last AND ".
-                                     "FirstName LIKE $quoted_first )";
-
+                if ($dbflag) {
+                    $quoted_last = $dbh->quote('%'.$srchlast.'%');
+                    $quoted_first = $dbh->quote('%'.$srchfirst.'%');
+                    $condition = "WHERE ( LastName LIKE $quoted_last AND ".
+                                         "FirstName LIKE $quoted_first )";
+                }
+                $ldapfilter = '(&(sn=*'.$srchlast.'*)(givenName=*'.$srchfirst.'*))';
             } elsif ($srchtype eq 'begins') {
-                $quoted_last = $dbh->quote($srchlast.'%');
-                $quoted_first = $dbh->quote($srchfirst.'%');
-                $condition = "WHERE ( LastName LIKE $quoted_last AND ".
-                                    "FirstName LIKE $quoted_first )";
-
+                if ($dbflag) {
+                    $quoted_last = $dbh->quote($srchlast.'%');
+                    $quoted_first = $dbh->quote($srchfirst.'%');
+                    $condition = "WHERE ( LastName LIKE $quoted_last AND ".
+                                        "FirstName LIKE $quoted_first )";
+                }
+                $ldapfilter = '(&(sn='.$srchlast.'*)(givenName='.$srchfirst.'*))';
             } else {
-                $quoted_last = $dbh->quote($srchterm);
-                $quoted_first = $dbh->quote($srchterm);
-                $condition = "WHERE ( LastName = $quoted_last AND ".
-                                    "FirstName = $quoted_first )";
+                if ($dbflag) {
+                    $quoted_last = $dbh->quote($srchterm);
+                    $quoted_first = $dbh->quote($srchterm);
+                    $condition = "WHERE ( LastName = $quoted_last AND ".
+                                        "FirstName = $quoted_first )";
+                }
+                $ldapfilter = '(&(sn='.$srchlast.')(givenName='.$srchfirst.'))';
             }
         }
     } elsif ($srchby eq 'id') {
-        if ($srchterm =~ /^[AZ]\d{8}$/) {
-            $condition = "WHERE Pid = '$srchterm'";
+        if ($dbflag) {
+            if ($srchterm =~ /^[AZ]\d{8}$/) {
+                $condition = "WHERE Pid = '$srchterm'";
+            }
         }
     }
-    
-    if ($srchby && !$condition) {
-	return;
-    }
-
-    foreach my $table (@{$srchtables}) {
-        my $statement = "SELECT MSUNetID,Pid,FirstName,LastName,Person_Type FROM $table $condition";
-        my $sth = $dbh->prepare("$statement");
-        $sth->execute();
-        while ( my($uname,$pid,$first,$last,$type) = $sth->fetchrow_array ) {
-            $pid=lc($pid);
-            if (ref($instusers->{$uname}) eq 'HASH') {
-                if (ref($instusers->{$uname}{'inststatus'}) eq 'ARRAY') {
-                    if (!grep(/^$type$/,@{$instusers->{$uname}{'inststatus'}})) {
-                        push(@{$instusers->{$uname}{'inststatus'}},$type);
+    if ($dbflag) {
+        foreach my $table (@{$srchtables}) {
+            next if ($srchby && $condition eq '');   
+            my $statement = "SELECT MSUNetID,Pid,FirstName,LastName,Person_Type FROM $table $condition";
+            my $sth = $dbh->prepare("$statement");
+            $sth->execute();
+            while ( my($uname,$pid,$first,$last,$type) = $sth->fetchrow_array ) {
+                $pid=lc($pid);
+                if (ref($instusers->{$uname}) eq 'HASH') {
+                    if (ref($instusers->{$uname}{'inststatus'}) eq 'ARRAY') {
+                        if (!grep(/^$type$/,@{$instusers->{$uname}{'inststatus'}})) {
+                            push(@{$instusers->{$uname}{'inststatus'}},$type);
+                        }
                     }
-                }
-                if ($pid ne $instusers->{$uname}{'id'}) {
-                    if ($instusers->{$uname}{'id'} =~ /^A\d{8}$/) {
-                        if ($pid =~ /^A\d{8}$/) {
-                            if (ref($multipids{$uname}) eq 'ARRAY') {
-                                if (!grep(/^$pid$/,@{$multipids{$uname}})) {
-                                    push(@{$multipids{$uname}},$pid);
+                    if ($pid ne $instusers->{$uname}{'id'}) {
+                        if ($instusers->{$uname}{'id'} =~ /^A\d{8}$/) {
+                            if ($pid =~ /^A\d{8}$/) {
+                                if (ref($multipids{$uname}) eq 'ARRAY') {
+                                    if (!grep(/^$pid$/,@{$multipids{$uname}})) {
+                                        push(@{$multipids{$uname}},$pid);
+                                    }
+                                } else {
+                                    @{$multipids{$uname}} = ($instusers->{$uname}{'id'},$pid);
                                 }
-                            } else {
-                                @{$multipids{$uname}} = ($instusers->{$uname}{'id'},$pid);
+                                $instusers->{$uname}{'id'} = $pid;
                             }
-                            $instusers->{$uname}{'id'} = $pid;
-                        }
-                    } elsif ($instusers->{$uname}{'id'} =~ /^Z\d{8}$/) {
-                        if ($pid =~ /^Z\d{8}$/) {
-                            if (ref($multipids{$uname}) eq 'ARRAY') {
-                                if (!grep(/^$pid$/,@{$multipids{$uname}})) {
-                                    push(@{$multipids{$uname}},$pid);
+                        } elsif ($instusers->{$uname}{'id'} =~ /^Z\d{8}$/) {
+                            if ($pid =~ /^Z\d{8}$/) {
+                                if (ref($multipids{$uname}) eq 'ARRAY') {
+                                    if (!grep(/^$pid$/,@{$multipids{$uname}})) {
+                                        push(@{$multipids{$uname}},$pid);
+                                    }
+                                } else {
+                                    @{$multipids{$uname}} = ($instusers->{$uname}{'id'},$pid);
                                 }
-                            } else {
-                                @{$multipids{$uname}} = ($instusers->{$uname}{'id'},$pid);
+                            } elsif ($pid =~ /^A\d{8}$/) {
+                                $instusers->{$uname}{'id'} = $pid;
                             }
-                        } elsif ($pid =~ /^A\d{8}$/) {
-                            $instusers->{$uname}{'id'} = $pid;
                         }
                     }
+                } else {
+                    $instusers->{$uname} = {firstname => $first,
+                                           lastname => $last,
+                                           id => $pid,
+                                           permanentemail => $uname.'@msu.edu',
+                                           inststatus => [$type],
+                                         };
+                }
+                if (defined($instids->{$pid})) {
+                    if (ref($instids->{$pid}) eq 'ARRAY') {
+                        if (!grep(/^$uname$/,@{$instids->{$pid}})) {
+                            push(@{$instids->{$pid}},$uname);
+                        }
+                    } elsif ($instids->{$pid} ne $uname) {
+                        @{$instids->{$pid}} = ($instids->{$pid},$uname);
+                    }
+                } else {
+                    $instids->{$pid} = $uname;
                 }
-            } else {
-                $instusers->{$uname} = {firstname => $first,
-                                       lastname => $last,
-                                       id => $pid,
-                                       permanentemail => $uname.'@msu.edu',
-                                       inststatus => [$type],
-                                     };
             }
-            if (defined($instids->{$pid})) {
-                if (ref($instids->{$pid}) eq 'ARRAY') {
-                    if (!grep(/^$uname$/,@{$instids->{$pid}})) {
-                        push(@{$instids->{$pid}},$uname);
+            $outcome = 'ok';
+        }
+    }
+    if ($ldapfilter ne '') {
+        my $ldapres = &ldap_search($ldapfilter,$instusers,$instids,$srchby,
+                                   $srchterm,$srchtype,$types);
+        if (!$dbflag) {
+            $outcome = $ldapres;
+        }
+    }
+    return $outcome;
+}
+
+sub ldap_search {
+    my ($ldapfilter,$instusers,$instids,$srchby,$srchterm,$srchtype,$types) = @_;
+    my $outcome;
+    my $ldap = Net::LDAP->new( 'ldap.msu.edu' );
+    if ($ldap) {
+        $ldap->bind;
+        my $mesg = $ldap->search(
+                            base   => "dc=msu, dc=edu",
+                            filter => $ldapfilter,
+                            attrs  => ['sn','givenName','title','uid','mail','employeeType'],
+                          );
+        if ($mesg->code) {
+            $ldap->unbind;
+            return;
+        } else {
+            $outcome = 'ok';
+        }
+        foreach my $entry ($mesg->entries) {
+            my $uname = $entry->get_value('uid');
+            next if ($uname eq '');
+            my $first = $entry->get_value('givenName');
+            my $last = $entry->get_value('sn');
+            my $email = $entry->get_value('mail');
+            my $type;
+            if (($entry->get_value('employeeType') eq 'Faculty') ||
+                ($entry->get_value('employeeType') eq 'Staff')) {
+                $type = $entry->get_value('employeeType');
+            } elsif ($entry->get_value('title') eq 'Student') {
+                $type = $entry->get_value('title');
+            }
+            if (ref($types) eq 'ARRAY') {
+                if (@{$types} > 0) {
+                    if (($type ne '') && !(grep(/^$type$/,@{$types}))) {
+                        next if (!grep(/^default$/,@{$types}));
+                    }
+                    next if (($type eq '') && (!grep(/^default$/,@{$types})));
+                }
+            }
+            if (ref($instusers->{$uname}) eq 'HASH') {
+                if (ref($instusers->{$uname}{'inststatus'}) eq 'ARRAY') {
+                    if (!grep(/^$type$/,@{$instusers->{$uname}{'inststatus'}})) {
+                        push(@{$instusers->{$uname}{'inststatus'}},$type);
                     }
-                } elsif ($instids->{$pid} ne $uname) {
-                    @{$instids->{$pid}} = ($instids->{$pid},$uname);
                 }
             } else {
-                $instids->{$pid} = $uname;
+                $instusers->{$uname} = {firstname => $first,
+                                        lastname => $last,
+                                        id => '',
+                                        permanentemail => $email,
+                                        inststatus => [$type],
+                                     };
             }
         }
+        $ldap->unbind;
     }
-    return;
+    return $outcome;
 }
 
 sub inst_usertypes {

--raeburn1210981311--