Ignore:
Timestamp:
04/25/12 15:51:28 (12 years ago)
Author:
nanardon
Message:

move code for get/set_field to attribute definitions

File:
1 edited

Legend:

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

    r933 r938  
    9898            create    => { inline => 1, ro => 1, }, 
    9999            date      => { inline => 1, ro => 1, }, 
    100             memberOf  => { multiple => 1, delayed => 1, }, 
     100            memberOf  => { 
     101                multiple => 1, delayed => 1, 
     102                get => sub { 
     103                    my ($self) = @_; 
     104                    my $obj = $self->object; 
     105                    my $sth = $obj->db->prepare_cached( 
     106                        q{ 
     107                        select name from "group" join 
     108                        group_attributes on group_attributes.okey = "group".ikey 
     109                        where value = ? and attr = ? 
     110                        } 
     111                    ); 
     112                    $sth->execute($obj->id, 'memberUID'); 
     113                    my @res; 
     114                    while (my $res = $sth->fetchrow_hashref) { 
     115                        push(@res, $res->{name}); 
     116                    } 
     117                    return \@res; 
     118                } 
     119            }, 
    101120            forward   => {}, 
    102121            aliases   => { 
     
    148167            uid => { iname => 'name', ro => 1 }, 
    149168            cn =>  { iname => 'name', ro => 1 }, 
    150             gecos => { ro => 1, }, 
    151             displayName  => { ro => 1, managed => 1, }, 
     169            gecos => { 
     170                ro => 1, 
     171                get => sub { 
     172                    my ($self) = @_; 
     173                    my $obj = $self->object; 
     174                    my $gecos = sprintf("%s,%s,%s,%s", 
     175                        join(' ', grep { $_ } 
     176                                ($obj->_get_c_field('givenName'), 
     177                                ($obj->_get_c_field('sn')))) 
     178                            || $obj->_get_c_field('description') || '', 
     179                        join(' - ', grep { $_ } (($obj->_get_c_field('site') || 
     180                                    $obj->_get_c_field('l')), 
     181                            $obj->_get_c_field('physicalDeliveryOfficeName'))) || '', 
     182                        $obj->_get_c_field('telephoneNumber') || '', 
     183                        $obj->_get_c_field('expireText') || '', 
     184                    ); 
     185                    $gecos =~ s/:/ /g; 
     186                    return to_ascii($gecos); 
     187                }, 
     188            }, 
     189            displayName  => { 
     190                ro => 1, managed => 1, 
     191                get => sub { 
     192                    my ($self) = @_; 
     193                    return join(' ', grep { $_ }  
     194                        ( 
     195                            $self->object->_get_c_field('givenName'), 
     196                            $self->object->_get_c_field('sn') 
     197                        ) 
     198                    ) 
     199                    || $self->object->_get_c_field('description') 
     200                    || $self->id; 
     201                }, 
     202            }, 
    152203            sAMAccountName  => { ro => 1, managed => 1  }, 
    153             accountExpires => { ro => 1, managed => 1 }, 
    154             shadowExpire => { ro => 1, managed => 1 }, 
     204            accountExpires => { 
     205                ro => 1, 
     206                managed => 1, 
     207                get => sub { 
     208                    my ($self) = @_; 
     209                    my $obj = $self->object; 
     210                    my $sth = $obj->db->prepare_cached( 
     211                        sprintf( 
     212                            q{select extract(epoch from expire) + 11644474161 as expire 
     213                            from %s where %s = ?}, 
     214                            $obj->db->quote_identifier($obj->object_table), 
     215                            $obj->db->quote_identifier($obj->key_field), 
     216                        ) 
     217                    ); 
     218                    $sth->execute($obj->id); 
     219                    my $res = $sth->fetchrow_hashref; 
     220                    $sth->finish; 
     221                    return $res->{expire} ? sprintf("%.f", $res->{expire} * 1E7) : '9223372036854775807'; 
     222                } 
     223            }, 
     224            shadowExpire => { 
     225                ro => 1, 
     226                managed => 1, 
     227                get => sub { 
     228                    my ($self) = @_; 
     229                    my $obj = $self->object; 
     230                    my $sth = $obj->db->prepare_cached( 
     231                        sprintf( 
     232                            q{select justify_hours(expire - '1/1/1970'::timestamp) as expire 
     233                            from %s where %s = ?}, 
     234                            $obj->db->quote_identifier($obj->object_table), 
     235                            $obj->db->quote_identifier($obj->key_field), 
     236                        ) 
     237                    ); 
     238                    $sth->execute($obj->id); 
     239                    my $res = $sth->fetchrow_hashref; 
     240                    $sth->finish; 
     241                    return -1 unless($res->{expire}); 
     242                    $res->{expire} =~ /(\d+) days\s*(\w)?/; 
     243                    return $1 + ($2 ? 1 : 0); 
     244                } 
     245            }, 
    155246            directReports => { 
    156247                reference => 'user', 
    157248                ro => 1, 
    158249                delayed => 1, 
     250                get => sub { 
     251                    my ($self) = @_; 
     252                    my $obj = $self->object; 
     253                    my $sth = $obj->db->prepare_cached( 
     254                        q{ 
     255                        select name from "user" join 
     256                        user_attributes on user_attributes.okey = "user".ikey 
     257                        where value = ? and attr = ? 
     258                        } 
     259                    ); 
     260                    $sth->execute($obj->id, 'manager'); 
     261                    my @res; 
     262                    while (my $res = $sth->fetchrow_hashref) { 
     263                        push(@res, $res->{name}); 
     264                    } 
     265                    return \@res; 
     266                }, 
    159267            }, 
    160268            managedObjects => { ro => 1, reference => 'group', }, 
     
    170278                ro => 1, 
    171279                reference => 'user', 
    172             }, 
    173             expireText => { ro => 1, }, 
     280                get => sub { 
     281                    my ($self) = @_; 
     282                    if (my $manager = $self->object->_get_c_field('manager')) { 
     283                        return $manager; 
     284                    } elsif (my $department = $self->object->_get_c_field('department')) { 
     285                        my $obj = $self->base->get_object('group', $department); 
     286                        return $obj->_get_c_field('managedBy'); 
     287                    } else { 
     288                        return; 
     289                    } 
     290                }, 
     291            }, 
     292            expireText => { 
     293                ro => 1, 
     294                get => sub { 
     295                    my ($self) = @_; 
     296                    my $obj = $self->object; 
     297                    my $sth = $obj->db->prepare_cached( 
     298                        sprintf( 
     299                            q{select to_char(expire, 'YYYY/MM/DD') as expire 
     300                            from %s where %s = ?}, 
     301                            $obj->db->quote_identifier($obj->object_table), 
     302                            $obj->db->quote_identifier($obj->key_field), 
     303                        ) 
     304                    ); 
     305                    $sth->execute($obj->id); 
     306                    my $res = $sth->fetchrow_hashref; 
     307                    $sth->finish; 
     308                    return $res->{expire} 
     309                }, 
     310            }, 
    174311            krb5ValidEnd => { ro => 1, }, 
    175312            cells  => { 
     
    185322            expired => { ro => 1 }, 
    186323            active => { ro => 1 }, 
    187                 pwdAccountLockedTime => { managed => 1, ro => 1 }, 
     324            pwdAccountLockedTime => { 
     325                managed => 1, 
     326                ro => 1, 
     327                get => sub { 
     328                    my ($self) = @_; 
     329                    my $obj = $self->object; 
     330                    if ($obj->_get_c_field('locked')) { 
     331                        return '000001010000Z'; 
     332                    } else { 
     333                        my $sth = $obj->db->prepare_cached( 
     334                            sprintf( 
     335                                q{select to_char(expire AT TIME ZONE 'Z', 'YYYYMMDDHH24MISSZ') as expire 
     336                                from %s where %s = ? and expire < now()}, 
     337                                $obj->db->quote_identifier($obj->object_table), 
     338                                $obj->db->quote_identifier($obj->key_field), 
     339                            ) 
     340                        ); 
     341                        $sth->execute($obj->id); 
     342                        my $res = $sth->fetchrow_hashref; 
     343                        $sth->finish; 
     344                        return $res->{expire} 
     345                    } 
     346                }, 
     347            }, 
    188348            userPassword => { readable => 0, }, 
    189349        } 
     
    193353sub get_field { 
    194354    my ($self, $field) = @_; 
    195     if ($field eq 'gecos') { 
    196         my $gecos = sprintf("%s,%s,%s,%s", 
    197             join(' ', grep { $_ }  
    198                     ($self->_get_c_field('givenName'), 
    199                     ($self->_get_c_field('sn')))) 
    200                 || $self->_get_c_field('description') || '', 
    201             join(' - ', grep { $_ } (($self->_get_c_field('site') || 
    202                         $self->_get_c_field('l')), 
    203                 $self->_get_c_field('physicalDeliveryOfficeName'))) || '', 
    204             $self->_get_c_field('telephoneNumber') || '', 
    205             $self->_get_c_field('expireText') || '', 
    206         ); 
    207         $gecos =~ s/:/ /g; 
    208         return to_ascii($gecos); 
    209     } elsif ($field eq 'displayName') { 
    210         return join(' ', grep { $_ } ($self->_get_c_field('givenName'), ($self->_get_c_field('sn')))) 
    211             || $self->_get_c_field('description') || $self->id; 
    212     } elsif ($field eq 'sAMAccountName') { 
     355    if ($field eq 'sAMAccountName') { 
    213356        return $self->id; 
    214     } elsif ($field eq 'memberOf') { 
    215         my $sth = $self->db->prepare_cached( 
    216             q{ 
    217             select name from "group" join 
    218             group_attributes on group_attributes.okey = "group".ikey 
    219             where value = ? and attr = ? 
    220             } 
    221         ); 
    222         $sth->execute($self->id, 'memberUID'); 
    223         my @res; 
    224         while (my $res = $sth->fetchrow_hashref) { 
    225             push(@res, $res->{name}); 
    226         } 
    227         return \@res; 
    228     } elsif ($field eq 'directReports') { 
    229         my $sth = $self->db->prepare_cached( 
    230             q{ 
    231             select name from "user" join 
    232             user_attributes on user_attributes.okey = "user".ikey 
    233             where value = ? and attr = ? 
    234             } 
    235         ); 
    236         $sth->execute($self->id, 'manager'); 
    237         my @res; 
    238         while (my $res = $sth->fetchrow_hashref) { 
    239             push(@res, $res->{name}); 
    240         } 
    241         return \@res; 
    242     } elsif ($field eq 'accountExpires') { 
    243         my $sth = $self->db->prepare_cached( 
    244             sprintf( 
    245                 q{select extract(epoch from expire) + 11644474161 as expire 
    246                 from %s where %s = ?}, 
    247                 $self->db->quote_identifier($self->object_table), 
    248                 $self->db->quote_identifier($self->key_field), 
    249             ) 
    250         ); 
    251         $sth->execute($self->id); 
    252         my $res = $sth->fetchrow_hashref; 
    253         $sth->finish; 
    254         return $res->{expire} ? sprintf("%.f", $res->{expire} * 1E7) : '9223372036854775807'; 
    255     } elsif ($field eq 'shadowExpire') { 
    256         my $sth = $self->db->prepare_cached( 
    257             sprintf( 
    258                 q{select justify_hours(expire - '1/1/1970'::timestamp) as expire 
    259                 from %s where %s = ?}, 
    260                 $self->db->quote_identifier($self->object_table), 
    261                 $self->db->quote_identifier($self->key_field), 
    262             ) 
    263         ); 
    264         $sth->execute($self->id); 
    265         my $res = $sth->fetchrow_hashref; 
    266         $sth->finish; 
    267         return -1 unless($res->{expire}); 
    268         $res->{expire} =~ /(\d+) days\s*(\w)?/; 
    269         return $1 + ($2 ? 1 : 0); 
    270357    } elsif ($field eq 'krb5ValidEnd') { 
    271358        my $sth = $self->db->prepare_cached( 
     
    281368        $sth->finish; 
    282369        return $res->{expire} 
    283     } elsif ($field eq 'expireText') { 
    284         my $sth = $self->db->prepare_cached( 
    285             sprintf( 
    286                 q{select to_char(expire, 'YYYY/MM/DD') as expire 
    287                 from %s where %s = ?}, 
    288                 $self->db->quote_identifier($self->object_table), 
    289                 $self->db->quote_identifier($self->key_field), 
    290             ) 
    291         ); 
    292         $sth->execute($self->id); 
    293         my $res = $sth->fetchrow_hashref; 
    294         $sth->finish; 
    295         return $res->{expire} 
    296370    } elsif ($field eq 'pwdAccountLockedTime') { 
    297         if ($self->_get_c_field('locked')) { 
    298             return '000001010000Z'; 
    299         } else { 
    300             my $sth = $self->db->prepare_cached( 
    301                 sprintf( 
    302                     q{select to_char(expire AT TIME ZONE 'Z', 'YYYYMMDDHH24MISSZ') as expire 
    303                     from %s where %s = ? and expire < now()}, 
    304                     $self->db->quote_identifier($self->object_table), 
    305                     $self->db->quote_identifier($self->key_field), 
    306                 ) 
    307             ); 
    308             $sth->execute($self->id); 
    309             my $res = $sth->fetchrow_hashref; 
    310             $sth->finish; 
    311             return $res->{expire} 
    312         } 
    313371    } elsif ($field eq 'otheraddress') { 
    314372        my $sth = $self->db->prepare_cached(q{ 
     
    380438        return $res->{as} 
    381439    } elsif ($field eq 'managerContact') { 
    382         if (my $manager = $self->_get_c_field('manager')) { 
    383             return $manager; 
    384         } elsif (my $department = $self->_get_c_field('department')) { 
    385             my $obj = $self->base->get_object('group', $department); 
    386             return $obj->_get_c_field('managedBy'); 
    387         } else { 
    388             return; 
    389         } 
    390440    } else { 
    391441        return $self->SUPER::get_field($field); 
Note: See TracChangeset for help on using the changeset viewer.