File Coverage

blib/lib/WebService/Cmis/ACL.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package WebService::Cmis::ACL;
2              
3             =head1 NAME
4              
5             WebService::Cmis::ACL
6              
7             Representation of a cmis ACL object
8              
9             =head1 SYNOPSIS
10              
11             =head1 DESCRIPTION
12              
13             =cut
14              
15 1     1   7392 use strict;
  1         3  
  1         44  
16 1     1   7 use warnings;
  1         2  
  1         42  
17 1     1   6 use WebService::Cmis qw(:namespaces);
  1         2  
  1         169  
18 1     1   7 use WebService::Cmis::ACE ();
  1         3  
  1         23  
19 1     1   503 use XML::LibXML qw(:libxml);
  0            
  0            
20             use Error qw(:try);
21              
22             =head1 METHODS
23              
24             =over 4
25              
26             =item new(%params)
27              
28             =cut
29              
30             sub new {
31             my $class = shift;
32              
33             my $this = bless({ @_ }, $class);
34              
35             if (defined $this->{xmlDoc}) {
36             $this->{entries} = $this->_getEntriesFromXml();
37             }
38             $this->{entries} = [] unless defined $this->{entries};
39            
40             return $this;
41             }
42              
43             sub DESTROY {
44             my $this = shift;
45              
46             undef $this->{xmlDoc};
47             undef $this->{entries};
48             }
49              
50             =item toString()
51              
52             return a string representation of this object
53              
54             =cut
55              
56             sub toString {
57             my $this = shift;
58              
59             my @result = ();
60             foreach my $ace ($this->getEntries) {
61             push @result, $ace->toString;
62             }
63              
64             return join("; ", @result);
65             }
66              
67             =item getSize() -> $number
68              
69             returns the number of ACE objects part of this list
70              
71             =cut
72              
73             sub getSize {
74             my $this = shift;
75              
76             return scalar(@{$this->{entries}});
77             }
78              
79             =item addEntry($ace) -> L
80              
81             adds an ACE entry to the ACL. returns $this object.
82              
83             =cut
84              
85             sub addEntry {
86             my ($this, $ace) = @_;
87              
88             push @{$this->{entries}}, $ace;
89              
90             return $this;
91             }
92              
93             =item removeEntry($idOrAce) -> L
94              
95             removes all specified entries. C<$idOrAce> can either be a principalId or an
96             ACE object. In the first case all ACEs for the principalId will be removed.
97             When an ACE object is specified, all equivalent ACEs in the ACL will be
98             removed. returns $this object.
99              
100             =cut
101              
102             sub removeEntry {
103             my ($this, $idOrAce) = @_;
104              
105             return unless $this->{entries};
106            
107             my @newEntries = ();
108              
109             if (ref($idOrAce)) {
110             my $testAce = $idOrAce;
111             my $testAceString = $testAce->toString;
112              
113             foreach my $ace (@{$this->{entries}}) {
114             push @newEntries, $ace unless $ace->toString eq $testAceString;
115             }
116              
117             } else {
118             my $principalId = $idOrAce;
119              
120             foreach my $ace (@{$this->{entries}}) {
121             push @newEntries, $ace unless $ace->{principalId} eq $principalId;
122             }
123             }
124              
125             $this->{entries} = \@newEntries;
126              
127             return $this;
128             }
129              
130             =item getEntries -> @aces
131              
132             returns a list of ACE objects for each access control
133             entry in the ACL.
134              
135             =cut
136              
137             sub getEntries {
138             my $this = shift;
139              
140             unless (defined $this->{entries}) {
141             $this->{entries} = $this->_getEntriesFromXml();
142             }
143              
144             return @{$this->{entries}};
145             }
146              
147             # private helper to for getting all ACEs for the XML representation of this ACL
148             sub _getEntriesFromXml {
149             my $this = shift;
150              
151             throw Error::Simple("no xmldoc for ACL object") unless defined $this->{xmlDoc};
152              
153             my $xcp = XML::LibXML::XPathContext->new($this->{xmlDoc});
154             $xcp->registerNs('cmis', CMIS_NS);
155              
156             my @result = ();
157             my $permNodes = $xcp->find("cmis:acl/cmis:permission");
158              
159             foreach my $node ($permNodes->get_nodelist) {
160             #print STDERR "node=".$node->toString(2)."\n";
161              
162             my $principalId = $xcp->findvalue('./cmis:principal/cmis:principalId', $node);
163             my $direct = $xcp->findvalue('./cmis:direct', $node);
164             my @perms = map {$_->textContent()} $xcp->findnodes('./cmis:permission', $node);
165             next unless @perms;
166              
167             #print STDERR "principalId=$principalId, direct=$direct, perms='".join(', ', @perms)."'\n";
168              
169             # create an ACE
170             push @result, new WebService::Cmis::ACE(
171             principalId => $principalId,
172             permissions => \@perms,
173             direct => $direct
174             );
175             }
176              
177             return \@result;
178             }
179              
180             =item getXmlDoc -> $xmlDoc
181              
182             This method rebuilds the local XML representation of the ACL based on
183             the ACE objects in the entries list and returns the resulting
184             XML Document.
185              
186             =cut
187              
188             sub getXmlDoc {
189             my $this = shift;
190              
191             return unless defined $this->{entries} && $this->getSize;
192              
193             my $xmlDoc = new XML::LibXML::Document('1.0', 'UTF-8');
194             my $aclNode = $xmlDoc->createElementNS(CMIS_NS, 'acl');
195             $xmlDoc->setDocumentElement($aclNode);
196              
197             foreach my $ace ($this->getEntries) {
198             my $permNode = $xmlDoc->createElement('permission');
199              
200             # principalId
201             $permNode->addNewChild(CMIS_NS, 'principal')
202             ->appendTextChild('principalId', $ace->{principalId});
203              
204             # direct
205             $permNode->appendTextChild('direct', $ace->{direct});
206              
207             # permissions
208             foreach my $perm (@{$ace->{permissions}}) {
209             next unless $perm;
210             $permNode->appendTextChild('permission', $perm);
211             }
212              
213             $aclNode->appendChild($permNode);
214             }
215            
216             return $this->{xmlDoc} = $xmlDoc;
217             }
218              
219             =back
220              
221             =head1 COPYRIGHT AND LICENSE
222              
223             Copyright 2012-2013 Michael Daum
224              
225             This module is free software; you can redistribute it and/or modify it under
226             the same terms as Perl itself. See F.
227              
228             =cut
229              
230             1;