TT#28510 Fix customer_id field for the subscriber role in the Subscribers API

Change-Id: I4f2350ff8c9f4a3229b54a526ce9726382d2f3ee
changes/27/18027/4
Irina Peshinskaya 8 years ago
parent 0a8c92f461
commit 632406b006

@ -104,11 +104,15 @@ has_field 'timezone' => (
title => ['The timezone of the subscriber.']
},
);
has_field 'customer' => (
type => '+NGCP::Panel::Field::CustomerContract',
#we need customer_id field in the form to don't delete customer_id from the resource as absent field during form validation
#but for subscriberadmin role value always will be pilot->account_id
#for subscriber we have read-only access
#but we use resource->{customer_id} to get customer in prepare resource
has_field 'customer_id' => (
type => 'PosInteger',
label => 'Customer',
validate_when_empty => 1,
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['The contract used for this subscriber.']

@ -263,6 +263,38 @@ if($remote_config->{config}->{features}->{cloudpbx}){
$test_machine->clear_test_data_all();#fake data aren't registered in this test machine, so they will stay.
}
#TT#28510
{
diag("28510: check subscriberadmin POST ;\n");
my $data = clone $test_machine->DATA_ITEM;
$data->{administrative} = 1;
my($subscriberadmin) = $test_machine->check_create_correct(1, sub {
my $num = $_[1]->{i};
$_[0]->{administrative} = 1;
$_[0]->{webusername} .= time().'_28510';
$_[0]->{webpassword} = 'api_test_webpassword';
$_[0]->{username} .= time().'_28510' ;
$_[0]->{pbx_extension} .= '28510';
$_[0]->{primary_number}->{ac} .= '28510';
$_[0]->{is_pbx_group} = 0;
$_[0]->{is_pbx_pilot} = ($pilot || $_[1]->{i} > 1)? 0 : 1;
} )->[0];
$test_machine->subscriber_user(join('@',@{$subscriberadmin->{content}}{qw/webusername domain/}));
$test_machine->subscriber_pass($subscriberadmin->{content}->{webpassword});
$test_machine->runas('subscriber');
$test_machine->check_create_correct(1, sub {
my $num = $_[1]->{i};
$_[0]->{webusername} .= time().'_28510_1';
$_[0]->{webpassword} = 'api_test_webpassword';
$_[0]->{username} .= time().'_28510_1' ;
$_[0]->{pbx_extension} .= '285101';
$_[0]->{primary_number}->{ac} .= '285101';
$_[0]->{is_pbx_group} = 1;
$_[0]->{is_pbx_pilot} = 0;
delete $_[0]->{alias_numbers};
} );
$test_machine->runas('admin');
}
}
#TT#8680
{

@ -102,7 +102,7 @@ has 'ua' => (
builder => 'init_ua',
);
has 'base_uri' => (
is => 'ro',
is => 'rw',
isa => 'Str',
lazy => 1,
default => sub {
@ -268,17 +268,26 @@ sub _create_ua {
verify_hostname => 0,
SSL_verify_mode => 0,
);
if($init_cert and ($role eq "default" || $role eq "admin" || $role eq "reseller")) {
$self->init_ssl_cert($ua);
if($init_cert) {
$self->init_ssl_cert($ua, $role);
}
return $ua;
}
sub init_ssl_cert {
my ($self, $ua) = @_;
my ($self, $ua, $role) = @_;
if($role ne "default" && $role ne "admin" && $role ne "reseller") {
$ua->ssl_opts(
SSL_cert_file => undef,
SSL_key_file => undef,
);
return;
}
lock $tmpfilename;
unless ($tmpfilename) {
my $_ua = $ua // $self->_create_ua(0);
my $res = $_ua->post(
$self->base_uri . '/api/admincerts/',
Content_Type => 'application/json',
Content => '{}'
@ -324,36 +333,48 @@ sub clear_cert {
delete $self->{ua};
return $self;
}
sub runas {
my $self = shift;
my($role_in,$uri) = @_;
my($user,$pass,$role,$realm,$port) = $self->get_role_credentials($role_in);
my $base_url = $self->base_uri;
$base_url =~ s/^(https?:[^:]+:)\d+(?:$|\/)/$1$port\//;
#print Dumper ["base_url",$base_url,"role",$role,$user,$pass];
$self->base_uri($base_url);
$uri //= $self->base_uri;
$uri =~ s/^https?:\/\///;
my($user,$pass,$role,$realm) = $self->get_role_credentials($role_in);
$uri =~ s/^https?:\/\/|\/$//g;
$self->runas_role($role);
#print Dumper ["runas",$uri, $realm, $user, $pass];
$self->ua->credentials( $uri, $realm, $user, $pass);
$self->init_ssl_cert($self->ua, $role);
diag("runas: $role;");
return $self;
}
sub get_role_credentials{
my $self = shift;
my($role) = @_;
my($user,$pass);
$role //= $self->runas_role // 'default';
my $realm;
my ($realm,$port);
if($role eq 'default' || $role eq 'admin'){
$user //= $ENV{API_USER} // 'administrator';
$pass //= $ENV{API_PASS} // 'administrator';
$realm = 'api_admin_http';
$port = '1443';
}elsif($role eq 'reseller'){
$user //= $ENV{API_USER_RESELLER} // 'api_test';
$pass //= $ENV{API_PASS_RESELLER} // 'api_test';
$realm = 'api_admin_http';
$port = '1443';
}elsif($role eq 'subscriber'){
$user = $self->subscriber_user;
$pass = $self->subscriber_pass;
$realm = 'api_subscriber_http';
$port = '443';
}
return($user,$pass,$role,$realm);
return($user,$pass,$role,$realm,$port);
}
sub clear_data_created{
my($self) = @_;
@ -432,7 +453,7 @@ sub get_item_hal{
#print "uri=$uri;";
my($res,$list_collection,$req) = $self->check_item_get($self->normalize_uri($uri));
($reshal,$location,$total_count,$reshal_collection) = $self->get_hal_from_collection($list_collection,$name);
#print Dumper $reshal;
#print Dumper [$location,$total_count,$reshal,$reshal_collection];
if($total_count || ('HASH' eq ref $reshal->{content} && $reshal->{content}->{total_count})){
$self->IS_EMPTY_COLLECTION(0);
$resitem = {
@ -478,9 +499,10 @@ sub get_hal_from_collection{
} elsif( ref $list_collection eq 'HASH' && $list_collection->{_links}->{self}->{href}) {
#preferencedefs collection
#or empty collection, see "/api/pbxdeviceprofiles/?name=two"
#or just got requested item, as /api/sibscribers/id
$reshal = $list_collection;
$location = $reshal->{_links}->{self}->{href};
$total_count = $reshal->{total_count};
$total_count = $reshal->{total_count} // 1;
#print Dumper ["get_hal_from_collection","4",$total_count];
}
return ($reshal,$location,$total_count,$reshal_collection);
@ -670,6 +692,7 @@ sub request_post{
my($self, $content, $uri, $req) = @_;
$uri ||= $self->get_uri_collection;
$uri = $self->normalize_uri($uri);
diag("request_post: uri: $uri;");
$content = $self->encode_content($content, $self->content_type->{POST} );
#form-data is set automatically, despite on $self->content_type->{POST}
$req ||= POST $uri,

Loading…
Cancel
Save