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.'] title => ['The timezone of the subscriber.']
}, },
); );
#we need customer_id field in the form to don't delete customer_id from the resource as absent field during form validation
has_field 'customer' => ( #but for subscriberadmin role value always will be pilot->account_id
type => '+NGCP::Panel::Field::CustomerContract', #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', label => 'Customer',
validate_when_empty => 1, validate_when_empty => 1,
required => 1,
element_attr => { element_attr => {
rel => ['tooltip'], rel => ['tooltip'],
title => ['The contract used for this subscriber.'] 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. $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 #TT#8680
{ {

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

Loading…
Cancel
Save