Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #1878 from metacpan/oalders/permission
Add 06perms to the front end
- Loading branch information
Showing
10 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |