Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .perlcriticrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ theme = core
[-RegularExpressions::RequireDotMatchAnything]
[-RegularExpressions::RequireExtendedFormatting]
[-RegularExpressions::RequireLineBoundaryMatching]
[-Subroutines::ProhibitExplicitReturnUndef]
[-Variables::ProhibitPunctuationVars]

[CodeLayout::RequireTrailingCommas]
Expand Down
47 changes: 47 additions & 0 deletions lib/MetaCPAN/Web/Controller/Permission.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package MetaCPAN::Web::Controller::Permission;

use Moose;
use namespace::autoclean;

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

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

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

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( { search_term => $name, permission => $perms } );
}

__PACKAGE__->meta->make_immutable;

1;
88 changes: 88 additions & 0 deletions lib/MetaCPAN/Web/Model/API/Permission.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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;
}

if ( $type eq 'distribution' ) {
return $self->_get_modules_in_distribution($name);
}

return $self->_get_author_modules($name);
}

sub _get_author_modules {
my $self = shift;
my $name = shift;

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

return $self->_search_perms($search);
}

sub _get_modules_in_distribution {
my $self = shift;
my $name = shift;
return undef unless $name;

# ugh. Can't see a better way to get a model.
my $model = MetaCPAN::Web::Model::API::Release->new(
api_secure => $self->{api_secure} );

my $res = $self->request("/package/modules/$name")->recv;
my @modules = $res->{modules} ? @{ $res->{modules} } : undef;

return undef unless @modules;

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

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

return $self->_search_perms($search);
}

sub _search_perms {
my $self = shift;
my $search = shift;

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 ? \@perms : undef;
}

__PACKAGE__->meta->make_immutable;

1;
3 changes: 3 additions & 0 deletions root/author.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
<li>
<a href="<% author.links.cpan_directory %>">CPAN directory</a> (<% author.release_count.cpan + author.release_count.latest %>)
</li>
<li>
<a href="/permission/author/<% author.pauseid %>">Module permissions</a>
</li>
<li>
<a href="<% author.links.backpan_directory %>" title="See all releases ever done by <% author.pauseid %>, not just those currently on CPAN.">BackPAN directory</a> (<% author.release_count.item('backpan-only') %>)
</li>
Expand Down
22 changes: 22 additions & 0 deletions root/inc/permission.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<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>
6 changes: 6 additions & 0 deletions root/inc/release-tools.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<i class="fa fa-star fa-fw black"></i>Rate this distribution
</a>
</li>
<li>
<a href="/permission/distribution/<% release.distribution %>">
<i class="fa fa-key fa-fw black"></i>
Upload permissions
</a>
</li>
<li>
<a href="/feed/distribution/<% release.distribution %>">
<i class="fa fa-rss-square fa-fw black"></i>Subscribe to distribution
Expand Down
5 changes: 5 additions & 0 deletions root/permission/author.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% title = 'author module permissions for ' . search_term %>

<h1>Author Module Permissions for <% search_term %></h1>

<% INCLUDE 'inc/permission.html' %>
5 changes: 5 additions & 0 deletions root/permission/distribution.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% title = 'module permissions for ' . search_term %>

<h1>Module Permissions for <% search_term %></h1>

<% INCLUDE 'inc/permission.html' %>
17 changes: 17 additions & 0 deletions root/permission/module.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<% title = 'module permissions for ' . search_term %>

<h1>Module Permissions for <% search_term %></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>
47 changes: 47 additions & 0 deletions t/controller/permission.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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' );
}

{
ok( my $res = $cb->( GET '/permission/author/!!!DOESNTEXIST' ),
'GET /permission/author/DOESNTEXIST' );
is( $res->code, 404, 'not found' );
}
{
ok( my $res = $cb->( GET '/permission/author/OALDERS' ),
'GET /permission/author/OALDERS' );
is( $res->code, 200, 'found' );
like( $res->content, qr{OALDERS}, 'OALDERS in content' );
like( $res->content, qr{HTML::Restrict}, 'owner in content' );
like( $res->content, qr{LWP::UserAgent}, 'co-maint in content' );
}
};

done_testing;