[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Devel] Re: CVS update: skolelinux/src/webmin-ldap-skolelinux fileimport-example.txt



[Petter Reinholdtsen]
> I guess the current implementation in wls2 is good enough for now,
> but I haven't tested it myself.

I found the code in ldap-users.pl, and extracted a small test code.
There are several bugs in the code.  It will produce too long user
names.

#!/usr/bin/perl

sub ldap_get_user {
    ($base, $uid) = @_;
    if (exists $username{$uid}) {
        return 1;
    } else {
        return undef;
    }
}

sub make_uid {
    my ($f, $s) = @_;
    $f = &lettertrans($f);
    $s = &lettertrans($s);
    $i = 1;
    $nr = 0;
    $f =~ tr/ //d;
    $f =~ tr/-//d;
    $f = substr($f, 0, 8);
    $uid = $f;

    $entry = &ldap_get_user($config{'basedn'} , $uid);

    while ( defined $entry ){

        $surname_part = substr($s, 0, $i);
        $uid = lc("$f"."$surname_part");

        if("$surname_part" eq "$s"){
            $nr++;
            if($nr > 0){
                chop($uid);
            }

            $uid = lc("$f"."$surname_part"."$nr");
        }
        $entry = &ldap_get_user($config{'basedn'} , $uid);
        $i++;
    }
    $username{$uid} = 1;
    return $uid;

    sub lettertrans($){
        ($l) = @_;
        $line = lc("$l");
        $line =~ s/\346/\141/g;
        $line =~ s/\370/\o/g;
        $line =~ s/\345/\141/g;
        $line =~ s/\306/\141/g;
        $line =~ s/\330/\o/g;
        $line =~ s/\305/\141/g;
        return $line;
    }
}

for (1 .. 10) {
  $uid = make_uid @ARGV;
  print "$uid\n";
}

Here are the output from a few example runs:

% ./test.pl Jan Pettersen
jan
janp
janpe
janpet
janpett
janpette
janpetter
janpetters
janpetterse
janpettersen1
% ./test.pl Andreas Schouldei
andreas
andreass
andreassc
andreassch
andreasscho
andreasschou
andreasschoul
andreasschould
andreasschoulde
andreasschouldei1
% ./test.pl Digernes Ine Høyland
digernes
digernesi
digernesin
digernesine1
digernesine2
digernesine3
digernesine4
digernesine5
digernesine6
digernesine7

I believe it is smarter to try the middle name instead of the first
name if unable to find a unique name, and then start to shorten down
the part from the first/middle name, and pick more and more characters
from the last name.  Anyway, I believe numbers in the username should
be the last resort when every other option have been tried.  People
remember letters easier then digits.

(I've since modified the wls2 code to try harder to limit the name to
8 characters. :)