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

Last change on this file since 2183 was 2183, checked in by nanardon, 5 years ago

another utf8 miss

  • Property svn:keywords set to Id Rev
File size: 6.7 KB
Line 
1package LATMOS::Accounts::Web::Controller::Users;
2
3use strict;
4use warnings;
5use utf8;
6use base 'Catalyst::Controller';
7
8use Date::Parse;
9use DateTime;
10use DateTime::TimeZone;
11use POSIX;
12
13=head1 NAME
14
15LATMOS::Accounts::Web::Controller::Users - Catalyst Controller
16
17=head1 DESCRIPTION
18
19Catalyst Controller.
20
21=head1 METHODS
22
23=cut
24
25
26=head2 index
27
28=cut
29
30sub index : Private {
31    my ( $self, $c ) = @_;
32
33    $c->stash->{ofilter} = $c->model('AttrFilter', 'user');
34    $c->stash->{page}{title} = "Liste des utilisateurs";
35}
36
37sub users :PathPart('users') Chained('/') CaptureArgs(1) {
38    my ( $self, $c, $username ) = @_;
39
40    $c->stash->{subform} = (split('/', ($c->req->path ||'')))[2];
41
42    my $base = $c->model('Accounts')->db;
43
44    $c->stash->{page}{title} = "Utilisateur :: $username";
45    $c->stash->{username} = $username;
46    $c->stash->{user} = $base->get_object('user', $username) or do {
47        $c->go('/no_object');
48        return;
49    };
50    $c->stash->{page}{title} = "Utilisateur :: $username";
51
52    if ($c->req->param('make_active')) {
53        $c->stash->{user}->set_c_fields('unexported' => undef);
54        $base->commit;
55    }
56    if ($c->req->param('make_inactive')) {
57        $c->stash->{user}->set_c_fields('unexported' => 1);
58        $base->commit;
59    }
60    if ($c->req->param('delete')) {
61        $base->delete_object('user', $username);
62        $base->commit;
63        $c->res->redirect('/users');
64        return;
65    }
66}
67
68sub Display :PathPart('') Chained('users') Args(0) {
69    my ( $self, $c ) = @_;
70
71    if ($c->config->{features}->{user_employment}) {
72        #Replace standard RH form by employment
73        $c->detach('/users/employment/index');
74        return 1;
75    }
76
77    $c->stash->{form} = $c->model('AttrForms', 'user', $c->stash->{user});
78    $c->stash->{form}->set_attrs;
79
80}
81
82sub Sys :PathPart('sys') Chained('users') Args(0) {
83    my ( $self, $c ) = @_;
84
85    $c->stash->{template} = 'users/Display.tt';
86
87    $c->stash->{form} = $c->model('AttrForms', 'usersys', $c->stash->{user});
88    $c->stash->{form}->set_attrs;
89
90}
91
92sub Mail :PathPart('mail') Chained('users') Args(0) {
93    my ($self, $c) = @_;
94
95    my $base = $c->model('Accounts')->db;
96
97    $c->stash->{db} = $base;
98    $c->stash->{template} = 'users/mail.tt';
99
100    if ($c->req->param('usermail')) {
101
102        my %expaliases = map { $_ => 1 } $c->req->param('expaliases');
103        foreach my $alias ($c->stash->{user}->get_attributes('aliases'), $c->stash->{user}->id) {
104            my $oalias = $base->get_object('aliases', $alias) or next;
105            if (($expaliases{ $alias } || 0) xor ($oalias->get_c_field('exported') || 0)) {
106                $oalias->set_c_fields('unexported' => ($expaliases{ $alias } ? undef : 1)) or do {
107                    $base->rollback;
108                    last;
109                };
110            }
111        }
112        $c->stash->{user}->set_c_fields(
113            mail => $c->req->param('mail') || undef,
114            otherEmail => $c->req->param('otherEmail') || undef,
115            aliases => [ grep { $_ } $c->req->param('aliases') ],
116            revaliases => $c->req->param('revaliases') || undef,
117            forward => $c->req->param('forward') || undef,
118        ) or do {
119            $base->rollback;
120            return;
121        };
122
123        $base->commit;
124    }
125}
126
127sub Password :PathPart('passwd') Chained('users') Args(0) {
128    my ($self, $c) = @_;
129    my $username = $c->stash->{username};
130
131    my $base = $c->model('Accounts')->db;
132
133    $c->stash->{template} = 'users/passwd.tt';
134    if ($c->req->param('passwd')) {
135        if ($base->check_acl($c->stash->{user}, 'userPassword', 'w')) {
136            if ($c->req->param('passwd') eq ($c->req->param('cpasswd') || '')) {
137                $c->stash->{pmerror} = $c->model('Accounts')->ChangeUserPassword(
138                    $username, $c->req->param('passwd') 
139                ) || 'Mot de passe changé';
140            } else {
141                $c->stash->{pmerror} = 'Mot de passe différents';
142            }
143        }
144    }
145}
146
147sub Groups :PathPart('groups') Chained('users') Args(0) {
148    my ($self, $c) = @_;
149    my $username = $c->stash->{username};
150    $c->stash->{template} = 'users/groups.tt';
151
152    my $base = $c->model('Accounts')->db;
153
154    # list of group for which users can be added
155    my %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []};
156    if ($c->req->param('addgroup')) {
157        $c->stash->{user}->set_c_fields('memberOf', [ keys %ingroups, $c->req->param('addgroup') ]);
158        $c->stash->{user}->base->commit;
159        # reread:
160        %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []};
161    } elsif ($c->req->param('delgroup')) {
162        $c->stash->{user}->set_c_fields('memberOf',
163            [ grep { $_ ne $c->req->param('delgroup') } keys %ingroups ]
164        );
165        $c->stash->{user}->base->commit;
166        %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []};
167    }
168
169    $c->stash->{othergroups} = [ grep { ! $ingroups{$_} } $base->list_objects('group') ];
170}
171
172sub AddressCommon :PathPart('') Chained('users') CaptureArgs(0) {
173    my ($self, $c) = @_;
174
175    my $username = $c->stash->{username};
176
177    my $base = $c->model('Accounts')->db;
178
179
180    if ($c->req->param('main')) {
181        if (my $ad = $base->get_object('address',
182                $c->req->param('main'))) {
183            $ad->base->commit;
184        }
185    }
186    $c->stash->{template} = 'users/address.tt';
187}
188
189sub Address :PathPart('address') Chained('AddressCommon') Args(0) {
190    my ($self, $c) = @_;
191
192    my $base = $c->model('Accounts')->db;
193    my $username = $c->stash->{username};
194
195    if ($c->req->param('del_addr')) {
196        $base->delete_object('address', $c->req->param('del_addr'));
197        $base->commit;
198    } elsif ($c->req->param('add_addr')) {
199        my $addname = $username . join('', map
200            {('a'..'z')[rand(26)]}(0..4));
201        if ($base->create_c_object('address',
202                $addname,
203                user => $username,
204            )) {
205            $base->commit;
206            $c->res->redirect($c->uri_for($username, 'address',
207                    $addname));
208            return;
209        }
210    }
211    my ($main) = $c->stash->{user}->get_attributes('otheraddress');
212    if ($main) {
213        $c->detach('AddressDisplay', [ $main ]);
214    }
215}
216
217sub AddressDisplay :PathPart('address') Chained('AddressCommon') Args(1) {
218    my ($self, $c, $arg) = @_;
219
220    my $base = $c->model('Accounts')->db;
221
222    $c->stash->{address} = $base->get_object('address', $arg);
223    $c->stash->{form} = $c->model('AttrForms', 'address',
224        $c->stash->{address});
225    $c->stash->{form}->set_attrs;
226}
227
228sub my :PathPart('my') Chained('users') Args(0) {
229    my ($self, $c) = @_;
230}
231
232=head1 AUTHOR
233
234Thauvin Olivier
235
236=head1 LICENSE
237
238This library is free software, you can redistribute it and/or modify
239it under the same terms as Perl itself.
240
241=cut
242
2431;
Note: See TracBrowser for help on using the repository browser.