MT#60558 add max subscribers/groups license checks

* max_subscribers, max_pbx_subscribers, max_pbx_groups license
  checks are added for subscriber/group creation in UI/API
* new accessor $c->license_meta that returns meta license
  flags hashref
* new accessor $c->license_max_subscribers
* new accessor $c->license_max_pbx_subscribers
* new accessor $c->license_max_pbx_groups
* the new accessors (except license_meta) return -1
  instead of 'unlimited' to ease off comprarsion
* 403 Forbidden is returned by the API if a license
  is violated.

Change-Id: I3f5a949efc84bf85b76b33404b37b362ec484d5f
mr13.0
Kirill Solomko 1 year ago
parent 9d021be65a
commit 43d112bd5e

@ -21,13 +21,24 @@ sub license_max_pbx_groups {
return NGCP::Panel::Utils::License::get_max_pbx_groups(@_);
}
sub license_max_pbx_subscribers {
return NGCP::Panel::Utils::License::get_max_pbx_subscribers(@_);
}
sub license_max_subscribers {
return NGCP::Panel::Utils::License::get_max_subscribers(@_);
}
sub license_max_pbx_subscribers {
return NGCP::Panel::Utils::License::get_max_pbx_subscribers(@_);
sub license_current_pbx_groups {
return NGCP::Panel::Utils::License::get_current_pbx_groups(@_);
}
1;
sub license_current_pbx_subscribers {
return NGCP::Panel::Utils::License::get_current_pbx_subscribers(@_);
}
sub license_current_subscribers {
return NGCP::Panel::Utils::License::get_current_subscribers(@_);
}
1;

@ -378,6 +378,38 @@ sub POST :Allow {
);
last unless $resource;
my $license_max_subscribers = $c->license_max_subscribers;
my $current_subscribers_count = $c->license_current_subscribers;
if ($license_max_subscribers >= 0 && $current_subscribers_count >= $license_max_subscribers) {
$self->error($c, HTTP_FORBIDDEN,
"Maximum number of subscribers for this platform is reached",
"Exceeded max number of license subscribers: $license_max_subscribers current: $current_subscribers_count",
);
last;
}
my $license_max_pbx_subscribers = $c->license_max_pbx_subscribers;
my $current_pbx_subscribers_count = $c->license_current_pbx_subscribers;
if ($schema->resultset('contracts')->find($resource->{customer_id})->product->class eq 'pbxaccount' &&
$license_max_pbx_subscribers >= 0 && $current_pbx_subscribers_count >= $license_max_pbx_subscribers) {
$self->error($c, HTTP_FORBIDDEN,
"Maximum number of PBX subscribers for this platform is reached",
"Exceeded max number of license pbx subscribers: $license_max_pbx_subscribers current: $current_pbx_subscribers_count",
);
last;
}
my $license_max_pbx_groups = $c->license_max_pbx_groups;
my $current_pbx_groups_count = $c->license_current_pbx_groups;
if (is_true($resource->{is_pbx_group}) &&
$license_max_pbx_groups >= 0 && $current_pbx_groups_count >= $license_max_pbx_groups) {
$self->error($c, HTTP_FORBIDDEN,
"Maximum number of PBX groups for this platform is reached",
"Exceeded max number of license pbx groups: $license_max_pbx_groups current: $current_pbx_groups_count",
);
last;
}
my $r = $self->prepare_resource($c, $schema, $resource);
last unless($r);
my $subscriber;

@ -745,6 +745,33 @@ sub details :Chained('base') :PathPart('details') :Args(0) {
sub subscriber_create :Chained('base') :PathPart('subscriber/create') :Args(0) {
my ($self, $c) = @_;
my $license_max_subscribers = $c->license_max_subscribers;
my $current_subscribers_count = $c->license_current_subscribers;
if ($license_max_subscribers >= 0 && $current_subscribers_count >= $license_max_subscribers) {
NGCP::Panel::Utils::Message::error(
c => $c,
error => "tried to exceed max number of license subscribers: $license_max_subscribers current: $current_subscribers_count",
desc => $c->loc('Maximum number of subscribers for this platform is reached'),
);
NGCP::Panel::Utils::Navigation::back_or($c,
$c->uri_for_action('/customer/details', [$c->stash->{contract}->id])
);
}
my $license_max_pbx_subscribers = $c->license_max_pbx_subscribers;
my $current_pbx_subscribers_count = $c->license_current_pbx_subscribers;
if ($c->stash->{contract}->product->class eq 'pbxaccount' &&
$license_max_pbx_subscribers >= 0 && $current_pbx_subscribers_count >= $license_max_pbx_subscribers) {
NGCP::Panel::Utils::Message::error(
c => $c,
error => "tried to exceed max number of license pbx subscribers: $license_max_pbx_subscribers current: $current_pbx_subscribers_count",
desc => $c->loc('Maximum number of PBX subscribers for this platform is reached'),
);
NGCP::Panel::Utils::Navigation::back_or($c,
$c->uri_for_action('/customer/details', [$c->stash->{contract}->id])
);
}
if (defined $c->stash->{contract}->max_subscribers &&
$c->stash->{contract}->voip_subscribers
->search({ status => { -not_in => ['terminated'] } })
@ -1412,6 +1439,20 @@ sub pbx_group_ajax :Chained('base') :PathPart('pbx/group/ajax') :Args(0) {
sub pbx_group_create :Chained('base') :PathPart('pbx/group/create') :Args(0) {
my ($self, $c) = @_;
my $license_max_pbx_groups = $c->license_max_pbx_groups;
my $current_pbx_groups_count = $c->license_current_pbx_groups;
if ($license_max_pbx_groups >= 0 && $current_pbx_groups_count >= $license_max_pbx_groups) {
NGCP::Panel::Utils::Message::error(
c => $c,
error => "tried to exceed max number of license pbx groups: $license_max_pbx_groups current: $current_pbx_groups_count",
desc => $c->loc('Maximum number of pbx groups for this platform is reached'),
);
NGCP::Panel::Utils::Navigation::back_or($c,
$c->uri_for_action('/customer/details', [$c->stash->{contract}->id])
);
}
if(defined $c->stash->{contract}->max_subscribers &&
$c->stash->{contract}->voip_subscribers
->search({ status => { -not_in => ['terminated'] } })

@ -182,6 +182,71 @@ sub get_license_meta {
return $meta;
}
sub get_license_count_type {
my ($c, $type, $lic) = @_;
return -1 if $c->config->{general}{ngcp_type} eq 'spce';
my $proc_dir = '/proc/ngcp';
unless (-d $proc_dir) {
$c->log->error("Failed to access $proc_dir");
return 0;
};
my $lic_file = $proc_dir . '/' . $type . '_' . $lic;
return unless (-r $lic_file);
sysopen(my $fd, "$lic_file", O_NONBLOCK|O_RDONLY) || do {
$c->log->error("Failed to open license file $lic_file: $!");
return 0;
};
my $value;
my @h = IO::Select->new($fd)->can_read(1);
map { $value = <$_> } @h;
close $fd;
chomp($value) if defined $value;
return -1 if $value eq 'unlimited';
return $value ? $value+0 : 0;
}
sub get_max_pbx_groups {
my ($c) = @_;
return get_license_count_type($c, 'max', 'pbx_groups');
}
sub get_max_pbx_subscribers {
my ($c) = @_;
return get_license_count_type($c, 'max', 'pbx_subscribers');
}
sub get_max_subscribers {
my ($c) = @_;
return get_license_count_type($c, 'max', 'subscribers');
}
sub get_current_pbx_groups {
my ($c) = @_;
return get_license_count_type($c, 'current', 'pbx_groups');
}
sub get_current_pbx_subscribers {
my ($c) = @_;
return get_license_count_type($c, 'current', 'pbx_subscribers');
}
sub get_current_subscribers {
my ($c) = @_;
return get_license_count_type($c, 'current', 'subscribers');
}
1;
=head1 NAME

@ -2640,6 +2640,40 @@ sub get_sub_username_and_aliases {
return \@usernames;
}
sub get_pbx_groups_count {
my $c = shift;
my $schema = $c->model('DB');
my $rs = $schema->resultset('provisioning_voip_subscribers')->search({
is_pbx_group => 1,
});
return $rs->count();
}
sub get_pbx_subscribers_count {
my $c = shift;
my $schema = $c->model('DB');
my $rs = $schema->resultset('provisioning_voip_subscribers')->search({
'product.class' => 'pbxaccount',
},{
join => { contract => 'product' },
});
return $rs->count();
}
sub get_subscribers_count {
my $c = shift;
my $schema = $c->model('DB');
my $rs = $schema->resultset('provisioning_voip_subscribers')->search({
});
return $rs->count();
}
1;
=head1 NAME

Loading…
Cancel
Save