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

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

Add log page to users objects

  • Property svn:keywords set to Id Rev
File size: 7.1 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 Add :PathPart('add') Chained('users') Args(0) {
93    my ( $self, $c ) = @_;
94
95    $c->stash->{template} = 'users/Add.tt';
96
97    $c->stash->{form} = $c->model('AttrForms', 'add', $c->stash->{user});
98    $c->stash->{form}->set_attrs;
99
100}
101
102sub Mail :PathPart('mail') Chained('users') Args(0) {
103    my ($self, $c) = @_;
104
105    my $base = $c->model('Accounts')->db;
106
107    $c->stash->{db} = $base;
108    $c->stash->{template} = 'users/mail.tt';
109
110    if ($c->req->param('usermail')) {
111
112        my %expaliases = map { $_ => 1 } $c->req->param('expaliases');
113        foreach my $alias ($c->stash->{user}->get_attributes('aliases'), $c->stash->{user}->id) {
114            my $oalias = $base->get_object('aliases', $alias) or next;
115            if (($expaliases{ $alias } || 0) xor ($oalias->get_c_field('exported') || 0)) {
116                $oalias->set_c_fields('unexported' => ($expaliases{ $alias } ? undef : 1)) or do {
117                    $base->rollback;
118                    last;
119                };
120            }
121        }
122        $c->stash->{user}->set_c_fields(
123            mail => $c->req->param('mail') || undef,
124            otherEmail => $c->req->param('otherEmail') || undef,
125            aliases => [ grep { $_ } $c->req->param('aliases') ],
126            revaliases => $c->req->param('revaliases') || undef,
127            forward => $c->req->param('forward') || undef,
128        ) or do {
129            $base->rollback;
130            return;
131        };
132
133        $base->commit;
134    }
135}
136
137sub Password :PathPart('passwd') Chained('users') Args(0) {
138    my ($self, $c) = @_;
139    my $username = $c->stash->{username};
140
141    my $base = $c->model('Accounts')->db;
142
143    $c->stash->{template} = 'users/passwd.tt';
144    if ($c->req->param('passwd')) {
145        if ($base->check_acl($c->stash->{user}, 'userPassword', 'w')) {
146            if ($c->req->param('passwd') eq ($c->req->param('cpasswd') || '')) {
147                $c->stash->{pmerror} = $c->model('Accounts')->ChangeUserPassword(
148                    $username, $c->req->param('passwd') 
149                ) || 'Mot de passe changé';
150            } else {
151                $c->stash->{pmerror} = 'Mot de passe différents';
152            }
153        }
154    }
155}
156
157sub Groups :PathPart('groups') Chained('users') Args(0) {
158    my ($self, $c) = @_;
159    my $username = $c->stash->{username};
160    $c->stash->{template} = 'users/groups.tt';
161
162    my $base = $c->model('Accounts')->db;
163
164    # list of group for which users can be added
165    my %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []};
166    if ($c->req->param('addgroup')) {
167        $c->stash->{user}->set_c_fields('memberOf', [ keys %ingroups, $c->req->param('addgroup') ]);
168        $c->stash->{user}->base->commit;
169        # reread:
170        %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []};
171    } elsif ($c->req->param('delgroup')) {
172        $c->stash->{user}->set_c_fields('memberOf',
173            [ grep { $_ ne $c->req->param('delgroup') } keys %ingroups ]
174        );
175        $c->stash->{user}->base->commit;
176        %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []};
177    }
178
179    $c->stash->{othergroups} = [ grep { ! $ingroups{$_} } $base->list_objects('group') ];
180}
181
182sub AddressCommon :PathPart('') Chained('users') CaptureArgs(0) {
183    my ($self, $c) = @_;
184
185    my $username = $c->stash->{username};
186
187    my $base = $c->model('Accounts')->db;
188
189
190    if ($c->req->param('main')) {
191        if (my $ad = $base->get_object('address',
192                $c->req->param('main'))) {
193            $ad->base->commit;
194        }
195    }
196    $c->stash->{template} = 'users/address.tt';
197}
198
199sub Address :PathPart('address') Chained('AddressCommon') Args(0) {
200    my ($self, $c) = @_;
201
202    my $base = $c->model('Accounts')->db;
203    my $username = $c->stash->{username};
204
205    if ($c->req->param('del_addr')) {
206        $base->delete_object('address', $c->req->param('del_addr'));
207        $base->commit;
208    } elsif ($c->req->param('add_addr')) {
209        my $addname = $username . join('', map
210            {('a'..'z')[rand(26)]}(0..4));
211        if ($base->create_c_object('address',
212                $addname,
213                user => $username,
214            )) {
215            $base->commit;
216            $c->res->redirect($c->uri_for($username, 'address',
217                    $addname));
218            return;
219        }
220    }
221    my ($main) = $c->stash->{user}->get_attributes('otheraddress');
222    if ($main) {
223        $c->detach('AddressDisplay', [ $main ]);
224    }
225}
226
227sub AddressDisplay :PathPart('address') Chained('AddressCommon') Args(1) {
228    my ($self, $c, $arg) = @_;
229
230    my $base = $c->model('Accounts')->db;
231
232    $c->stash->{address} = $base->get_object('address', $arg);
233    $c->stash->{form} = $c->model('AttrForms', 'address',
234        $c->stash->{address});
235    $c->stash->{form}->set_attrs;
236}
237
238sub my :PathPart('my') Chained('users') Args(0) {
239    my ($self, $c) = @_;
240}
241
242sub log :PathPart('log') Chained('users') Args(0) {
243    my ($self, $c) = @_;
244}
245
246=head1 AUTHOR
247
248Thauvin Olivier
249
250=head1 LICENSE
251
252This library is free software, you can redistribute it and/or modify
253it under the same terms as Perl itself.
254
255=cut
256
2571;
Note: See TracBrowser for help on using the repository browser.