From 8ee1e8e08b644b9592e61003462f04e96abf3041 Mon Sep 17 00:00:00 2001 From: Irina Peshinskaya Date: Wed, 5 Dec 2018 06:26:13 +0100 Subject: [PATCH] TT#47268 Fix getting banned items Change-Id: I751b3c9e2cf32bef0b3c9c9fd77b6fbdbb83ea8b --- lib/NGCP/Panel/Controller/API/BannedIpsItem.pm | 2 +- lib/NGCP/Panel/Controller/API/BannedUsersItem.pm | 2 +- lib/NGCP/Panel/Role/API/BannedIps.pm | 3 ++- lib/NGCP/Panel/Role/API/BannedUsers.pm | 3 ++- lib/NGCP/Panel/Utils/Security.pm | 15 +++++++++++---- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/BannedIpsItem.pm b/lib/NGCP/Panel/Controller/API/BannedIpsItem.pm index e2035a6e41..ebbd7497d3 100644 --- a/lib/NGCP/Panel/Controller/API/BannedIpsItem.pm +++ b/lib/NGCP/Panel/Controller/API/BannedIpsItem.pm @@ -17,7 +17,7 @@ sub allowed_methods { sub delete_item { my($self, $c, $item, $old_resource, $resource, $form) = @_; - my $ip = $item; + my $ip = $item->{ip}; NGCP::Panel::Utils::Security::ip_unban($c, $ip); return 1; } diff --git a/lib/NGCP/Panel/Controller/API/BannedUsersItem.pm b/lib/NGCP/Panel/Controller/API/BannedUsersItem.pm index cba0f536c9..2b99aed81f 100644 --- a/lib/NGCP/Panel/Controller/API/BannedUsersItem.pm +++ b/lib/NGCP/Panel/Controller/API/BannedUsersItem.pm @@ -17,7 +17,7 @@ sub allowed_methods { sub delete_item { my($self, $c, $item, $old_resource, $resource, $form) = @_; - my $user = $item; + my $user = $item->{username}; NGCP::Panel::Utils::Security::user_unban($c, $user); return 1; } diff --git a/lib/NGCP/Panel/Role/API/BannedIps.pm b/lib/NGCP/Panel/Role/API/BannedIps.pm index d14097e4bd..c53d9f4da8 100644 --- a/lib/NGCP/Panel/Role/API/BannedIps.pm +++ b/lib/NGCP/Panel/Role/API/BannedIps.pm @@ -30,7 +30,8 @@ sub valid_id { } sub item_by_id{ my ($self, $c, $id) = @_; - return $id; + my $list = NGCP::Panel::Utils::Security::list_banned_ips($c, id => $id ); + return ref $list eq 'ARRAY' ? $list->[0] : undef ; } 1; # vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Role/API/BannedUsers.pm b/lib/NGCP/Panel/Role/API/BannedUsers.pm index 2378c6e383..377f2fadd7 100644 --- a/lib/NGCP/Panel/Role/API/BannedUsers.pm +++ b/lib/NGCP/Panel/Role/API/BannedUsers.pm @@ -31,7 +31,8 @@ sub valid_id { sub item_by_id{ my ($self, $c, $id) = @_; - return $id; + my $list = NGCP::Panel::Utils::Security::list_banned_users($c, id => $id ); + return ref $list eq 'ARRAY' ? $list->[0] : undef ; } diff --git a/lib/NGCP/Panel/Utils/Security.pm b/lib/NGCP/Panel/Utils/Security.pm index 285daa261c..db1765f99a 100644 --- a/lib/NGCP/Panel/Utils/Security.pm +++ b/lib/NGCP/Panel/Utils/Security.pm @@ -7,7 +7,7 @@ use NGCP::Panel::Utils::XMLDispatcher; use NGCP::Panel::Utils::DateTime; sub list_banned_ips { - my ( $c ) = @_; + my ( $c, %params ) = @_; my $xml_parser = XML::LibXML->new(); my $ip_xml = <<'EOF'; @@ -23,13 +23,18 @@ EOF my $ip_res = NGCP::Panel::Utils::XMLDispatcher::dispatch($c, "loadbalancer", 1, 1, $ip_xml); my @ips = (); - for my $host (grep {$$_[1]} @$ip_res) { + XML_RESULTS_LOOP: for my $host (grep {$$_[1]} @$ip_res) { my $xmlDoc = $xml_parser->parse_string($host->[2]); foreach my $node ($xmlDoc->findnodes('//member')) { my $name = $node->findvalue('./name'); my $value = $node->findvalue('./value/string'); if ($name eq 'name') { - push @ips, { ip => $value }; + if (!defined $params{id} || $params{id} eq $value) { + push @ips, { ip => $value, id => $value, }; + if (defined $params{id}) { + last XML_RESULTS_LOOP; + } + } } } } @@ -74,7 +79,8 @@ EOF } } my $config_failed_auth_attempts = $c->config->{security}->{failed_auth_attempts} // 3; - for my $key (keys %{ $usr }) { + #in case we requested to filter by username, we will use it as the only possible key + for my $key ( ( defined $params{id} && exists $usr->{$params{id}}) ? ( $params{id} ) : (keys %{ $usr }) ) { my $last_auth = $usr->{$key}->{last_auth} ? NGCP::Panel::Utils::DateTime::epoch_local($usr->{$key}->{last_auth}) : undef; if ($last_auth) { $last_auth->set_time_zone($c->session->{user_tz}) if $c->session->{user_tz}; @@ -84,6 +90,7 @@ EOF && $usr->{$key}->{auth_count} >= $config_failed_auth_attempts ) { push @users, { username => $key, + id => $key, auth_count => $usr->{$key}->{auth_count}, last_auth => $last_auth, };