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

Last change on this file since 2054 was 2002, checked in by nanardon, 7 years ago

Move code to model to make it reusable

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