1 | package LATMOS::Accounts::Web::Controller::Users; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'Catalyst::Controller'; |
---|
6 | |
---|
7 | =head1 NAME |
---|
8 | |
---|
9 | LATMOS::Accounts::Web::Controller::Users - Catalyst Controller |
---|
10 | |
---|
11 | =head1 DESCRIPTION |
---|
12 | |
---|
13 | Catalyst Controller. |
---|
14 | |
---|
15 | =head1 METHODS |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | |
---|
20 | =head2 index |
---|
21 | |
---|
22 | =cut |
---|
23 | |
---|
24 | sub index : Private { |
---|
25 | my ( $self, $c ) = @_; |
---|
26 | |
---|
27 | $c->stash->{ofilter} = $c->model('AttrFilter', 'user'); |
---|
28 | $c->stash->{page}{title} = "Liste des utilisateurs"; |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | sub change_password : Private { |
---|
33 | my ($self, $c, $username, $password) = @_; |
---|
34 | |
---|
35 | my $base = $c->model('Accounts')->db; |
---|
36 | $c->stash->{user} = $base->get_object('user', $username) or do { |
---|
37 | $c->forward('/no_object'); |
---|
38 | return "Cannot get user object"; |
---|
39 | }; |
---|
40 | |
---|
41 | my $sync = $c->model('Accounts')->sync_access; |
---|
42 | |
---|
43 | # This is widelly burk: |
---|
44 | # synchaccess do not manage connect() at time, opening a |
---|
45 | # backdoor then :\ |
---|
46 | foreach ($sync->bases) { $_->{_user} = $username } |
---|
47 | |
---|
48 | my $suser = $sync->get_object('user', $username); |
---|
49 | my $msg = $c->stash->{user}->check_password($password); |
---|
50 | if ($msg ne 'ok') { |
---|
51 | return $msg . ', mot de passe inchangé'; |
---|
52 | } elsif ($suser && $suser->_set_password($password)) { |
---|
53 | $sync->commit; |
---|
54 | return; |
---|
55 | } else { |
---|
56 | return 'Erreur lors du changement de mot de passe'; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | sub default : LocalPath { |
---|
61 | my ( $self, $c, undef, $username, $subform, $arg ) = @_; |
---|
62 | |
---|
63 | my $base = $c->model('Accounts')->db; |
---|
64 | |
---|
65 | $c->stash->{page}{title} = "Utilisateur :: $username"; |
---|
66 | $c->stash->{username} = $username; |
---|
67 | $c->stash->{user} = $base->get_object('user', $username) or do { |
---|
68 | $c->forward('/no_object'); |
---|
69 | return; |
---|
70 | }; |
---|
71 | $c->stash->{subform} = $subform || ''; |
---|
72 | $c->stash->{page}{title} = "Utilisateur :: $username"; |
---|
73 | |
---|
74 | if ($c->req->param('make_active')) { |
---|
75 | $c->stash->{user}->set_c_fields('exported' => 1); |
---|
76 | $base->commit; |
---|
77 | } |
---|
78 | if ($c->req->param('make_inactive')) { |
---|
79 | $c->stash->{user}->set_c_fields('exported' => 0); |
---|
80 | $base->commit; |
---|
81 | } |
---|
82 | if ($c->req->param('delete')) { |
---|
83 | $base->delete_object('user', $username); |
---|
84 | $base->commit; |
---|
85 | $c->res->redirect('/users'); |
---|
86 | } |
---|
87 | |
---|
88 | for ($subform || '') { |
---|
89 | # Exceptions... |
---|
90 | /^passwd$/ and do { |
---|
91 | $c->stash->{template} = 'users/passwd.tt'; |
---|
92 | if ($c->req->param('passwd')) { |
---|
93 | if ($base->check_acl($c->stash->{user}, 'userPassword', 'w')) { |
---|
94 | if ($c->req->param('passwd') eq ($c->req->param('cpasswd') || '')) { |
---|
95 | $c->stash->{pmerror} = $c->forward('change_password', |
---|
96 | [ $username, $c->req->param('passwd') ] ) || 'Mot de passe changé'; |
---|
97 | } else { |
---|
98 | $c->stash->{pmerror} = 'Mot de passe différents'; |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | last; |
---|
103 | }; |
---|
104 | /^groups$/ and do { |
---|
105 | $c->stash->{template} = 'users/groups.tt'; |
---|
106 | # list of group for which users can be added |
---|
107 | my %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []}; |
---|
108 | if ($c->req->param('addgroup')) { |
---|
109 | $c->stash->{user}->set_c_fields('memberOf', [ keys %ingroups, $c->req->param('addgroup') ]); |
---|
110 | $c->stash->{user}->base->commit; |
---|
111 | # reread: |
---|
112 | %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []}; |
---|
113 | } elsif ($c->req->param('delgroup')) { |
---|
114 | $c->stash->{user}->set_c_fields('memberOf', |
---|
115 | [ grep { $_ ne $c->req->param('delgroup') } keys %ingroups ] |
---|
116 | ); |
---|
117 | $c->stash->{user}->base->commit; |
---|
118 | %ingroups = map { $_ => 1 } @{ $c->stash->{user}->get_c_field('memberOf') || []}; |
---|
119 | } |
---|
120 | |
---|
121 | $c->stash->{othergroups} = [ grep { ! $ingroups{$_} } $base->list_objects('group') ]; |
---|
122 | last; |
---|
123 | }; |
---|
124 | /^address$/ and do { |
---|
125 | if ($arg) { |
---|
126 | $c->stash->{address} = $base->get_object('address', $arg); |
---|
127 | } else { |
---|
128 | |
---|
129 | if ($c->req->param('del_addr')) { |
---|
130 | $base->delete_object('address', $c->req->param('del_addr')); |
---|
131 | $base->commit; |
---|
132 | } elsif ($c->req->param('add_addr')) { |
---|
133 | $c->stash->{form} = $c->model('AttrForms', 'address', 'address', |
---|
134 | $base); |
---|
135 | my $addname = $username . join('', map |
---|
136 | {('a'..'z')[rand(26)]}(0..4)); |
---|
137 | if ($base->create_c_object('address', |
---|
138 | $addname, |
---|
139 | (map { $_ => ($c->req->param($_) || undef) } |
---|
140 | $c->stash->{form}->attributes('w')), |
---|
141 | user => $username, |
---|
142 | )) { |
---|
143 | $base->commit; |
---|
144 | $c->res->redirect($c->uri_for($username, 'address', |
---|
145 | $addname)); |
---|
146 | return; |
---|
147 | } |
---|
148 | } elsif ($c->req->param('main')) { |
---|
149 | if (my $ad = $base->get_object('address', |
---|
150 | $c->req->param('main'))) { |
---|
151 | warn $c->req->param('main'); |
---|
152 | warn $ad->set_c_fields('isMainAddress' => 1); |
---|
153 | $ad->base->commit; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | my ($main) = $c->stash->{user}->get_attributes('otheraddress'); |
---|
158 | $c->stash->{address} = $base->get_object('address', $main); |
---|
159 | } |
---|
160 | if ($c->stash->{address}) { |
---|
161 | $c->stash->{form} = $c->model('AttrForms', 'address', |
---|
162 | $c->stash->{address}); |
---|
163 | $c->stash->{form}->set_attrs; |
---|
164 | } |
---|
165 | $c->stash->{template} = 'users/address.tt'; |
---|
166 | |
---|
167 | last; |
---|
168 | }; |
---|
169 | /^mail$/ and do { |
---|
170 | |
---|
171 | $c->stash->{db} = $base; |
---|
172 | $c->stash->{template} = 'users/mail.tt'; |
---|
173 | |
---|
174 | if ($c->req->param('usermail')) { |
---|
175 | $c->stash->{user}->set_c_fields( |
---|
176 | mail => $c->req->param('mail') || undef, |
---|
177 | aliases => [ grep { $_ } $c->req->param('aliases') ], |
---|
178 | revaliases => $c->req->param('revaliases') || undef, |
---|
179 | forward => $c->req->param('forward') || undef, |
---|
180 | ) and $base->commit; |
---|
181 | } |
---|
182 | |
---|
183 | last; |
---|
184 | }; |
---|
185 | /^my$/ and do { |
---|
186 | $c->stash->{db} = $base; |
---|
187 | $c->stash->{template} = 'users/my.tt'; |
---|
188 | }; |
---|
189 | /^dump$/ and do { |
---|
190 | $c->stash->{db} = $base; |
---|
191 | $c->stash->{template} = 'users/dump.tt' |
---|
192 | #$c->res->body($c->stash->{user}->dump({only_rw => 1})); |
---|
193 | }; |
---|
194 | |
---|
195 | $c->stash->{form} = $c->model('AttrForms', 'user' . $c->stash->{subform}, $c->stash->{user}); |
---|
196 | $c->stash->{form}->set_attrs; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | =head1 AUTHOR |
---|
201 | |
---|
202 | Thauvin Olivier |
---|
203 | |
---|
204 | =head1 LICENSE |
---|
205 | |
---|
206 | This library is free software, you can redistribute it and/or modify |
---|
207 | it under the same terms as Perl itself. |
---|
208 | |
---|
209 | =cut |
---|
210 | |
---|
211 | 1; |
---|