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

filter: Statementstmt 0.0% 0 / 232 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 340 CC 36 SCAR 45.9
CC
36
Cov
0%
show_role
272.0
addrole
42.0
role_management
42.0
CRAP 98.5
1 package OpusVL::AppKit::Controller::AppKit::Admin::Access;
2  
3 use Moose;
4 use namespace::autoclean;
5 use Tree::Simple::View::HTML;
6 use Tree::Simple::VisitorFactory;
7  
8 BEGIN { extends 'Catalyst::Controller::HTML::FormFu'; };
9 with 'OpusVL::AppKit::RolesFor::Controller::GUI';
10  
11 __PACKAGE__->config
12 (
13 appkit_myclass => 'OpusVL::AppKit',
14 );
15  
16  
17  
18 sub auto
19 : AppKitFeature('Role Administration')
20 : Action
21 {
22 my ( $self, $c ) = @_;
23  
24 # add to the bread crumb..
25 push ( @{ $c->stash->{breadcrumbs} }, { name => 'Access', url => $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('index') ) } );
26  
27 }
28  
29  
30 sub index
31 : Path
32 : Args(0)
33 : AppKitFeature('Role Administration')
34 {
35 my ( $self, $c ) = @_;
36 $c->stash->{template} = 'appkit/admin/access/show_role.tt';
37 }
38  
39  
40 sub addrole
41 : Local
42 : AppKitFeature('Role Administration')
43 {
44 my ( $self, $c ) = @_;
45  
46 if ( $c->req->method eq 'POST')
47 {
48 my $rolename = $c->req->param('rolename');
49 if($rolename)
50 {
51 my $role = grep /^\Q$rolename\E$/, $c->user->roles;
52 $c->flash->{error_msg} = 'Role already exists' if $role;
53 $role = $c->user->add_to_roles( { role => $rolename } ) if !$role;
54  
55 if ( $role )
56 {
57 $c->res->redirect( $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('show_role'), [ $rolename ] ) );
58 }
59 else
60 {
61 $c->stash->{error_msg} = 'Role not added';
62 }
63 }
64 else
65 {
66 $c->stash->{error_msg} = 'Specify a role name!';
67 }
68 }
69  
70 # basically run the action for the index for this page..
71 $c->go( $c->controller('AppKit::Admin::Access')->action_for('index') );
72 }
73  
74  
75 sub role_specific
76 : Chained('/')
77 : PathPart('admin/access/role')
78 : CaptureArgs(1)
79 : AppKitFeature('Role Administration')
80 {
81 my ( $self, $c, $rolename ) = @_;
82  
83 # put role into stash..
84 $c->stash->{role} = $c->model('AppKitAuthDB::Role')->find( { role => $rolename } );
85 if(!$c->stash->{role})
86 {
87 $c->detach('/not_found');
88 }
89  
90 }
91  
92  
93 sub role_management
94 : Chained('role_specific')
95 : PathPart('management')
96 : Args(0)
97 : AppKitForm
98 : AppKitFeature('Role Administration')
99 {
100 my ($self, $c) = @_;
101  
102 my $form = $c->stash->{form};
103 my $role = $c->stash->{role};
104  
105 if($c->req->param('cancel'))
106 {
107 $c->response->redirect($c->uri_for($self->action_for('show_role'), [ $role->role ]));
108 $c->detach;
109 }
110 my $selection = $form->get_all_element({ type => 'Checkboxgroup', name => 'roles_allowed_roles'});
111 my @all_roles = $c->model('AppKitAuthDB::Role')->all;
112 my @options = map { [ $_->id, $_->role ] } @all_roles;
113 $selection->options(\@options);
114 my @selected = $role->roles_allowed_roles->get_column('role_allowed')->all;
115 my $can_change_any_role = $role->can_change_any_role;
116 if(@selected || $can_change_any_role)
117 {
118 my $defaults = { can_change_any_role => $can_change_any_role };
119 $defaults->{ roles_allowed_roles } = \@selected if @selected;
120 $form->default_values( {
121 roles_allowed_roles => \@selected,
122 can_change_any_role => $can_change_any_role,
123 } );
124 }
125 $form->process;
126  
127 if($form->submitted_and_valid)
128 {
129 my $ids = $form->param_array('roles_allowed_roles');
130 my $can_change_any_role = $form->param_value('can_change_any_role');
131 if(!$can_change_any_role)
132 {
133 $role->delete_related('roles_allowed_roles');
134 $role->create_related('roles_allowed_roles', { role_allowed => $_}) for @$ids;
135 }
136 $role->can_change_any_role($can_change_any_role);
137 $c->flash->{status_msg} = 'Permissions changed';
138 }
139  
140 }
141  
142  
143 sub user_for_role
144 : Chained('role_specific')
145 : PathPart('user')
146 : CaptureArgs(1)
147 : AppKitFeature('Role Administration')
148 {
149 my ( $self, $c, $user_id ) = @_;
150 $c->stash->{roleuser} = $c->model('AppKitAuthDB::User')->find( $user_id );
151 }
152  
153  
154 sub user_delete_from_role
155 : Chained('user_for_role')
156 : PathPart('delete')
157 : Args(0)
158 : AppKitFeature('Role Administration')
159 {
160 my ( $self, $c ) = @_;
161 # delete user/role lookup..
162 $c->stash->{role}->delete_related('user_roles', { user_id => $c->stash->{roleuser}->id } );
163 # refresh show page..
164 $c->res->redirect( $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('show_role'), [ $c->stash->{role}->role ] ) ) ;
165 }
166  
167  
168 sub delete_role
169 : Chained('role_specific')
170 : PathPart('delrole')
171 : Args(0)
172 : AppKitForm("appkit/admin/confirm.yml")
173 : AppKitFeature('Role Administration')
174 {
175 my ( $self, $c ) = @_;
176  
177 $c->stash->{question} = "Are you sure you want to delete the role: " . $c->stash->{role}->role;
178 $c->stash->{template} = 'appkit/admin/confirm.tt';
179  
180 if ( $c->stash->{form}->submitted_and_valid )
181 {
182 $c->stash->{role}->delete;
183 $c->flash->{status_msg} = "Role deleted";
184 $c->res->redirect( $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('index') ) );
185 }
186 elsif( $c->req->method eq 'POST' )
187 {
188 $c->flash->{status_msg} = "Role NOT deleted";
189 $c->res->redirect( $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('index') ) );
190 }
191  
192 }
193  
194  
195 sub user_add_to_role
196 : Chained('role_specific')
197 : PathPart('adduser')
198 : Args(0)
199 : AppKitFeature('Role Administration')
200 {
201 my ( $self, $c ) = @_;
202  
203 if ( $c->req->method eq 'POST' )
204 {
205 # create the look up..
206 my $user_id = $c->req->param('user_id');
207 $c->stash->{role}->update_or_create_related('user_roles', { user_id => $user_id } );
208 }
209  
210 # refresh show page..
211 $c->res->redirect( $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('show_role'), [ $c->stash->{role}->role ] ) ) ;
212 }
213  
214  
215 sub action_rule_for_role
216 : Chained('role_specific')
217 : PathPart('rule')
218 : Args(2)
219 : AppKitFeature('Role Administration')
220 {
221 my ( $self, $c, $action, $action_path ) = @_;
222  
223 # find any access control rule for the passed action path..
224 my $aclrule = $c->model('AppKitAuthDB::Aclrule')->find( { actionpath => $action_path } );
225  
226 if ( ! $aclrule )
227 {
228 $aclrule = $c->model('AppKitAuthDB::Aclrule')->create( { actionpath => $action_path } );
229 }
230  
231 if ( $action eq 'deny' )
232 {
233 $c->stash->{status_msg} .= "Removed role " . $c->stash->{role}->role . " from access control rule:" . $aclrule->actionpath;
234 $aclrule->delete_related('aclrule_roles', { role_id => $c->stash->{role}->id } );
235 }
236 elsif ( $action eq 'allow' )
237 {
238 $c->stash->{status_msg} .= "Added role " . $c->stash->{role}->role . " to access control rule:" . $aclrule->actionpath;
239 $aclrule->create_related('aclrule_roles', { role_id => $c->stash->{role}->id } );
240 }
241 elsif ( $action eq 'revoke' )
242 {
243 $c->stash->{status_msg} .= "Revoked access control for:" . $aclrule->actionpath;
244 $aclrule->delete;
245 }
246  
247 # refresh show page..
248 $c->res->redirect( $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('show_role'), [ $c->stash->{role}->role ] ) ) ;
249 }
250  
251  
252 sub show_role
253 : Chained('role_specific')
254 : PathPart('show')
255 : Args(0)
256 : AppKitFeature('Role Administration')
257 {
258 my ( $self, $c ) = @_;
259  
260 push ( @{ $c->stash->{breadcrumbs} }, { name => $c->stash->{role}->role, url => $c->uri_for( $c->controller('AppKit::Admin::Access')->action_for('show_role'), [ $c->stash->{role}->id ] ) } );
261  
262 # stash the tree..
263 $c->stash->{action_tree} = $c->appkit_actiontree;
264  
265 # get role to show from stash..
266 my $show_role = $c->stash->{role}->role;
267  
268 # build my visitor to get the path to the root..
269 my $path2root_visitor = Tree::Simple::VisitorFactory->getVisitor("PathToRoot");
270 $path2root_visitor->setNodeFilter(sub { my ($t) = @_; return $t->getNodeValue()->node_name });
271 $c->stash->{appkit_features} = $c->appkit_features->feature_list($show_role);
272 $c->stash->{descriptions} = $c->model('AppKitAuthDB::Aclfeature')->descriptions;
273  
274 # test if need to process some rules submission...
275 if ( $c->req->method eq 'POST' )
276 {
277 my @features_allowed;
278 my @features_denied;
279 for my $app (keys %{$c->stash->{appkit_features}})
280 {
281 my $features = $c->stash->{appkit_features}->{$app};
282 for my $feature (keys %$features)
283 {
284 if($c->req->params->{"feature_$app/$feature"})
285 {
286 push @features_allowed, "$app/$feature";
287 }
288 else
289 {
290 push @features_denied, "$app/$feature";
291 }
292 }
293 }
294 for my $feature (@features_allowed)
295 {
296 $c->log->debug("****************ALLOWING FEATURE:" . $feature . "\n") if $c->debug;
297 my $aclfeature = $c->model('AppKitAuthDB::Aclfeature')->find_or_create( { feature => $feature } );
298 $c->stash->{role}->update_or_create_related('aclfeature_roles', { aclfeature_id => $aclfeature->id } );
299 }
300 for my $feature (@features_denied)
301 {
302 $c->log->debug("****************DENYING FEATURE:" . $feature . "\n") if $c->debug;
303 my $aclfeature = $c->model('AppKitAuthDB::Aclfeature')->find_or_create( { feature => $feature } );
304 $c->stash->{role}->search_related('aclfeature_roles', { aclfeature_id => $aclfeature->id } )->delete;
305 }
306 # now we run traverse the tree finding if we are allowing access or not...
307  
308 my $allowed = [];
309 my $denied = [];
310 $c->stash->{action_tree}->traverse
311 (
312 sub
313 {
314 my ($_tree) = @_;
315 $_tree->accept($path2root_visitor);
316 my $path = $path2root_visitor->getPathAsString("/");
317 if ( $c->req->params->{'action_' . $path} )
318 {
319 push ( @$allowed, $path );
320 }
321 else
322 {
323 push ( @$denied, $path );
324 }
325 },
326 );
327  
328 foreach my $path ( @$allowed )
329 {
330 $c->log->debug("***************ALLOWING:" . $path . "\n") if $c->debug;
331 my $aclrule = $c->model('AppKitAuthDB::Aclrule')->find_or_create( { actionpath => $path } );
332 $c->stash->{role}->update_or_create_related('aclrule_roles', { aclrule_id => $aclrule->id } );
333 }
334 foreach my $path ( @$denied )
335 {
336 $c->log->debug("****************DENYING:" . $path . "\n") if $c->debug;
337 my $aclrule = $c->model('AppKitAuthDB::Aclrule')->find_or_create( { actionpath => $path } );
338 $c->stash->{role}->search_related('aclrule_roles', { aclrule_id => $aclrule->id } )->delete;
339 }
340  
341 # now we have allowed and denied access to the different parts of the tree... we need to rebuild it..
342 $c->stash->{action_tree} = $c->appkit_actiontree(1); # built with a 'force re-read'
343 $c->stash->{appkit_features} = $c->appkit_features->feature_list($show_role);
344  
345 }
346  
347  
348 # create the tree view...
349 # need to prune items that are in_feature
350 # to prevent confusion.
351 my $display_tree = $c->stash->{action_tree}->clone;
352 my @remove;
353 $display_tree->traverse(sub {
354 my ($tree) = @_;
355 push @remove, $tree if($tree->getNodeValue->in_feature);
356 push @remove, $tree if($tree->getNodeValue->action_attrs && defined $tree->getNodeValue->action_attrs->{AppKitAllAccess});
357 });
358 for my $item (@remove)
359 {
360 my $parent = $item->getParent;
361 $parent->removeChild($item);
362 while($parent->getChildCount == 0)
363 {
364 my $item = $parent;
365 $parent = $parent->getParent;
366 last if !$parent->can('removeChild');
367 $parent->removeChild($item);
368 }
369 }
370 my $tree_view = Tree::Simple::View::HTML->new
371 (
372 $display_tree =>
373 (
374 list_css => "list-style: circle;",
375 list_item_css => "font-family: courier;",
376 node_formatter => sub
377 {
378 my ($tree) = @_;
379 my $node_string = $tree->getNodeValue()->node_name;
380  
381 $tree->accept($path2root_visitor);
382 my $checkbox_name = $path2root_visitor->getPathAsString("/");
383  
384 my $checked = '';
385 my $color = 'blue';
386  
387 if($tree->getNodeValue->in_feature)
388 {
389 # it's part of a feature so avoid using this mechanism.
390 $color = 'grey';
391 }
392 elsif ( defined $tree->getNodeValue->action_path )
393 {
394 $color = 'red';
395 if ( my $roles = $tree->getNodeValue->access_only )
396 {
397 my $matched_role = 0;
398 foreach my $allowed_role ( @{ $tree->getNodeValue->access_only } )
399 {
400 $matched_role = 1 if ( $allowed_role eq $show_role );
401 }
402 if ( $matched_role ) # rules and a matched.. therefore, access :)..
403 {
404 $checked = 'checked';
405 $color = 'green';
406 }
407 }
408 $node_string = "<input type='checkbox' name='action_$checkbox_name' value='allow' $checked>" . $node_string;
409 }
410 else
411 {
412 $node_string = $node_string;
413 }
414 $node_string = "<font color='$color'>" . $node_string . "</font>";
415 return $node_string;
416 }
417 )
418 );
419 $c->stash->{access_control_role_tree} = $tree_view;
420  
421 # manually set (as we may forward to this action).
422 $c->stash->{template} = 'appkit/admin/access/show_role.tt';
423 }
424  
425 sub edit_descriptions
426 : Local
427 : AppKitFeature('Feature Documentation')
428 : AppKitForm
429 {
430 my ($self, $c) = @_;
431  
432 my $form = $c->stash->{form};
433 my @features = $c->model('AppKitAuthDB::Aclfeature')->sorted->all;
434 my $count = @features;
435 $self->add_final_crumb($c, 'Feature documentation');
436 $form->get_all_element('features')->repeat($count);
437 my $defaults = {};
438 if($form->submitted_and_valid)
439 {
440 my %by_id = map { $_->id => $_ } @features;
441 my $schema = $c->model('AppKitAuthDB')->schema;
442 my $guard = $schema->storage->txn_scope_guard;
443 for(my $i = 1; $i <= $count; $i++)
444 {
445 my $id = $c->req->params->{"id_$i"};
446 my $description = $c->req->params->{"feature_description_$i"} || undef;
447 my $feature = $by_id{$id};
448 if($feature)
449 {
450 $feature->update({ feature_description => $description });
451 }
452 }
453 $guard->commit;
454 $c->flash->{status_msg} = 'Changes saved';
455 $c->res->redirect($c->req->uri);
456 }
457 else
458 {
459 my $i = 1;
460 for my $f (@features)
461 {
462 $form->get_all_element("feature_description_$i")->label($f->feature);
463 $defaults->{"id_$i"} = $f->id;
464 #$defaults->{"feature_$i"} = $f->feature;
465 $defaults->{"feature_description_$i"} = $f->feature_description;
466 $i++;
467 }
468 $form->default_values($defaults);
469 }
470 }
471  
472 __END__
473  
474 =pod
475  
476 =encoding UTF-8
477  
478 =head1 NAME
479  
480 OpusVL::AppKit::Controller::AppKit::Admin::Access
481  
482 =head1 VERSION
483  
484 version 2.22
485  
486 =head2 auto
487  
488 Default action for this controller.
489  
490 =head2 index
491  
492 default action for access administration.
493  
494 =head2 addrole
495  
496 Add a role
497  
498 =head2 role_specific
499  
500 Start of chain.
501 Action to capture role specific action..
502  
503 =head2 role_management
504  
505 =head2 user_for_role
506  
507 Middle of chain.
508  
509 =head2 user_delete_from_role
510  
511 End of chain.
512 Add a user to a role (and give it a value)
513  
514 =head2 delete_role
515  
516 End of chain.
517 Deletes a role (after confirmation)
518  
519 =head2 user_add_to_role
520  
521 End of chain.
522 Adds a user to a role
523  
524 =head2 action_rule_for_role
525  
526 End of chain.
527  
528 =head2 show_role
529  
530 End of chain.
531 Action to display role info page.
532  
533 =head1 AUTHOR
534  
535 OpusVL - www.opusvl.com
536  
537 =head1 COPYRIGHT AND LICENSE
538  
539 This software is copyright (c) 2010 by OpusVL - www.opusvl.com.
540  
541 This is free software; you can redistribute it and/or modify it under
542 the same terms as the Perl 5 programming language system itself.
543  
544 =cut