source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller/Users.pm @ 1620

Last change on this file since 1620 was 1619, checked in by nanardon, 9 years ago

Select nextEmployment if there is no current one

  • Property svn:keywords set to Id Rev
File size: 10.9 KB
Line 
1package LATMOS::Accounts::Web::Controller::Users;
2
3use strict;
4use warnings;
5use base 'Catalyst::Controller';
6
7use Date::Parse;
8use DateTime;
9use DateTime::TimeZone;
10use POSIX;
11
12=head1 NAME
13
14LATMOS::Accounts::Web::Controller::Users - Catalyst Controller
15
16=head1 DESCRIPTION
17
18Catalyst Controller.
19
20=head1 METHODS
21
22=cut
23
24
25=head2 index
26
27=cut
28
29sub index : Private {
30    my ( $self, $c ) = @_;
31
32    $c->stash->{ofilter} = $c->model('AttrFilter', 'user');
33    $c->stash->{page}{title} = "Liste des utilisateurs";
34}
35
36
37sub change_password : Private {
38    my ($self, $c, $username, $password) = @_;
39
40    my $base = $c->model('Accounts')->db;
41    $c->stash->{user} = $base->get_object('user', $username) or do {
42        $c->forward('/no_object');
43        return "Cannot get user object";
44    };
45
46    my $sync = $c->model('Accounts')->sync_access;
47
48    # This is widelly burk:
49    # synchaccess do not manage connect() at time, opening a
50    # backdoor then :\
51    foreach ($sync->bases) { $_->{_user} = $c->user->username }
52
53    my $suser = $sync->get_object_ifexists('user', $username);
54    my $msg = $c->stash->{user}->check_password($password);
55    if ($msg ne 'ok') {
56        return $msg . ', mot de passe inchangé';
57    } elsif ($suser && $suser->_set_password($password)) {
58        $sync->commit;
59        return;
60    } else {
61        return 'Erreur lors du changement de mot de passe';
62    }
63}
64
65sub 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            $arg ||= $c->stash->{user}->get_attributes('currentEmployment') ||
103                     $c->stash->{user}->get_attributes('nextEmployment');
104
105            $c->stash->{employment} = $base->get_object('employment', $arg) unless($arg =~ /^\@/);
106            $c->stash->{forme} = $c->model('AttrForms', 'employment',
107                $c->stash->{employment} ? $c->stash->{employment} : 'employment', $base);
108            $c->stash->{forme}->set_attrs if($c->stash->{employment});
109            $c->stash->{template} = 'users/employment.tt';
110            if ($arg) {
111                if ($arg =~ m/^\@/) {
112                    $c->stash->{newemployment} = 1;
113                    if ($arg =~ m/\@duplicate/i) {
114                        my $oldempl = $c->model('Accounts')->db->get_object('employment', 
115                            $c->req->param('source')
116                            ? $c->req->param('source')
117                            : ($c->stash->{user}->listEmployment)[0])
118                            or return;
119                        foreach (qw(company managerContact contratType department)) {
120                            $c->req->params->{$_} = $oldempl->get_attributes($_);
121                        }
122                        if (my $lastday = $oldempl->get_attributes('lastday')) {
123                            my $time = DateTime->from_epoch(epoch => str2time($lastday));
124                            $time->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
125                            $time->add( days => 1 );
126                            $c->req->params->{firstday} = $time->ymd;
127                        }
128                    } elsif ($arg =~ m/\@create/i) {
129
130                        for (1) {
131                            my $frtoen = $c->req->param('firstday');
132                            # Date::Parse understand only mm/dd/yyyy...
133                            $frtoen =~ s:^(\d+)/(\d+)/(\d+):$2/$1/$3:;
134                            my $time = str2time($frtoen) or last;
135                            my $lastd = DateTime->from_epoch( epoch => $time )
136                                ->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
137
138                            my ($empl) = $c->stash->{user}->listEmployment;
139                            if ($empl) {
140                                my $obj = $c->model('Accounts')->db->get_object('employment', $empl);
141                                if (!$obj->get_attributes('lastday')) {
142                                    $obj->set_c_fields(lastday => $lastd->subtract(days => 1)->ymd('-'));
143                                } 
144                            }
145
146                            my $id = $username . '-' . $lastd->ymd('-');
147                            $c->req->params->{user} = $username;
148                            if ($id = $c->stash->{forme}->set_attrs([ 'user',
149                                        map { $_->name } $c->stash->{forme}->write_attributes], $id)) {
150                                $c->res->redirect($c->uri_for('/users', $username, 'employment', $id));
151                                return 1;
152                            }
153                        }
154                    }
155                }
156            }
157            if (my $delemp = $c->req->param('delemployment')) {
158                $base->delete_object('employment', $delemp);
159                $base->commit;
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
283=head1 AUTHOR
284
285Thauvin Olivier
286
287=head1 LICENSE
288
289This library is free software, you can redistribute it and/or modify
290it under the same terms as Perl itself.
291
292=cut
293
2941;
Note: See TracBrowser for help on using the repository browser.