Changeset 683


Ignore:
Timestamp:
01/21/10 01:06:46 (14 years ago)
Author:
nanardon
Message:
  • implement a basic attribute value check, at time allow just to limit to a set of value
Location:
LATMOS-Accounts
Files:
1 added
4 edited

Legend:

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

    r623 r683  
    4848        }; 
    4949        $self->{_acls} = $acls; 
     50    } 
     51 
     52    if ($self->val('_default_', 'allowed_values')) { 
     53        $self->{_allowed_values} = Config::IniFiles->new( 
     54            -file => $self->val('_default_', 'allowed_values'), 
     55            -allowempty => 1, 
     56        ) or do { 
     57            return; 
     58        }; 
     59    } else { 
     60        $self->{_allowed_values} = Config::IniFiles->new(); 
    5061    } 
    5162 
     
    143154        label => $section, 
    144155        acls => $self->{_acls}, 
     156        allowed_values => $self->{_allowed_values}, 
    145157    ) or do { 
    146158        la_log(LA_WARN, "Cannot instanciate base $section ($type)"); 
  • LATMOS-Accounts/lib/LATMOS/Accounts/Bases.pm

    r679 r683  
    4848    $base->{defattr} = $options{defattr}; 
    4949    $base->{_acls} = $options{acls}; 
     50    $base->{_allowed_values} = $options{allowed_values}; 
    5051    la_log(LA_DEBUG, 'Instanciate base %s (%s)', ($options{label} || 'N/A'), $pclass); 
    5152    $base 
     
    7576sub type { 
    7677    $_[0]->{_type}; 
     78} 
     79 
     80sub allowed_values { 
     81    $_[0]->{_allowed_values} 
     82} 
     83 
     84sub check_allowed_values { 
     85    my ($self, $otype, $attr, $attrvalues) = @_; 
     86    my @values = ref $attrvalues ? @{ $attrvalues } : $attrvalues; 
     87    foreach my $value (@values) { 
     88        $value or next; 
     89        if (my @allowed = $self->allowed_values->val("$otype.$attr", 'allowed')) { 
     90            grep { $value eq $_ } @allowed or do { 
     91                $self->log(LA_ERR, 
     92                    "value `%s' is not allow for %s.%s per configuration (allowed_values)", 
     93                    $value, $otype, $attr 
     94                ); 
     95                return; 
     96            }; 
     97        } 
     98    } 
     99    return 1; 
    77100} 
    78101 
     
    237260        return; 
    238261    } 
     262    foreach my $cfield (keys %cdata) { 
     263        $self->check_allowed_values($otype, $cfield, $cdata{$cfield}) or do { 
     264            $self->log(LA_ERR, "Cannot create $otype, wrong value"); 
     265            return; 
     266        }; 
     267    } 
     268 
    239269    $self->_create_c_object($otype, $id, %cdata); 
    240270} 
  • LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Objects.pm

    r675 r683  
    253253=cut 
    254254 
     255sub check_allowed_values { 
     256    my ($self, $attr, $values) = @_; 
     257    $self->base->check_allowed_values($self->type, $attr, $values); 
     258} 
     259 
    255260=head2 set_c_fields(%data) 
    256261 
     
    262267sub set_c_fields { 
    263268    my ($self, %cdata) = @_; 
    264     my %data; 
    265269    eval { 
    266270        foreach my $cfield (keys %cdata) { 
    267271            $self->base->check_acl($self, $cfield, 'w') or  
    268                 die "permission denied"; 
     272                die "permission denied\n"; 
     273        } 
     274 
     275        foreach my $cfield (keys %cdata) { 
     276            $self->check_allowed_values($cfield, $cdata{$cfield}) or 
     277                die "incorrect attribute\n"; 
    269278        } 
    270279    }; 
    271     return if($@); 
     280    if($@) { 
     281        $self->base->log(LA_ERR, "Cannot modified %s/%s: %s", 
     282            $self->type, $self->id, $@ 
     283        ); 
     284        return; 
     285    } 
    272286    $self->_set_c_fields(%cdata); 
    273287} 
  • LATMOS-Accounts/sample/latmos-account.ini

    r562 r683  
    99# The acls file to use to limit access 
    1010#acls = 
     11# An inifile containing per attributes values restriction 
     12# allowed_values = 
    1113# smtp: the server used to send mail 
    1214smtp = localhost 
Note: See TracChangeset for help on using the changeset viewer.