blib/lib/OpusVL/AppKit/Controller/AppKit/Admin/Users.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 224 CC 24 SCAR 34.4
CC
24
Cov
0%
get_parameter_input
90.0
edit_user
30.0
adduser
20.0
CRAP 31.3
1 package OpusVL::AppKit::Controller::AppKit::Admin::Users;
2  
3 use Moose;
4 use namespace::autoclean;
5 use String::MkPasswd qw/mkpasswd/;
6  
7 BEGIN { extends 'Catalyst::Controller::HTML::FormFu'; };
8 with 'OpusVL::AppKit::RolesFor::Controller::GUI';
9  
10 __PACKAGE__->config
11 (
12 appkit_myclass => 'OpusVL::AppKit',
13 );
14  
15  
16 sub auto
17 : Action
18 : AppKitFeature('User Administration')
19 {
20 my ( $self, $c ) = @_;
21  
22 # add to the bread crumb..
23 push ( @{ $c->stash->{breadcrumbs} }, { name => 'Users', url => $c->uri_for( $c->controller('AppKit::Admin::Users')->action_for('index') ) } );
24  
25 # stash all users..
26 my $users_rs = $c->model('AppKitAuthDB::User')->search;
27 $users_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
28 my @users = $users_rs->all;
29 $c->stash->{users} = \@users;
30  
31 }
32  
33  
34 sub index
35 : Path
36 : Args(0)
37 : AppKitFeature('User Administration')
38 {
39 my ( $self, $c ) = @_;
40 $c->stash->{template} = 'appkit/admin/users/show_user.tt';
41 }
42  
43  
44 sub adduser
45 : Local
46 : Args(0)
47 : AppKitFeature('User Administration')
48 : AppKitForm("appkit/admin/users/user_form.yml")
49 {
50 my ( $self, $c ) = @_;
51  
52 push ( @{ $c->stash->{breadcrumbs} }, { name => 'Add', url => $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('adduser') ) } );
53  
54 my $form = $c->stash->{form};
55  
56 my $ignore_password = $c->model('AppKitAuthDB')->schema->can('password_check');
57 if($ignore_password)
58 {
59 my $password = $form->get_all_element('password');
60 $password->parent->remove_element($password);
61 }
62 else
63 {
64 # add 'Required' constraint to form .. adding means you must have set a password...
65 $form->get_all_element('password')->constraint('Required');
66 }
67 $form->process();
68  
69 if ( $c->stash->{form}->submitted_and_valid )
70 {
71 my $password = $ignore_password ? mkpasswd : $form->param_value('password');
72
73 # FIXME: is this a security hole?
74 # should we be recording the fact we're not using the password field
75 # really?
76
77 my $newuser = $c->model('AppKitAuthDB::User')->new_result( { password => $password } );
78  
79 $c->stash->{form}->model->update( $newuser );
80  
81 $c->stash->{status_msg} = "User added";
82 $c->stash->{thisuser} = $newuser;
83 $c->res->redirect( $c->uri_for( $c->controller('AppKit::Admin::Users')->action_for('show_user'), [ $c->stash->{thisuser}->id ] ) ) ;
84 }
85 $c->stash->{template} = "appkit/admin/users/user_form.tt";
86 }
87  
88  
89 sub user_specific
90 : Chained('/')
91 : PathPart('user')
92 : CaptureArgs(1)
93 : AppKitFeature('User Administration')
94 {
95 my ( $self, $c, $user_id ) = @_;
96 ( $c->stash->{thisuser} ) = $c->model('AppKitAuthDB::User')->find( $user_id );
97 }
98  
99  
100 sub show_user
101 : Chained('user_specific')
102 : PathPart('show')
103 : AppKitFeature('User Administration')
104 : Args(0)
105 {
106 my ( $self, $c ) = @_;
107  
108 push ( @{ $c->stash->{breadcrumbs} }, { name => $c->stash->{thisuser}->username, url => $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('show_user'), [ $c->stash->{thisuser}->id ] ) } );
109  
110 # test if need to process user submission...
111 if ( $c->req->method eq 'POST' )
112 {
113 # add related user lookup for the submitted roles...
114 my $user_roles = $c->req->params->{user_role};
115 $user_roles = [ $user_roles ] if defined $user_roles && ! ref $user_roles;
116 foreach my $role_id ( @$user_roles )
117 {
118 $c->stash->{thisuser}->find_or_create_related('users_roles', { role_id => $role_id } );
119 }
120  
121 #$c->log->debug("************************** SUBMITTED ROLES: $#$user_roles :" . join('|', @$user_roles) );
122  
123 #.. delete any roles not required..
124 $c->stash->{thisuser}->search_related('users_roles', { role_id => { 'NOT IN' => $user_roles } } )->delete;
125  
126 $c->stash->{status_msg} = "User Roles updated";
127 }
128  
129 # capture and stash role information for the user..
130 my @roles;
131 foreach my $role_rs ( $c->user->roles_modifiable->all )
132 {
133 my $checked = '';
134 if ( $c->stash->{thisuser}->search_related('users_roles', { role_id => $role_rs->id } )->count > 0 )
135 {
136 $checked = 'checked';
137 }
138 push( @roles, { role => $role_rs->role, input => "<INPUT TYPE='checkbox' NAME='user_role' VALUE='".$role_rs->id."' $checked>" } );
139 }
140 $c->stash->{roles} = \@roles;
141 }
142  
143 sub reset_password
144 : Chained('user_specific')
145 : PathPart('reset')
146 : AppKitFeature('User Password Administration')
147 : Args(0)
148 : AppKitForm
149 {
150 my ( $self, $c ) = @_;
151  
152 my $user = $c->stash->{thisuser};
153 my $prev_url = $c->uri_for( $self->action_for('show_user'), [ $user->id ] );
154  
155 push ( @{ $c->stash->{breadcrumbs} }, { name => 'Reset password', url => $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('reset_password'), [ $user->id ] ) } );
156  
157 $c->forward('/appkit/admin/users/reset_password_form', [ $prev_url, $user ] );
158 }
159  
160 # to allow other controllers to forward to this setting their own
161 # breadcrumbs and passing their own url.
162 sub reset_password_form
163 : Action
164 : AppKitFeature('User Password Administration,Password Reset')
165 {
166 my ($self, $c, $prev_url, $user) = @_;
167  
168 if ($c->req->param('cancel'))
169 {
170 $c->response->redirect( $prev_url );
171 $c->detach;
172 }
173  
174 my $form = $c->stash->{form};
175 if ( $form->submitted_and_valid )
176 {
177 my $password = $form->param_value('newpassword');
178  
179 $user->update( { password => $password } );
180 $c->flash->{status_msg} = 'Reset password';
181 $c->response->redirect( $prev_url );
182 }
183 else
184 {
185 $c->stash->{form}->default_values( {
186 newpassword => mkpasswd,
187 user => $user->username,
188 });
189 }
190 }
191  
192  
193 sub edit_user
194 : Chained('user_specific')
195 : PathPart('form')
196 : Args(0)
197 : AppKitForm("appkit/admin/users/user_form.yml")
198 : AppKitFeature('User Administration')
199 {
200 my ( $self, $c ) = @_;
201  
202 my $form = $c->stash->{form};
203 my $ignore_password = $c->model('AppKitAuthDB')->schema->can('password_check');
204 if($ignore_password)
205 {
206 my $password = $form->get_all_element('password');
207 $password->parent->remove_element($password);
208 }
209 push ( @{ $c->stash->{breadcrumbs} }, { name => 'Edit', url => $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('edit_user'), [ $c->stash->{thisuser}->id ] ) } );
210  
211 if ( $form->submitted_and_valid )
212 {
213 # update the user from the form..
214 $form->model->update( $c->stash->{thisuser} );
215  
216 # .. alter password if we have been passed one..
217 unless($ignore_password)
218 {
219 $c->stash->{thisuser}->update(
220 { password => $c->stash->{form}->param_value('password') }
221 ) if $c->stash->{form}->param_value('password');
222 }
223  
224 $c->stash->{status_msg} = "User updated";
225 }
226  
227 # set default values..
228 $form->model->default_values( $c->stash->{thisuser} );
229 $c->stash->{template} = "appkit/admin/users/user_form.tt";
230 }
231  
232  
233 sub delete_user
234 : Chained('user_specific')
235 : PathPart('delete')
236 : Args(0)
237 : AppKitForm("appkit/admin/confirm.yml")
238 : AppKitFeature('User Administration')
239 {
240 my ( $self, $c ) = @_;
241  
242 $c->stash->{question} = "Are you sure you want to delete the user:" . $c->stash->{thisuser}->username;
243 $c->stash->{template} = 'appkit/admin/confirm.tt';
244  
245 if ( $c->stash->{form}->submitted_and_valid )
246 {
247 $c->stash->{thisuser}->delete;
248 $c->flash->{status_msg} = "User deleted";
249 $c->res->redirect( $c->uri_for( $c->controller('AppKit::Admin::User')->action_for('index') ) );
250 }
251 elsif( $c->req->method eq 'POST' )
252 {
253 $c->flash->{status_msg} = "User NOT deleted";
254 $c->res->redirect( $c->uri_for( $c->controller('AppKit::Admin::User')->action_for('index') ) );
255 }
256  
257 }
258  
259  
260 sub delete_parameter
261 : Chained('user_specific')
262 : PathPart('deleteparameter')
263 : Args(1)
264 : AppKitFeature('User Administration')
265 {
266 my ( $self, $c, $param_id ) = @_;
267  
268 $c->stash->{thisuser}->delete_related('users_parameters', { parameter_id => $param_id } );
269 $c->flash->{status_msg} = "Parameter deleted";
270 $c->res->redirect( $c->uri_for( $c->controller('AppKit::Admin::User')->action_for('show_user'), [ $c->stash->{thisuser}->id ] ) );
271 }
272  
273  
274 sub add_parameter
275 : Chained('user_specific')
276 : PathPart('addparameter')
277 : Args(0)
278 : AppKitFeature('User Administration')
279 {
280 my ( $self, $c ) = @_;
281  
282 if ( $c->req->method eq 'POST' )
283 {
284 my $parameter_id = $c->req->param('parameter_id');
285 my $parameter_value = $c->req->param('parameter_value');
286 $c->stash->{thisuser}->update_or_create_related('users_parameters', { parameter_id => $parameter_id, value => $parameter_value } );
287 $c->stash->{status_msg} = "Parameter updated";
288 }
289  
290 # refresh show page..
291 $c->res->redirect( $c->uri_for( $c->controller('AppKit::Admin::User')->action_for('show_user'), [ $c->stash->{thisuser}->id ] ) ) ;
292 }
293  
294  
295 sub get_parameter_input
296 : Chained('user_specific')
297 : PathPart('addparaminput')
298 : Args(1)
299 : AppKitFeature('User Administration')
300 {
301 my ( $self, $c, $param_id ) = @_;
302  
303 my $param = $c->model('AppKitAuthDB::Parameter')->find( $param_id );
304 return undef unless $param;
305  
306 # get and values ther might be (for the user in the stash)...
307 my $up = $c->stash->{thisuser}->find_related('users_parameters', { parameter_id => $param_id } );
308 my $value = $up->value if ( $up );
309  
310 # output correct HTML..
311 my $html = '';
312 if ( $param->data_type eq 'boolean' )
313 {
314 $html .= '<div class="radio label">';
315 $html .= "<label for='parameter_value_true'>True</label><input type='radio' name='parameter_value' value='1' id='parameter_value_true' " . ( $value ? "checked='1'" : '') . ">";
316 $html .= '</div><div class="radio label">';
317 $html .= "<label for='parameter_value_false'>False</label><input type='radio' name='parameter_value' value='0' id='parameter_value_false' " . ( $value ? '' : "checked='1'") . ">";
318 $html .= "</div>";
319 }
320 elsif ( $param->data_type eq 'select' )
321 {
322 $html .= "<select name='parameter_value'> \n";
323 foreach my $pdef ( $param->parameter_defaults )
324 {
325 my $thisval = $pdef->data;
326 my $selected = $thisval eq $value ? 'selected' : '';
327 $html .= "<option $selected value='$thisval'> $thisval</option>\n";
328 }
329 $html .= "</select> \n";
330 }
331 elsif ( $param->data_type eq 'integer' )
332 {
333 $html .= "<input type='text' name='parameter_value' value='$value' id='parameter_value' size='5'>";
334 }
335 else
336 {
337 $html .= "<input type='text' name='parameter_value' value='$value' id='parameter_value'>";
338 }
339  
340 $c->stash->{no_wrapper} = 1;
341 $c->stash->{html} = $html;
342  
343 }
344  
345 __END__
346  
347 =pod
348  
349 =encoding UTF-8
350  
351 =head1 NAME
352  
353 OpusVL::AppKit::Controller::AppKit::Admin::Users
354  
355 =head1 VERSION
356  
357 version 2.22
358  
359 =head2 auto
360  
361 Default action for this controller.
362  
363 =head2 index
364  
365 default action for access administration.
366  
367 =head2 adduser
368  
369 =head2 user_specific
370  
371 Start of chain.
372  
373 =head2 show_user
374  
375 End of chain.
376 Display a users details.
377  
378 =head2 edit_user
379  
380 End of chain.
381 Display a users details.
382  
383 =head2 delete_user
384  
385 End of chain.
386  
387 =head2 delete_parameter
388  
389 End of chain.
390  
391 =head2 add_parameter
392  
393 End of chain.
394  
395 =head2 get_parameter_input
396  
397 End of chain.
398 Returns the input for a parameter.
399  
400 =head1 AUTHOR
401  
402 OpusVL - www.opusvl.com
403  
404 =head1 COPYRIGHT AND LICENSE
405  
406 This software is copyright (c) 2010 by OpusVL - www.opusvl.com.
407  
408 This is free software; you can redistribute it and/or modify it under
409 the same terms as the Perl 5 programming language system itself.
410  
411 =cut