| 1 | package Net::Amazon::S3::ACL; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use version; our $VERSION = qv('0.1.0'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Net::Amazon::S3::ACL::XMLHelper qw( xpc ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Net::Amazon::S3::ACL::Grant; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use base qw(Class::Accessor::Fast); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | __PACKAGE__->mk_accessors(qw( owner_id owner_displayname grants )); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Module implementation here | ||||||||||||||||||||||||||||
| 17 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | my $params = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 21 | 1 | my $xml = delete $params->{xml}; | |||||||||||||||||||||||||||
| 22 | 1 | my $self = $class->SUPER::new($params); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | $self->parse($xml) if defined $xml; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | $self->grants({}) unless $self->grants(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | return $self; | |||||||||||||||||||||||||||
| 28 | } ## end sub new | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 1 | my ($self, $xml) = @_; | |||||||||||||||||||||||||||
| 32 | 1 | my $xpc = xpc($xml); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | $self->owner_id($xpc->findvalue('//s3:Owner/s3:ID')); | |||||||||||||||||||||||||||
| 35 | 1 | $self->owner_displayname($xpc->findvalue('//s3:Owner/s3:DisplayName')); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | my %grants = map { | ||||||||||||||||||||||||||||
| 38 | 1 | my $grant = Net::Amazon::S3::ACL::Grant->create( | |||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||
| 40 | xpc => $xpc, | ||||||||||||||||||||||||||||
| 41 | node => $_, | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | 3 | ($grant->key() => $grant); | |||||||||||||||||||||||||||
| 45 | } $xpc->findnodes('.//s3:AccessControlList/s3:Grant'); | ||||||||||||||||||||||||||||
| 46 | 1 | $self->grants(\%grants); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | return $self; | |||||||||||||||||||||||||||
| 49 | } ## end sub parse | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 25 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | 25 | ▶ | if (@_ == 1 && blessed($_[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 and builtin::blessed($_[0])
MC/DC100%2/2
@_ == 1 and builtin::blessed($_[0])
@_ == 1
builtin::blessed($_[0])
Truth table100%3/3
A and B
A= @_ == 1B= builtin::blessed($_[0])
| |||||||||||||||||||||||||||||
| 54 | 1 | $self->_delete(undef, @_); | |||||||||||||||||||||||||||
| 55 | 1 | return $self; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my @grants = ref $_[0] ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 24 | ▶ | while (my ($target, $item) = splice @grants, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 24 | $self->_delete($target, $item); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 24 | return $self; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 25 | ▶ | sub _delete { | ||||||||||||||||||||||||||
|
Subroutine
_delete: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||
| 66 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 67 | 25 | my $item = Net::Amazon::S3::ACL::Grant->canonical(@_); | |||||||||||||||||||||||||||
| 68 | #use Data::Dumper; warn Dumper $item; | ||||||||||||||||||||||||||||
| 69 | 25 | my $target = $item->key(); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 25 | ▶ | my $grants = $self->grants() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 25 | ▶ | my $perms = $item->{permissions} || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$item->{'permissions'} || []
MC/DC0%0/1
$item->{'permissions'} || []
$item->{'permissions'}
Truth table50%1/2
A
A= $item->{'permissions'}
| |||||||||||||||||||||||||||||
| 73 | 25 | ▶ | if (scalar(@$perms)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 2 | ▶ | my $previous = $grants->{$target} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 2 | my %forbidden = map { $_ => 1 } @$perms; | |||||||||||||||||||||||||||
| 76 | $previous->{permissions} = | ||||||||||||||||||||||||||||
| 77 | 2 | ▶ | [grep { !$forbidden{$_} } @{$previous->{permissions} || []}]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$previous->{'permissions'} || []
MC/DC0%0/1
$previous->{'permissions'} || []
$previous->{'permissions'}
Truth table50%1/2
A
A= $previous->{'permissions'}
| |||||||||||||||||||||||||||||
| 78 | 2 | ▶ | delete $grants->{$target} unless @{$previous->{permissions}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | } ## end if (defined($item->{permissions... | ||||||||||||||||||||||||||||
| 80 | else { | ||||||||||||||||||||||||||||
| 81 | #use Data::Dumper; warn Dumper($grants, \$target); | ||||||||||||||||||||||||||||
| 82 | 23 | delete $grants->{$target}; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 25 | return; | |||||||||||||||||||||||||||
| 85 | } ## end sub _delete | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 22 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 89 | 22 | ▶ | if (@_ == 1 && blessed($_[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and builtin::blessed($_[0])
MC/DC50%1/2
@_ == 1 and builtin::blessed($_[0])
@_ == 1
builtin::blessed($_[0])
Truth table66%2/3
A and B
A= @_ == 1B= builtin::blessed($_[0])
| |||||||||||||||||||||||||||||
| 90 | 1 | $self->_add(undef, @_); | |||||||||||||||||||||||||||
| 91 | 1 | return $self; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my @grants = ref $_[0] ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 21 | ▶ | while (my ($target, $item) = splice @grants, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 24 | $self->_add($target, $item); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 21 | return $self; | |||||||||||||||||||||||||||
| 99 | } ## end sub add | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 25 | ▶ | sub _add { | ||||||||||||||||||||||||||
|
Subroutine
_add: called CC=6 SCAR=18.7
| |||||||||||||||||||||||||||||
| 102 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 103 | 25 | my $item = Net::Amazon::S3::ACL::Grant->canonical(@_); | |||||||||||||||||||||||||||
| 104 | #use Data::Dumper; warn Dumper $item; | ||||||||||||||||||||||||||||
| 105 | 25 | my $target = $item->key(); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 25 | my $grants = $self->grants(); | |||||||||||||||||||||||||||
| 108 | 25 | ▶ | $self->grants($grants = {}) unless $grants; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 25 | ▶ | my $previous = $grants->{$target} ||= $item; | ||||||||||||||||||||||||||
|
Condition33%1/3
$grants->{$target} ||= $item
MC/DC0%0/2
$grants->{$target} ||= $item
$grants->{$target}
$item
Truth table33%1/3
A ||= B
A= $grants->{$target}B= $item
| |||||||||||||||||||||||||||||
| 111 | 25 | my %flag; # to filter out duplicates | |||||||||||||||||||||||||||
| 112 | $previous->{permissions} = [ | ||||||||||||||||||||||||||||
| 113 | 52 | ▶ | grep { !$flag{$_}++ } @{$previous->{permissions} || []}, | ||||||||||||||||||||||||||
|
Condition50%1/2
$previous->{'permissions'} || []
MC/DC0%0/1
$previous->{'permissions'} || []
$previous->{'permissions'}
Truth table50%1/2
A
A= $previous->{'permissions'}
| |||||||||||||||||||||||||||||
| 114 | 25 | ▶ | @{$item->{permissions} || []} | ||||||||||||||||||||||||||
|
Condition50%1/2
$item->{'permissions'} || []
MC/DC0%0/1
$item->{'permissions'} || []
$item->{'permissions'}
Truth table50%1/2
A
A= $item->{'permissions'}
| |||||||||||||||||||||||||||||
| 115 | ]; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 25 | ▶ | delete $grants->{$target} unless @{$previous->{permissions}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 25 | return; | |||||||||||||||||||||||||||
| 119 | } ## end sub _add | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | ▶ | sub stringify { | ||||||||||||||||||||||||||
|
Subroutine
stringify: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | my $owner_chunk = $self->_stringify_owner(); | |||||||||||||||||||||||||||
| 125 | 1 | my $grants_chunk = $self->_stringify_grants(); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Indent for pretty printing | ||||||||||||||||||||||||||||
| 128 | 1 | s/^/ /mxsg for $owner_chunk, $grants_chunk; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | return <<"END_OF_ACL"; | |||||||||||||||||||||||||||
| 131 | <?xml version="1.0" encoding="UTF-8"?> | ||||||||||||||||||||||||||||
| 132 | <AccessControlPolicy> | ||||||||||||||||||||||||||||
| 133 | $owner_chunk$grants_chunk | ||||||||||||||||||||||||||||
| 134 | </AccessControlPolicy> | ||||||||||||||||||||||||||||
| 135 | END_OF_ACL | ||||||||||||||||||||||||||||
| 136 | } ## end sub stringify | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | ▶ | sub _stringify_owner { | ||||||||||||||||||||||||||
|
Subroutine
_stringify_owner: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 139 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | ▶ | defined(my $owner_id = $self->owner_id()) or return ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 1 | my $owner_displayname = $self->owner_displayname(); | |||||||||||||||||||||||||||
| 143 | 1 | ▶ | $owner_displayname = '' unless defined $owner_displayname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | return <<"END_OF_OWNER"; | |||||||||||||||||||||||||||
| 146 | <Owner> | ||||||||||||||||||||||||||||
| 147 | <ID>$owner_id</ID> | ||||||||||||||||||||||||||||
| 148 | <DisplayName>$owner_displayname</DisplayName> | ||||||||||||||||||||||||||||
| 149 | </Owner> | ||||||||||||||||||||||||||||
| 150 | END_OF_OWNER | ||||||||||||||||||||||||||||
| 151 | } ## end sub _stringify_owner | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1 | ▶ | sub _stringify_grants { | ||||||||||||||||||||||||||
|
Subroutine
_stringify_grants: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 154 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | my $list = join "\n", | ||||||||||||||||||||||||||||
| 157 | 4 | map { $_->stringify(); } | |||||||||||||||||||||||||||
| 158 | 4 | grep { $_->is_valid()} | |||||||||||||||||||||||||||
| 159 | 1 | values %{$self->grants()}; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 1 | $list =~ s/^/ /mxsg; # indented | |||||||||||||||||||||||||||
| 162 | 1 | return "<AccessControlList>\n$list\n</AccessControlList>"; | |||||||||||||||||||||||||||
| 163 | } ## end sub _stringify_acl | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 1 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 1 | ▶ | eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 1 | require YAML; | |||||||||||||||||||||||||||
| 170 | 1 | return YAML::Dump({ | |||||||||||||||||||||||||||
| 171 | grants => $self->grants(), | ||||||||||||||||||||||||||||
| 172 | owner => { | ||||||||||||||||||||||||||||
| 173 | id => $self->owner_id(), | ||||||||||||||||||||||||||||
| 174 | displayname => $self->owner_displayname(), | ||||||||||||||||||||||||||||
| 175 | }, | ||||||||||||||||||||||||||||
| 176 | }); | ||||||||||||||||||||||||||||
| 177 | } or return $self->stringify(); | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 3 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||