/
ACL.pm
185 lines (121 loc) · 3.54 KB
/
ACL.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package WebService::Cmis::ACL;
=head1 NAME
WebService::Cmis::ACL
Representation of a cmis ACL object
=head1 SYNOPSIS
=head1 DESCRIPTION
=cut
use strict;
use warnings;
use WebService::Cmis qw(:namespaces);
use WebService::Cmis::ACE ();
use XML::LibXML qw(:libxml);
use Error qw(:try);
=head1 METHODS
=over 4
=item new(I<%args>)
=cut
sub new {
my $class = shift;
my $this = bless({ @_ }, $class);
if (defined $this->{xmlDoc}) {
$this->{entries} = $this->_getEntriesFromXml();
}
return $this;
}
=item DESTROY
destructor for this ACL object
=cut
sub DESTROY {
my $this = shift;
undef $this->{xmlDoc};
undef $this->{entries};
}
=item toString()
return a string representation of this object
=cut
sub toString {
my $this = shift;
my @result = ();
foreach my $ace ($this->getEntries) {
push @result, $ace->toString;
}
return join("\n", @result);
}
=item addEntry($ace)
adds an ACE entry to the ACL.
=cut
sub addEntry {
my ($this, $ace) = @_;
$this->{entries}{$ace->{principalId}} = $ace;
}
=item removeEntry($principalId) -> $ace;
removes the ACE entry given a specific principalId.
=cut
sub removeEntry {
my ($this, $principalId) = @_;
return delete $this->{entries}{$principalId};
}
=item getEntries -> @aces
returns a list of ACE objects for each access control
entry in the ACL.
=cut
sub getEntries {
my $this = shift;
unless (defined $this->{entries}) {
$this->{entries} = $this->_getEntriesFromXml();
}
return @{$this->{entries}};
}
# private helper to for getting all ACEs for the XML representation of this ACL
sub _getEntriesFromXml {
my $this = shift;
throw Error::Simple("no xmldoc for ACL object") unless defined $this->{xmlDoc};
my @result = ();
my ($aclNode) = $this->{xmlDoc}->getElementsByTagNameNS(CMIS_NS, 'acl'); # ok
foreach my $node ($aclNode->childNodes) {
next unless $node->nodeType == XML_ELEMENT_NODE && $node->localname eq 'permission';
my $principalId = $node->findvalue('./cmis:principal/cmis:principalId');
my $direct = $node->findvalue('./cmis:direct');
my @perms = $node->findvalue('./cmis:permission');
#print STDERR "principalId=$principalId, direct=$direct, perms=".join(', ', @perms)."\n";
# create an ACE
push @result, new WebService::Cmis::ACE(
principalId => $principalId,
permissions => \@perms,
direct => $direct
);
}
return \@result;
}
=item getXmlDoc -> $xmlDoc
This method rebuilds the local XML representation of the ACL based on
the ACE objects in the entries list and returns the resulting
XML Document.
=cut
sub getXmlDoc {
my $this = shift;
return unless defined $this->{entries} && scalar(@{$this->{entries}});
my $xmlDoc = new XML::LibXML::Document('1.0', 'UTF-8');
my $aclNode = $xmlDoc->createElementNS(CMIS_NS, 'cmis:acl');
$aclNode->setAttribute('xmlns:cmis', CMIS_NS);
foreach my $ace ($this->getEntries) {
my $permNode = $xmlDoc->createElementNS(CMIS_NS, 'cmis:permission');
# principalId
$permNode->addNewChild(CMIS_NS, 'principal')
->addNewChild(CMIS_NS, 'principalId')
->addChild($xmlDoc->createTextNode($ace->{principalId}));
# direct
$permNode->addNewChild(CMIS_NS, 'direct')
->addChild($xmlDoc->createTextNode($ace->{direct}));
# permissions
foreach my $perm (@{$ace->{permissions}}) {
$permNode->addNewChild(CMIS_NS, 'permission')
->addChild($xmlDoc->createTextNode($perm));
}
$aclNode->appendChild($permNode);
}
$xmlDoc->setDocumentElement($aclNode);
return $this->{xmlDoc} = $xmlDoc;
}
1;