package LATMOS::Accounts::Bases::Sql::User;

use 5.010000;
use strict;
use warnings;
use overload '""' => 'stringify';

use LATMOS::Accounts::Utils;
use LATMOS::Accounts::Log;
use POSIX qw(strftime);
use base qw(LATMOS::Accounts::Bases::Sql::objects);

our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];

=head1 NAME

LATMOS::Ad - Perl extension for blah blah blah

=head1 DESCRIPTION

Account base access over standard unix file format.

=head1 FUNCTIONS

=cut

=head2 new(%config)

Create a new LATMOS::Ad object for windows AD $domain.

domain / server: either the Ad domain or directly the server

ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>.

=cut

sub _object_table { 'user' }

sub _key_field { 'name' }

sub _has_extended_attributes { 1 }

sub stringify {
    my ($self) = @_;

    return join(' ', grep { $_ }
        (
            $self->get_field('givenName'),
            $self->get_field('sn')
        )
    )
    || $self->get_field('description')
    || $self->id;
}

sub _get_attr_schema {
    my ($class, $base) = @_;

    my $subsetaddress = sub {
        my ($self, $data) = @_;
        my $fmainaddress = $self->object->_get_c_field('mainaddress');

        # set address attribute => create address object on the fly
        # except if attr is empty !
        if (!$fmainaddress && $data) {
            $fmainaddress = $self->object->id . '-' . join('', map { ('a'..'z')[rand(26)] }
                (0..4));
            $self->base->_create_c_object(
                'address', $fmainaddress,
                user => $self->object->id,
                isMainAddress => 1, ) or do {
                $self->base->log(LA_ERR,
                    "Cannot create main address for user %s", $self->object->id);
                return;
            };
        }
        if ($fmainaddress && 
            (my $address = $self->base->get_object('address', $fmainaddress))) {
            if ($address->attribute($self->name) &&
                !$address->attribute($self->name)->ro) {
                return $address->set_c_fields($self->name => $data) ||0;
            }
        }
    };

    $class->SUPER::_get_attr_schema($base,
        {
            exported => {
                post => sub {
                    my ($self, $value) = @_;
                    my $attr = $self->name;

                    if (my $obj = $self->base->
                            get_object('revaliases', $self->object->id)) {
                        my $ares = $obj->set_c_fields(
                            ($attr eq 'exported' ? 'exported' : 'unexported') => $value
                        );
                        if (!defined($ares)) {
                            $self->base->log(LA_ERR,
                                'Cannot set revaliases exported attribute for user %s',
                                $self->object->id);
                        }
                    }
                    my $must_expire = $self->name eq 'exported'
                        ? ($value ? 0 : 1 )
                        : ($value ? 1 : 0 );

                    foreach my $al ($self->object->get_attributes('aliases')) {
                        my $obj = $self->base->get_object('aliases', $al) or next;
                        $obj->_set_c_fields(
                            expire => $must_expire
                                ? strftime(
                                    "%Y-%m-%d %H:%M:%S",
                                    localtime(time + 3600 * 24 * 365)
                                  )
                                : undef,
                        );
                    }
                },
            },
            uidNumber => {
                inline => 1,
                iname => 'uidnumber',
                uniq => 1,
                mandatory => 1,
                formopts => { length => 7 },
            },
            uidnumber => { inline => 1, hide => 1, monitored => 1 },
            gidNumber => {
                inline => 1,
                iname => 'gidnumber',
                mandatory => 1,
                can_values => sub {
                    map { $_->id, $_->get_attributes('gidNumber') }
                    map { $base->get_object('group', $_) }
                    $base->list_objects('group')
                },
                can_values => sub {
                    my $sth = $base->db->prepare_cached(
                        q{
                        select name, gidnumber from "group" where exported = true
                        }
                    );
                    $sth->execute();
                    my @res;
                    while (my $res = $sth->fetchrow_hashref) {
                        push(@res, $res->{name});
                        push(@res, $res->{gidnumber});
                    }
                    return @res;
                },
                display => sub {
                    my ($self, $val) = @_;
                    my ($gr) = $self->base->search_objects('group', "gidNumber=$val")
                        or return;
                    return $gr;
                },
                input => sub {
                    my ($val) = @_;
                    $val =~ /^\d+$/ and return $val;
                    my ($gr) = $base->search_objects('group', "name=$val") or return;
                    return $base->get_object('group', $gr)->get_attributes('gidNumber');
                },
                reference => 'group',
            },
            loginShell => { mandatory => 1 },
            gidnumber => { inline => 1, hide => 1,
                can_values => sub {
                    map { $_->id, $_->get_attributes('gidNumber') }
                    map { $base->get_object('group', $_) }
                    $base->list_objects('group')
                },
                display => sub {
                    my ($self, $val) = @_;
                    my ($gr) = $self->base->search_objects('group', "gidNumber=$val")
                        or return;
                    return $gr;
                },
                input => sub {
                    my ($val) = @_;
                    $val =~ /^\d+$/ and return $val;
                    my ($gr) = $base->search_objects('group', "name=$val") or return;
                    return $base->get_object('group', $gr)->get_attributes('gidNumber');
                },
                mandatory => 1,
                reference => 'group',
                monitored => 1,
            },
            locked    => {
                formtype => 'CHECKBOX',
                formopts => { rawvalue => 1, },
                monitored => 1,
            },
            expire        => { inline => 1, formtype => 'DATE', monitored => 1, },
            endcircuit    => { inline => 1, formtype => 'DATE', monitored => 1, },
            cn        => {
                inline => 1, ro => 1,
                get => sub {
                    my ($self) = @_;
                    return join(' ', grep { $_ } 
                        (
                            $self->object->_get_c_field('givenName'),
                            $self->object->_get_c_field('sn')
                        )
                    )
                    || $self->object->_get_c_field('description')
                    || $self->object->id;
                },
            },
            memberOf  => {
                reference => 'group',
                multiple => 1, delayed => 1,
                get => sub {
                    my ($self) = @_;
                    my $obj = $self->object;
                    my $sth = $obj->db->prepare_cached(
                        q{
                        select name from "group" join
                        group_attributes on group_attributes.okey = "group".ikey
                        where value = ? and attr = ?
                        } . ($self->base->{wexported} ? '' : ' and "group".exported = true')
                    );
                    $sth->execute($obj->id, 'memberUID');
                    my @res;
                    while (my $res = $sth->fetchrow_hashref) {
                        push(@res, $res->{name});
                    }
                    return \@res;
                },
                set => sub {
                    my ($self, $values) = @_;
                    my %old = map { $_ => 'o' } @{ $self->get };
                    foreach my $group (grep { $_ } ref $values ? @{ $values } : $values) {
                        if ($old{$group}) {
                            $old{$group} = undef; # no change
                        } else {
                            $old{$group} = 'n';
                        }
                    }

                    my $res = 0;
                    foreach my $group (keys %old) {
                        $old{$group} or next; # no change

                        my $ogroup = $self->base->get_object('group', $group) or next;
                        ($ogroup->_get_c_field('sutype') || '') =~ /^(jobtype|contrattype)$/ and next;

                        if ($old{$group} eq 'n') {
                            $res += $ogroup->_addAttributeValue('memberUID', $self->object->id);
                        } else {
                            if (($self->object->_get_c_field('department') || '') eq $ogroup->id) {
                                $self->base->log(LA_WARN,
                                    "Don't removing user %s from group %s: is its department",
                                    $self->object->id, $ogroup->id);
                                next;
                            }
                            $res += $ogroup->_delAttributeValue('memberUID', $self->object->id);
                        }
                    }
                    return $res;
                },
            },
            forward => {
                managed => 1,
                get => sub {
                    my ($self) = @_;
                    my $sth = $self->base->db->prepare(q{
                        select forward from aliases where name = ?
                        } . ($self->base->{wexported} ? '' : ' and exported = true'));
                    $sth->execute($self->object->id);
                    my $res = $sth->fetchrow_hashref;
                    $sth->finish;
                    return $res->{forward}
                },
                set => sub {
                    my ($self, $data) = @_;
                    if ($data) {
                        if (my $f = $self->base->get_object('aliases', $self->object->id)) {
                            return $f->_set_c_fields(forward => $data);
                        } else {
                            if ($self->base->_create_c_object(
                                    'aliases', $self->object->id,
                                    forward => $data,
                                    description => 'automatically created for ' . $self->object->id,
                                )) {
                                return 1;
                            } else {
                                $self->base->log(LA_ERR, "Cannot add forward for %s",
                                    $self->id);
                            }
                        }
                    } else {
                        if (my $res = $self->base->_delete_object('aliases', $self->object->id)) {
                            return $res;
                        } else {
                            $self->base->log(LA_ERR, "Cannot remove forward for %s",
                                $self->object->id);
                        }
                    }
                    return;
                },
            },
            aliases   => {
                #reference => 'aliases',
                delayed => 1,
                formtype => 'TEXT',
                multiple => 1,
                get => sub {
                    my ($self) = @_;
                    my $sth = $self->base->db->prepare(q{
                        select name from aliases where array[lower($1)] =
                        string_to_array(lower(array_to_string("forward", ',')), ',')
                        } . ($self->base->{wexported} ? '' : 'and exported = true'));
                    $sth->execute($self->object->id);
                    my @values;
                    while (my $res = $sth->fetchrow_hashref) {
                        push(@values, $res->{name});
                    }
                    return \@values;
                },
                set => sub {
                    my ($self, $data) = @_;

                    my $res = 0;
                    my %aliases = map { $_ => 1 } grep { $_ } (ref $data ? @{$data} : $data);
                    foreach ($self->object->_get_attributes('aliases')) {
                        $aliases{$_} ||= 0;
                        $aliases{$_} +=2;
                    }
                    foreach (keys %aliases) {
                        if ($aliases{$_} == 2) {
                            if ($self->base->_delete_object('aliases', $_)) {
                                $res++
                            } else {
                                $self->base->log(LA_ERR,
                                    "Cannot remove aliases %s from user %s", $_,
                                    $self->object->id);
                            }
                        } elsif ($aliases{$_} == 1) {
                            if ($self->base->_create_c_object(
                                    'aliases', $_,
                                    forward => [ $self->object->id ],
                                    description => 'automatically created for ' . $self->object->id,
                                )) {
                                $res++
                            } else {
                                $self->base->log(LA_ERR, 'Cannot set forward %s to user %s',
                                    $_, $self->object->id);
                                return;
                            }
                        } # 3 no change
                    }
                    $res
                },
            },
            revaliases => {
                formtype => 'TEXT',
                get => sub {
                    my ($self) = @_;

                    if (my $obj = $self->base->
                        get_object('revaliases', $self->object->id)) {
                        return $obj->get_attributes('as');
                    } else {
                        return;
                    }
                },
                set => sub {
                    my ($self, $data) = @_;
                
                    my $res = 0;
                    if ($data) {
                        if (my $obj = $self->base->
                            get_object('revaliases', $self->object->id)) {
                            my $ares = $obj->set_c_fields(
                                'as' => $data,
                                'exported' => ($self->object->get_attributes('exported') || 0),
                            );
                            warn $ares;
                            if (defined($ares)) {
                                $res+=$ares;
                            } else {
                                $self->base->log(LA_ERR, 'Cannot set revaliases for user %s',
                                    $self->object->id);
                            }
                        } else {
                            if ($self->base->_create_c_object(
                                    'revaliases',
                                    $self->object->id, as => $data,
                                    'exported' => ($self->object->get_attributes('exported') || 0),
                                    description => 'automatically created for ' . $self->object->id,
                                )) {
                                $res++;
                            } else {
                                $self->base->log(LA_ERR, 'Cannot set revaliases for user %s',
                                    $self->object->id);
                            }
                        }
                    } else {
                        $self->base->_delete_object('revaliases', $self->object->id);
                        $res++;
                    }

                    $res
                },
            },
            manager => {
                reference => 'user',
                ro => 1,
                get => sub {
                    my ($self) = @_;
                    if (my $manager = $self->object->_get_c_field('managerContact')) {
                        return $manager;
                    } elsif (my $department = $self->object->_get_c_field('department')) {
                        my $obj = $self->base->get_object('group', $department);
                        return $obj->_get_c_field('managedBy');
                    } else {
                        return;
                    }
                },
            },
            department => {
                reference => 'group',
                can_values => sub {
                    $base->search_objects('group', 'sutype=dpmt')
                },
                monitored => 1,
            },
            contratType => {
                reference => 'group',
                can_values => sub {
                    $base->search_objects('group', 'sutype=contrattype')
                },
                monitored => 1,
            },
            site => {
                reference => 'site',
                can_values => sub {
                    $base->search_objects('site')
                },
                get => sub {
                    my ($self) = @_;
                    if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) {
                        $self->base->get_object('address', $fmainaddress)
                            ->_get_c_field($self->name);
                    } else {
                        return;
                    }
                },
                set => $subsetaddress,
            },
            co => {
                get => sub {
                    my ($self) = @_;
                    if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) {
                        $self->base->get_object('address', $fmainaddress)
                            ->_get_c_field($self->name);
                    } else {
                        return;
                    }
                },
                set => $subsetaddress,
            },
            l => {
                get => sub {
                    my ($self) = @_;
                    if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) {
                        $self->base->get_object('address', $fmainaddress)
                            ->_get_c_field($self->name);
                    } else {
                        return;
                    }
                },
                set => $subsetaddress,
            },
            postalCode => {
                get => sub {
                    my ($self) = @_;
                    if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) {
                        $self->base->get_object('address', $fmainaddress)
                            ->_get_c_field($self->name);
                    } else {
                        return;
                    }
                },
                set => $subsetaddress,
            },
            streetAddress => {
                formtype => 'TEXTAREA',
                get => sub {
                    my ($self) = @_;
                    if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) {
                        $self->base->get_object('address', $fmainaddress)
                            ->_get_c_field($self->name);
                    } else {
                        return;
                    }
                },
                set => $subsetaddress,
            },
            postOfficeBox => {
                get => sub {
                    my ($self) = @_;
                    if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) {
                        $self->base->get_object('address', $fmainaddress)
                            ->_get_c_field($self->name);
                    } else {
                        return;
                    }
                },
                set => $subsetaddress,
            },
            st => {
                get => sub {
                    my ($self) = @_;
                    if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) {
                        $self->base->get_object('address', $fmainaddress)
                            ->_get_c_field($self->name);
                    } else {
                        return;
                    }
                },
                set => $subsetaddress,
            },
            facsimileTelephoneNumber => {
                get => sub {
                    my ($self) = @_;
                    if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) {
                        $self->base->get_object('address', $fmainaddress)
                            ->_get_c_field($self->name);
                    } else {
                        return;
                    }
                },
                set => $subsetaddress,
            },
            o => {
                ro => 1,
                iname => 'company',
            },
            ou => { iname => 'department', ro => 1 },
            telephoneNumber => {
                get => sub {
                    my ($self) = @_;
                    if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) {
                        $self->base->get_object('address', $fmainaddress)
                            ->_get_c_field($self->name);
                    } else {
                        return;
                    }
                },
                set => $subsetaddress,
            },
            physicalDeliveryOfficeName => {
                get => sub {
                    my ($self) = @_;
                    if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) {
                        $self->base->get_object('address', $fmainaddress)
                            ->_get_c_field($self->name);
                    } else {
                        return;
                    }
                },
                set => $subsetaddress,
            },
            uid => { iname => 'name', ro => 1 },
            cn =>  { iname => 'name', ro => 1 },
            gecos => {
                ro => 1,
                get => sub {
                    my ($self) = @_;
                    my $obj = $self->object;
                    my $gecos = sprintf("%s,%s,%s,%s",
                        join(' ', grep { $_ }
                                ($obj->_get_c_field('givenName'),
                                ($obj->_get_c_field('sn'))))
                            || $obj->_get_c_field('description') || '',
                        join(' - ', grep { $_ } (($obj->_get_c_field('site') ||
                                    $obj->_get_c_field('l')),
                            $obj->_get_c_field('physicalDeliveryOfficeName'))) || '',
                        $obj->_get_c_field('telephoneNumber') || '',
                        $obj->_get_c_field('expireText') || '',
                    );
                    $gecos =~ s/:/ /g;
                    return to_ascii($gecos);
                },
            },
            displayName  => {
                ro => 1, managed => 1,
                get => sub {
                    my ($self) = @_;
                    return join(' ', grep { $_ } 
                        (
                            $self->object->_get_c_field('givenName'),
                            $self->object->_get_c_field('sn')
                        )
                    )
                    || $self->object->_get_c_field('description')
                    || $self->object->id;
                },
            },
            sAMAccountName  => {
                ro => 1,
                managed => 1,
                iname => 'name',
            },
            accountExpires => {
                ro => 1,
                managed => 1,
                get => sub {
                    my ($self) = @_;
                    my $obj = $self->object;
                    my $sth = $obj->db->prepare_cached(
                        sprintf(
                            q{select extract(epoch from COALESCE(endcircuit,  expire)) + 11644474161 as expire
                            from %s where %s = ?},
                            $obj->db->quote_identifier($obj->_object_table),
                            $obj->db->quote_identifier($obj->_key_field),
                        )
                    );
                    $sth->execute($obj->id);
                    my $res = $sth->fetchrow_hashref;
                    $sth->finish;
                    return $res->{expire} ? sprintf("%.f", $res->{expire} * 1E7) : '9223372036854775807';
                }
            },
            shadowExpire => {
                ro => 1,
                managed => 1,
                get => sub {
                    my ($self) = @_;
                    my $obj = $self->object;
                    my $sth = $obj->db->prepare_cached(
                        sprintf(
                            q{select justify_hours(COALESCE(endcircuit,  expire) - '1/1/1970'::timestamp) as expire
                            from %s where %s = ?},
                            $obj->db->quote_identifier($obj->_object_table),
                            $obj->db->quote_identifier($obj->_key_field),
                        )
                    );
                    $sth->execute($obj->id);
                    my $res = $sth->fetchrow_hashref;
                    $sth->finish;
                    return -1 unless($res->{expire});
                    $res->{expire} =~ /(\d+) days\s*(\w)?/;
                    return $1 + ($2 ? 1 : 0);
                }
            },
            directReports => {
                reference => 'user',
                ro => 1,
                delayed => 1,
                get => sub {
                    my ($self) = @_;
                    my $obj = $self->object;
                    my $sth = $obj->db->prepare_cached(
                        q{
                        SELECT
                        "user".name FROM
                        public."user",
                        public.user_attributes_groups,
                        public.group_attributes_users,
                        public.group_attributes_base gb,
                        public."group"
                        WHERE
                        "user".ikey = user_attributes_groups.okey AND
                        user_attributes_groups.value = "group".name AND
                        group_attributes_users.okey = gb.okey AND
                        "group".ikey = group_attributes_users.okey AND
                        gb.attr = 'sutype' AND
                        gb.value = 'dpmt' AND
                        group_attributes_users.attr = 'managedBy' AND
                        group_attributes_users.value = ?
                        } . ($self->base->{wexported} ? '' : ' and "user".exported = true') . q{
                            and "user".ikey not in
                        (select okey from user_attributes_users
                        where attr = 'manager' and value != ? )
                        union
                        select "user".name FROM public."user",
                        user_attributes_users where
                        user_attributes_users.attr = 'manager' and user_attributes_users.value = ?
                            and "user".ikey = user_attributes_users.okey
                        } . ($self->base->{wexported} ? '' : ' and "user".exported = true')
                    );
                    $sth->execute($obj->id, $obj->id, $obj->id);
                    my @res;
                    while (my $res = $sth->fetchrow_hashref) {
                        push(@res, $res->{name});
                    }
                    return \@res;
                },
            },
            managedObjects => { ro => 1, reference => 'group', },
            otheraddress => {
                ro => 1,
                reference => 'address',
                get => sub {
                    my ($self) = @_;
                    my $sth = $self->base->db->prepare_cached(q{
                        select name from address left join address_attributes
                        on address.ikey = address_attributes.okey and
                        address_attributes.attr = 'isMainAddress'
                        where "user" = ?
                        order by address_attributes.attr
                        });
                    $sth->execute($self->object->id);
                    my @values;
                    while (my $res = $sth->fetchrow_hashref) {
                        push(@values, $res->{name});
                    }
                    return \@values;
                },
            },
            mainaddress => {
                ro => 1,
                reference => 'address',
                get => sub {
                    my ($self) = @_;
                    my $sth = $self->base->db->prepare_cached(q{
                        select name from address join address_attributes on ikey = okey
                        where "user" = ? and attr = 'isMainAddress'
                        });
                    $sth->execute($self->object->id);
                    my $res = $sth->fetchrow_hashref;
                    $sth->finish;
                    return $res->{name};
                },
            },
            postalAddress => {
                ro => 1,
                get => sub {
                    my ($self) = @_;
                    if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) {
                        $self->base->get_object('address', $fmainaddress)
                            ->_get_c_field($self->name);
                    } else {
                        return;
                    }
                },
            },
            facsimileTelephoneNumber => { ro => 1, },
            allsite   => {
                ro => 1,
                reference => 'site',
            },
            managerContact => {
                delayed => 1,
                can_values => sub {
                    my %uniq = map { $_ => 1 } grep { $_ }
                    ($_[1] ? $_[1]->get_attributes('managerContact') : ()),
                    $base->search_objects('user', 'active=*');
                    sort keys %uniq;
                },
                reference => 'user',
                monitored => 1,
                iname => 'manager',
            },
            expireText => {
                ro => 1,
                get => sub {
                    my ($self) = @_;
                    my $obj = $self->object;
                    my $sth = $obj->db->prepare_cached(
                        sprintf(
                            q{select to_char(COALESCE(endcircuit,  expire), 'YYYY/MM/DD') as expire
                            from %s where %s = ?},
                            $obj->db->quote_identifier($obj->_object_table),
                            $obj->db->quote_identifier($obj->_key_field),
                        )
                    );
                    $sth->execute($obj->id) or $obj->db->rollback;
                    my $res = $sth->fetchrow_hashref;
                    $sth->finish;
                    return $res->{expire}
                },
            },
            krb5ValidEnd => {
                ro => 1,
                managed => 1,
                get => sub {
                    my ($self) = @_;
                    my $sth = $self->object->db->prepare_cached(
                        sprintf(
                            q{select date_part('epoch', COALESCE(endcircuit,  expire))::int as expire
                            from %s where %s = ?},
                            $self->object->db->quote_identifier($self->object->_object_table),
                            $self->object->db->quote_identifier($self->object->_key_field),
                        )
                    );
                    $sth->execute($self->object->id) or $self->object->db->rollback;
                    my $res = $sth->fetchrow_hashref;
                    $sth->finish;
                    return $res->{expire}
                },
            },
            cells  => {
                ro => 1,
                reference => 'group',
            },
            departments => {
                reference => 'group',
                delayed => 1,
                ro => 1,
            },
            arrivalDate => { },
            expired => { ro => 1 },
            active => { ro => 1 },
            pwdAccountLockedTime => {
                managed => 1,
                ro => 1,
                get => sub {
                    my ($self) = @_;
                    my $obj = $self->object;
                    if ($obj->_get_c_field('locked')) {
                        return '000001010000Z';
                    } else {
                        my $sth = $obj->db->prepare_cached(
                            sprintf(
                                q{select to_char(COALESCE(endcircuit,  expire) AT TIME ZONE 'Z', 'YYYYMMDDHH24MISSZ') as expire
                                from %s where %s = ? and expire < now()},
                                $obj->db->quote_identifier($obj->_object_table),
                                $obj->db->quote_identifier($obj->_key_field),
                            )
                        );
                        $sth->execute($obj->id);
                        my $res = $sth->fetchrow_hashref;
                        $sth->finish;
                        return $res->{expire}
                    }
                },
            },
            userPassword => { readable => 0, },
            wWWHomePage => { },
            title => { },
            snNative => { },
            givenNameNative => { },
            sn => { },
            shadowWarning => { },
            shadowMin => { },
            shadowMax => { },
            shadowLastChange => { },
            shadowInactive => { },
            shadowFlag => { },
            otherTelephone => { },
            nickname => { },
            mobile => { },
            mail => { },
            labeledURI => { },
            jobType => { },
            ipPhone => { },
            initials => { },
            homePhone => { },
            homeDirectory => { },
            halReference => { },
            grade => { },
            givenName => { },
            encryptedPassword => { },
            description => { },
            company => { },
            comment => { },
            college => { },
            passwordLastSet => {
                ro => 1,
            },
        }
    )
}

sub _get_state {
    my ($self, $state) = @_;
    for ($state) {
        /^expired$/ and do {
            my $attribute = $self->attribute('expire');
            $attribute->check_acl('r') or return;
            my $sth = $self->db->prepare_cached(
                q{ select coalesce(expire < now(), false) as exp from "user" 
                where "user".name = ?}
            );
            $sth->execute($self->id);
            my $res = $sth->fetchrow_hashref;
            $sth->finish;
            return $res->{exp} ? 1 : 0;
        };
    }
}

1;

__END__

=head1 SEE ALSO

=head1 AUTHOR

Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.

=cut
