TT#4335 api subscriberpreferences for subadmin

Change-Id: Ia15acf95c29617515b83be9e04a6785813cea1e6
changes/03/11203/3
Gerhard Jungwirth 9 years ago
parent dd6f75184b
commit fcacadc568

@ -39,7 +39,7 @@ __PACKAGE__->config(
action => {
map { $_ => {
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => [qw/admin reseller/],
AllowedRole => [qw/admin reseller subscriberadmin/],
Args => 0,
Does => [qw(ACL CheckTrailingSlash RequireSSL)],
Method => $_,
@ -54,6 +54,7 @@ sub auto :Private {
$self->set_body($c);
$self->log_request($c);
return 1;
}
sub GET :Allow {
@ -108,6 +109,7 @@ sub end : Private {
my ($self, $c) = @_;
$self->log_response($c);
return;
}
1;

@ -82,7 +82,7 @@ __PACKAGE__->config(
action => {
map { $_ => {
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => [qw/admin reseller/],
AllowedRole => [qw/admin reseller subscriberadmin/],
Args => 0,
Does => [qw(ACL CheckTrailingSlash RequireSSL)],
Method => $_,
@ -97,6 +97,7 @@ sub auto :Private {
$self->set_body($c);
$self->log_request($c);
return 1;
}
sub GET :Allow {
@ -116,7 +117,7 @@ sub GET :Allow {
for my $subscriber (@$subscribers) {
next unless($subscriber->provisioning_voip_subscriber);
my $contract = $subscriber->contract;
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
my $balance; $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $contract,
now => $now) if !exists $contract_map{$contract->id}; #apply underrun lock level
$contract_map{$contract->id} = 1;
@ -182,6 +183,7 @@ sub end : Private {
my ($self, $c) = @_;
$self->log_response($c);
return;
}
1;

@ -43,7 +43,7 @@ __PACKAGE__->config(
action => {
(map { $_ => {
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => [qw/admin reseller/],
AllowedRole => [qw/admin reseller subscriberadmin/],
Args => 1,
Does => [qw(ACL RequireSSL)],
Method => $_,
@ -51,7 +51,7 @@ __PACKAGE__->config(
} } @{ __PACKAGE__->allowed_methods }),
@{ __PACKAGE__->get_journal_action_config(__PACKAGE__->resource_name,{
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => [qw/admin reseller/],
AllowedRole => [qw/admin reseller subscriberadmin/],
Does => [qw(ACL RequireSSL)],
}) }
},

@ -63,9 +63,16 @@ sub get_resource {
my $prefs;
my %profile_attrs = (); # for filtering profiles based list
my %profile_allowed_attrs; # for filtering subscriber attrs on its profile
my $has_profile = 0;
my $attr = 0;
if($type eq "subscribers") {
$prefs = $item->provisioning_voip_subscriber->voip_usr_preferences;
my $profile = $item->provisioning_voip_subscriber->voip_subscriber_profile;
if ($profile) {
$has_profile = 1;
%profile_allowed_attrs = map { $_ => 1 } $profile->profile_attributes->get_column('attribute_id')->all;
}
} elsif($type eq "profiles") {
$attr = 1;
%profile_attrs = map { $_ => 1 } $item->profile_attributes->get_column('attribute_id')->all;
@ -94,6 +101,19 @@ sub get_resource {
my $value;
my $processed = 0;
if ($c->user->roles eq 'subscriberadmin') {
my $attrname = $pref->attribute->attribute;
unless ( $pref->attribute->expose_to_customer ) {
$c->log->debug("skipping attribute $attrname, not exposing to customer");
next;
}
if ($has_profile && !$profile_allowed_attrs{$pref->attribute_id}) {
$c->log->debug("skipping attribute $attrname, not in profile");
next;
}
}
SWITCH: for ($pref->attribute->attribute) {
/^rewrite_calle[re]_(in|out)_dpid$/ && do {
if(exists $resource->{rewrite_rule_set}) {
@ -292,6 +312,13 @@ sub _item_rs {
}, {
join => { 'contract' => 'contact' },
});
} elsif ($c->user->roles eq 'subscriberadmin') {
$item_rs = $c->model('DB')->resultset('voip_subscribers')->search({
'contract.id' => $c->user->account_id,
'me.status' => { '!=' => 'terminated' },
},{
join => 'contract',
});
}
} elsif($type eq "peerings") {
if($c->user->roles eq "admin") {
@ -372,6 +399,7 @@ sub get_preference_rs {
c => $c,
attribute => $attr,
prov_subscriber => $elem,
($c->user->roles eq "subscriberadmin") ? (subscriberadmin => 1) : (),
);
} elsif($type eq "peerings") {
$rs = NGCP::Panel::Utils::Preferences::get_peer_preference_rs(
@ -441,6 +469,21 @@ sub update_item {
$accessor = $item->username . '@' . $item->domain->domain;
$elem = $item->provisioning_voip_subscriber;
$full_rs = $elem->voip_usr_preferences;
if ($c->user->roles eq 'subscriberadmin') {
$full_rs = $full_rs->search_rs({
'attribute.expose_to_customer' => 1,
},{
join => 'attribute',
});
if ($elem && $elem->voip_subscriber_profile) {
my @allowed_attr_ids = $elem->voip_subscriber_profile->profile_attributes
->get_column('attribute_id')->all;
$full_rs = $full_rs->search_rs({
'attribute.id' => { '-in' => \@allowed_attr_ids },
});
}
}
$pref_type = 'usr_pref';
$reseller_id = $item->contract->contact->reseller_id;
} elsif($type eq "peerings") {
@ -593,16 +636,16 @@ sub update_item {
foreach my $pref(keys %{ $resource }) {
next unless(defined $resource->{$pref});
my $rs = $self->get_preference_rs($c, $type, $elem, $pref);
unless($rs) {
my $pref_rs = $self->get_preference_rs($c, $type, $elem, $pref);
unless($pref_rs) {
$c->log->debug("removing unknown preference '$pref' from update");
next;
}
$rs = $rs->search(undef, {
$pref_rs = $pref_rs->search(undef, {
order_by => { '-asc' => 'id' },
});
# TODO: can't we get this via $rs->search_related or $rs->related_resultset?
# TODO: can't we get this via $pref_rs->search_related or $pref_rs->related_resultset?
my $meta = $c->model('DB')->resultset('voip_preferences')->find({
attribute => $pref, $pref_type => 1,
});
@ -744,26 +787,26 @@ sub update_item {
};
# default
if($meta->max_occur != 1) {
$rs->delete;
$pref_rs->delete;
foreach my $v(@{ $resource->{$pref} }) {
return unless $self->check_pref_value($c, $meta, $v, $pref_type);
if(JSON::is_bool($v)){
$v = $v ? 1 : 0 ;
}
$rs->create({ value => $v });
$pref_rs->create({ value => $v });
}
} elsif($rs->first) {
} elsif($pref_rs->first) {
return unless $self->check_pref_value($c, $meta, $resource->{$pref}, $pref_type);
if(JSON::is_bool($resource->{$pref})){
$resource->{$pref} = $resource->{$pref} ? 1 : 0 ;
}
$rs->first->update({ value => $resource->{$pref} });
$pref_rs->first->update({ value => $resource->{$pref} });
} else {
return unless $self->check_pref_value($c, $meta, $resource->{$pref}, $pref_type);
if(JSON::is_bool($resource->{$pref})){
$resource->{$pref} = $resource->{$pref} ? 1 : 0 ;
}
$rs->create({ value => $resource->{$pref} });
$pref_rs->create({ value => $resource->{$pref} });
}
} # SWITCH
if ($type eq "subscribers" && ($pref eq 'voicemail_echo_number' || $pref eq 'cli')) {

@ -918,11 +918,27 @@ sub get_usr_preference_rs {
my $attribute = $params{attribute};
my $prov_subscriber = $params{prov_subscriber};
my $schema = $params{schema} // $c->model('DB');
my $is_subadmin = $params{subscriberadmin};
my $pref_rs = $schema->resultset('voip_preferences')->find({
attribute => $attribute, 'usr_pref' => 1,
});
my $pref_rs = $schema->resultset('voip_preferences')->search_rs({
attribute => $attribute,
usr_pref => 1,
$is_subadmin ? (expose_to_customer => 1) : (),
})->first;
return unless($pref_rs);
# filter by allowed attrs from profile
if ($is_subadmin && $prov_subscriber && $prov_subscriber->voip_subscriber_profile) {
my $found_attr = $prov_subscriber->voip_subscriber_profile
->profile_attributes->search_rs({
attribute_id => $pref_rs->id,
})->first;
unless ($found_attr) {
$c->log->debug("get_usr_preference_rs skipping attr '$attribute' not in profile");
return;
}
}
$pref_rs = $pref_rs->voip_usr_preferences;
if($prov_subscriber) {
$pref_rs = $pref_rs->search({
@ -1144,10 +1160,14 @@ sub api_preferences_defs{
my $schema = $params{schema} // $c->model('DB');
my $preferences_group = $params{preferences_group};
my $is_subadmin = ($c->user->roles eq 'subscriberadmin');
my $preferences = $c->model('DB')->resultset('voip_preferences')->search({
internal => { '!=' => 1 }, # also fetch -1 for ncos, rwr
$preferences_group => 1,
$is_subadmin ? (expose_to_customer => 1) : (),
});
my $resource = {};
for my $pref($preferences->all) {
my $fields = { $pref->get_inflated_columns };

Loading…
Cancel
Save