Ignore:
Timestamp:
12/22/15 17:36:36 (9 years ago)
Author:
nanardon
Message:

Allow fine control on ro attributes

This patch allow to control why the status attributes become read-only when
using employment.

File:
1 edited

Legend:

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

    r1522 r1524  
    8787    }; 
    8888 
    89     $class->SUPER::_get_attr_schema($base, 
    90         { 
     89    my $attrs = { 
    9190            exported => { 
    9291                post => sub { 
     
    197196            }, 
    198197            expire        => { 
    199                 ro => sub { !$_[0] and return 1; $_[0]->listEmployment ? 1 : 0 }, 
    200198                inline => 1, 
    201199                formtype => 'DATETIME', 
     
    203201            }, 
    204202            endcircuit    => { 
    205                 ro => sub { !$_[0] and return 1; $_[0]->listEmployment ? 1 : 0 }, 
    206203                inline => 1, 
    207204                formtype => 'DATE', 
     
    511508            }, 
    512509            department => { 
    513                 ro => sub { !$_[0] and return 1; $_[0]->listEmployment ? 1 : 0 }, 
    514510                reference => 'group', 
    515511                can_values => sub { 
     
    519515            }, 
    520516            contratType => { 
    521                 ro => sub { !$_[0] and return 1; $_[0]->listEmployment ? 1 : 0 }, 
    522517                reference => 'group', 
    523518                can_values => sub { 
     
    837832            }, 
    838833            managerContact => { 
    839                 ro => sub { !$_[0] and return 1; $_[0]->listEmployment ? 1 : 0 }, 
    840834                delayed => 1, 
    841835                can_values => sub { 
     
    950944            encryptedPassword => { }, 
    951945            description => { }, 
    952             company => { 
    953                 ro => sub { !$_[0] and return 1; $_[0]->listEmployment ? 1 : 0 }, 
    954             }, 
     946            company => { }, 
    955947            comment => { }, 
    956948            college => { }, 
     
    983975                } 
    984976            }, 
     977            nextEmployment => { 
     978                managed => 1, 
     979                ro => 1, 
     980                reference => 'employment', 
     981                get => sub { 
     982                    my ($attr) = @_; 
     983                    my $self = $attr->object; 
     984 
     985                    my $sth = $self->base->db->prepare_cached( 
     986                        q{ 
     987                        select name from employment where firstday > now() and 
     988                        (lastday is null or lastday >= now() - '1 days'::interval) and "user" = ? 
     989                        limit 1 
     990                        } 
     991                    ); 
     992                    $sth->execute($self->id); 
     993                    my $res = $sth->fetchrow_hashref; 
     994                    $sth->finish; 
     995                    if ($res) { 
     996                        return $res->{name} 
     997                    } else { 
     998                        return; 
     999                    } 
     1000                } 
     1001            }, 
    9851002            appliedEmployement => { 
    9861003                hidden => 1, 
    9871004                reference => 'employment', 
    9881005            }, 
    989         } 
    990     ) 
     1006    }; 
     1007 
     1008    foreach (qw(expire contratType managerContact company endcircuit department)) { 
     1009        $attrs->{$_}{ro} = sub { 
     1010 
     1011            my $setting = $base->config('employment_lock_user') || 'any'; 
     1012 
     1013            for ($setting) { 
     1014                /^always$/ and return 1; 
     1015                /^never$/ and return 0; 
     1016 
     1017                !$_[0] and return 1; 
     1018 
     1019                /^any$/i and return $_[0]->listEmployment ? 1 : 0; 
     1020                /^active/i and do { 
     1021                    return $_[0]->_get_c_field('currentEmployment') 
     1022                        ? 1 
     1023                        : $_[0]->_get_c_field('nextEmployment') ? 1 : 0; 
     1024                }; 
     1025                /(\S+)=(\S+)/ and do { 
     1026                    my $attr = $_[0]->_get_c_field($1); 
     1027                    if (defined($attr)) { 
     1028                        if ($2 eq '*') { 
     1029                            return 1; 
     1030                        } elsif($2 eq $attr) { 
     1031                            return 1; 
     1032                        } else { 
     1033                            return 0; 
     1034                        } 
     1035                    } else { 
     1036                        return 0; 
     1037                    } 
     1038                }; 
     1039            } 
     1040            return $_[0]->listEmployment ? 1 : 0; # default is any! 
     1041        }; 
     1042    } 
     1043 
     1044    $class->SUPER::_get_attr_schema($base, $attrs) 
    9911045} 
    9921046 
Note: See TracChangeset for help on using the changeset viewer.