TT#149456 fix and improve user role_id retreival, api tests

* NGCP::Panel::Utils::UserRole::_field_to_name now supports
  both hash and object to parse the roles from
* NGCP::Panel::Utils::UserRole::resolve_role_id returns undef
  if no roles has been passed or there are no entries for the
  role in the database
* Fix NGCP::Panel::Utils::Journals to correctly fetch and set
  $journal{$role_id}
* Adjust api-journals.t, remove tx_id, user_id, role_id from
  checks

Change-Id: Ieff23bd4291f3b88ba92bbfc2b00b57f66bf76e1
mr10.3
Kirill Solomko 4 years ago
parent 16b4dfe5dd
commit ac94626f9e

@ -18,6 +18,7 @@ use IO::Compress::Deflate qw($DeflateError);
use IO::Uncompress::Inflate qw();
use Sereal::Decoder qw();
use Sereal::Encoder qw();
use NGCP::Panel::Utils::UserRole;
use constant CREATE_JOURNAL_OP => 'create';
use constant UPDATE_JOURNAL_OP => 'update';
@ -653,8 +654,9 @@ sub _create_journal {
}
}
$journal{reseller_id} = $resource->{reseller_id};
$journal{role_id} = $c->user->acl_role->id // 0;
$journal{role_id} = NGCP::Panel::Utils::UserRole::resolve_role_id($c, $c->user);
$journal{tx_id} = $c->session->{api_request_tx_id};
try {

@ -1,9 +1,19 @@
package NGCP::Panel::Utils::UserRole;
use Sipwise::Base;
use Scalar::Util qw(blessed);
sub _flags_to_name {
my (%flags) = @_;
my $params = shift;
return unless $params && ref $params;
my %flags;
if (blessed($params)) { # object
map { $flags{$_} = $params->$_ }
qw(is_system is_superuser is_ccare lawful_intercept);
} else {
%flags = %{$params};
}
# "system" - is_system = 1,
# "admin" - is_superuser = 1
@ -38,10 +48,10 @@ sub _flags_to_name {
sub resolve_role_id {
my ($c, $params) = @_;
my $role_name = &_flags_to_name(%$params);
my $role = $c->model('DB')->resultset('acl_roles')->search({role => $role_name})->first;
my $role_name = _flags_to_name($params) // return;
my $role = $c->model('DB')->resultset('acl_roles')->find({role => $role_name});
return $role->id;
return $role ? $role->id : undef;
}
1;
1;

@ -2458,7 +2458,7 @@ sub _test_journal_collection {
foreach my $journal (@{ $collection->{_embedded}->{'ngcp:journal'} }) {
ok(exists $page_journals->{$journal->{id}},"check existence of linked journal item among embedded");
my $original = delete $page_journals->{$journal->{id}};
delete $original->{content};
delete @{$original}{qw(content tx_id user_id role_id)};
is_deeply($original,$journal,"compare created and embedded journal item deeply");
}
ok((scalar keys %{ $page_journals }) == 0,"check if all embedded journal items are linked");

Loading…
Cancel
Save