Ignore:
Timestamp:
01/26/16 18:42:54 (8 years ago)
Author:
nanardon
Message:

Split the users/ code

Location:
trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller/Users.pm

    r1650 r1662  
    3434} 
    3535 
     36sub users :PathPart('users') Chained('/') CaptureArgs(1) { 
     37    my ( $self, $c, $username ) = @_; 
     38 
     39    $c->stash->{subform} = (split('/', ($c->req->path ||'')))[2]; 
     40 
     41    my $base = $c->model('Accounts')->db; 
     42 
     43    $c->stash->{page}{title} = "Utilisateur :: $username"; 
     44    $c->stash->{username} = $username; 
     45    $c->stash->{user} = $base->get_object('user', $username) or do { 
     46        $c->forward('/no_object'); 
     47        return; 
     48    }; 
     49    $c->stash->{page}{title} = "Utilisateur :: $username"; 
     50 
     51    if ($c->req->param('make_active')) { 
     52        $c->stash->{user}->set_c_fields('unexported' => undef); 
     53        $base->commit; 
     54    } 
     55    if ($c->req->param('make_inactive')) { 
     56        $c->stash->{user}->set_c_fields('unexported' => 1); 
     57        $base->commit; 
     58    } 
     59    if ($c->req->param('delete')) { 
     60        $base->delete_object('user', $username); 
     61        $base->commit; 
     62        $c->res->redirect('/users'); 
     63        return; 
     64    } 
     65} 
     66 
     67sub Display :PathPart('') Chained('users') Args(0) { 
     68    my ( $self, $c ) = @_; 
     69 
     70    if ($c->config->{features}->{user_employment}) { 
     71        #Replace standard RH form by employment 
     72        $c->detach('/users/employment/index'); 
     73        return 1; 
     74    } 
     75 
     76    $c->stash->{form} = $c->model('AttrForms', 'user', $c->stash->{user}); 
     77    $c->stash->{form}->set_attrs; 
     78 
     79} 
     80 
     81sub Sys :PathPart('sys') Chained('users') Args(0) { 
     82    my ( $self, $c ) = @_; 
     83 
     84    $c->stash->{template} = 'users/Display.tt'; 
     85 
     86    $c->stash->{form} = $c->model('AttrForms', 'usersys', $c->stash->{user}); 
     87    $c->stash->{form}->set_attrs; 
     88 
     89} 
     90 
     91sub Mail :PathPart('mail') Chained('users') Args(0) { 
     92    my ($self, $c) = @_; 
     93 
     94    my $base = $c->model('Accounts')->db; 
     95 
     96    $c->stash->{db} = $base; 
     97    $c->stash->{template} = 'users/mail.tt'; 
     98 
     99    if ($c->req->param('usermail')) { 
     100 
     101        my %expaliases = map { $_ => 1 } $c->req->param('expaliases'); 
     102        foreach my $alias ($c->stash->{user}->get_attributes('aliases'), $c->stash->{user}->id) { 
     103            my $oalias = $base->get_object('aliases', $alias) or next; 
     104            if (($expaliases{ $alias } || 0) xor ($oalias->get_c_field('exported') || 0)) { 
     105                $oalias->set_c_fields('unexported' => ($expaliases{ $alias } ? undef : 1)) or do { 
     106                    $base->rollback; 
     107                    last; 
     108                }; 
     109            } 
     110        } 
     111        $c->stash->{user}->set_c_fields( 
     112            mail => $c->req->param('mail') || undef, 
     113            aliases => [ grep { $_ } $c->req->param('aliases') ], 
     114            revaliases => $c->req->param('revaliases') || undef, 
     115            forward => $c->req->param('forward') || undef, 
     116        ) or do { 
     117            $base->rollback; 
     118            last; 
     119        }; 
     120 
     121        $base->commit; 
     122    } 
     123} 
     124 
     125sub Password :PathPart('passwd') Chained('users') Args(0) { 
     126    my ($self, $c) = @_; 
     127    my $username = $c->stash->{username}; 
     128 
     129    my $base = $c->model('Accounts')->db; 
     130 
     131    $c->stash->{template} = 'users/passwd.tt'; 
     132    if ($c->req->param('passwd')) { 
     133        if ($base->check_acl($c->stash->{user}, 'userPassword', 'w')) { 
     134            if ($c->req->param('passwd') eq ($c->req->param('cpasswd') || '')) { 
     135                $c->stash->{pmerror} = $c->forward('change_password', 
     136                    [ $username, $c->req->param('passwd') ] ) || 'Mot de passe changé'; 
     137            } else { 
     138                $c->stash->{pmerror} = 'Mot de passe différents'; 
     139            } 
     140        } 
     141    } 
     142} 
     143 
     144sub Groups :PathPart('groups') Chained('users') Args(0) { 
     145    my ($self, $c) = @_; 
     146    my $username = $c->stash->{username}; 
     147    $c->stash->{template} = 'users/groups.tt'; 
     148 
     149    my $base = $c->model('Accounts')->db; 
     150 
     151    # list of group for which users can be added 
     152    my %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []}; 
     153    if ($c->req->param('addgroup')) { 
     154        $c->stash->{user}->set_c_fields('memberOf', [ keys %ingroups, $c->req->param('addgroup') ]); 
     155        $c->stash->{user}->base->commit; 
     156        # reread: 
     157        %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []}; 
     158    } elsif ($c->req->param('delgroup')) { 
     159        $c->stash->{user}->set_c_fields('memberOf', 
     160            [ grep { $_ ne $c->req->param('delgroup') } keys %ingroups ] 
     161        ); 
     162        $c->stash->{user}->base->commit; 
     163        %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []}; 
     164    } 
     165 
     166    $c->stash->{othergroups} = [ grep { ! $ingroups{$_} } $base->list_objects('group') ]; 
     167} 
     168 
     169sub AddressCommon :PathPart('') Chained('users') CaptureArgs(0) { 
     170    my ($self, $c) = @_; 
     171 
     172    my $username = $c->stash->{username}; 
     173 
     174    my $base = $c->model('Accounts')->db; 
     175 
     176 
     177    if ($c->req->param('main')) { 
     178        if (my $ad = $base->get_object('address', 
     179                $c->req->param('main'))) { 
     180            $ad->base->commit; 
     181        } 
     182    } 
     183    $c->stash->{template} = 'users/address.tt'; 
     184} 
     185 
     186sub Address :PathPart('address') Chained('AddressCommon') Args(0) { 
     187    my ($self, $c) = @_; 
     188 
     189    my $base = $c->model('Accounts')->db; 
     190    my $username = $c->stash->{username}; 
     191 
     192    if ($c->req->param('del_addr')) { 
     193        $base->delete_object('address', $c->req->param('del_addr')); 
     194        $base->commit; 
     195    } elsif ($c->req->param('add_addr')) { 
     196        my $addname = $username . join('', map 
     197            {('a'..'z')[rand(26)]}(0..4)); 
     198        if ($base->create_c_object('address', 
     199                $addname, 
     200                user => $username, 
     201            )) { 
     202            $base->commit; 
     203            $c->res->redirect($c->uri_for($username, 'address', 
     204                    $addname)); 
     205            return; 
     206        } 
     207    } 
     208    my ($main) = $c->stash->{user}->get_attributes('otheraddress'); 
     209    if ($main) { 
     210        $c->detach('AddressDisplay', [ $main ]); 
     211    } 
     212} 
     213 
     214sub AddressDisplay :PathPart('address') Chained('AddressCommon') Args(1) { 
     215    my ($self, $c, $arg) = @_; 
     216 
     217    my $base = $c->model('Accounts')->db; 
     218 
     219    $c->stash->{address} = $base->get_object('address', $arg); 
     220    $c->stash->{form} = $c->model('AttrForms', 'address', 
     221        $c->stash->{address}); 
     222    $c->stash->{form}->set_attrs; 
     223} 
     224 
     225sub my :PathPart('my') Chained('users') Args(0) { 
     226    my ($self, $c) = @_; 
     227} 
    36228 
    37229sub change_password : Private { 
     
    63255} 
    64256 
    65 sub default : LocalPath { 
    66     my ( $self, $c, undef, $username, $subform, $arg ) = @_; 
    67  
    68     if ($c->config->{features}->{user_employment}) { 
    69         # Replace standard RH form by employment 
    70         $subform ||= 'employment'; 
    71     } 
    72  
    73     my $base = $c->model('Accounts')->db; 
    74  
    75     $c->stash->{page}{title} = "Utilisateur :: $username"; 
    76     $c->stash->{username} = $username; 
    77     $c->stash->{user} = $base->get_object('user', $username) or do { 
    78         $c->forward('/no_object'); 
    79         return; 
    80     }; 
    81     $c->stash->{subform} = $subform || ''; 
    82     $c->stash->{page}{title} = "Utilisateur :: $username"; 
    83  
    84     if ($c->req->param('make_active')) { 
    85         $c->stash->{user}->set_c_fields('unexported' => undef); 
    86         $base->commit; 
    87     } 
    88     if ($c->req->param('make_inactive')) { 
    89         $c->stash->{user}->set_c_fields('unexported' => 1); 
    90         $base->commit; 
    91     } 
    92     if ($c->req->param('delete')) { 
    93         $base->delete_object('user', $username); 
    94         $base->commit; 
    95         $c->res->redirect('/users'); 
    96     } 
    97  
    98  
    99     for ($subform || '') { 
    100         # Exceptions... 
    101         /^employment$/ and do { 
    102             if (my $delemp = $c->req->param('delemployment')) { 
    103                 $base->delete_object('employment', $delemp); 
    104                 $base->commit; 
    105             } 
    106             $arg ||= $c->stash->{user}->get_attributes('currentEmployment') || 
    107                      $c->stash->{user}->get_attributes('nextEmployment'); 
    108  
    109             $c->stash->{employment} = $base->get_object('employment', $arg) unless($arg =~ /^\@/); 
    110             $c->stash->{forme} = $c->model('AttrForms', 'employment', 
    111                 $c->stash->{employment} ? $c->stash->{employment} : 'employment', $base); 
    112             $c->stash->{forme}->set_attrs if($c->stash->{employment}); 
    113             $c->stash->{template} = 'users/employment.tt'; 
    114             if ($arg) { 
    115                 if ($arg =~ m/^\@/) { 
    116                     $c->stash->{newemployment} = 1; 
    117                     if ($arg =~ m/\@duplicate/i) { 
    118                         my $oldempl = $c->model('Accounts')->db->get_object('employment',  
    119                             $c->req->param('source') 
    120                             ? $c->req->param('source') 
    121                             : ($c->stash->{user}->listEmployment)[0]) 
    122                             or return; 
    123                         foreach (qw(company managerContact contratType department)) { 
    124                             $c->req->params->{$_} = $oldempl->get_attributes($_); 
    125                         } 
    126                         if (my $lastday = $oldempl->get_attributes('lastday')) { 
    127                             my $time = DateTime->from_epoch(epoch => str2time($lastday)); 
    128                             $time->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
    129                             $time->add( days => 1 ); 
    130                             $c->req->params->{firstday} = $time->ymd; 
    131                         } 
    132                     } elsif ($arg =~ m/\@create/i) { 
    133  
    134                         for (1) { 
    135                             my $frtoen = $c->req->param('firstday'); 
    136                             # Date::Parse understand only mm/dd/yyyy... 
    137                             $frtoen =~ s:^(\d+)/(\d+)/(\d+):$2/$1/$3:; 
    138                             my $time = str2time($frtoen) or last; 
    139                             my $lastd = DateTime->from_epoch( epoch => $time ) 
    140                                 ->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
    141  
    142                             my ($empl) = $c->stash->{user}->listEmployment; 
    143                             if ($empl) { 
    144                                 my $obj = $c->model('Accounts')->db->get_object('employment', $empl); 
    145                                 if (!$obj->get_attributes('lastday')) { 
    146                                     $obj->set_c_fields(lastday => $lastd->subtract(days => 1)->ymd('-')); 
    147                                 }  
    148                             } 
    149  
    150                             my $id = $username . '-' . $lastd->ymd('-'); 
    151                             $c->req->params->{user} = $username; 
    152                             if ($id = $c->stash->{forme}->set_attrs([ 'user', 
    153                                         map { $_->name } $c->stash->{forme}->write_attributes], $id)) { 
    154                                 $c->res->redirect($c->uri_for('/users', $username, 'employment', $id)); 
    155                                 return 1; 
    156                             } 
    157                         } 
    158                     } 
    159                 } 
    160             } 
    161              
    162         }; 
    163         /^passwd$/ and do { 
    164             $c->stash->{template} = 'users/passwd.tt'; 
    165             if ($c->req->param('passwd')) { 
    166                 if ($base->check_acl($c->stash->{user}, 'userPassword', 'w')) { 
    167                     if ($c->req->param('passwd') eq ($c->req->param('cpasswd') || '')) { 
    168                         $c->stash->{pmerror} = $c->forward('change_password', 
    169                             [ $username, $c->req->param('passwd') ] ) || 'Mot de passe changé'; 
    170                     } else { 
    171                         $c->stash->{pmerror} = 'Mot de passe différents'; 
    172                     } 
    173                 } 
    174             } 
    175             last; 
    176         }; 
    177         /^groups$/ and do { 
    178             $c->stash->{template} = 'users/groups.tt'; 
    179             # list of group for which users can be added 
    180             my %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []}; 
    181             if ($c->req->param('addgroup')) { 
    182                 $c->stash->{user}->set_c_fields('memberOf', [ keys %ingroups, $c->req->param('addgroup') ]); 
    183                 $c->stash->{user}->base->commit; 
    184                 # reread: 
    185                 %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []}; 
    186             } elsif ($c->req->param('delgroup')) { 
    187                 $c->stash->{user}->set_c_fields('memberOf', 
    188                     [ grep { $_ ne $c->req->param('delgroup') } keys %ingroups ] 
    189                 ); 
    190                 $c->stash->{user}->base->commit; 
    191                 %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []}; 
    192             } 
    193  
    194             $c->stash->{othergroups} = [ grep { ! $ingroups{$_} } $base->list_objects('group') ]; 
    195             last; 
    196         }; 
    197         /^address$/ and do { 
    198             if ($arg) { 
    199                 $c->stash->{address} = $base->get_object('address', $arg); 
    200             } else { 
    201  
    202                 if ($c->req->param('del_addr')) { 
    203                     $base->delete_object('address', $c->req->param('del_addr')); 
    204                     $base->commit; 
    205                 } elsif ($c->req->param('add_addr')) { 
    206                     my $addname = $username . join('', map 
    207                         {('a'..'z')[rand(26)]}(0..4)); 
    208                     if ($base->create_c_object('address', 
    209                         $addname, 
    210                         user => $username, 
    211                     )) { 
    212                         $base->commit; 
    213                         $c->res->redirect($c->uri_for($username, 'address', 
    214                                 $addname)); 
    215                         return; 
    216                     } 
    217                 } elsif ($c->req->param('main')) { 
    218                     if (my $ad = $base->get_object('address', 
    219                             $c->req->param('main'))) { 
    220                         $ad->base->commit; 
    221                     } 
    222                 } 
    223  
    224                 my ($main) = $c->stash->{user}->get_attributes('otheraddress'); 
    225                 $c->stash->{address} = $base->get_object('address', $main); 
    226             } 
    227             if ($c->stash->{address}) { 
    228                 $c->stash->{form} = $c->model('AttrForms', 'address', 
    229                     $c->stash->{address}); 
    230                 $c->stash->{form}->set_attrs; 
    231             } 
    232             $c->stash->{template} = 'users/address.tt'; 
    233  
    234             last; 
    235         }; 
    236         /^mail$/ and do { 
    237  
    238             $c->stash->{db} = $base; 
    239             $c->stash->{template} = 'users/mail.tt'; 
    240  
    241             if ($c->req->param('usermail')) { 
    242  
    243                 my %expaliases = map { $_ => 1 } $c->req->param('expaliases'); 
    244                 foreach my $alias ($c->stash->{user}->get_attributes('aliases'), $c->stash->{user}->id) { 
    245                     my $oalias = $base->get_object('aliases', $alias) or next; 
    246                     if (($expaliases{ $alias } || 0) xor ($oalias->get_c_field('exported') || 0)) { 
    247                         $oalias->set_c_fields('unexported' => ($expaliases{ $alias } ? undef : 1)) or do { 
    248                             $base->rollback; 
    249                             last; 
    250                         }; 
    251                     } 
    252                 } 
    253                 $c->stash->{user}->set_c_fields( 
    254                     mail => $c->req->param('mail') || undef, 
    255                     aliases => [ grep { $_ } $c->req->param('aliases') ], 
    256                     revaliases => $c->req->param('revaliases') || undef, 
    257                     forward => $c->req->param('forward') || undef, 
    258                 ) or do { 
    259                     $base->rollback; 
    260                     last; 
    261                 }; 
    262  
    263                 $base->commit; 
    264             } 
    265  
    266             last; 
    267         }; 
    268         /^my$/ and do { 
    269             $c->stash->{db} = $base; 
    270             $c->stash->{template} = 'users/my.tt'; 
    271         }; 
    272         /^dump$/ and do { 
    273             $c->stash->{db} = $base; 
    274             $c->stash->{template} = 'users/dump.tt' 
    275             #$c->res->body($c->stash->{user}->dump({only_rw => 1})); 
    276         }; 
    277  
    278         $c->stash->{form} = $c->model('AttrForms', 'user' . $c->stash->{subform}, $c->stash->{user}); 
    279         $c->stash->{form}->set_attrs; 
    280     } 
    281 } 
    282  
    283257=head1 AUTHOR 
    284258 
Note: See TracChangeset for help on using the changeset viewer.