Changeset 1076


Ignore:
Timestamp:
07/25/12 09:52:11 (12 years ago)
Author:
nanardon
Message:

Factorize default value computation

This avoid the difference of behavior between OCHelper and direct object
creation.

Location:
trunk/LATMOS-Accounts/lib/LATMOS/Accounts
Files:
2 edited

Legend:

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

    r1071 r1076  
    314314} 
    315315 
     316=head2 compute_default($otype, $id, %cdata) 
     317 
     318Return a hash containing value to set for new object 
     319 
     320=cut 
     321 
     322sub compute_default { 
     323    my ($self, $otype, $id, %cdata) = @_; 
     324 
     325    my %default; 
     326    foreach my $def (keys %{ $self->{defattr} || {}}) { 
     327        if ($def =~ /^$otype\.(.*)$/) { 
     328            $default{$1} = $self->{defattr}{$def} if(!$cdata{$1}); 
     329        } 
     330    } 
     331 
     332    # computed default value (not a simple set) 
     333    if (lc($otype) eq 'user') { 
     334        if (!$cdata{homeDirectory}) { 
     335            $default{homeDirectory} = $self->{defattr}{'user.homebase'} 
     336                ? $self->{defattr}{'user.homebase'} . "/$id"  
     337                : ''; 
     338        } 
     339 
     340        if (!$cdata{uidNumber}) { 
     341            $default{uidNumber} ||= $self->find_next_numeric_id('user', 'uidNumber', 
     342            $self->{defattr}{'user.min_uid'}, $self->{defattr}{'user.max_uid'}); 
     343        } 
     344 
     345        my $mailid = $cdata{givenName} && $cdata{sn} 
     346            ? sprintf('%s.%s', 
     347                to_ascii(lc($cdata{givenName})), 
     348                to_ascii(lc($cdata{sn})),) 
     349            : undef; 
     350        $mailid =~ s/\s+/-/g if($mailid); 
     351 
     352        if ($mailid && 
     353            $self->is_supported_object('aliases') && 
     354            ! $self->get_object('aliases', $mailid)) { 
     355            if (my $attr = $self->attribute($otype, 'mail')) { 
     356                if ((!$attr->ro) && $self->{defattr}{'user.maildomain'}) { 
     357                    $default{mail} ||= sprintf('%s@%s', 
     358                    $mailid, 
     359                    $self->{defattr}{'user.maildomain'}); 
     360                } 
     361            } 
     362            if (my $attr = $self->attribute($otype, 'aliases')) { 
     363                $default{aliases} ||= $mailid unless ($attr->ro); 
     364            } 
     365            if (my $attr = $self->attribute($otype, 'revaliases')) { 
     366                $default{revaliases} ||= $mailid unless ($attr->ro); 
     367            } 
     368        } 
     369    } elsif (lc($otype) eq 'group') { 
     370        if (!$cdata{gidNumber}) { 
     371            $default{gidNumber} ||= $self->find_next_numeric_id( 
     372                'group', 'gidNumber', 
     373                $self->{defattr}{'group.min_gid'}, 
     374                $self->{defattr}{'group.max_gid'} 
     375            ); 
     376        } 
     377    } 
     378 
     379    return %default; 
     380} 
     381 
    316382sub _create_c_object { 
    317383    my ($self, $otype, $id, %cdata) = @_; 
    318384 
    319385    $id ||= ''; # Avoid undef 
    320      
     386 
    321387    if (my $chk = ( 
    322388        lc($otype) eq 'user' || lc($otype) eq 'group') 
     
    334400 
    335401    # populating default value 
    336     foreach my $def (keys %{ $self->{defattr} || {}}) { 
    337         if ($def =~ /^$otype\.(.*)$/) { 
    338             $cdata{$1} = $self->{defattr}{$def} if(!$cdata{$1}); 
    339         } 
    340     } 
    341     if (lc($otype) eq 'user') { 
    342         $cdata{homeDirectory} ||= $self->{defattr}{'user.homebase'} ? 
    343             $self->{defattr}{'user.homebase'} . "/$id" : ''; 
    344         $cdata{uidNumber} ||= $self->find_next_numeric_id('user', 'uidNumber', 
    345             $self->{defattr}{'user.min_uid'}, $self->{defattr}{'user.max_uid'}); 
    346         my $mailid = $cdata{givenName} && $cdata{sn} 
    347             ? sprintf('%s.%s', 
    348                 to_ascii(lc($cdata{givenName})), 
    349                 to_ascii(lc($cdata{sn})),) 
    350             : undef; 
    351         $mailid =~ s/\s*//g if($mailid); 
    352  
    353         if ($mailid && 
    354             $self->is_supported_object('aliases') && 
    355             ! $self->get_object('aliases', $mailid)) { 
    356             if (my $attr = $self->attribute($otype, 'mail')) { 
    357                 if ((!$attr->ro) && $self->{defattr}{'user.maildomain'}) { 
    358                     $cdata{mail} ||= sprintf('%s@%s', 
    359                     $mailid, 
    360                     $self->{defattr}{'user.maildomain'}); 
    361                 } 
    362             } 
    363             if (my $attr = $self->attribute($otype, 'aliases')) { 
    364                 $cdata{aliases} ||= $mailid unless ($attr->ro); 
    365             } 
    366             if (my $attr = $self->attribute($otype, 'revaliases')) { 
    367                 $cdata{revaliases} ||= $mailid unless ($attr->ro); 
    368             } 
    369         } 
    370     } elsif (lc($otype) eq 'group') { 
    371         $cdata{gidNumber} ||= $self->find_next_numeric_id('group', 'gidNumber', 
    372             $self->{defattr}{'group.min_gid'}, $self->{defattr}{'group.max_gid'}); 
    373     } 
     402    { 
     403        my %default = $self->compute_default($otype, $id, %cdata); 
     404        foreach my $k (keys %default) { 
     405            $cdata{$k} = $default{$k}; 
     406        } 
     407    } 
     408 
    374409    my %data; 
    375410    foreach my $cfield (keys %cdata) { 
     
    381416    $self->create_object($otype, $id, %data); 
    382417} 
    383  
    384418 
    385419sub _allowed_values { 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/OCHelper/User.pm

    r1023 r1076  
    5959        push(@{$info->{ask}}, 'uidNumber'); 
    6060        push(@{$info->{ask}}, 'gidNumber'); 
    61         my $mailid = $info->{contents}{givenName} && $info->{contents}{sn} 
    62             ? sprintf('%s.%s', 
    63                 to_ascii(lc($info->{contents}{givenName})), 
    64                 to_ascii(lc($info->{contents}{sn})),) 
    65             : undef; 
    66         $mailid =~ s/ /-/g if ($mailid); # replace space by '-' in mail 
    67         push(@{$info->{ask}}, 'mail'); 
    68         $info->{contents}{mail} = $mailid && $self->base->{defattr}{'user.maildomain'} 
    69             ? sprintf('%s@%s', $mailid, 
    70                 $self->base->{defattr}{'user.maildomain'}) 
    71             : undef; 
     61 
     62       my %default = $self->base->compute_default( 
     63            'user', $login || '', 
     64            %{ $info->{contents} || {} } 
     65        ); 
     66 
     67        if ($default{mail}) { 
     68            push(@{$info->{ask}}, 'mail'); 
     69            $info->{contents}{mail} = $default{mail}; 
     70        } 
    7271        if (my $attr = $self->base->attribute('user', 'aliases')) { 
    7372            if (!$attr->ro) { 
    7473                push(@{$info->{ask}}, 'aliases'); 
    75                 $info->{contents}{aliases} = $mailid 
     74                $info->{contents}{aliases} = $default{aliases}; 
    7675            } 
    7776        } 
     
    7978            if (!$attr->ro) { 
    8079                push(@{$info->{ask}}, 'revaliases'); 
    81                 $info->{contents}{revaliases} = $mailid 
     80                $info->{contents}{revaliases} = $default{revaliases}; 
    8281            } 
    8382        } 
Note: See TracChangeset for help on using the changeset viewer.