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

Last change on this file since 1607 was 1519, checked in by nanardon, 9 years ago

Translate date to english before parsing

  • Property svn:keywords set to Id Rev
File size: 10.8 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
104            $c->stash->{employment} = $base->get_object('employment', $arg) unless($arg =~ /^\@/);
105            $c->stash->{forme} = $c->model('AttrForms', 'employment',
106                $c->stash->{employment} ? $c->stash->{employment} : 'employment', $base);
107            $c->stash->{forme}->set_attrs if($c->stash->{employment});
108            $c->stash->{template} = 'users/employment.tt';
109            if ($arg) {
110                if ($arg =~ m/^\@/) {
111                    $c->stash->{newemployment} = 1;
112                    if ($arg =~ m/\@duplicate/i) {
113                        my $oldempl = $c->model('Accounts')->db->get_object('employment', 
114                            $c->req->param('source')
115                            ? $c->req->param('source')
116                            : ($c->stash->{user}->listEmployment)[0])
117                            or return;
118                        foreach (qw(company managerContact contratType department)) {
119                            $c->req->params->{$_} = $oldempl->get_attributes($_);
120                        }
121                        if (my $lastday = $oldempl->get_attributes('lastday')) {
122                            my $time = DateTime->from_epoch(epoch => str2time($lastday));
123                            $time->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
124                            $time->add( days => 1 );
125                            $c->req->params->{firstday} = $time->ymd;
126                        }
127                    } elsif ($arg =~ m/\@create/i) {
128
129                        for (1) {
130                            my $frtoen = $c->req->param('firstday');
131                            # Date::Parse understand only mm/dd/yyyy...
132                            $frtoen =~ s:^(\d+)/(\d+)/(\d+):$2/$1/$3:;
133                            my $time = str2time($frtoen) or last;
134                            my $lastd = DateTime->from_epoch( epoch => $time )
135                                ->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
136
137                            my ($empl) = $c->stash->{user}->listEmployment;
138                            if ($empl) {
139                                my $obj = $c->model('Accounts')->db->get_object('employment', $empl);
140                                if (!$obj->get_attributes('lastday')) {
141                                    $obj->set_c_fields(lastday => $lastd->subtract(days => 1)->ymd('-'));
142                                } 
143                            }
144
145                            my $id = $username . '-' . $lastd->ymd('-');
146                            $c->req->params->{user} = $username;
147                            if ($id = $c->stash->{forme}->set_attrs([ 'user',
148                                        map { $_->name } $c->stash->{forme}->write_attributes], $id)) {
149                                $c->res->redirect($c->uri_for('/users', $username, 'employment', $id));
150                                return 1;
151                            }
152                        }
153                    }
154                }
155            }
156            if (my $delemp = $c->req->param('delemployment')) {
157                $base->delete_object('employment', $delemp);
158                $base->commit;
159            }
160           
161        };
162        /^passwd$/ and do {
163            $c->stash->{template} = 'users/passwd.tt';
164            if ($c->req->param('passwd')) {
165                if ($base->check_acl($c->stash->{user}, 'userPassword', 'w')) {
166                    if ($c->req->param('passwd') eq ($c->req->param('cpasswd') || '')) {
167                        $c->stash->{pmerror} = $c->forward('change_password',
168                            [ $username, $c->req->param('passwd') ] ) || 'Mot de passe changé';
169                    } else {
170                        $c->stash->{pmerror} = 'Mot de passe différents';
171                    }
172                }
173            }
174            last;
175        };
176        /^groups$/ and do {
177            $c->stash->{template} = 'users/groups.tt';
178            # list of group for which users can be added
179            my %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []};
180            if ($c->req->param('addgroup')) {
181                $c->stash->{user}->set_c_fields('memberOf', [ keys %ingroups, $c->req->param('addgroup') ]);
182                $c->stash->{user}->base->commit;
183                # reread:
184                %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []};
185            } elsif ($c->req->param('delgroup')) {
186                $c->stash->{user}->set_c_fields('memberOf',
187                    [ grep { $_ ne $c->req->param('delgroup') } keys %ingroups ]
188                );
189                $c->stash->{user}->base->commit;
190                %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []};
191            }
192
193            $c->stash->{othergroups} = [ grep { ! $ingroups{$_} } $base->list_objects('group') ];
194            last;
195        };
196        /^address$/ and do {
197            if ($arg) {
198                $c->stash->{address} = $base->get_object('address', $arg);
199            } else {
200
201                if ($c->req->param('del_addr')) {
202                    $base->delete_object('address', $c->req->param('del_addr'));
203                    $base->commit;
204                } elsif ($c->req->param('add_addr')) {
205                    my $addname = $username . join('', map
206                        {('a'..'z')[rand(26)]}(0..4));
207                    if ($base->create_c_object('address',
208                        $addname,
209                        user => $username,
210                    )) {
211                        $base->commit;
212                        $c->res->redirect($c->uri_for($username, 'address',
213                                $addname));
214                        return;
215                    }
216                } elsif ($c->req->param('main')) {
217                    if (my $ad = $base->get_object('address',
218                            $c->req->param('main'))) {
219                        $ad->base->commit;
220                    }
221                }
222
223                my ($main) = $c->stash->{user}->get_attributes('otheraddress');
224                $c->stash->{address} = $base->get_object('address', $main);
225            }
226            if ($c->stash->{address}) {
227                $c->stash->{form} = $c->model('AttrForms', 'address',
228                    $c->stash->{address});
229                $c->stash->{form}->set_attrs;
230            }
231            $c->stash->{template} = 'users/address.tt';
232
233            last;
234        };
235        /^mail$/ and do {
236
237            $c->stash->{db} = $base;
238            $c->stash->{template} = 'users/mail.tt';
239
240            if ($c->req->param('usermail')) {
241
242                my %expaliases = map { $_ => 1 } $c->req->param('expaliases');
243                foreach my $alias ($c->stash->{user}->get_attributes('aliases'), $c->stash->{user}->id) {
244                    my $oalias = $base->get_object('aliases', $alias) or next;
245                    if (($expaliases{ $alias } || 0) xor ($oalias->get_c_field('exported') || 0)) {
246                        $oalias->set_c_fields('unexported' => ($expaliases{ $alias } ? undef : 1)) or do {
247                            $base->rollback;
248                            last;
249                        };
250                    }
251                }
252                $c->stash->{user}->set_c_fields(
253                    mail => $c->req->param('mail') || undef,
254                    aliases => [ grep { $_ } $c->req->param('aliases') ],
255                    revaliases => $c->req->param('revaliases') || undef,
256                    forward => $c->req->param('forward') || undef,
257                ) or do {
258                    $base->rollback;
259                    last;
260                };
261
262                $base->commit;
263            }
264
265            last;
266        };
267        /^my$/ and do {
268            $c->stash->{db} = $base;
269            $c->stash->{template} = 'users/my.tt';
270        };
271        /^dump$/ and do {
272            $c->stash->{db} = $base;
273            $c->stash->{template} = 'users/dump.tt'
274            #$c->res->body($c->stash->{user}->dump({only_rw => 1}));
275        };
276
277        $c->stash->{form} = $c->model('AttrForms', 'user' . $c->stash->{subform}, $c->stash->{user});
278        $c->stash->{form}->set_attrs;
279    }
280}
281
282=head1 AUTHOR
283
284Thauvin Olivier
285
286=head1 LICENSE
287
288This library is free software, you can redistribute it and/or modify
289it under the same terms as Perl itself.
290
291=cut
292
2931;
Note: See TracBrowser for help on using the repository browser.