TT#28460 Entities classes use more unified config

And introduce EntityPreferences

Change-Id: I4753f14e1ba2c1e6d01559b44ba0138891c91e81
changes/97/18297/13
Irina Peshinskaya 8 years ago
parent e1c660e442
commit b9f593b7fc

@ -410,9 +410,12 @@ sub allowed_methods_filtered {
}
}
#
#old: allowed_roles = [qw/admin subscriber /]
#old:
#sub config_allowed_roles {
# return [qw/admin subscriber /];
#}
#
#from now also possible:
#also possible:
#sub config_allowed_roles {
# return {
# 'Default' => [qw/admin reseller subscriberadmin/],
@ -420,29 +423,41 @@ sub allowed_methods_filtered {
# 'POST' => [qw/admin reseller/],
# 'PUT' => [qw/admin reseller/],
# 'PATCH' => [qw/admin reseller/],
# 'Journal' => [qw/admin/],
# };
#}
#
#sub config_allowed_roles {
# return [ [qw/admin subscriber/], [qw/admin/] ];
# #where [qw/admin/] - is Journal roles spec
#}
#
sub config_allowed_roles {
return [qw/admin reseller/];
}
sub get_allowed_roles {
my($self, $method) = @_;
my($self, $roles_config_in, $method) = @_;
my $roles_config = $self->config_allowed_roles;
my ($allowed_roles_default,$allowed_roles_per_methods);
my $roles_config = $roles_config_in // $self->config_allowed_roles;
my ($allowed_roles_default, $allowed_roles_journal, $allowed_roles_per_methods);
if('HASH' eq ref $roles_config){
$allowed_roles_default = delete $roles_config->{Default};
$allowed_roles_per_methods = {map {
$_ => $roles_config->{$_} // $allowed_roles_default,
} @{ $self->allowed_methods }};
} @{ $self->allowed_methods }, 'Journal' };
}else{
$allowed_roles_default = 'ARRAY' eq ref $roles_config ? $roles_config : [$roles_config];
$allowed_roles_default = 'ARRAY' eq ref $roles_config ? $roles_config : [$self->config_allowed_roles];
if ('ARRAY' eq ref $roles_config->[0]) {
$allowed_roles_default = $roles_config->[0];
$allowed_roles_journal = $roles_config->[1] // $allowed_roles_default;
}
$allowed_roles_per_methods = {map {
$_ => $allowed_roles_default,
} @{ $self->allowed_methods }};
$allowed_roles_per_methods->{Journal} = $allowed_roles_journal;
}
return $method ? $allowed_roles_per_methods->{$method} : $allowed_roles_per_methods;
}
@ -909,18 +924,18 @@ sub hal_from_item {
Data::HAL::Link->new(relation => 'collection', href => sprintf("/api/%s/", $self->resource_name)),
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
Data::HAL::Link->new(
relation => 'self',
relation => 'self',
href => sprintf(
"%s%s",
$self->dispatch_path,
"%s%s",
$self->dispatch_path,
$self->get_item_id($c, $item, undef, undef, { purpose => 'hal_links_href' })
),
),
Data::HAL::Link->new(
relation => "ngcp:".$self->resource_name,
relation => "ngcp:".$self->resource_name,
href => sprintf(
"/api/%s/%s",
$self->resource_name,
"/api/%s/%s",
$self->resource_name,
$self->get_item_id($c, $item, undef, undef, { purpose => 'hal_links_href' })
)
),
@ -1127,14 +1142,26 @@ sub complete_transaction{
}
#------ accessors ---
sub dispatch_path {
my $self = shift;
return '/api/'.$self->resource_name.'/';
sub resource_name{
return $_[0]->config->{resource_name};
}
#need it for sub config, when config is not defined yet, so we just format known resource_name properly
sub dispatch_path{
return '/api/'.($_[0]->resource_name // $_[1]).'/';
}
sub relation {
my $self = shift;
return 'http://purl.org/sipwise/ngcp-api/#rel-'.$self->resource_name;
return 'http://purl.org/sipwise/ngcp-api/#rel-'.$_[0]->resource_name;
}
sub item_name{
return $_[0]->config->{item_name};
}
sub allowed_methods{
return $_[0]->config->{allowed_methods};
}
#------ /accessors ---

@ -2,7 +2,6 @@ package NGCP::Panel::Role::Entities;
use warnings;
use strict;
use parent qw/Catalyst::Controller/;
use boolean qw(true);
use Safe::Isa qw($_isa);
@ -11,27 +10,150 @@ use HTTP::Status qw(:constants);
use Data::HAL qw();
use Data::HAL::Link qw();
##### --------- common part
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
if ($self->config->{log_request}) {
$self->log_request($c);
}
return 1;
}
sub end :Private {
my ($self, $c) = @_;
if ($self->config->{log_response}) {
$self->log_response($c);
}
return 1;
}
sub GET {
my ($self) = shift;
return $self->get(@_);
}
sub HEAD {
my ($self) = shift;
return $self->head(@_);
}
sub OPTIONS {
my ($self) = shift;
return $self->options(@_);
}
sub set_config {
my $self = shift;
my $allowed_roles_by_methods = $self->get_allowed_roles();
$self->config(
action => {
map { $_ => {
my ($params) = @_;
$params //= {};
#Global => {
#resource_name
#item_name
#allowed_roles
#allowed_methods
#log_response
#log_request
#}
#AllMethods | METHOD => {
#AllowedRole
#ACLDetachTo
#Args
#Does
#DoesAdd
#Method
#Path
#ContentType => ['multipart/form-data'],#allowed REQUEST content type
#Uploads => [qw/front_image mac_image/],#uploads filenames
# or
#Uploads => {'greetingfile' => ['audio/x-wav', 'application/octet-stream']},
#own_transaction_control->{PUT|POST|PATCH|DELETE|ALL} = 0|1 - don't start transaction guard in parent classes, implementation need to control it
#ReturnContentType => 'binary'#mostly for GET. value different from 'application/json' says that method is going to return binary data using get_item_binary_data
#}
my $obj_name = $self;
if (!defined $params->{interface_type}) {
if ($obj_name =~/Item$/) {
$params->{interface_type} = 'item';
} elsif ($obj_name =~/PreferenceDefs$/) {
$params->{interface_type} = 'preferencedefs';
} else {
$params->{interface_type} = 'collection';
}
}
$params->{resource_name} //= $self->resource_name;
if (!defined $params->{resource_name}) {
$params->{resource_name} = lc $obj_name;
$params->{resource_name} =~s/.*?::([^:]+)$/$1/;
$params->{resource_name} =~s/item$//;
}
$params->{item_name} //= $self->item_name;
if (!defined $params->{item_name}) {
$params->{item_name} = $params->{resource_name};
$params->{item_name} =~s/s$//;
}
my $params_all_methods = delete $params->{AllMethods} // {};
my $allowed_roles_by_methods = $self->get_allowed_roles(delete $params->{allowed_roles});
my $params_action_add = delete $params->{action_add} // {};
my $config_action = {
(map {
my $params_method = delete $params->{$_} // {};
$_ => {
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => $allowed_roles_by_methods->{$_},
Args => 0,
Does => [qw(ACL CheckTrailingSlash RequireSSL)],
Args => ( $params->{interface_type} eq 'item' ? 1 : 0 ),
Does => [
'ACL',
( $params->{interface_type} eq 'item' ? () : ('CheckTrailingSlash') ),
'RequireSSL',
( ref $params_all_methods->{DoesAdd} eq 'ARRAY' ? @$params_all_methods->{DoesAdd} : () ),
( ref $params_method->{DoesAdd} eq 'ARRAY' ? @$params_method->{DoesAdd} : () ),
],
Method => $_,
Path => $self->dispatch_path,
%{$self->_set_config($_)},
} } @{ $self->allowed_methods }
},
#action_roles => [qw(HTTPMethods)],
%{$self->_set_config()},
#log_response = 0|1 - don't log response body
#own_transaction_control->{PUT|POST|PATCH|DELETE} = 0|1 - don't start transaction guard
);
Path => $self->dispatch_path($params->{resource_name}),
ReturnContentType => 'application/json',
%{$params_all_methods},
%{$params_method},
} } @{ $self->allowed_methods }),
%{$params_action_add},
};
#$config_action = {
# %{$config_action},
# %{$self->_set_config('AllMethods', $config_action)},
#};
#$config_action = {
# %{$config_action},
# map {
# %{$self->_set_config($_, $config_action)},
# } } @{ $self->allowed_methods },
#};
my $config = {
action => $config_action,
( 'item' eq $params->{interface_type}
?
@{ $self->get_journal_action_config($self->resource_name,{
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => $allowed_roles_by_methods->{'Journal'},
Does => [qw(ACL RequireSSL)],
}) }
:
() ),
log_response => 1,
log_request => 1,
%{$params},
};
#$config = {
# %{$config},
#Global to don't pass undefined method and initiate it every time in the _set_config
# %{$self->_set_config('Global')},
#};
$self->config($config);
}
sub gather_default_action_roles {
@ -40,6 +162,31 @@ sub gather_default_action_roles {
return @roles;
}
sub head {
my ($self, $c) = @_;
$c->forward(qw(GET));
$c->response->body(q());
return;
}
##### --------- /common part
sub options {
my ($self, $c) = @_;
my $allowed_methods = $self->allowed_methods_filtered($c);
my $post_allowed = grep { $_ eq 'POST' } @{ $allowed_methods };
$c->response->headers(HTTP::Headers->new(
Allow => join(', ', @{ $allowed_methods }),
# $post_allowed
# ? (
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 get {
my ($self, $c) = @_;
my $header_accept = $c->request->header('Accept');
@ -138,52 +285,6 @@ sub post {
return;
}
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$self->log_request($c);
}
sub head {
my ($self, $c) = @_;
$c->forward(qw(GET));
$c->response->body(q());
return;
}
sub options {
my ($self, $c) = @_;
my $allowed_methods = $self->allowed_methods_filtered($c);
$c->response->headers(HTTP::Headers->new(
Allow => join(', ', @{ $allowed_methods }),
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);
}
sub GET {
my ($self) = shift;
return $self->get(@_);
}
sub HEAD {
my ($self) = shift;
return $self->head(@_);
}
sub OPTIONS {
my ($self) = shift;
return $self->options(@_);
}
sub POST {
my ($self) = shift;

@ -4,7 +4,6 @@ use warnings;
use strict;
use parent qw/Catalyst::Controller/;
use boolean qw(true);
use Safe::Isa qw($_isa);
use Path::Tiny qw(path);
@ -16,35 +15,150 @@ use NGCP::Panel::Utils::Generic qw(:all);
use NGCP::Panel::Utils::DateTime;
use NGCP::Panel::Utils::ValidateJSON qw();
##### --------- common part
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
if($self->config->{log_request}){
$self->log_request($c);
}
return 1;
}
sub end :Private {
my ($self, $c) = @_;
if($self->config->{log_response}){
$self->log_response($c);
}
return 1;
}
sub GET {
my ($self) = shift;
return $self->get(@_);
}
sub HEAD {
my ($self) = shift;
return $self->head(@_);
}
sub OPTIONS {
my ($self) = shift;
return $self->options(@_);
}
sub set_config {
my $self = shift;
my $allowed_roles_by_methods = $self->get_allowed_roles();
$self->config(
action => {
map { $_ => {
my ($params) = @_;
$params //= {};
#Global => {
#resource_name
#item_name
#allowed_roles
#allowed_methods
#log_response
#log_request
#}
#AllMethods | METHOD => {
#AllowedRole
#ACLDetachTo
#Args
#Does
#DoesAdd
#Method
#Path
#ContentType => ['multipart/form-data'],#allowed REQUEST content type
#Uploads => [qw/front_image mac_image/],#uploads filenames
# or
#Uploads => {'greetingfile' => ['audio/x-wav', 'application/octet-stream']},
#own_transaction_control->{PUT|POST|PATCH|DELETE|ALL} = 0|1 - don't start transaction guard in parent classes, implementation need to control it
#ReturnContentType => 'binary'#mostly for GET. value different from 'application/json' says that method is going to return binary data using get_item_binary_data
#}
my $obj_name = $self;
if (!defined $params->{interface_type}) {
if ($obj_name =~/Item$/) {
$params->{interface_type} = 'item';
} elsif ($obj_name =~/PreferenceDefs$/) {
$params->{interface_type} = 'preferencedefs';
} else {
$params->{interface_type} = 'collection';
}
}
$params->{resource_name} //= $self->resource_name;
if (!defined $params->{resource_name}) {
$params->{resource_name} = lc $obj_name;
$params->{resource_name} =~s/.*?::([^:]+)$/$1/;
$params->{resource_name} =~s/item$//;
}
$params->{item_name} //= $self->item_name;
if (!defined $params->{item_name}) {
$params->{item_name} = $params->{resource_name};
$params->{item_name} =~s/s$//;
}
my $params_all_methods = delete $params->{AllMethods} // {};
my $allowed_roles_by_methods = $self->get_allowed_roles(delete $params->{allowed_roles});
my $params_action_add = delete $params->{action_add} // {};
my $config_action = {
(map {
my $params_method = delete $params->{$_} // {};
$_ => {
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => $allowed_roles_by_methods->{$_},
Args => 1,
Does => [qw(ACL RequireSSL)],
Args => ( $params->{interface_type} eq 'item' ? 1 : 0 ),
Does => [
'ACL',
( $params->{interface_type} eq 'item' ? () : ('CheckTrailingSlash') ),
'RequireSSL',
( ref $params_all_methods->{DoesAdd} eq 'ARRAY' ? @$params_all_methods->{DoesAdd} : () ),
( ref $params_method->{DoesAdd} eq 'ARRAY' ? @$params_method->{DoesAdd} : () ),
],
Method => $_,
Path => $self->dispatch_path,
Path => $self->dispatch_path($params->{resource_name}),
ReturnContentType => 'application/json',
%{$self->_set_config($_)},
} } @{ $self->allowed_methods }
},
@{ $self->get_journal_action_config($self->resource_name,{
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => [qw/admin reseller/],
Does => [qw(ACL RequireSSL)],
}) },
#action_roles => [qw(HTTPMethods)],
%{$params_all_methods},
%{$params_method},
} } @{ $self->allowed_methods }),
%{$params_action_add},
};
#$config_action = {
# %{$config_action},
# %{$self->_set_config('AllMethods', $config_action)},
#};
#$config_action = {
# %{$config_action},
# map {
# %{$self->_set_config($_, $config_action)},
# } } @{ $self->allowed_methods },
#};
my $config = {
action => $config_action,
( 'item' eq $params->{interface_type}
?
@{ $self->get_journal_action_config($self->resource_name,{
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => $allowed_roles_by_methods->{'Journal'},
Does => [qw(ACL RequireSSL)],
}) }
:
() ),
log_response => 1,
log_request => 1,
%{$self->_set_config()},
#log_response = 0|1 - don't log response body, used for binary data download api
#log_request = 0|1 - don't log response body, used for binary data upload api
#own_transaction_control = {post|put|patch|delete|all => 1|0}
);
%{$params},
};
#$config = {
# %{$config},
#Global to don't pass undefined method and initiate it every time in the _set_config
# %{$self->_set_config('Global')},
#};
$self->config($config);
}
sub gather_default_action_roles {
@ -53,6 +167,14 @@ sub gather_default_action_roles {
return @roles;
}
sub head {
my ($self, $c) = @_;
$c->forward(qw(GET));
$c->response->body(q());
return;
}
##### --------- /common part
sub get {
my ($self, $c, $id) = @_;
@ -194,55 +316,22 @@ sub delete_item{
$item->delete();
}
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
if($self->config->{log_request}){
$self->log_request($c);
}
}
sub head {
my ($self, $c, $id) = @_;
$c->forward(qw(GET));
$c->response->body(q());
return;
}
sub options {
my ($self, $c, $id) = @_;
my $allowed_methods = $self->allowed_methods_filtered($c);
my $patch_allowed = grep { $_ eq 'PATCH' } @{ $allowed_methods };
$c->response->headers(HTTP::Headers->new(
Allow => join(', ', @{ $allowed_methods }),
$patch_allowed
? (
Accept_Patch => 'application/json-patch+json',
) : (),
));
$c->response->content_type('application/json');
$c->response->body(JSON::to_json({ methods => $allowed_methods })."\n");
return;
}
sub end :Private {
my ($self, $c) = @_;
if($self->config->{log_response}){
$self->log_response($c);
}
}
sub GET {
my ($self) = shift;
return $self->get(@_);
}
sub HEAD {
my ($self) = shift;
return $self->head(@_);
}
sub OPTIONS {
my ($self) = shift;
return $self->options(@_);
}
sub PUT {
my ($self) = shift;
return $self->put(@_);

@ -0,0 +1,220 @@
package NGCP::Panel::Role::EntityPreferenceDefs;
use warnings;
use strict;
use parent qw/Catalyst::Controller/;
use boolean qw(true);
use Safe::Isa qw($_isa);
use HTTP::Headers qw();
use HTTP::Status qw(:constants);
use Data::HAL qw();
use Data::HAL::Link qw();
##### --------- common part
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
if($self->config->{log_request}){
$self->log_request($c);
}
return 1;
}
sub end :Private {
my ($self, $c) = @_;
if($self->config->{log_response}){
$self->log_response($c);
}
return 1;
}
sub GET {
my ($self) = shift;
return $self->get(@_);
}
sub HEAD {
my ($self) = shift;
return $self->head(@_);
}
sub OPTIONS {
my ($self) = shift;
return $self->options(@_);
}
sub set_config {
my $self = shift;
my ($params) = @_;
$params //= {};
#Global => {
#resource_name
#item_name
#allowed_roles
#allowed_methods
#log_response
#log_request
#}
#AllMethods | METHOD => {
#AllowedRole
#ACLDetachTo
#Args
#Does
#DoesAdd
#Method
#Path
#ContentType => ['multipart/form-data'],#allowed REQUEST content type
#Uploads => [qw/front_image mac_image/],#uploads filenames
# or
#Uploads => {'greetingfile' => ['audio/x-wav', 'application/octet-stream']},
#own_transaction_control->{PUT|POST|PATCH|DELETE|ALL} = 0|1 - don't start transaction guard in parent classes, implementation need to control it
#ReturnContentType => 'binary'#mostly for GET. value different from 'application/json' says that method is going to return binary data using get_item_binary_data
#}
my $obj_name = $self;
if (!defined $params->{interface_type}) {
if ($obj_name =~/Item$/) {
$params->{interface_type} = 'item';
} elsif ($obj_name =~/PreferenceDefs$/) {
$params->{interface_type} = 'preferencedefs';
} else {
$params->{interface_type} = 'collection';
}
}
$params->{resource_name} //= $self->resource_name;
if (!defined $params->{resource_name}) {
$params->{resource_name} = lc $obj_name;
$params->{resource_name} =~s/.*?::([^:]+)$/$1/;
$params->{resource_name} =~s/item$//;
}
$params->{item_name} //= $self->item_name;
if (!defined $params->{item_name}) {
$params->{item_name} = $params->{resource_name};
$params->{item_name} =~s/s$//;
}
my $params_all_methods = delete $params->{AllMethods} // {};
my $allowed_roles_by_methods = $self->get_allowed_roles(delete $params->{allowed_roles});
my $params_action_add = delete $params->{action_add} // {};
my $config_action = {
(map {
my $params_method = delete $params->{$_} // {};
$_ => {
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => $allowed_roles_by_methods->{$_},
Args => ( $params->{interface_type} eq 'item' ? 1 : 0 ),
Does => [
'ACL',
( $params->{interface_type} eq 'item' ? () : ('CheckTrailingSlash') ),
'RequireSSL',
( ref $params_all_methods->{DoesAdd} eq 'ARRAY' ? @$params_all_methods->{DoesAdd} : () ),
( ref $params_method->{DoesAdd} eq 'ARRAY' ? @$params_method->{DoesAdd} : () ),
],
Method => $_,
Path => $self->dispatch_path($params->{resource_name}),
ReturnContentType => 'application/json',
%{$params_all_methods},
%{$params_method},
} } @{ $self->allowed_methods }),
%{$params_action_add},
};
#$config_action = {
# %{$config_action},
# %{$self->_set_config('AllMethods', $config_action)},
#};
#$config_action = {
# %{$config_action},
# map {
# %{$self->_set_config($_, $config_action)},
# } } @{ $self->allowed_methods },
#};
my $config = {
action => $config_action,
( 'item' eq $params->{interface_type}
?
@{ $self->get_journal_action_config($self->resource_name,{
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => $allowed_roles_by_methods->{'Journal'},
Does => [qw(ACL RequireSSL)],
}) }
:
() ),
log_response => 1,
log_request => 1,
%{$params},
};
#$config = {
# %{$config},
#Global to don't pass undefined method and initiate it every time in the _set_config
# %{$self->_set_config('Global')},
#};
$self->config($config);
}
sub gather_default_action_roles {
my ($self, %args) = @_; my @roles = ();
push @roles, 'NGCP::Panel::Role::HTTPMethods' if $args{attributes}->{Method};
return @roles;
}
sub head {
my ($self, $c) = @_;
$c->forward(qw(GET));
$c->response->body(q());
return;
}
##### --------- /common part
sub get {
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 $resource = NGCP::Panel::Utils::Preferences::api_preferences_defs( c => $c, preferences_group => $self->config->{'preferences_group'} );
$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 options {
my ($self, $c) = @_;
my $allowed_methods = $self->allowed_methods_filtered($c);
$c->response->headers(HTTP::Headers->new(
Allow => join(', ', @{ $allowed_methods }),
#TODO: Adapt Collection test to don't check Accept_Post if collection dosn't provide POST method
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;
}
1;
# vim: set tabstop=4 expandtab:
Loading…
Cancel
Save