Skip to content

Commit

Permalink
Add /permission/module/Foo and /permission/distribution/Foo
Browse files Browse the repository at this point in the history
  • Loading branch information
oalders committed May 11, 2017
1 parent cc11b67 commit d0014c1
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/MetaCPAN/Web/Controller/Permission.pm
@@ -0,0 +1,41 @@
package MetaCPAN::Web::Controller::Permission;

use Moose;
use namespace::autoclean;

BEGIN { extends 'MetaCPAN::Web::Controller' }

sub distribution : Local Args(1) {
my ( $self, $c, $distribution ) = @_;

$c->forward( 'get', $c, [ 'distribution', $distribution ] );
}

sub module : Local Args(1) {
my ( $self, $c, $module ) = @_;

$c->forward( 'get', $c, [ 'module', $module ] );
}

sub get : Private {
my $self = shift;
my $c = shift;
my ( $type, $name ) = @_;

my $perms = $c->model('API::Permission')->get( $type, $name );

if ( !$perms ) {
$c->stash(
{
message => 'Permissions not found for ' . $name
}
);
$c->detach('/not_found');
}

$c->stash( { name => $name, permission => $perms } );
}

__PACKAGE__->meta->make_immutable;

1;
67 changes: 67 additions & 0 deletions lib/MetaCPAN/Web/Model/API/Permission.pm
@@ -0,0 +1,67 @@
package MetaCPAN::Web::Model::API::Permission;

use Moose;

extends 'MetaCPAN::Web::Model::API';

use MetaCPAN::Web::Model::API::Release ();

=head1 NAME
MetaCPAN::Web::Model::Permission - Catalyst Model for 06perms
=cut

sub get {
my ( $self, $type, $name ) = @_;

if ( $type eq 'module' ) {
my $module = $self->request( '/permission/' . $name )->recv;

# return undef if there's a 404
return $module->{code} ? undef : $module;
}

my $release = $self->request( '/release/' . $name )->recv;

# ugh
my $model = MetaCPAN::Web::Model::API::Release->new(
api_secure => $self->{api_secure} );

my $pkg_search = {
query => {
bool => {
must => [
{ term => { distribution => $release->{distribution} } },
{ term => { dist_version => $release->{version} } },
]
},
},
size => 1_000,
};

my $found_modules
= $self->request( '/package/_search', $pkg_search )->recv;

my @modules = map { $_->{_source}->{module_name} }
@{ $found_modules->{hits}->{hits} };

return undef unless @modules;

my @perm_search
= map { +{ term => { module_name => $_ } } } @modules;

my $search = {
query => { bool => { should => \@perm_search } },
size => 1_000,
};

my $perms_found = $self->request( '/permission/_search', $search )->recv;
my @perms = sort { $a->{module_name} cmp $b->{module_name} }
map { $_->{_source} } @{ $perms_found->{hits}->{hits} };
return \@perms;
}

__PACKAGE__->meta->make_immutable;

1;
26 changes: 26 additions & 0 deletions root/permission/distribution.html
@@ -0,0 +1,26 @@
<% title = 'module permissions for ' . name %>

<h1>Module Permissions</h1>

<table class="table table-striped">
<tr>
<th>Name</th>
<th>Owner</th>
<th>Co-Maintainers</th>
</tr>
<% FOREACH module IN permission %>
<tr>
<td>
<a href="/pod/<% module.module_name %>"><% module.module_name %></a>
</td>
<td>
<a href="/author/<% module.owner %>"><% module.owner %></a>
</td>
<td>
<% FOREACH co_maintainer IN module.co_maintainers %>
<a href="/author/<% co_maintainer %>"><% co_maintainer %></a>
<% END %>
</td>
</tr>
<% END %>
</table>
17 changes: 17 additions & 0 deletions root/permission/module.html
@@ -0,0 +1,17 @@
<% title = 'module permissions for ' . name %>

<h1>Module Permissions</h1>

<p>
<a href="/pod/<% permission.module_name %>"><% permission.module_name %></a> is owned by <a href="/author/<% permission.owner %>"><% permission.owner %></a>.
</p>

<p>
This module is also maintained by
</p>

<ul>
<% FOREACH co_maintainer IN permission.co_maintainers %>
<li><a href="/author/<% co_maintainer %>"><% co_maintainer %></a></li>
<% END %>
</ul>
33 changes: 33 additions & 0 deletions t/controller/permission.t
@@ -0,0 +1,33 @@
use strict;
use warnings;
use Test::More;
use MetaCPAN::Web::Test;

test_psgi app, sub {
my $cb = shift;

{
ok( my $res = $cb->( GET '/permission/module/DOESNTEXIST' ),
'GET /permission/module/DOESNTEXIST' );
is( $res->code, 404, 'not found' );
}
{
ok( my $res = $cb->( GET '/permission/module/Moose' ),
'GET /permission/module/Moose' );
is( $res->code, 200, 'found' );
like( $res->content, qr{ETHER}, 'ETHER in content' );
}
{
ok( my $res = $cb->( GET '/permission/distribution/DOESNTEXIST' ),
'GET /permission/distribution/DOESNTEXIST' );
is( $res->code, 404, 'not found' );
}
{
ok( my $res = $cb->( GET '/permission/distribution/Moose' ),
'GET /permission/distribution/Moose' );
is( $res->code, 200, 'found' );
like( $res->content, qr{ETHER}, 'ETHER in content' );
}
};

done_testing;

0 comments on commit d0014c1

Please sign in to comment.