Skip to content

Commit

Permalink
Merge pull request #683 from metacpan/mickey/permission_new_endpoints
Browse files Browse the repository at this point in the history
Added new permission API endpoints
  • Loading branch information
oalders committed Jun 6, 2017
2 parents 7609d9b + 7b8aeea commit 0969575
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
70 changes: 70 additions & 0 deletions lib/MetaCPAN/Document/Permission.pm
Expand Up @@ -21,5 +21,75 @@ has co_maintainers => (
isa => ArrayRef,
);

__PACKAGE__->meta->make_immutable;

package MetaCPAN::Document::Permission::Set;

use strict;
use warnings;

use Moose;
use Ref::Util qw( is_arrayref );

use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

extends 'ElasticSearchX::Model::Document::Set';

sub by_author {
my ( $self, $pauseid ) = @_;

my $body = {
query => {
bool => {
should => [
{ term => { owner => $pauseid } },
{ term => { co_maintainers => $pauseid } },
],
},
},
size => 5_000,
};

my $ret = $self->es->search(
index => $self->index->name,
type => 'permission',
body => $body,
);
return unless $ret->{hits}{total};

my $data = [
sort { $a->{module_name} cmp $b->{module_name} }
map { $_->{_source} } @{ $ret->{hits}{hits} }
];

return { permissions => $data };
}

sub by_modules {
my ( $self, $modules ) = @_;
$modules = [$modules] unless is_arrayref($modules);

my $body = {
query => {
terms => { module_name => $modules },
},
size => 1_000,
};

my $ret = $self->es->search(
index => $self->index->name,
type => 'permission',
body => $body,
);
return unless $ret->{hits}{total};

my $data = [
sort { $a->{module_name} cmp $b->{module_name} }
map { $_->{_source} } @{ $ret->{hits}{hits} }
];

return { permissions => $data };
}

__PACKAGE__->meta->make_immutable;
1;
22 changes: 22 additions & 0 deletions lib/MetaCPAN/Server/Controller/Permission.pm
Expand Up @@ -7,5 +7,27 @@ BEGIN { extends 'MetaCPAN::Server::Controller' }

with 'MetaCPAN::Server::Role::JSONP';

sub by_author : Path('by_author') : Args(1) {
my ( $self, $c, $pauseid ) = @_;
my $data = $self->model($c)->raw->by_author($pauseid);
return unless $data;
$c->stash($data);
}

sub by_module : Path('by_module') : Args(1) {
my ( $self, $c, $module ) = @_;
my $data = $self->model($c)->raw->by_modules($module);
return unless $data;
$c->stash($data);
}

sub by_modules : Path('by_module') : Args(0) {
my ( $self, $c ) = @_;
my @modules = $c->req->param('module');
my $data = $self->model($c)->raw->by_modules( \@modules );
return unless $data;
$c->stash($data);
}

__PACKAGE__->meta->make_immutable;
1;

0 comments on commit 0969575

Please sign in to comment.