Changeset 1992


Ignore:
Timestamp:
04/27/17 00:47:36 (7 years ago)
Author:
nanardon
Message:

Allow sub-object creation when creating object

Location:
trunk/LATMOS-Accounts
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/Changes

    r1974 r1992  
     15.2.15 
     2    When creating object related object can be directly 
     3      created at the same time (man latmos-accounts: "INPUT FILE" section) 
     4    Improve code to suggest collision avoidance login at user creation 
     5 
    165.2.14 
    27 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases.pm

    r1974 r1992  
    77use LATMOS::Accounts::Bases::Attributes; 
    88use LATMOS::Accounts::Log; 
    9 use LATMOS::Accounts::Utils qw(exec_command to_ascii); 
     9use LATMOS::Accounts::Utils qw(exec_command to_ascii ); 
    1010 
    1111our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; 
     
    195195    return @sobj; 
    196196} 
     197 
     198=head2 ListSubObjectOType($otype) 
     199 
     200Return a list of sub object type supported by C<otype> objects. 
     201 
     202=cut 
     203 
     204sub ListSubObjectOType { 
     205    my ($self, $otype) = @_; 
     206 
     207    # finding perl class: 
     208    my $pclass = $self->_load_obj_class($otype) or return; 
     209    my $definition = "$pclass"->GetOtypeDef($self) or return; 
     210 
     211    return keys %{ $definition || {} } 
     212} 
     213 
     214=head2 GetSubObjectKey($otype, $sotype) 
     215 
     216Return the key linking sub object C<$sotype> to object type C<$otype> 
     217 
     218=cut 
     219 
     220sub GetSubObjectKey { 
     221    my ($self, $otype, $sotype) = @_; 
     222 
     223    # finding perl class: 
     224    my $pclass = $self->_load_obj_class($otype) or return; 
     225    my $definition = "$pclass"->GetOtypeDef($self, $sotype) or return; 
     226 
     227    return $definition->{$sotype}; 
     228} 
     229 
    197230 
    198231=head2 ordered_objects 
     
    376409 
    377410    my %data; 
     411    my $sub; 
    378412    foreach my $cfield (keys %cdata) { 
    379         my $attribute = $self->attribute($otype, $cfield) or next; 
    380         $attribute->ro and next; 
    381         $data{$cfield} = $cdata{$cfield}; 
    382     } 
     413        # Parsing subobject creation: 
     414 
     415        if (my ($sotype, $key, $scfield) = $cfield =~ /^(\w+)(?:\[(\w+)\])?\.(.*)/) { 
     416            $key ||= '_'; 
     417            $sub->{$sotype}{$key}{$scfield} = $cdata{$cfield}; 
     418        } else { 
     419            my $attribute = $self->attribute($otype, $cfield) or next; 
     420            $attribute->ro and next; 
     421            $data{$cfield} = $cdata{$cfield}; 
     422        } 
     423    } 
     424 
    383425    $self->create_object($otype, $id, %data) or return; 
    384426    my $obj = $self->get_object($otype, $id) or return; 
     
    393435                ? join(', ', @{ $data{$attrname} }) 
    394436                : $data{$attrname}) || '(none)'); 
     437    } 
     438 
     439    if ($sub) { 
     440 
     441        # Security: if caller is create_c_object calling it to check permission ? 
     442        # See below 
     443        # my ($caller) = caller(); 
     444        # my $subcreate = $caller eq 'create_c_object' ? 'create_c_object' : __SUB__; 
     445         
     446        # Trying to create subobject 
     447        foreach my $sotype (keys %$sub) { 
     448            my $SubKeyRef = $self->GetSubObjectKey($otype, $sotype) or do { 
     449                $self->log(LA_ERR, "Cannot create object type $sotype, subtype of $otype not defined"); 
     450                return; 
     451            }; 
     452            foreach my $skey (keys %{ $sub->{$sotype} || {} }) { 
     453                # Building id 
     454                my $info = $sub->{$sotype}{$skey} || {}; 
     455                # For id: if key is given using it, otherwise using random 
     456                my $sid =  
     457                    $info->{$SubKeyRef} || 
     458                    $id . '-' . join('', map { ('a' .. 'z')[rand(26)] } (0 .. 6)); 
     459                $info->{$SubKeyRef} = $id; 
     460 
     461                # Here we don't check permission to create sub object: 
     462                $self->_create_c_object($sotype, $sid, %{ $info || {} }) or return; 
     463            } 
     464        } 
    395465    } 
    396466 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Objects.pm

    r1935 r1992  
    197197    @attrs = grep { $_->readable } @attrs if($for =~ /r/); 
    198198    map { $_->name } grep { !$_->hidden }  @attrs; 
     199} 
     200 
     201=head2 GetOtypeDef 
     202 
     203This function is called to provide sub object definition. Must be overwritten 
     204per object class when need. 
     205 
     206Must return a hashref listing each sub object type and their related key atribute: 
     207 
     208    return { 
     209        addresses => 'user', 
     210    } 
     211 
     212=cut 
     213 
     214sub GetOtypeDef { 
     215    my ($class) = @_; 
     216 
     217    return; 
    199218} 
    200219 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm

    r1964 r1992  
    5858    || $self->get_field('description') 
    5959    || $self->id; 
     60} 
     61 
     62sub GetOtypeDef { 
     63    my ($class) = @_; 
     64 
     65    { 
     66        address => 'user', 
     67        employment => 'user', 
     68    }, 
    6069} 
    6170 
  • trunk/LATMOS-Accounts/man/man8/latmos-accounts.pod

    r1324 r1992  
    139139=back 
    140140 
     141=head1 INPUT FILE 
     142 
     143The input file is a list of attribute and value separate by a semi-colon. 
     144Multi-values attributes must be repeated for each value. 
     145Attributes not listed are left untouched. 
     146 
     147Example: 
     148 
     149    sn: Myname 
     150    givenName: Myfirstname 
     151 
     152Some object have related objects, for example C<user> have multiple C<address> 
     153and C<employment>. It can be usefull to create those object in same time the 
     154main object. 
     155 
     156At creation the related object attributes can given using attribute in form 
     157C<OTYPE.ATTRIBUTE>. 
     158 
     159Example: 
     160 
     161    sn: Myname 
     162    givenName: Myfirstname 
     163    address.streetAddress: 5th Avenue, 123 
     164 
     165Multiple objects of same type can be given a key between C<[]>, this key may 
     166use any letter C<a-z>, C<A-Z> or number C<0-9>. 
     167 
     168    sn: Myname 
     169    givenName: Myfirstname 
     170    address[0].streetAddress: 5th Avenue, 123 
     171    address[aa].streetAddress: 5th Avenue, 123 
     172 
    141173=head1 TOOLS LIST 
    142174 
Note: See TracChangeset for help on using the changeset viewer.