Changeset 1984


Ignore:
Timestamp:
04/18/17 17:49:15 (7 years ago)
Author:
nanardon
Message:

Add function to generate login from first/last name

Location:
trunk/LATMOS-Accounts
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/OCHelper/User.pm

    r1076 r1984  
    3434             
    3535            if ($info->{contents}{sn}) { 
    36                 my $login = lc(to_ascii($info->{contents}{sn})); 
    37                 $login =~ s/ //g; # strip space in login 
    38                 length($login) > 10 and $login = substr($login, 0, 8); 
    39                 for (1) { 
    40                     $self->base->get_object('user', $login) or do { 
    41                         $info->{name}{content} = $login; 
    42                         last; 
    43                     }; 
    44                     if ($info->{contents}{givenName}) { 
    45                         length($login) > 8 and $login = substr($login, 0, 8); 
    46                         $login .= substr(lc(to_ascii($info->{contents}{givenName})), 0, 1); 
    47                         $self->base->get_object('user', $login) or do { 
    48                             $info->{name}{contents} = $login; 
    49                             last; 
    50                         }; 
    51                     } 
     36                my $login = buildLogin( 
     37                    sub { 
     38                        !$self->base->get_object('user', $_[0]); 
     39                    }, 
     40                    $info->{contents}{sn}, 
     41                    $info->{contents}{givenName}, 
     42                ); 
     43                if ($login) { 
     44                    $info->{name}{content} = $login; 
    5245                } 
    5346            } 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm

    r1770 r1984  
    2121 
    2222@ISA = qw(Exporter); 
    23 @EXPORT = qw(to_ascii exec_command switch_user run_via_sudo yesno); 
    24 @EXPORT_OK = qw(to_ascii exec_command switch_user run_via_sudo yesno); 
     23@EXPORT = qw(to_ascii exec_command switch_user run_via_sudo buildLogin yesno); 
     24@EXPORT_OK = qw(to_ascii exec_command switch_user run_via_sudo buildLogin yesno); 
    2525 
    2626=head2 to_ascii($text) 
     
    314314} 
    315315 
     316=head2 buildLogin([$cb, ] @names) 
     317 
     318Try to find a proper login from @names. 
     319 
     320Optionnal $cb is a callback to check the solution over exiting data. If return false, another solution is tried 
     321 
     322Example: 
     323 
     324    buildLogin(sub { $_[0] ? 1 : 0 }, "sn", "givenName") 
     325 
     326=cut 
     327 
     328sub buildLogin { 
     329    my (@names) = @_; 
     330    my $cb = undef; 
     331 
     332    if (ref $names[0]) { 
     333        $cb = shift(@names); 
     334    } else { 
     335        $cb = sub { 1 }; # always validating 
     336    } 
     337 
     338    # Cleaning names: 
     339    foreach (@names) { 
     340        $_ ||= ''; 
     341        $_ = lc(to_ascii($_)); 
     342        s/[^\w]//g; 
     343    } 
     344 
     345    @names = grep { $_ } @names; 
     346    my $base = shift(@names) or return; 
     347    my $rest = join('', grep { $_ } @names); 
     348 
     349    if ((my $l = length($base)) > 10) { 
     350        my $len = 8; 
     351        while ($len < $l) { 
     352            my $try  = substr($base, 0, $len); 
     353            if ($cb->($try)) { 
     354                return $try; 
     355            } 
     356            $len++; 
     357        } 
     358    } elsif ($cb->($base)) { 
     359        return $base; 
     360    } 
     361 
     362    my $try = $base; 
     363    while (my $len = length($try) - length($base) + 1) { 
     364        if ($len > length($rest)) { 
     365            last; 
     366        } 
     367        $try = $base . substr($rest, 0, $len); 
     368        if ($cb->($try)) { 
     369            return $try; 
     370        } 
     371    } 
     372    return; 
     373} 
     374 
    316375=head2 yesno($value, $default) 
    317376 
  • trunk/LATMOS-Accounts/t/05_utils.t

    r1378 r1984  
    11use strict; 
    22use warnings; 
    3 use Test::More tests => 21; 
     3use Test::More tests => 30; 
    44use File::Temp qw(mkstemp); 
    55 
     
    4545ok(!LATMOS::Accounts::Utils::check_ug_validity('t5oto'), '"t5oto" is valid username'); 
    4646 
     47is(LATMOS::Accounts::Utils::buildLogin('Toto'), 'toto', "buildLogin return toto"); 
     48is(LATMOS::Accounts::Utils::buildLogin('To\'to'), 'toto', "buildLogin return toto"); 
     49is(LATMOS::Accounts::Utils::buildLogin('#'), undef, "Impossible name return undef"); 
     50is(LATMOS::Accounts::Utils::buildLogin('TotoTataTiti'), 'tototata', "buildLogin return 8 byte length login"); 
     51is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^toto$/ ? 0 : 1 }, 'toto'), undef, "buildLogin: cb works"); 
     52is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^toto$/ ? 0 : 1 }, 'Toto', 'Titi'), 'totot', "buildLogin: try others login"); 
     53is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^totot?$/ ? 0 : 1 }, 'toto', 'titi'), 'tototi', "buildLogin: try others login"); 
     54is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^toto/ ? 0 : 1 }, 'toto', 'titi'), undef, "buildLogin: return undef if no possibility"); 
     55is(LATMOS::Accounts::Utils::buildLogin(sub { length($_[0]) > 9 }, 'TotoTataTiti'), 'tototatati', "buildLogin return 8 byte length login"); 
     56 
    4757is(yesno('yes'),  1, 'yes is true'); 
    4858is(yesno('true'), 1, 'true is true'); 
Note: See TracChangeset for help on using the changeset viewer.