« blib/lib/App/Netdisco/Web/Plugin/AdminTask/PseudoDevice.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin.pm »
| 1 | package App::Netdisco::Web::PortControl; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::Ajax; | ||
| 5 | use Dancer::Plugin::DBIC; | ||
| 6 | use Dancer::Plugin::Auth::Extensible; | ||
| 7 | |||
| 8 | use App::Netdisco::JobQueue qw/jq_insert jq_userlog/; | ||
| 9 | use App::Netdisco::Util::Port qw/device_acl_by_role_check/; | ||
| 10 | use App::Netdisco::Backend::Job; | ||
| 11 | |||
| 12 | my %action_map = ( | ||
| 13 | 'location' => 'location', | ||
| 14 | 'contact' => 'contact', | ||
| 15 | 'c_port' => 'portcontrol', | ||
| 16 | 'c_name' => 'portname', | ||
| 17 | 'c_pvid' => 'vlan', | ||
| 18 | 'c_power' => 'power', | ||
| 19 | ); | ||
| 20 | |||
| 21 | sub _action_for_field { | ||
| 22 | my $field = shift; | ||
| 23 | return undef unless defined $field and length $field; | ||
| 24 | return $action_map{ $field } if exists $action_map{ $field }; | ||
| 25 | return $field | ||
| 26 | if scalar grep {$_ eq $field} @{ setting('_inline_actions') || [] }; | ||
| 27 | return undef; | ||
| 28 | } | ||
| 29 | |||
| 30 | ajax '/ajax/portcontrol' => require_any_role [qw(admin port_control)] => sub { | ||
| 31 | send_error('No device/port/field', 400) | ||
| 32 | unless param('device') and (param('port') or param('field')); | ||
| 33 | |||
| 34 | my $action = _action_for_field( param('field') ); | ||
| 35 | send_error('Unknown field. Send one of location, contact, c_port, c_name,' | ||
| 36 | .' c_pvid, c_power, or a configured custom field.', 400) | ||
| 37 | unless defined $action; | ||
| 38 | |||
| 39 | # the details view offers these controls only for a device the role ACL | ||
| 40 | # covers, and location, contact and the custom fields reach the queue | ||
| 41 | # without a worker check phase to scope them | ||
| 42 | send_error('Forbidden', 403) | ||
| 43 | unless user_has_role('admin') | ||
| 44 | or device_acl_by_role_check( param('device'), logged_in_user ); | ||
| 45 | |||
| 46 | my $log = sprintf 'd:[%s] p:[%s] f:[%s]. a:[%s] v[%s]', | ||
| 47 | param('device'), (param('port') || ''), param('field'), | ||
| 48 | (param('action') || ''), (param('value') || ''); | ||
| 49 | |||
| 50 | my $subaction = ($action =~ m/^(?:power|portcontrol)/ | ||
| 51 | ? (param('action') ."-other") | ||
| 52 | : param('value')); | ||
| 53 | |||
| 54 | send_error('Configuration overrides in a port or value need the admin' | ||
| 55 | .' role. Send the port name and value on their own.', 403) | ||
| 56 | if not user_has_role('admin') | ||
| 57 | and App::Netdisco::Backend::Job->carries_config_overrides({ | ||
| 58 | action => $action, port => param('port'), subaction => $subaction }); | ||
| 59 | |||
| 60 | schema(vars->{'tenant'})->txn_do(sub { | ||
| 61 | if (param('port')) { | ||
| 62 | my $act = "$action $subaction"; | ||
| 63 | $act =~ s/-other$//; | ||
| 64 | $act =~ s/^portcontrol/port/; | ||
| 65 | $act =~ s/^device_port_custom_field_/custom_field: /; | ||
| 66 | |||
| 67 | schema(vars->{'tenant'})->resultset('DevicePortLog')->create({ | ||
| 68 | ip => param('device'), | ||
| 69 | port => param('port'), | ||
| 70 | action => $act, | ||
| 71 | username => session('logged_in_user'), | ||
| 72 | userip => request->remote_address, | ||
| 73 | reason => (param('reason') || 'other'), | ||
| 74 | log => param('log'), | ||
| 75 | }); | ||
| 76 | } | ||
| 77 | |||
| 78 | jq_insert({ | ||
| 79 | device => param('device'), | ||
| 80 | port => param('port'), | ||
| 81 | action => $action, | ||
| 82 | subaction => $subaction, | ||
| 83 | username => session('logged_in_user'), | ||
| 84 | userip => request->remote_address, | ||
| 85 | log => $log, | ||
| 86 | }); | ||
| 87 | }); | ||
| 88 | |||
| 89 | content_type('application/json'); | ||
| 90 | to_json({}); | ||
| 91 | }; | ||
| 92 | |||
| 93 | ajax '/ajax/userlog' => require_login sub { | ||
| 94 | my @jobs = jq_userlog( session('logged_in_user') ); | ||
| 95 | |||
| 96 | my %status = ( | ||
| 97 | 'done' => [ | ||
| 98 | map {s/\[\]/<empty>/; $_} | ||
| 99 | map { $_->log } | ||
| 100 | grep { $_->status eq 'done' } | ||
| 101 | grep { defined } | ||
| 102 | @jobs | ||
| 103 | ], | ||
| 104 | 'error' => [ | ||
| 105 | map {s/\[\]/<empty>/; $_} | ||
| 106 | map { $_->log } | ||
| 107 | grep { $_->status eq 'error' } | ||
| 108 | grep { defined } | ||
| 109 | @jobs | ||
| 110 | ], | ||
| 111 | ); | ||
| 112 | |||
| 113 | content_type('application/json'); | ||
| 114 | to_json(\%status); | ||
| 115 | }; | ||
| 116 | |||
| 117 | true; |