Project

General

Profile

« Previous | Next » 

Revision 8408

Added by Jing Tao over 10 years ago

Add a new method to get the next avaliable uid.

View differences:

ldapweb.cgi
310 310
              'emailverification' => \&handleEmailVerification,
311 311
              'lookupname'        => \&handleLookupName,
312 312
              'searchnamesbyemail'=> \&handleSearchNameByEmail,
313
              #'getnextuid'        => \&getNextUidNumber,
313 314
             );
314 315

  
315 316
# call the appropriate routine based on the stage
......
1553 1554
    return $templateVars;
1554 1555
} 
1555 1556

  
1557
#Method to get the next avaliable uid number. We use the mechanism - http://www.rexconsulting.net/ldap-protocol-uidNumber.html
1558
sub getNextUidNumber {
1559
    my $base="cn=uidNext,dc=ecoinformatics,dc=org";
1560
    my $uid_attribute_name = "description";
1561
    my $maxAttempt = 300;
1562
    
1563
    my $ldapUsername = $ldapConfig->{'unaffiliated'}{'user'};
1564
    my $ldapPassword = $ldapConfig->{'unaffiliated'}{'password'};
1565
    
1566
    my $realUidNumber="";
1567
    my $uidNumber="";
1568
    my $entry;
1569
    my $mesg;
1570
    my $ldap;
1571
    
1572
    debug("ldap server: $ldapurl");
1573
    
1574
    #if main ldap server is down, a html file containing warning message will be returned
1575
    $ldap = Net::LDAP->new($ldapurl, timeout => $timeout) or handleLDAPBindFailure($ldapurl);
1576
    
1577
    if ($ldap) {
1578
        $ldap->start_tls( verify => 'require',
1579
                      cafile => $ldapServerCACertFile);
1580
        my $bindresult = $ldap->bind( version => 3, dn => $ldapUsername, password => $ldapPassword);
1581
        #read the uid value stored in uidObject class
1582
        for(my $index=0; $index<$maxAttempt; $index++) {
1583
            $mesg = $ldap->search(base  => $base, filter => '(objectClass=*)');
1584
            if ($mesg->count() > 0) {
1585
                debug("Find the cn - $base");
1586
                $entry = $mesg->pop_entry;
1587
                $uidNumber = $entry->get_value($uid_attribute_name);
1588
                if($uidNumber) {
1589
                    debug("uid number is $uidNumber");
1590
                    #remove the uid attribute with the read value
1591
                    my $delMesg = $ldap->modify($base, delete => { $uid_attribute_name => $uidNumber});
1592
                    if($delMesg->is_error()) {
1593
                        my $error=$delMesg->error();
1594
                        my $errorName = $delMesg->error_name();
1595
                        debug("can't remove the attribute - $error");
1596
                        debug("can't remove the attribute and the error name - $errorName");
1597
                        #can't remove the attribute with the specified value - that means somebody modify the value in another route, so try it again
1598
                    } else {
1599
                        debug("Remove the attribute successfully and write a new increased value back");
1600
                        my $newValue = $uidNumber +1;
1601
                        $delMesg = $ldap->modify($base, add => {$uid_attribute_name => $newValue});
1602
                        $realUidNumber = $uidNumber;
1603
                        last;
1604
                    }
1605
               } else {
1606
                 debug("can't find the attribute - $uid_attribute_name in the $base and we will try again");
1607
               }
1608
            } 
1609
        }
1610
        $ldap->unbind;   # take down session
1611
    }
1612
    return $realUidNumber;
1613
}
1614

  
1615

  

Also available in: Unified diff