Merge remote-tracking branch 'origin' into ipeshinskaya/InvoiceTemplate2

agranig/subprof
Irka 12 years ago
commit e923b71022

@ -145,9 +145,7 @@ sub PUT :Allow {
my $old_resource = { $profile->get_inflated_columns };
my $form = $self->get_form($c);
use Data::Printer; p $profile;
$profile = $self->update_profile($c, $profile, $old_resource, $resource, $form);
use Data::Printer; p $profile;
last unless $profile;
$guard->commit;

@ -24,7 +24,7 @@ class_has 'api_description' => (
);
with 'NGCP::Panel::Role::API';
with 'NGCP::Panel::Role::API::DomainPreferences';
with 'NGCP::Panel::Role::API::Preferences';
class_has('resource_name', is => 'ro', default => 'domainpreferences');
class_has('dispatch_path', is => 'ro', default => '/api/domainpreferences/');
@ -56,7 +56,7 @@ sub GET :Allow {
my $page = $c->request->params->{page} // 1;
my $rows = $c->request->params->{rows} // 10;
{
my $domains = $self->item_rs($c);
my $domains = $self->item_rs($c, "domains");
my $total_count = int($domains->count);
$domains = $domains->search(undef, {
page => $page,
@ -64,7 +64,7 @@ sub GET :Allow {
});
my (@embedded, @links);
for my $domain ($domains->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $domain);
push @embedded, $self->hal_from_item($c, $domain, "domains");
push @links, Data::HAL::Link->new(
relation => 'ngcp:'.$self->resource_name,
href => sprintf('%s%d', $self->dispatch_path, $domain->id),

@ -17,7 +17,7 @@ require Catalyst::ActionRole::HTTPMethods;
require Catalyst::ActionRole::RequireSSL;
with 'NGCP::Panel::Role::API';
with 'NGCP::Panel::Role::API::DomainPreferences';
with 'NGCP::Panel::Role::API::Preferences';
class_has('resource_name', is => 'ro', default => 'domainpreferences');
class_has('dispatch_path', is => 'ro', default => '/api/domainpreferences/');
@ -48,10 +48,10 @@ sub GET :Allow {
my ($self, $c, $id) = @_;
{
last unless $self->valid_id($c, $id);
my $domain = $self->item_by_id($c, $id);
my $domain = $self->item_by_id($c, $id, "domains");
last unless $self->resource_exists($c, domainpreference => $domain);
my $hal = $self->hal_from_item($c, $domain);
my $hal = $self->hal_from_item($c, $domain, "domains");
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
(map { # XXX Data::HAL must be able to generate links with multiple relations
@ -101,15 +101,15 @@ sub PATCH :Allow {
);
last unless $json;
my $domain = $self->item_by_id($c, $id);
my $domain = $self->item_by_id($c, $id, "domains");
last unless $self->resource_exists($c, domainpreferences => $domain);
my $old_resource = $self->get_resource($c, $domain);
my $old_resource = $self->get_resource($c, $domain, "domains");
my $resource = $self->apply_patch($c, $old_resource, $json);
last unless $resource;
# last param is "no replace" to NOT delete existing prefs
# for proper PATCH behavior
$domain = $self->update_item($c, $domain, $old_resource, $resource, 0);
$domain = $self->update_item($c, $domain, $old_resource, $resource, 0, "domains");
last unless $domain;
$guard->commit;
@ -119,7 +119,7 @@ sub PATCH :Allow {
$c->response->header(Preference_Applied => 'return=minimal');
$c->response->body(q());
} else {
my $hal = $self->hal_from_item($c, $domain);
my $hal = $self->hal_from_item($c, $domain, "domains");
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
$hal->http_headers,
), $hal->as_json);
@ -138,7 +138,7 @@ sub PUT :Allow {
my $preference = $self->require_preference($c);
last unless $preference;
my $domain = $self->item_by_id($c, $id);
my $domain = $self->item_by_id($c, $id, "domains");
last unless $self->resource_exists($c, systemcontact => $domain);
my $resource = $self->get_valid_put_data(
c => $c,
@ -146,11 +146,11 @@ sub PUT :Allow {
media_type => 'application/json',
);
last unless $resource;
my $old_resource = $self->get_resource($c, $domain);
my $old_resource = $self->get_resource($c, $domain, "domains");
# last param is "replace" to delete all existing prefs
# for proper PUT behavior
$domain = $self->update_item($c, $domain, $old_resource, $resource, 1);
$domain = $self->update_item($c, $domain, $old_resource, $resource, 1, "domains");
last unless $domain;
$guard->commit;
@ -160,7 +160,7 @@ sub PUT :Allow {
$c->response->header(Preference_Applied => 'return=minimal');
$c->response->body(q());
} else {
my $hal = $self->hal_from_item($c, $domain);
my $hal = $self->hal_from_item($c, $domain, "domains");
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
$hal->http_headers,
), $hal->as_json);

@ -105,7 +105,6 @@ sub DELETE :Allow {
$domain->delete;
try {
use Data::Printer; p $self->config->{features};
unless($c->config->{features}->{debug}) {
$self->xmpp_domain_disable($c, $domain);
$self->sip_domain_reload($c);

@ -41,11 +41,16 @@ sub auto :Private {
sub GET : Allow {
my ($self, $c) = @_;
my $blacklist = {
"DomainPreferenceDefs" => 1,
"SubscriberPreferenceDefs" => 1,
};
my @colls = $self->get_collections;
foreach my $coll(@colls) {
my $mod = $coll;
$mod =~ s/^.+\/([a-zA-Z0-9_]+)\.pm$/$1/;
next if($mod eq "DomainPreferenceDefs"); # not a "real" collection
next if(exists $blacklist->{$mod});
my $rel = lc $mod;
my $full_mod = 'NGCP::Panel::Controller::API::'.$mod;

@ -0,0 +1,130 @@
package NGCP::Panel::Controller::API::SubscriberPreferenceDefs;
use Sipwise::Base;
use namespace::sweep;
use boolean qw(true);
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Headers qw();
use HTTP::Status qw(:constants);
use MooseX::ClassAttribute qw(class_has);
use NGCP::Panel::Utils::DateTime;
use Path::Tiny qw(path);
use Safe::Isa qw($_isa);
use JSON::Types qw();
BEGIN { extends 'Catalyst::Controller::ActionRole'; }
require Catalyst::ActionRole::ACL;
require Catalyst::ActionRole::CheckTrailingSlash;
require Catalyst::ActionRole::HTTPMethods;
require Catalyst::ActionRole::RequireSSL;
with 'NGCP::Panel::Role::API';
class_has('resource_name', is => 'ro', default => 'subscriberpreferencedefs');
class_has('dispatch_path', is => 'ro', default => '/api/subscriberpreferencedefs/');
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-subscriberpreferencedefs');
__PACKAGE__->config(
action => {
map { $_ => {
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => 'admin',
Args => 0,
Does => [qw(ACL CheckTrailingSlash RequireSSL)],
Method => $_,
Path => __PACKAGE__->dispatch_path,
} } @{ __PACKAGE__->allowed_methods }
},
action_roles => [qw(HTTPMethods)],
);
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$self->log_request($c);
}
sub GET :Allow {
my ($self, $c) = @_;
{
my @links;
push @links,
Data::HAL::Link->new(
relation => 'curies',
href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}',
name => 'ngcp',
templated => true,
),
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
Data::HAL::Link->new(relation => 'self', href => sprintf('%s', $self->dispatch_path));
my $hal = Data::HAL->new(
links => [@links],
);
my $preferences = $c->model('DB')->resultset('voip_preferences')->search({
internal => 0,
usr_pref => 1,
});
my $resource = {};
for my $pref($preferences->all) {
my $fields = { $pref->get_inflated_columns };
# remove internal fields
for my $del(qw/type attribute expose_to_customer internal peer_pref usr_pref dom_pref voip_preference_groups_id id modify_timestamp/) {
delete $fields->{$del};
}
$fields->{max_occur} = int($fields->{max_occur});
$fields->{read_only} = JSON::Types::bool($fields->{read_only});
if($fields->{data_type} eq "enum") {
my @enums = $pref->voip_preferences_enums->search({
dom_pref => 1,
})->all;
$fields->{enum_values} = [];
foreach my $enum(@enums) {
my $efields = { $enum->get_inflated_columns };
for my $del(qw/id preference_id usr_pref dom_pref peer_pref/) {
delete $efields->{$del};
}
$efields->{default_val} = JSON::Types::bool($efields->{default_val});
push @{ $fields->{enum_values} }, $efields;
}
}
$resource->{$pref->attribute} = $fields;
}
$hal->resource($resource);
my $response = HTTP::Response->new(HTTP_OK, undef,
HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json);
$c->response->headers($response->headers);
$c->response->body($response->content);
return;
}
return;
}
sub HEAD :Allow {
my ($self, $c) = @_;
$c->forward(qw(GET));
$c->response->body(q());
return;
}
sub OPTIONS :Allow {
my ($self, $c) = @_;
my $allowed_methods = $self->allowed_methods;
$c->response->headers(HTTP::Headers->new(
Allow => $allowed_methods->join(', '),
Accept_Post => 'application/hal+json; profile=http://purl.org/sipwise/ngcp-api/#rel-'.$self->resource_name,
));
$c->response->content_type('application/json');
$c->response->body(JSON::to_json({ methods => $allowed_methods })."\n");
return;
}
sub end : Private {
my ($self, $c) = @_;
$self->log_response($c);
}
# vim: set tabstop=4 expandtab:

@ -0,0 +1,131 @@
package NGCP::Panel::Controller::API::SubscriberPreferences;
use Sipwise::Base;
use namespace::sweep;
use boolean qw(true);
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Headers qw();
use HTTP::Status qw(:constants);
use MooseX::ClassAttribute qw(class_has);
use NGCP::Panel::Utils::DateTime;
use Path::Tiny qw(path);
use Safe::Isa qw($_isa);
BEGIN { extends 'Catalyst::Controller::ActionRole'; }
require Catalyst::ActionRole::ACL;
require Catalyst::ActionRole::CheckTrailingSlash;
require Catalyst::ActionRole::HTTPMethods;
require Catalyst::ActionRole::RequireSSL;
class_has 'api_description' => (
is => 'ro',
isa => 'Str',
default =>
'Specifies certain properties (preferences) for a <a href="#subscribers">Subscriber</a>. The full list of properties can be obtained via <a href="/api/subscriberpreferencedefs/">SubscriberPreferenceDefs</a>.'
);
with 'NGCP::Panel::Role::API';
with 'NGCP::Panel::Role::API::Preferences';
class_has('resource_name', is => 'ro', default => 'subscriberpreferences');
class_has('dispatch_path', is => 'ro', default => '/api/subscriberpreferences/');
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-subscriberpreferences');
__PACKAGE__->config(
action => {
map { $_ => {
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => 'admin',
Args => 0,
Does => [qw(ACL CheckTrailingSlash RequireSSL)],
Method => $_,
Path => __PACKAGE__->dispatch_path,
} } @{ __PACKAGE__->allowed_methods }
},
action_roles => [qw(HTTPMethods)],
);
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$self->log_request($c);
}
sub GET :Allow {
my ($self, $c) = @_;
my $page = $c->request->params->{page} // 1;
my $rows = $c->request->params->{rows} // 10;
{
my $subscribers = $self->item_rs($c, "subscribers");
my $total_count = int($subscribers->count);
$subscribers = $subscribers->search(undef, {
page => $page,
rows => $rows,
});
my (@embedded, @links);
for my $subscriber ($subscribers->search({}, {order_by => {-asc => 'me.id'}})->all) {
next unless($subscriber->provisioning_voip_subscriber);
push @embedded, $self->hal_from_item($c, $subscriber, "subscribers");
push @links, Data::HAL::Link->new(
relation => 'ngcp:'.$self->resource_name,
href => sprintf('%s%d', $self->dispatch_path, $subscriber->id),
);
}
push @links,
Data::HAL::Link->new(
relation => 'curies',
href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}',
name => 'ngcp',
templated => true,
),
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
Data::HAL::Link->new(relation => 'self', href => sprintf('%s?page=%s&rows=%s', $self->dispatch_path, $page, $rows));
if(($total_count / $rows) > $page ) {
push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('%s?page=%d&rows=%d', $self->dispatch_path, $page + 1, $rows));
}
if($page > 1) {
push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('%s?page=%d&rows=%d', $self->dispatch_path, $page - 1, $rows));
}
my $hal = Data::HAL->new(
embedded => [@embedded],
links => [@links],
);
$hal->resource({
total_count => $total_count,
});
my $response = HTTP::Response->new(HTTP_OK, undef,
HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json);
$c->response->headers($response->headers);
$c->response->body($response->content);
return;
}
return;
}
sub HEAD :Allow {
my ($self, $c) = @_;
$c->forward(qw(GET));
$c->response->body(q());
return;
}
sub OPTIONS :Allow {
my ($self, $c) = @_;
my $allowed_methods = $self->allowed_methods;
$c->response->headers(HTTP::Headers->new(
Allow => $allowed_methods->join(', '),
Accept_Post => 'application/hal+json; profile=http://purl.org/sipwise/ngcp-api/#rel-'.$self->resource_name,
));
$c->response->content_type('application/json');
$c->response->body(JSON::to_json({ methods => $allowed_methods })."\n");
return;
}
sub end : Private {
my ($self, $c) = @_;
$self->log_response($c);
}
# vim: set tabstop=4 expandtab:

@ -0,0 +1,181 @@
package NGCP::Panel::Controller::API::SubscriberPreferencesItem;
use Sipwise::Base;
use namespace::sweep;
use boolean qw(true);
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Headers qw();
use HTTP::Status qw(:constants);
use MooseX::ClassAttribute qw(class_has);
use NGCP::Panel::Utils::ValidateJSON qw();
use NGCP::Panel::Utils::DateTime;
use Path::Tiny qw(path);
use Safe::Isa qw($_isa);
BEGIN { extends 'Catalyst::Controller::ActionRole'; }
require Catalyst::ActionRole::ACL;
require Catalyst::ActionRole::HTTPMethods;
require Catalyst::ActionRole::RequireSSL;
with 'NGCP::Panel::Role::API';
with 'NGCP::Panel::Role::API::Preferences';
class_has('resource_name', is => 'ro', default => 'subscriberpreferences');
class_has('dispatch_path', is => 'ro', default => '/api/subscriberpreferences/');
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-subscriberpreferences');
__PACKAGE__->config(
action => {
map { $_ => {
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => 'admin',
Args => 1,
Does => [qw(ACL RequireSSL)],
Method => $_,
Path => __PACKAGE__->dispatch_path,
} } @{ __PACKAGE__->allowed_methods }
},
action_roles => [qw(HTTPMethods)],
);
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$self->log_request($c);
}
sub GET :Allow {
my ($self, $c, $id) = @_;
{
last unless $self->valid_id($c, $id);
my $subscriber = $self->item_by_id($c, $id, "subscribers");
last unless $self->resource_exists($c, subscriberpreference => $subscriber);
my $hal = $self->hal_from_item($c, $subscriber, "subscribers");
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
(map { # XXX Data::HAL must be able to generate links with multiple relations
s|rel="(http://purl.org/sipwise/ngcp-api/#rel-resellers)"|rel="item $1"|;
s/rel=self/rel="item self"/;
$_
} $hal->http_headers),
), $hal->as_json);
$c->response->headers($response->headers);
$c->response->body($response->content);
return;
}
return;
}
sub HEAD :Allow {
my ($self, $c, $id) = @_;
$c->forward(qw(GET));
$c->response->body(q());
return;
}
sub OPTIONS :Allow {
my ($self, $c, $id) = @_;
my $allowed_methods = $self->allowed_methods;
$c->response->headers(HTTP::Headers->new(
Allow => $allowed_methods->join(', '),
Accept_Patch => 'application/json-patch+json',
));
$c->response->content_type('application/json');
$c->response->body(JSON::to_json({ methods => $allowed_methods })."\n");
return;
}
sub PATCH :Allow {
my ($self, $c, $id) = @_;
my $guard = $c->model('DB')->txn_scope_guard;
{
my $preference = $self->require_preference($c);
last unless $preference;
my $json = $self->get_valid_patch_data(
c => $c,
id => $id,
media_type => 'application/json-patch+json',
ops => [qw/add replace remove copy/],
);
last unless $json;
my $subscriber = $self->item_by_id($c, $id, "subscribers");
last unless $self->resource_exists($c, subscriberpreferences => $subscriber);
my $old_resource = $self->get_resource($c, $subscriber, "subscribers");
my $resource = $self->apply_patch($c, $old_resource, $json);
last unless $resource;
# last param is "no replace" to NOT delete existing prefs
# for proper PATCH behavior
$subscriber = $self->update_item($c, $subscriber, $old_resource, $resource, 0, "subscribers");
last unless $subscriber;
$guard->commit;
if ('minimal' eq $preference) {
$c->response->status(HTTP_NO_CONTENT);
$c->response->header(Preference_Applied => 'return=minimal');
$c->response->body(q());
} else {
my $hal = $self->hal_from_item($c, $subscriber, "subscribers");
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
$hal->http_headers,
), $hal->as_json);
$c->response->headers($response->headers);
$c->response->header(Preference_Applied => 'return=representation');
$c->response->body($response->content);
}
}
return;
}
sub PUT :Allow {
my ($self, $c, $id) = @_;
my $guard = $c->model('DB')->txn_scope_guard;
{
my $preference = $self->require_preference($c);
last unless $preference;
my $subscriber = $self->item_by_id($c, $id, "subscribers");
last unless $self->resource_exists($c, systemcontact => $subscriber);
my $resource = $self->get_valid_put_data(
c => $c,
id => $id,
media_type => 'application/json',
);
last unless $resource;
my $old_resource = $self->get_resource($c, $subscriber, "subscribers");
# last param is "replace" to delete all existing prefs
# for proper PUT behavior
$subscriber = $self->update_item($c, $subscriber, $old_resource, $resource, 1, "subscribers");
last unless $subscriber;
$guard->commit;
if ('minimal' eq $preference) {
$c->response->status(HTTP_NO_CONTENT);
$c->response->header(Preference_Applied => 'return=minimal');
$c->response->body(q());
} else {
my $hal = $self->hal_from_item($c, $subscriber, "subscribers");
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
$hal->http_headers,
), $hal->as_json);
$c->response->headers($response->headers);
$c->response->header(Preference_Applied => 'return=representation');
$c->response->body($response->content);
}
}
return;
}
sub end : Private {
my ($self, $c) = @_;
$self->log_response($c);
}
# vim: set tabstop=4 expandtab:

@ -68,9 +68,7 @@ sub GET :Allow {
my (@embedded, @links);
my $form = $self->get_form($c);
for my $subscriber ($subscribers->search({}, {order_by => {-asc => 'me.id'}})->all) {
say ">>>>>>>>>>> transforming item into resource";
my $resource = $self->transform_resource($c, $subscriber, $form);
use Data::Printer; p $resource;
push @embedded, $self->hal_from_item($c, $subscriber, $resource, $form);
push @links, Data::HAL::Link->new(
relation => 'ngcp:'.$self->resource_name,

@ -90,8 +90,9 @@ sub OPTIONS :Allow {
sub PUT :Allow {
my ($self, $c, $id) = @_;
my $guard = $c->model('DB')->txn_scope_guard;
my $schema = $c->model('DB');
my $guard = $schema->txn_scope_guard;
{
my $preference = $self->require_preference($c);
last unless $preference;
@ -103,21 +104,66 @@ sub PUT :Allow {
media_type => 'application/json',
);
last unless $resource;
my $update = 1;
my $r = $self->prepare_resource($c, $schema, $resource, $update);
last unless $r;
$resource = $r->{resource};
my $form = $self->get_form($c);
$subscriber = $self->update_item($c, $subscriber, $r, $resource, $form);
last unless $subscriber;
say ">>>>>>>>>>>>>> new resource:";
use Data::Printer; p $resource;
$guard->commit;
if ('minimal' eq $preference) {
$c->response->status(HTTP_NO_CONTENT);
$c->response->header(Preference_Applied => 'return=minimal');
$c->response->body(q());
} else {
$resource = $self->transform_resource($c, $subscriber, $form);
my $hal = $self->hal_from_item($c, $subscriber, $resource, $form);
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
$hal->http_headers,
), $hal->as_json);
$c->response->headers($response->headers);
$c->response->header(Preference_Applied => 'return=representation');
$c->response->body($response->content);
}
}
return;
}
sub PATCH :Allow {
my ($self, $c, $id) = @_;
my $schema = $c->model('DB');
my $guard = $schema->txn_scope_guard;
{
my $preference = $self->require_preference($c);
last unless $preference;
my $subscriber = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, subscriber => $subscriber);
my $json = $self->get_valid_patch_data(
c => $c,
id => $id,
media_type => 'application/json-patch+json',
ops => ["add", "replace", "copy", "remove"],
);
last unless $json;
my $form = $self->get_form($c);
my $old_resource = $self->transform_resource($c, $subscriber, $form);
my $resource = $self->apply_patch($c, $old_resource, $json);
last unless $resource;
say ">>>>>>>>>>>>>> old resource:";
use Data::Printer; p $old_resource;
my $update = 1;
my $r = $self->prepare_resource($c, $schema, $resource, $update);
last unless $r;
$resource = $r->{resource};
$subscriber = $self->update_item($c, $subscriber, $old_resource, $resource, $form);
$subscriber = $self->update_item($c, $subscriber, $r, $resource, $form);
last unless $subscriber;
say ">>>>>>>>>>>>> updated item";
$guard->commit;
if ('minimal' eq $preference) {
@ -125,7 +171,8 @@ sub PUT :Allow {
$c->response->header(Preference_Applied => 'return=minimal');
$c->response->body(q());
} else {
my $hal = $self->hal_from_item($c, $subscriber, $form);
$resource = $self->transform_resource($c, $subscriber, $form);
my $hal = $self->hal_from_item($c, $subscriber, $resource, $form);
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
$hal->http_headers,
), $hal->as_json);
@ -133,7 +180,7 @@ sub PUT :Allow {
$c->response->header(Preference_Applied => 'return=representation');
$c->response->body($response->content);
}
}
return;
}

@ -47,8 +47,6 @@ sub calls_matrix_ajax :Chained('/') :PathPart('calls/ajax') :Args(0) {
$to_epoch = NGCP::Panel::Utils::DateTime::current_local->truncate(to => 'day')->add(days => 1)->epoch();
}
use Data::Printer; p $from_epoch; p $to_epoch;
my $rs = $c->model('DB')->resultset('cdr')->search({
-and => [
start_time => { '>=' => $from_epoch },

@ -214,9 +214,14 @@ sub _prune_row {
sub error_page :Private {
my ($self,$c) = @_;
$c->log->error( 'Failed to find path ' . $c->request->path );
$c->stash(template => 'notfound_page.tt');
if($c->request->path =~ /^api\/.+/) {
$c->response->content_type('application/json');
$c->response->body(JSON::to_json({ code => 404, message => 'Path not found' })."\n");
} else {
$c->stash(template => 'notfound_page.tt');
}
$c->response->status(404);
}
@ -224,7 +229,12 @@ sub denied_page :Private {
my ($self,$c) = @_;
$c->log->error('Access denied to path ' . $c->request->path );
$c->stash(template => 'denied_page.tt');
if($c->request->path =~ /^api\/.+/) {
$c->response->content_type('application/json');
$c->response->body(JSON::to_json({ code => 403, message => 'Path forbidden' })."\n");
} else {
$c->stash(template => 'denied_page.tt');
}
$c->response->status(403);
}

@ -420,7 +420,7 @@ sub webphone_ajax :Chained('base') :PathPart('webphone/ajax') :Args(0) {
my $config = {
sip => {
# wss/5061 vs ws/5060
ws_servers => 'ws://' . $subscriber->domain->domain . ':5060/ws',
ws_servers => 'wss://' . $c->request->uri->host . ':' . $c->request->uri->port . '/wss/sip/',
uri => 'sip:' . $subscriber->username . '@' . $subscriber->domain->domain,
password => $subscriber->password,
},
@ -428,7 +428,7 @@ sub webphone_ajax :Chained('base') :PathPart('webphone/ajax') :Args(0) {
# wss/5281 vs ws/5280
# - ws causes "insecure" error in firefox
# - wss fails if self signed cert is not accepted in firefox/chromium
wsURL => 'wss://' . $subscriber->domain->domain . ':5281/xmpp-websocket/',
wsURL => 'wss://' . $c->request->uri->host . ':' . $c->request->uri->port . '/wss/xmpp/',
jid => $subscriber->username . '@' . $subscriber->domain->domain,
server => $subscriber->domain->domain,
credentials => { password => $subscriber->password },
@ -460,49 +460,8 @@ sub terminate :Chained('base') :PathPart('terminate') :Args(0) :Does(ACL) :ACLDe
NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/subscriber'));
}
my $schema = $c->model('DB');
try {
$schema->txn_do(sub {
if($subscriber->provisioning_voip_subscriber->is_pbx_group) {
my $pbx_group = $schema->resultset('voip_pbx_groups')->find({
subscriber_id => $subscriber->provisioning_voip_subscriber->id
});
if($pbx_group) {
$pbx_group->provisioning_voip_subscribers->update_all({
pbx_group_id => undef,
});
}
$pbx_group->delete;
}
my $prov_subscriber = $subscriber->provisioning_voip_subscriber;
if($prov_subscriber) {
NGCP::Panel::Utils::Subscriber::update_pbx_group_prefs(
c => $c,
schema => $schema,
old_group_id => $prov_subscriber->voip_pbx_group->id,
new_group_id => undef,
username => $prov_subscriber->username,
domain => $prov_subscriber->domain->domain,
) if($prov_subscriber->voip_pbx_group);
$prov_subscriber->delete;
}
if ($c->user->roles eq 'subscriberadmin') {
NGCP::Panel::Utils::Subscriber::update_subadmin_sub_aliases(
schema => $schema,
subscriber_id => $subscriber->id,
contract_id => $subscriber->contract_id,
alias_selected => [], #none, thus moving them back to our subadmin
sadmin_id => $schema->resultset('voip_subscribers')
->find({uuid => $c->user->uuid})->id
);
} else {
$subscriber->voip_numbers->update_all({
subscriber_id => undef,
reseller_id => undef,
});
}
$subscriber->update({ status => 'terminated' });
});
NGCP::Panel::Utils::Subscriber::terminate(c => $c, subscriber => $subscriber);
$c->flash(messages => [{type => 'success', text => $c->loc('Successfully terminated subscriber') }]);
} catch($e) {
NGCP::Panel::Utils::Message->error(
@ -606,12 +565,24 @@ sub preferences_edit :Chained('preferences_base') :PathPart('edit') :Args(0) {
$c, $prov_subscriber, $old_auth_prefs);
}
NGCP::Panel::Utils::Preferences::create_preference_form( c => $c,
pref_rs => $pref_rs,
enums => \@enums,
base_uri => $c->uri_for_action('/subscriber/preferences', [$c->req->captures->[0]]),
edit_uri => $c->uri_for_action('/subscriber/preferences_edit', $c->req->captures),
);
try {
NGCP::Panel::Utils::Preferences::create_preference_form( c => $c,
pref_rs => $pref_rs,
enums => \@enums,
base_uri => $c->uri_for_action('/subscriber/preferences', [$c->req->captures->[0]]),
edit_uri => $c->uri_for_action('/subscriber/preferences_edit', $c->req->captures),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
log => "Failed to handle preference: $e",
desc => $c->loc('Failed to handle preference'),
);
NGCP::Panel::Utils::Navigation::back_or($c,
$c->uri_for_action('/subscriber/preferences', [$c->req->captures->[0]]));
return;
}
if(keys %{ $old_auth_prefs }) {
my $new_auth_prefs = {};
@ -1993,7 +1964,10 @@ sub edit_master :Chained('master') :PathPart('edit') :Args(0) :Does(ACL) :ACLDet
unless ($subadmin_pbx) {
for my $num($subscriber->voip_numbers->all) {
next if($subscriber->primary_number && $num->id == $subscriber->primary_number->id);
$num->delete;
$num->update({
subscriber_id => undef,
reseller_id => undef,
});
}
}

@ -53,6 +53,7 @@ sub render {
sub validate {
my ( $self ) = @_;
return $self->add_error($self->label . " is invalid")
if($self->required and (
!defined $self->value or !length($self->value)

@ -50,9 +50,9 @@ sub validate {
for my $sub_error( keys %sub_errors ) {
$self->add_error($sub_error);
}
$self->field('cc')->clear_errors;
$self->field('ac')->clear_errors;
$self->field('sn')->clear_errors;
$self->field('cc')->clear_errors if $self->field('cc');
$self->field('ac')->clear_errors if $self->field('ac');
$self->field('sn')->clear_errors if $self->field('sn');
if ($self->has_errors) {
#dont add more errors

@ -105,11 +105,13 @@ sub field_list {
name => $meta->attribute,
type => 'Text',
do_label => 0,
do_wrapper => 0,
do_wrapper => 1,
element_attr => {
class => ['ngcp_pref_input'],
}
};
}
}
$field->{label} = $is_subscriber ? $meta->label : $meta->attribute;
push @field_list, $field;
}
@ -117,8 +119,6 @@ sub field_list {
return \@field_list;
}
has_field 'save' => (
type => 'Submit',
value => 'Save',

@ -97,7 +97,7 @@ has_field 'status' => (
label => 'Status',
element_attr => {
rel => ['tooltip'],
title => ['The status of the subscriber.']
title => ['The status of the subscriber (one of "active", "locked", "terminated").']
},
);

@ -1,10 +1,12 @@
package NGCP::Panel::Form::Subscriber::TrustedSource;
use Sipwise::Base;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
use Moose::Util::TypeConstraints;
use HTML::FormHandler::Widget::Block::Bootstrap;
use Data::Validate::IP qw/is_ipv4 is_ipv6/;
has '+widget_wrapper' => ( default => 'Bootstrap' );
has_field 'submitid' => ( type => 'Hidden' );
@ -54,5 +56,16 @@ has_block 'actions' => (
render_list => [qw/save/],
);
sub validate_src_ip {
my ($self, $field) = @_;
my $ip = $field->value;
unless(is_ipv4($ip) || is_ipv6($ip)) {
$field->add_error("Invalid IPv4 or IPv6 address.");
}
return 1;
}
1;
# vim: set tabstop=4 expandtab:

@ -65,8 +65,6 @@ sub validate {
my $data = $self->field('data')->value;
my $upload = $self->field('faxfile')->value;
use Data::Printer; print ">>>>>>>>>>>>>>>>>>>>>> upload\n"; p $data; p $upload; p $self->fields;
unless($data || $upload) {
$self->field('faxfile')->add_error("You need to specify a file to fax, if no text is entered in the content field");
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -195,8 +195,7 @@ sub require_preference {
my @preference = grep { 'return' eq $_->[0] } split_header_words($c->request->header('Prefer'));
return $preference[0][1]
if 1 == @preference && ('minimal' eq $preference[0][1] || 'representation' eq $preference[0][1]);
$self->error($c, HTTP_BAD_REQUEST, "This request is required to express an expectation about the response. Use the 'Prefer' header with either 'return=representation' or 'return='minimal' preference.");
return;
return 'minimal';
}
sub require_wellformed_json {

@ -3,7 +3,7 @@ use Moose::Role;
use Sipwise::Base;
use boolean qw(true);
use Try::Tiny;
use TryCatch;
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);

@ -3,7 +3,7 @@ use Moose::Role;
use Sipwise::Base;
use boolean qw(true);
use Try::Tiny;
use TryCatch;
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);

@ -3,7 +3,7 @@ use Moose::Role;
use Sipwise::Base;
use boolean qw(true);
use Try::Tiny;
use TryCatch;
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);

@ -3,7 +3,7 @@ use Moose::Role;
use Sipwise::Base;
use boolean qw(true);
use Try::Tiny;
use TryCatch;
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);

@ -3,7 +3,7 @@ use Moose::Role;
use Sipwise::Base;
use boolean qw(true);
use Try::Tiny;
use TryCatch;
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);

@ -1,4 +1,4 @@
package NGCP::Panel::Role::API::DomainPreferences;
package NGCP::Panel::Role::API::Preferences;
use Moose::Role;
use Sipwise::Base;
@ -8,6 +8,7 @@ use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);
use JSON::Types;
use Data::Validate::IP qw/is_ipv4 is_ipv6/;
use NGCP::Panel::Utils::XMLDispatcher;
use NGCP::Panel::Utils::Prosody;
@ -17,7 +18,7 @@ sub get_form {
}
sub hal_from_item {
my ($self, $c, $item) = @_;
my ($self, $c, $item, $type) = @_;
my $hal = Data::HAL->new(
links => [
@ -30,20 +31,30 @@ sub hal_from_item {
Data::HAL::Link->new(relation => 'collection', href => sprintf("%s", $self->dispatch_path)),
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $item->id)),
Data::HAL::Link->new(relation => 'ngcp:domains', href => sprintf("/api/domains/%d", $item->id)),
Data::HAL::Link->new(relation => "ngcp:$type", href => sprintf("/api/%s/%d", $type, $item->id)),
],
relation => 'ngcp:'.$self->resource_name,
);
my $resource = $self->get_resource($c, $item);
my $resource = $self->get_resource($c, $item, $type);
use Data::Printer; p $resource;
$hal->resource($resource);
return $hal;
}
sub get_resource {
my ($self, $c, $item) = @_;
my $prefs = $item->provisioning_voip_domain->voip_dom_preferences->search({
my ($self, $c, $item, $type) = @_;
my $prefs;
if($type eq "subscribers") {
$prefs = $item->provisioning_voip_subscriber->voip_usr_preferences;
} elsif($type eq "domains") {
$prefs = $item->provisioning_voip_domain->voip_dom_preferences;
} elsif($type eq "peerings") {
$prefs = $item->voip_peer_preferences;
return;
}
$prefs = $prefs->search({
}, {
join => 'attribute',
order_by => { '-asc' => 'id' },
@ -123,8 +134,6 @@ sub get_resource {
}
given($pref->attribute->data_type) {
when("int") { $value = int($pref->value) if($pref->value->is_int) }
when("boolean") { $value = JSON::Types::bool($pref->value) if(defined $pref->value) }
@ -139,49 +148,145 @@ sub get_resource {
}
}
$resource->{domain_id} = int($item->id);
$resource->{domainpreferences_id} = int($item->id);
if($type eq "domains") {
$resource->{domain_id} = int($item->id);
$resource->{id} = int($item->id);
} elsif($type eq "subscribers") {
$resource->{subscriber_id} = int($item->id);
$resource->{id} = int($item->id);
} elsif($type eq "peerings") {
$resource->{peering_id} = int($item->id);
$resource->{id} = int($item->id);
}
return $resource;
}
sub item_rs {
my ($self, $c) = @_;
# we actually return the domain rs here, as we can easily
# go to dom_preferences from there
my ($self, $c, $type) = @_;
my $item_rs;
if($c->user->roles eq "admin") {
$item_rs = $c->model('DB')->resultset('domains');
} elsif($c->user->roles eq "reseller") {
$item_rs = $c->model('DB')->resultset('admins')->find(
{ id => $c->user->id, } )
->reseller
->domain_resellers
->search_related('domain');
if($type eq "domains") {
# we actually return the domain rs here, as we can easily
# go to dom_preferences from there
if($c->user->roles eq "admin") {
$item_rs = $c->model('DB')->resultset('domains');
} elsif($c->user->roles eq "reseller") {
$item_rs = $c->model('DB')->resultset('admins')->find(
{ id => $c->user->id, } )
->reseller
->domain_resellers
->search_related('domain');
}
} elsif($type eq "subscribers") {
if($c->user->roles eq "admin") {
$item_rs = $c->model('DB')->resultset('voip_subscribers');
} elsif($c->user->roles eq "reseller") {
$item_rs = $c->model('DB')->resultset('voip_subscribers')->search({
'contact.reseller_id' => $c->user->reseller_id,
'status' => { '!=' => 'terminated' },
}, {
join => { 'contract' => 'contact' },
});
}
} elsif($type eq "peerings") {
if($c->user->roles eq "admin") {
$item_rs = $c->model('DB')->resultset('voip_peer_hosts');
} else {
return;
}
}
return $item_rs;
}
sub item_by_id {
my ($self, $c, $id) = @_;
my ($self, $c, $id, $type) = @_;
my $item_rs = $self->item_rs($c);
my $item_rs = $self->item_rs($c, $type);
return $item_rs->find($id);
}
sub get_preference_rs {
my ($self, $c, $type, $elem, $attr) = @_;
my $rs;
if($type eq "domains") {
$rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs(
c => $c,
attribute => $attr,
prov_domain => $elem,
);
} elsif($type eq "subscribers") {
$rs = NGCP::Panel::Utils::Preferences::get_usr_preference_rs(
c => $c,
attribute => $attr,
prov_subscriber => $elem,
);
} elsif($type eq "peerings") {
$rs = NGCP::Panel::Utils::Preferences::get_peer_preference_rs(
c => $c,
attribute => $attr,
peer_host => $elem,
);
}
return $rs;
}
sub update_item {
my ($self, $c, $item, $old_resource, $resource, $replace) = @_;
my ($self, $c, $item, $old_resource, $resource, $replace, $type) = @_;
delete $resource->{id};
delete $resource->{domain_id};
delete $resource->{domainpreferences_id};
my $accessor;
my $elem;
my $pref_type;
my $reseller_id;
my $full_rs;
print ">>>>>>>>>> before cleanup\n";
use Data::Printer; p $resource;
if($type eq "domains") {
delete $resource->{domain_id};
delete $resource->{domainpreferences_id};
delete $old_resource->{domain_id};
delete $old_resource->{domainpreferences_id};
$accessor = $item->domain;
$elem = $item->provisioning_voip_domain;
$full_rs = $elem->voip_dom_preferences;
$pref_type = 'dom_pref';
$reseller_id = $item->domain_resellers->first->reseller_id;
} elsif($type eq "subscribers") {
delete $resource->{subscriber_id};
delete $resource->{subscriberpreferences_id};
delete $old_resource->{subscriber_id};
delete $old_resource->{subscriberpreferences_id};
$accessor = $item->username . '@' . $item->domain->domain;
$elem = $item->provisioning_voip_subscriber;
$full_rs = $elem->voip_usr_preferences;
$pref_type = 'usr_pref';
$reseller_id = $item->contract->contact->reseller_id;
} elsif($type eq "peerings") {
delete $resource->{peer_id};
delete $resource->{peerpreferences_id};
delete $old_resource->{peer_id};
delete $old_resource->{peerpreferences_id};
$accessor = $item->name;
$elem = $item;
$full_rs = $elem->voip_peer_preferences;
$pref_type = 'peer_pref';
$reseller_id = 1;
} else {
return;
}
print ">>>>>>>>>> after cleanup\n";
use Data::Printer; p $resource;
if($replace) {
# in case of PUT, we remove all old entries
try {
$item->provisioning_voip_domain->voip_dom_preferences->delete_all;
$full_rs->delete_all;
} catch($e) {
$c->log->error("failed to clear preferences for domain '".$item->domain."': $e");
$c->log->error("failed to clear preferences for '$accessor': $e");
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error.");
return;
};
@ -192,17 +297,14 @@ sub update_item {
given($k) {
# no special treatment for *_sound_set deletion, as id is stored in right name
$c->log->debug("+++++++++++++ check $k for deletion");
when(/^rewrite_rule_set$/) {
$c->log->debug("+++++++++++++ check $k for deletion");
unless(exists $resource->{$k}) {
$c->log->debug("+++++++++++++ $k marked for deletion");
foreach my $p(qw/caller_in_dpid callee_in_dpid caller_out_dpid callee_out_dpid/) {
my $rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs(
c => $c,
attribute => 'rewrite_' . $p,
prov_domain => $item->provisioning_voip_domain,
);
my $rs = $self->get_preference_rs($c, $type, $elem, 'rewrite_' . $p);
next unless $rs; # unknown resource, just ignore
$rs->delete_all;
}
@ -210,22 +312,14 @@ sub update_item {
}
when(/^(adm_)?ncos$/) {
unless(exists $resource->{$k}) {
my $rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs(
c => $c,
attribute => $k . '_id',
prov_domain => $item->provisioning_voip_domain,
);
my $rs = $self->get_preference_rs($c, $type, $elem, $k . '_id');
next unless $rs; # unknown resource, just ignore
$rs->delete_all;
}
}
when(/^(man_)?allowed_ips$/) {
unless(exists $resource->{$k}) {
my $rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs(
c => $c,
attribute => $k . '_grp',
prov_domain => $item->provisioning_voip_domain,
);
my $rs = $self->get_preference_rs($c, $type, $elem, $k . '_grp');
next unless $rs; # unknown resource, just ignore
if($rs->first) {
$c->model('DB')->resultset('voip_allowed_ip_groups')->search({
@ -237,22 +331,15 @@ sub update_item {
}
default {
unless(exists $resource->{$k}) {
my $rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs(
c => $c,
attribute => $k,
prov_domain => $item->provisioning_voip_domain,
);
my $rs = $self->get_preference_rs($c, $type, $elem, $k);
next unless $rs; # unknown resource, just ignore
$rs->delete_all;
}
}
}
}
# TODO: also go over special cases (rewrite_rule_set) and delete them
# if not available in $resource
} catch($e) {
$c->log->error("failed to clear preference for domain '".$item->domain."': $e");
$c->log->error("failed to clear preference for '$accessor': $e");
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error.");
return;
};
@ -260,13 +347,9 @@ sub update_item {
foreach my $pref(keys %{ $resource }) {
next unless(defined $resource->{$pref});
my $rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs(
c => $c,
attribute => $pref,
prov_domain => $item->provisioning_voip_domain,
);
my $rs = $self->get_preference_rs($c, $type, $elem, $pref);
unless($rs) {
$c->log->debug("removing unknown dom_preference '$pref' from update");
$c->log->debug("removing unknown preference '$pref' from update");
next;
}
$rs = $rs->search(undef, {
@ -275,7 +358,7 @@ sub update_item {
# TODO: can't we get this via $rs->search_related or $rs->related_resultset?
my $meta = $c->model('DB')->resultset('voip_preferences')->find({
attribute => $pref, 'dom_pref' => 1,
attribute => $pref, $pref_type => 1,
});
unless($meta) {
$c->log->error("failed to get voip_preference entry for '$pref'");
@ -301,21 +384,17 @@ sub update_item {
my $rwr_set = $c->model('DB')->resultset('voip_rewrite_rule_sets')->find({
name => $resource->{$pref},
reseller_id => $item->domain_resellers->first->reseller_id,
reseller_id => $reseller_id,
});
unless($rwr_set) {
$c->log->error("no rewrite rule set '".$resource->{$pref}."' for reseller id ".$item->domain_resellers->first->reseller_id." found");
$c->log->error("no rewrite rule set '".$resource->{$pref}."' for reseller id $reseller_id found");
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Unknown rewrite_rule_set '".$resource->{$pref}."'");
return;
}
foreach my $k(qw/caller_in_dpid callee_in_dpid caller_out_dpid callee_out_dpid/) {
my $rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs(
c => $c,
attribute => 'rewrite_'.$k,
prov_domain => $item->provisioning_voip_domain,
);
my $rs = $self->get_preference_rs($c, $type, $elem, 'rewrite_'.$k);
if($rs->first) {
$rs->first->update({ value => $rwr_set->$k });
} else {
@ -328,18 +407,14 @@ sub update_item {
my $pref_name = $pref . "_id";
my $ncos = $c->model('DB')->resultset('ncos_levels')->find({
level => $resource->{$pref},
reseller_id => $item->domain_resellers->first->reseller_id,
reseller_id => $reseller_id,
});
unless($ncos) {
$c->log->error("no ncos level '".$resource->{$pref}."' for reseller id ".$item->domain_resellers->first->reseller_id." found");
$c->log->error("no ncos level '".$resource->{$pref}."' for reseller id $reseller_id found");
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Unknown ncos_level '".$resource->{$pref}."'");
return;
}
my $rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs(
c => $c,
attribute => $pref_name,
prov_domain => $item->provisioning_voip_domain,
);
my $rs = $self->get_preference_rs($c, $type, $elem, $pref_name);
if($rs->first) {
$rs->first->update({ value => $ncos->id });
} else {
@ -351,18 +426,14 @@ sub update_item {
# TODO: not applicable for domains, but for subs, check for contract_id!
my $set = $c->model('DB')->resultset('voip_sound_sets')->find({
name => $resource->{$pref},
reseller_id => $item->domain_resellers->first->reseller_id,
reseller_id => $reseller_id,
});
unless($set) {
$c->log->error("no $pref '".$resource->{$pref}."' for reseller id ".$item->domain_resellers->first->reseller_id." found");
$c->log->error("no $pref '".$resource->{$pref}."' for reseller id $reseller_id found");
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Unknown $pref'".$resource->{$pref}."'");
return;
}
my $rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs(
c => $c,
attribute => $pref,
prov_domain => $item->provisioning_voip_domain,
);
my $rs = $self->get_preference_rs($c, $type, $elem, $pref);
if($rs->first) {
$rs->first->update({ value => $set->id });
} else {
@ -374,11 +445,7 @@ sub update_item {
my $pref_name = $pref . "_grp";
my $aig_rs;
my $seq;
my $rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs(
c => $c,
attribute => $pref_name,
prov_domain => $item->provisioning_voip_domain,
);
my $rs = $self->get_preference_rs($c, $type, $elem, $pref_name);
if($rs->first) {
$aig_rs = $c->model('DB')->resultset('voip_allowed_ip_groups')->search({
group_id => $rs->first->value
@ -401,7 +468,10 @@ sub update_item {
}
foreach my $ip(@{ $resource->{$pref} }) {
# TODO: check for valid ipv4/v6
unless($self->validate_ipnet($c, $pref, $ip)) {
$c->log->error("invalid $pref entry '$ip'");
return;
}
$aig_rs->create({ ipnet => $ip });
}
@ -415,20 +485,20 @@ sub update_item {
if($meta->max_occur != 1) {
$rs->delete_all;
foreach my $v(@{ $resource->{$pref} }) {
return unless $self->check_pref_value($c, $meta, $v);
return unless $self->check_pref_value($c, $meta, $v, $pref_type);
$rs->create({ value => $v });
}
} elsif($rs->first) {
return unless $self->check_pref_value($c, $meta, $resource->{$pref});
return unless $self->check_pref_value($c, $meta, $resource->{$pref}, $pref_type);
$rs->first->update({ value => $resource->{$pref} });
} else {
return unless $self->check_pref_value($c, $meta, $resource->{$pref});
return unless $self->check_pref_value($c, $meta, $resource->{$pref}, $pref_type);
$rs->create({ value => $resource->{$pref} });
}
}
}
} catch($e) {
$c->log->error("failed to update preference for domain '".$item->domain."': $e");
$c->log->error("failed to update preference for '$accessor': $e");
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error.");
return;
}
@ -438,7 +508,7 @@ sub update_item {
}
sub check_pref_value {
my ($self, $c, $meta, $value) = @_;
my ($self, $c, $meta, $value, $pref_type) = @_;
my $err;
my $vtype = ref $value;
@ -461,7 +531,7 @@ sub check_pref_value {
if($meta->data_type eq "enum") {
my $enum = $c->model('DB')->resultset('voip_preferences_enum')->find({
preference_id => $meta->id,
dom_pref => 1,
$pref_type => 1,
value => $value,
});
unless($enum) {
@ -474,5 +544,27 @@ sub check_pref_value {
return 1;
}
sub validate_ipnet {
my ($self, $c, $pref, $ipnet) = @_;
my ($ip, $net) = split /\//, $ipnet;
if(is_ipv4($ip)) {
return 1 unless(defined $net);
unless($net->is_int && $net >= 0 && $net <= 32) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid IPv4 network portion in $pref entry '$ipnet', must be 0 <= net <= 32");
return;
}
} elsif(is_ipv6($ip)) {
return 1 unless(defined $net);
unless($net->is_int && $net >= 0 && $net <= 128) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid IPv6 network portion in $pref entry '$ipnet', must be 0 <= net <= 128");
return;
}
} else {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid IPv4 or IPv6 address in $pref entry '$ipnet', must be valid address with optional /net suffix");
return;
}
return 1;
}
1;
# vim: set tabstop=4 expandtab:

@ -3,7 +3,7 @@ use Moose::Role;
use Sipwise::Base;
use boolean qw(true);
use Try::Tiny;
use TryCatch;
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);

@ -3,7 +3,7 @@ use Moose::Role;
use Sipwise::Base;
use boolean qw(true);
use Try::Tiny;
use TryCatch;
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);
@ -12,6 +12,7 @@ use Test::More;
use NGCP::Panel::Form::Subscriber::SubscriberAPI;
use NGCP::Panel::Utils::XMLDispatcher;
use NGCP::Panel::Utils::Prosody;
use NGCP::Panel::Utils::Subscriber;
sub get_form {
my ($self, $c) = @_;
@ -34,8 +35,7 @@ sub transform_resource {
}
$form //= $self->get_form($c);
$self->validate_form(
last unless $self->validate_form(
c => $c,
resource => \%resource,
form => $form,
@ -167,7 +167,7 @@ sub get_billing_profile {
}
sub prepare_resource {
my ($self, $c, $schema, $resource) = @_;
my ($self, $c, $schema, $resource, $update) = @_;
my $domain;
if($resource->{domain}) {
@ -189,7 +189,6 @@ sub prepare_resource {
delete $resource->{domain};
$resource->{domain_id} = $domain->id;
}
$resource->{e164} = delete $resource->{primary_number};
$resource->{contract_id} = delete $resource->{customer_id};
$resource->{status} //= 'active';
@ -221,7 +220,7 @@ sub prepare_resource {
my $customer = $self->get_customer($c, $resource->{contract_id});
return unless($customer);
if(defined $customer->max_subscribers && $customer->voip_subscribers->search({
if(!$update && defined $customer->max_subscribers && $customer->voip_subscribers->search({
status => { '!=' => 'terminated' }
})->count >= $customer->max_subscribers) {
@ -241,7 +240,6 @@ sub prepare_resource {
contract => $customer,
show_locked => 1,
);
use Data::Printer; say ">>>>>>>>>>>>>>>>>>>> subs"; p $subs;
my $admin_subscribers = NGCP::Panel::Utils::Subscriber::get_admin_subscribers(
voip_subscribers => $subs->{subscribers});
unless(@{ $admin_subscribers }) {
@ -278,9 +276,16 @@ sub prepare_resource {
domain_id => $resource->{domain_id},
status => { '!=' => 'terminated' },
});
if($subscriber) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Subscriber already exists.");
return;
if($update) {
unless($subscriber) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Subscriber does not exist.");
return;
}
} else {
if($subscriber) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Subscriber already exists.");
return;
}
}
my $alias_numbers = [];
@ -297,7 +302,6 @@ sub prepare_resource {
} elsif(ref $resource->{alias_numbers} eq "HASH") {
push @{ $alias_numbers }, { e164 => $resource->{alias_numbers} };
} else {
use Data::Printer; p $resource->{alias_numbers}; say ">>>>>>>>>>> '".(ref $resource->{alias_numbers})."'";
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid parameter 'alias_numbers', must be hash or array of hashes.");
return;
}
@ -320,25 +324,71 @@ sub prepare_resource {
}
sub update_item {
my ($self, $c, $item, $old_resource, $resource, $form) = @_;
my ($self, $c, $item, $full_resource, $resource, $form) = @_;
my $subscriber = $item;
my $customer = $full_resource->{customer};
my $admin = $full_resource->{admin};
my $alias_numbers = $full_resource->{alias_numbers};
my $preferences = $full_resource->{preferences};
if($subscriber->status ne $resource->{status}) {
if($resource->{status} eq 'locked') {
$resource->{lock} = 4;
} elsif($subscriber->status eq 'locked' && $resource->{status} eq 'active') {
$resource->{lock} ||= 0;
} elsif($resource->{status} eq 'terminated') {
try {
NGCP::Panel::Utils::Subscriber::terminate(c => $c, subscriber => $subscriber);
} catch($e) {
$c->log->error("failed to terminate subscriber id ".$subscriber->id);
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to terminate subscriber");
}
return;
}
}
try {
NGCP::Panel::Utils::Subscriber::lock_provisoning_voip_subscriber(
c => $c,
prov_subscriber => $subscriber->provisioning_voip_subscriber,
level => $resource->{lock},
);
} catch($e) {
$c->log->error("failed to lock subscriber id ".$subscriber->id." with level ".$resource->{lock});
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to update subscriber lock");
return;
}
$form //= $self->get_form($c);
NGCP::Panel::Utils::Subscriber::update_subscriber_numbers(
schema => $c->model('DB'),
primary_number => $resource->{e164},
alias_numbers => $alias_numbers,
reseller_id => $customer->contact->reseller_id,
subscriber_id => $subscriber->id,
);
print ">>>>>>>>>>>>> validate before update\n";
my $billing_res = {
external_id => $resource->{external_id},
status => $resource->{status},
};
my $provisioning_res = {
password => $resource->{password},
webusername => $resource->{webusername},
webpassword => $resource->{webpassword},
admin => $resource->{administrative},
is_pbx_group => $resource->{is_pbx_group},
pbx_group_id => $resource->{pbx_group_id},
modify_timestamp => NGCP::Panel::Utils::DateTime::current_local,
$resource->{e164} = delete $resource->{primary_number};
};
return unless $self->validate_form(
c => $c,
form => $form,
resource => $resource,
);
$subscriber->update($billing_res);
$subscriber->provisioning_voip_subscriber->update($provisioning_res);
$subscriber->discard_changes;
print ">>>>>>>>>>>>> update\n";
$item->update($resource);
print ">>>>>>>>>>>>> done update\n";
# TODO: status handling (termination, ...)
return $item;
return $subscriber;
}
1;

@ -3,7 +3,7 @@ use Moose::Role;
use Sipwise::Base;
use boolean qw(true);
use Try::Tiny;
use TryCatch;
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);

@ -3,6 +3,30 @@ use strict;
use warnings;
use NGCP::Panel::Form::Preferences;
use Sipwise::Base;
use Data::Validate::IP qw/is_ipv4 is_ipv6/;
sub validate_ipnet {
my ($field) = @_;
my ($ip, $net) = split /\//, $field->value;
if(is_ipv4($ip)) {
return 1 unless(defined $net);
unless($net->is_int && $net >= 0 && $net <= 32) {
$field->add_error("Invalid IPv4 network portion, must be 0 <= net <= 32");
return;
}
} elsif(is_ipv6($ip)) {
return 1 unless(defined $net);
unless($net->is_int && $net >= 0 && $net <= 128) {
$field->add_error("Invalid IPv6 network portion, must be 0 <= net <= 128");
return;
}
} else {
$field->add_error("Invalid IPv4 or IPv6 address, must be valid address with optional /net suffix.");
return;
}
return 1;
}
sub load_preference_list {
my %params = @_;
@ -193,6 +217,9 @@ sub create_preference_form {
my $preference_id = $c->stash->{preference}->first ? $c->stash->{preference}->first->id : undef;
my $attribute = $c->stash->{preference_meta}->attribute;
if ($attribute eq "allowed_ips") {
unless(validate_ipnet($form->field($attribute))) {
goto OUT;
}
unless (defined $aip_group_id) {
#TODO put this in a transaction
@ -218,7 +245,9 @@ sub create_preference_form {
ipnet => $form->field($attribute)->value,
});
} elsif ($attribute eq "man_allowed_ips") {
unless(validate_ipnet($form->field($attribute))) {
goto OUT;
}
unless (defined $man_aip_group_id) {
#TODO put this in a transaction
my $new_group = $c->model('DB')->resultset('voip_aig_sequence')
@ -258,7 +287,7 @@ sub create_preference_form {
);
$c->flash(messages => [{type => 'success', text => "Preference $attribute successfully updated."}]);
$c->response->redirect($base_uri);
return;
return 1;
} elsif ($attribute eq "ncos" || $attribute eq "adm_ncos") {
my $selected_level = $c->stash->{ncos_levels_rs}->find(
$form->field($attribute)->value
@ -278,7 +307,7 @@ sub create_preference_form {
$c->flash(messages => [{type => 'success', text => "Preference $attribute successfully updated."}]);
$c->response->redirect($base_uri);
return;
return 1;
} elsif ($attribute eq "sound_set") {
my $selected_set = $c->stash->{sound_sets_rs}->find(
$form->field($attribute)->value
@ -296,7 +325,7 @@ sub create_preference_form {
$c->flash(messages => [{type => 'success', text => "Preference $attribute successfully updated."}]);
$c->response->redirect($base_uri);
return;
return 1;
} elsif ($attribute eq "contract_sound_set") {
my $selected_set = $c->stash->{contract_sound_sets_rs}->find(
$form->field($attribute)->value
@ -314,7 +343,7 @@ sub create_preference_form {
$c->flash(messages => [{type => 'success', text => "Preference $attribute successfully updated."}]);
$c->response->redirect($base_uri);
return;
return 1;
} else {
if( ($c->stash->{preference_meta}->data_type ne 'enum' &&
$form->field($attribute)->value eq '') ||
@ -336,9 +365,11 @@ sub create_preference_form {
}
$c->flash(messages => [{type => 'success', text => "Preference $attribute successfully updated."}]);
$c->response->redirect($base_uri);
return;
return 1;
}
}
OUT:
my $delete_param = $c->request->params->{delete};
my $deactivate_param = $c->request->params->{deactivate};
@ -396,6 +427,8 @@ sub create_preference_form {
$c->stash(form => $form,
aip_grp_rs => $aip_grp_rs,
man_aip_grp_rs => $man_aip_grp_rs);
return 1;
}
sub set_rewrite_preferences {
@ -434,7 +467,9 @@ sub get_usr_preference_rs {
my $pref_rs = $c->model('DB')->resultset('voip_preferences')->find({
attribute => $attribute, 'usr_pref' => 1,
})->voip_usr_preferences;
});
return unless($pref_rs);
$pref_rs = $pref_rs->voip_usr_preferences;
if($prov_subscriber) {
$pref_rs = $pref_rs->search({
subscriber_id => $prov_subscriber->id,
@ -459,6 +494,22 @@ sub get_dom_preference_rs {
});
}
sub get_peer_preference_rs {
my %params = @_;
my $c = $params{c};
my $attribute = $params{attribute};
my $host = $params{peer_host};
my $preference = $c->model('DB')->resultset('voip_preferences')->find({
attribute => $attribute, 'peer_pref' => 1,
});
return unless($preference);
return $preference->voip_peer_preferences->search_rs({
peer_host_id => $host->id,
});
}
sub get_peer_auth_params {
my ($c, $prov_subscriber, $prefs) = @_;

@ -138,7 +138,6 @@ sub create_subscriber {
my $reseller = $contract->contact->reseller;
my $billing_domain = $schema->resultset('domains')
->find($params->{domain}{id} // $params->{domain_id});
use Data::Printer; print ">>>>>>>>>>>>>>>>>>>>>>>>>>> billing_dom\n"; p $billing_domain;
my $prov_domain = $schema->resultset('voip_domains')
->find({domain => $billing_domain->domain});
@ -328,8 +327,14 @@ sub update_subscriber_numbers {
id => $subscriber_id,
});
my $prov_subs = $billing_subs->provisioning_voip_subscriber;
my @nums = (); my @dbnums = ();
if (defined $primary_number) {
if(exists $params{primary_number} && !defined $primary_number) {
$billing_subs->update({
primary_number_id => undef,
});
}
elsif(defined $primary_number) {
my $old_cc;
my $old_ac;
@ -383,12 +388,21 @@ sub update_subscriber_numbers {
primary_number_id => $number->id,
});
if(defined $prov_subs) {
$schema->resultset('voip_dbaliases')->create({
my $dbalias = $prov_subs->voip_dbaliases->find({
username => $cli,
domain_id => $prov_subs->domain->id,
subscriber_id => $prov_subs->id,
is_primary => 1,
});
if($dbalias) {
if(!$dbalias->is_primary) {
$dbalias->update({ is_primary => 1 });
}
} else {
$dbalias = $prov_subs->voip_dbaliases->create({
username => $cli,
domain_id => $prov_subs->domain->id,
is_primary => 1,
});
}
push @dbnums, $dbalias->id;
if(defined $prov_subs->voicemail_user) {
$prov_subs->voicemail_user->update({
mailbox => $cli,
@ -456,9 +470,6 @@ sub update_subscriber_numbers {
if(defined $alias_numbers && ref($alias_numbers) eq 'ARRAY') {
# note that this only adds new alias numbers
# old entries in voip_numbers and voip_dbaliases are usually deleted
# before calling this sub
my $number;
for my $alias(@$alias_numbers) {
@ -488,15 +499,38 @@ sub update_subscriber_numbers {
subscriber_id => $subscriber_id,
});
}
$schema->resultset('voip_dbaliases')->create({
username => $number->cc . ($number->ac // '') . $number->sn,
subscriber_id => $prov_subs->id,
domain_id => $prov_subs->domain->id,
is_primary => 0,
push @nums, $number->id;
my $cli = $number->cc . ($number->ac // '') . $number->sn;
my $dbalias = $prov_subs->voip_dbaliases->find({
username => $cli,
});
if($dbalias) {
if($dbalias->is_primary) {
$dbalias->update({ is_primary => 0 });
}
} else {
$dbalias = $prov_subs->voip_dbaliases->create({
username => $cli,
domain_id => $prov_subs->domain->id,
is_primary => 0,
});
}
push @dbnums, $dbalias->id;
}
}
push @nums, $billing_subs->primary_number_id
if($billing_subs->primary_number_id);
$billing_subs->voip_numbers->search({
id => { 'not in' => \@nums },
})->update_all({
subscriber_id => undef,
reseller_id => undef,
});
$prov_subs->voip_dbaliases->search({
id => { 'not in' => \@dbnums },
})->delete;
return;
}
@ -528,6 +562,56 @@ sub update_subadmin_sub_aliases {
}
}
sub terminate {
my %params = @_;
my $c = $params{c};
my $subscriber = $params{subscriber};
my $schema = $c->model('DB');
$schema->txn_do(sub {
if($subscriber->provisioning_voip_subscriber->is_pbx_group) {
my $pbx_group = $schema->resultset('voip_pbx_groups')->find({
subscriber_id => $subscriber->provisioning_voip_subscriber->id
});
if($pbx_group) {
$pbx_group->provisioning_voip_subscribers->update_all({
pbx_group_id => undef,
});
}
$pbx_group->delete;
}
my $prov_subscriber = $subscriber->provisioning_voip_subscriber;
if($prov_subscriber) {
update_pbx_group_prefs(
c => $c,
schema => $schema,
old_group_id => $prov_subscriber->voip_pbx_group->id,
new_group_id => undef,
username => $prov_subscriber->username,
domain => $prov_subscriber->domain->domain,
) if($prov_subscriber->voip_pbx_group);
$prov_subscriber->delete;
}
if ($c->user->roles eq 'subscriberadmin') {
update_subadmin_sub_aliases(
schema => $schema,
subscriber_id => $subscriber->id,
contract_id => $subscriber->contract_id,
alias_selected => [], #none, thus moving them back to our subadmin
sadmin_id => $schema->resultset('voip_subscribers')
->find({uuid => $c->user->uuid})->id
);
} else {
$subscriber->voip_numbers->update_all({
subscriber_id => undef,
reseller_id => undef,
});
}
$subscriber->update({ status => 'terminated' });
});
}
1;
=head1 NAME

@ -416,6 +416,10 @@ div.ngcp-modal .control-group.error .dataTables_wrapper input[type="text"] {
padding: 4px 0;
width: 100%;
}
.modal-body .control-group .controls input.ngcp_pref_input {
width: 220px;
float: left;
}
.modal-body .control-group .controls input.ngcp_e164_cc {
width: 15%;
}

@ -1,23 +1,24 @@
{
"sEmptyTable": "Nessun dato presente nella tabella",
"sInfo": "Vista da _START_ a _END_ di _TOTAL_ elementi",
"sInfoEmpty": "Vista da 0 a 0 di 0 elementi",
"sInfoFiltered": "(filtrati da _MAX_ elementi totali)",
"sInfoPostFix": "",
"sInfoThousands": ",",
"sLengthMenu": "Visualizza _MENU_ elementi",
"sLoadingRecords": "Caricamento...",
"sProcessing": "Elaborazione...",
"sSearch": "Cerca:",
"sZeroRecords": "La ricerca non ha portato alcun risultato.",
"oPaginate": {
"sFirst": "Inizio",
"sPrevious": "Precedente",
"sNext": "Successivo",
"sLast": "Fine"
},
"oAria": {
"sSortAscending": ": attiva per ordinare la colonna in ordine crescente",
"sSortDescending": ": attiva per ordinare la colonna in ordine decrescente"
}
"sProcessing": "Procesando...",
"sLengthMenu": "Mostrar _MENU_ registros",
"sZeroRecords": "No se encontraron resultados",
"sEmptyTable": "Ningún dato disponible en esta tabla",
"sInfo": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
"sInfoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
"sInfoFiltered": "(filtrado de un total de _MAX_ registros)",
"sInfoPostFix": "",
"sSearch": "Buscar:",
"sUrl": "",
"sInfoThousands": ",",
"sLoadingRecords": "Cargando...",
"oPaginate": {
"sFirst": "Primero",
"sLast": "Último",
"sNext": "Siguiente",
"sPrevious": "Anterior"
},
"oAria": {
"sSortAscending": ": Activar para ordenar la columna de manera ascendente",
"sSortDescending": ": Activar para ordenar la columna de manera descendente"
}
}

@ -574,6 +574,12 @@
}
};
$('#xmpp-buddy-add').click(function(obj) {
var jid = $('#xmpp-buddy-add-jid').val();
console.log(">>>>>>>>> adding jid " + jid);
chat.subscribe(jid);
});
});
@ -591,6 +597,14 @@
<input id="xmpp-toggle-offline" style="float:left;" type="checkbox" data-on="success" data-off="default" data-on-label="[% c.loc('Show Offline') %]" data-off-label="[% c.loc('Hide Offline') %]">
<input id="sip_toggle_video" checked style="float:left" type="checkbox" data-on="success" data-off="default" data-on-label="[% c.loc('Audio&amp;Video') %]" data-off-label="[% c.loc('Audio Only') %]">
</div>
<div class="span6" style="margin:0; clear:both; padding:0;">
<div class="span4" style="margin:0;">
<input type="text" id="xmpp-buddy-add-jid" class="span4"/>
</div>
<div class="span2" style="margin:0; float:right;">
<button class="btn btn-primary btn-medium" id="xmpp-buddy-add"><i class="icon-plus"></i> Add Buddy</button>
</div>
</div>
<div class="row span6" style="margin:0; clear:both;">
<select id="xmpp-pres" class="selectpicker span6">
[% FOR opt IN

@ -10,6 +10,7 @@
<li><a href="[% c.uri_for_action('/subscriber/reglist', [subscriber.id]) %]">[% c.loc('Registered Devices') %]</a></li>
<li><a href="[% c.uri_for_action('/subscriber/preferences', [subscriber.id]) %]">[% c.loc('Subscriber Settings') %]</a></li>
<li><a href="[% c.uri_for_action('/subscriber/webpass', [subscriber.id]) %]">[% c.loc('User Details') %]</a></li>
<li><a href="[% c.uri_for_action('/subscriber/webfax', [subscriber.id]) %]">[% c.loc('Web Fax') %]</a></li>
</ul>
</li>
[% # vim: set tabstop=4 syntax=html expandtab: -%]

@ -11,6 +11,7 @@
<li><a href="[% c.uri_for_action('/subscriber/preferences', [subscriber.id]) %]">[% c.loc('Subscriber Settings') %]</a></li>
<li><a href="[% c.uri_for_action('/customer/details', [c.user.account_id]) %]">[% c.loc('Customer Settings') %]</a></li>
<li><a href="[% c.uri_for_action('/subscriber/webpass', [subscriber.id]) %]">[% c.loc('User Details') %]</a></li>
<li><a href="[% c.uri_for_action('/subscriber/webfax', [subscriber.id]) %]">[% c.loc('Web Fax') %]</a></li>
</ul>
</li>
[% # vim: set tabstop=4 syntax=html expandtab: -%]

Loading…
Cancel
Save