blib/lib/App/Netdisco/Web/Plugin/AdminTask/ACLManager.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 163 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package App::Netdisco::Web::Plugin::AdminTask::ACLManager;
2  
3 use Dancer ':syntax';
4 use Dancer::Plugin::DBIC;
5 use Dancer::Plugin::Auth::Extensible;
6 use Dancer::Plugin::Ajax;
7  
8 use App::Netdisco::Web::Plugin;
9 use App::Netdisco::Util::ManagedConfig
10 qw/acl_txn_do yaml_setting is_reserved_group_name/;
11  
12 register_admin_task({
13 tag => "aclmanager",
14 label => "Access Control Lists"
15 });
16  
17 ajax '/ajax/content/admin/aclmanager' => require_role admin => sub {
18 my @acls = schema(vars->{'tenant'})->resultset('AccessControlListName')
19 ->order_by([qw/acl_type acl_name/])->all;
20  
21 template 'ajax/admintask/aclmanager.tt', {
22 results => \@acls,
23 }, { layout => undef };
24 };
25  
26 ajax '/ajax/control/admin/aclmanager/add' => require_role setting('defanged_admin') => sub {
27 my $acl = param('acl_name');
28 send_error('Bad Request', 400) unless $acl;
29 my $type = param('acl_type');
30 send_error('Bad Request', 400) unless $type
31 and $type =~ m/^(?:host|host_host|host_port)$/;
32 # a host ACL becomes host_groups->{<name>}, so it must not take a name
33 # Netdisco relies on, such as __ANY__
34 send_error('Bad Request', 400)
35 if $type eq 'host' and is_reserved_group_name($acl);
36 send_error('Bad Request', 400)
37 if schema(vars->{'tenant'})->resultset('AccessControlListName')
38 ->search({acl_name => $acl})->count();
39  
40 acl_txn_do(sub {
41 schema(vars->{'tenant'})->resultset('AccessControlListName')
42 ->create({
43 acl_name => $acl,
44 acl_type => $type,
45 });
46  
47 my $new = schema(vars->{'tenant'})->resultset('AccessControlListMap')
48 ->create({
49 acl_name => $acl,
50 left_acl => {}, right_acl => {},
51 });
52  
53 $new->left_acl->update({ rules => ['group:__ANY__'] });
54 $new->right_acl->update({ rules => ['group:__ANY__'] })
55 if $type eq 'host_host';
56 });
57  
58 return '';
59 };
60  
61 ajax '/ajax/control/admin/aclmanager/delete' => require_role setting('defanged_admin') => sub {
62 my $acl = param('acl_name');
63 send_error('Bad Request', 400) unless $acl;
64  
65 unless (exists +(yaml_setting('portctl_by_role') || {})->{$acl}) {
66 schema('netdisco')->txn_do(sub {
67 my $users = schema('netdisco')->resultset('User');
68  
69 # "contained by" rather than equality: a user left holding only this
70 # name more than once, or holding an empty list, must have port
71 # control turned off. array_remove below would otherwise leave them
72 # with {}, which the role checks read as "Enabled (any port)".
73 $users->search(\[ 'portctl_roles <@ ARRAY[?]::text[]', $acl ])
74 ->update({ portctl_roles => undef, port_control => \'false' });
75  
76 $users->search(\[ '? = ANY(portctl_roles)', $acl ])
77 ->update({ portctl_roles => \[ 'array_remove(portctl_roles, ?)', $acl ] });
78 });
79 }
80  
81 schema('netdisco')->resultset('User')
82 ->search({token_acl => $acl})
83 ->update({token_acl => undef});
84  
85 acl_txn_do(sub {
86 schema(vars->{'tenant'})->resultset('AccessControlListName')
87 ->find({ acl_name => $acl })->delete;
88  
89 my $maps = schema(vars->{'tenant'})->resultset('AccessControlListMap')
90 ->search({ acl_name => $acl })
91 or return;
92  
93 schema(vars->{'tenant'})->resultset('AccessControlList')
94 ->search({id => { -in => [ $maps->left_acls ] }})->delete;
95 schema(vars->{'tenant'})->resultset('AccessControlList')
96 ->search({id => { -in => [ $maps->right_acls ] }})->delete;
97  
98 $maps->delete;
99 });
100  
101 return '';
102 };
103  
104 ajax '/ajax/control/admin/aclmanager/update' => require_role setting('defanged_admin') => sub {
105 my $acl = param('acl_name');
106 my $old_acl = param('old-acl_name');
107 send_error('Bad Request', 400) unless $acl and $old_acl;
108 my $type = param('acl_type');
109 send_error('Bad Request', 400) unless $type
110 and $type =~ m/^(?:host|host_host|host_port)$/;
111 # as for add: also reached by turning a role into a host ACL
112 send_error('Bad Request', 400)
113 if $type eq 'host' and is_reserved_group_name($acl);
114  
115 acl_txn_do(sub {
116 schema(vars->{'tenant'})->resultset('AccessControlListName')
117 ->search({ acl_name => $old_acl })
118 ->update({ acl_name => $acl, acl_type => $type });
119  
120 my $maps = schema(vars->{'tenant'})->resultset('AccessControlListMap')
121 ->search({ acl_name => $old_acl });
122  
123 $maps->update({ acl_name => $acl });
124 if ($type eq 'host') {
125 schema(vars->{'tenant'})->resultset('AccessControlList')
126 ->search({id => { -in => [ $maps->right_acls ] }})
127 ->update({ rules => [] });
128 }
129 });
130  
131 # Users are renamed only once the ACL rename has committed. Renaming onto
132 # a name that already exists fails on the acl_name primary key, and when
133 # users were renamed first they kept the new name anyway: a user moved
134 # onto the other ACL's rights, or one holding both was left with the name
135 # twice. Users live in the default database and a tenant's ACLs in its
136 # own, so one transaction cannot cover both; the ordering does instead,
137 # as acl_txn_do throws before this runs.
138 schema('netdisco')->resultset('User')
139 ->search(\[ '? = ANY(portctl_roles)', $old_acl ])
140 ->update({ portctl_roles => \[ 'array_replace(portctl_roles, ?, ?)', $old_acl, $acl ] });
141  
142 return '';
143 };
144  
145 true;
146