Silki/Controller/User.pm untested

filter: Statementstmt 0.0% 0 / 173 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 303 CC 24 SCAR 33.7
CC
24
Cov
0%
authentication_POST
110.0
users_collection_POST
42.0
password_reminder_POST
30.0
CRAP 29.0
1 package Silki::Controller::User;
2 {
3 $Silki::Controller::User::VERSION = '0.29';
4 }
5  
6 use strict;
7 use warnings;
8 use namespace::autoclean;
9  
10 use File::MimeInfo qw( mimetype );
11 use Silki::I18N qw( loc );
12 use Silki::Schema::TimeZone;
13 use Silki::Schema::User;
14 use Silki::Schema::UserImage;
15 use Silki::Util qw( string_is_empty );
16  
17 use Moose;
18  
19 BEGIN { extends 'Silki::Controller::Base' }
20  
21 with qw(
22 Silki::Role::Controller::Pager
23 Silki::Role::Controller::User
24 );
25  
26 sub _set_user : Chained('/') : PathPart('user') : CaptureArgs(1) {
27 }
28  
29 sub _make_user_uri {
30 my $self = shift;
31 my $c = shift;
32 my $user = shift;
33 my $view = shift || q{};
34  
35 return $user->uri( view => $view );
36 }
37  
38 sub wikis : Chained('_set_user') : PathPart('wikis') : Args(0) : ActionClass('+Silki::Action::REST') {
39 }
40  
41 sub wikis_GET {
42 my $self = shift;
43 my $c = shift;
44  
45 my $user = $c->stash()->{user};
46  
47 my $wikis = $user->all_wikis();
48  
49 my @entity = map {
50 {
51 wiki_id => $_->wiki_id(),
52 title => $_->title(),
53 short_name => $_->short_name(),
54 }
55 } $wikis->all();
56  
57 return $self->status_ok( $c, entity => \@entity );
58 }
59  
60 sub _set_confirmation : Chained('_set_user') : PathPart('confirmation') : CaptureArgs(1) {
61 my $self = shift;
62 my $c = shift;
63 my $key = shift;
64  
65 my $user = Silki::Schema::User->new( confirmation_key => $key );
66  
67 $c->redirect_and_detach( $c->domain()->uri( with_host => 1 ) )
68 unless $user && $user->user_id() == $c->stash()->{user}->user_id();
69  
70 return;
71 }
72  
73 sub pending_confirmation : Chained('_set_confirmation') : PathPart('status') : Args(0) {
74 my $self = shift;
75 my $c = shift;
76  
77 $c->stash()->{template} = '/user/pending-confirmation';
78 }
79  
80 sub confirmation_form : Chained('_set_confirmation') : PathPart('preferences_form') : Args(0) {
81 my $self = shift;
82 my $c = shift;
83  
84 $c->stash()->{template} = '/user/confirmation-form';
85 }
86  
87 sub login_form : Local {
88 my $self = shift;
89 my $c = shift;
90  
91 $c->stash()->{template} = '/user/login-form';
92 }
93  
94 sub authentication : Local : ActionClass('+Silki::Action::REST') {
95 }
96  
97 sub authentication_GET_html {
98 my $self = shift;
99 my $c = shift;
100  
101 my $method = $c->request()->param('x-tunneled-method');
102  
103 if ( $method && $method eq 'DELETE' ) {
104 $self->authentication_DELETE($c);
105 return;
106 }
107 else {
108 $c->redirect_and_detach(
109 $c->domain()->application_uri( path => '/user/login_form' ) );
110 }
111 }
112  
113 sub authentication_POST {
114 my $self = shift;
115 my $c = shift;
116  
117 my $username = $c->request()->params->{username};
118 my $pw = $c->request()->params->{password};
119  
120 my @errors;
121  
122 push @errors, {
123 field => 'password',
124 message => loc('You must provide a password.')
125 }
126 if string_is_empty($pw);
127  
128 my $user;
129 unless (@errors) {
130 $user = Silki::Schema::User->new(
131 username => $username,
132 );
133  
134 if ($user) {
135 if ( $user->is_disabled() ) {
136 undef $user;
137  
138 push @errors,
139 loc(
140 'This user account has been disabled by a site admin.');
141 }
142 else {
143 undef $user unless $user->check_password($pw);
144  
145 if ($user) {
146 $c->redirect_and_detach(
147 $user->confirmation_uri(
148 view => 'status',
149 host => $c->domain()->web_hostname(),
150 with_host => 1,
151 )
152 ) if $user->requires_activation();
153 }
154  
155 push @errors,
156 loc(
157 'The username or password you provided was not valid.')
158 unless $user;
159 }
160 }
161 }
162  
163 unless ($user) {
164 $c->redirect_with_error(
165 error => \@errors,
166 uri => $c->domain()->application_uri(
167 path => '/user/login_form',
168 with_host => 1
169 ),
170 form_data => $c->request()->params(),
171 );
172 }
173  
174 $self->_login_user( $c, $user );
175 }
176  
177 sub _login_user {
178 my $self = shift;
179 my $c = shift;
180 my $user = shift;
181  
182 my %expires
183 = $c->request()->param('remember') ? ( expires => '+1y' ) : ();
184  
185 $c->set_authen_cookie(
186 value => { user_id => $user->user_id() },
187 %expires,
188 );
189  
190 $c->session_object()
191 ->add_message( 'Welcome to the site, ' . $user->best_name() );
192  
193 my $redirect_to = $c->request()->params()->{return_to}
194 || $c->domain()->application_uri( path => q{} );
195  
196 $c->redirect_and_detach($redirect_to);
197 }
198  
199 sub authentication_DELETE {
200 my $self = shift;
201 my $c = shift;
202  
203 $c->unset_authen_cookie();
204  
205 $c->session_object()->add_message('You have been logged out.');
206  
207 my $redirect = $c->request()->params()->{return_to}
208 || $c->domain()->application_uri( path => q{} );
209 $c->redirect_and_detach($redirect);
210 }
211  
212 sub forgot_password_form : Local {
213 my $self = shift;
214 my $c = shift;
215  
216 $c->stash()->{template} = '/user/forgot-password-form';
217 }
218  
219 sub password_reminder : Local : ActionClass('+Catalyst::Action::REST') {
220 }
221  
222 sub password_reminder_POST {
223 my $self = shift;
224 my $c = shift;
225  
226 my $username = $c->request()->params()->{username};
227  
228 my $user;
229  
230 my @errors;
231 if ( string_is_empty($username) ) {
232 push @errors, {
233 field => 'username',
234 message => loc('You must provide an email address.'),
235 };
236 }
237 else {
238 $user = Silki::Schema::User->new( username => $username );
239  
240 if ($user) {
241 push @errors,
242 loc('This user account has been disabled by a site admin.')
243 if $user->is_disabled();
244 }
245 else {
246 push @errors, {
247 field => 'username',
248 message =>
249 loc( "There is no user with the email address %1.", $username ),
250 };
251 }
252 }
253  
254 if (@errors) {
255 $c->redirect_with_error(
256 error => \@errors,
257 uri => $c->domain()->application_uri(
258 path => '/user/forgot_password_form',
259 with_host => 1,
260 ),
261 form_data => {
262 username => $username,
263 return_to => $c->request()->params()->{return_to},
264 },
265 );
266 }
267  
268 $user->forgot_password( domain => $c->domain() );
269  
270 $c->session_object()->add_message(
271 loc(
272 'A message telling you how to change your password has been sent to your email address.'
273 )
274 );
275  
276 $c->redirect_and_detach(
277 $c->domain()->application_uri(
278 path => '/user/forgot_password_form',
279 with_host => 1,
280 query =>
281 { return_to => $c->request()->parameters()->{return_to} },
282 with_host => 1,
283 )
284 );
285 }
286  
287 sub purge_confirmation : Chained('_set_user') : PathPart('purge_confirmation') : Args(0) {
288 my $self = shift;
289 my $c = shift;
290  
291 $self->_require_site_admin($c);
292  
293 $c->stash()->{template} = '/user/purge-confirmation';
294 }
295  
296 sub user_DELETE {
297 my $self = shift;
298 my $c = shift;
299  
300 $self->_require_site_admin($c);
301  
302 my $user = $c->stash()->{user};
303  
304 my $msg = loc(
305 'Deleted the user %1 - %2',
306 $user->best_name(),
307 $user->email_address()
308 );
309  
310 $user->delete( user => $c->user() );
311  
312 $c->session_object()->add_message($msg);
313  
314 $c->redirect_and_detach(
315 $c->domain()->uri( view => 'users', with_host => 1 ) );
316 }
317  
318 sub new_user_form : Local {
319 my $self = shift;
320 my $c = shift;
321  
322 $c->stash()->{template} = '/user/new-user-form';
323 }
324  
325 sub users_collection : Path('/users') : ActionClass('+Silki::Action::REST') {
326 }
327  
328 sub users_collection_GET_html {
329 my $self = shift;
330 my $c = shift;
331  
332 $self->_require_site_admin($c);
333  
334 my $include_disabled = $c->request()->params()->{include_disabled};
335  
336 my $count_meth = $include_disabled ? 'Count' : 'ActiveUserCount';
337  
338 my ( $limit, $offset ) = $self->_make_pager( $c, Silki::Schema::User->$count_meth() );
339  
340 my $meth = $include_disabled ? 'All' : 'ActiveUsers';
341  
342 $c->stash()->{include_disabled} = $include_disabled;
343  
344 $c->stash()->{users} = Silki::Schema::User->$meth(
345 limit => $limit,
346 offset => $offset,
347 );
348  
349 $c->stash()->{template} = '/user/users';
350 }
351  
352 sub users_collection_POST {
353 my $self = shift;
354 my $c = shift;
355  
356 my %insert = $c->request()->user_params();
357  
358 my $upload = $c->request()->upload('image');
359  
360 my @errors = $self->_check_passwords_match(\%insert);
361  
362 $insert{requires_activation} = 1;
363  
364 my $user;
365 unless (@errors) {
366 eval {
367 Silki::Schema->RunInTransaction(
368 sub {
369 $user = Silki::Schema::User->insert(
370 %insert,
371 user => $c->user(),
372 );
373  
374 if ($upload) {
375 Silki::Schema::UserImage->insert(
376 user_id => $user->user_id(),
377 mime_type => mimetype( $upload->tempname() ),
378 file_size => $upload->size(),
379 contents =>
380 do { my $fh = $upload->fh(); local $/; <$fh> },
381 );
382 }
383 }
384 );
385 };
386  
387 my $e = $@;
388 die $e if $e && ! ref $e;
389  
390 push @errors, @{ $e->errors() } if $e;
391 }
392  
393 if (@errors) {
394 $c->redirect_with_error(
395 error => \@errors,
396 uri => $c->domain()->application_uri(
397 path => '/user/new_user_form',
398 with_host => 1
399 ),
400 form_data => \%insert,
401 );
402 }
403  
404 $user->send_activation_email(
405 sender => Silki::Schema::User->SystemUser(),
406 domain => $c->domain(),
407 );
408  
409 $c->redirect_and_detach(
410 $user->confirmation_uri(
411 view => 'status',
412 host => $c->domain()->web_hostname(),
413 with_host => 1,
414 )
415 );
416 }
417  
418 __PACKAGE__->meta()->make_immutable();
419  
420 1;
421  
422 # ABSTRACT: Controller class for users
423  
424 __END__