Grantee.php
6.13 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
namespace Aws\S3\Model;
use Aws\S3\Enum\Group;
use Aws\S3\Enum\GranteeType;
use Aws\Common\Exception\InvalidArgumentException;
use Aws\Common\Exception\UnexpectedValueException;
use Aws\Common\Exception\LogicException;
use Guzzle\Common\ToArrayInterface;
/**
* Amazon S3 Grantee model
*/
class Grantee implements ToArrayInterface
{
/**
* @var array A map of grantee types to grant header value prefixes
*/
protected static $headerMap = array(
GranteeType::USER => 'id',
GranteeType::EMAIL => 'emailAddress',
GranteeType::GROUP => 'uri'
);
/**
* @var string The account ID, email, or URL identifying the grantee
*/
protected $id;
/**
* @var string The display name of the grantee
*/
protected $displayName;
/**
* @var string The type of the grantee (CanonicalUser or Group)
*/
protected $type;
/**
* Constructs a Grantee
*
* @param string $id Grantee identifier
* @param string $displayName Grantee display name
* @param string $expectedType The expected type of the grantee
*/
public function __construct($id, $displayName = null, $expectedType = null)
{
$this->type = GranteeType::USER;
$this->setId($id, $expectedType);
$this->setDisplayName($displayName);
}
/**
* Sets the account ID, email, or URL identifying the grantee
*
* @param string $id Grantee identifier
* @param string $expectedType The expected type of the grantee
*
* @return Grantee
*
* @throws UnexpectedValueException if $expectedType is set and the grantee
* is not of that type after instantiation
* @throws InvalidArgumentException when the ID provided is not a string
*/
public function setId($id, $expectedType = null)
{
if (in_array($id, Group::values())) {
$this->type = GranteeType::GROUP;
} elseif (!is_string($id)) {
throw new InvalidArgumentException('The grantee ID must be provided as a string value.');
}
if (strpos($id, '@') !== false) {
$this->type = GranteeType::EMAIL;
}
if ($expectedType && $expectedType !== $this->type) {
throw new UnexpectedValueException('The type of the grantee after '
. 'setting the ID did not match the specified, expected type "'
. $expectedType . '" but received "' . $this->type . '".');
}
$this->id = $id;
return $this;
}
/**
* Gets the grantee identifier
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Gets the grantee email address (if it is set)
*
* @return null|string
*/
public function getEmailAddress()
{
return $this->isAmazonCustomerByEmail() ? $this->id : null;
}
/**
* Gets the grantee URI (if it is set)
*
* @return null|string
*/
public function getGroupUri()
{
return $this->isGroup() ? $this->id : null;
}
/**
* Sets the display name of the grantee
*
* @param string $displayName Grantee name
*
* @return Grantee
*
* @throws LogicException when the grantee type not CanonicalUser
*/
public function setDisplayName($displayName)
{
if ($this->type === GranteeType::USER) {
if (empty($displayName) || !is_string($displayName)) {
$displayName = $this->id;
}
$this->displayName = $displayName;
} else {
if ($displayName) {
throw new LogicException('The display name can only be set '
. 'for grantees specified by ID.');
}
}
return $this;
}
/**
* Gets the grantee display name
*
* @return string
*/
public function getDisplayName()
{
return $this->displayName;
}
/**
* Gets the grantee type (determined by ID)
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Returns true if this grantee object represents a canonical user by ID
*
* @return bool
*/
public function isCanonicalUser()
{
return ($this->type === GranteeType::USER);
}
/**
* Returns true if this grantee object represents a customer by email
*
* @return bool
*/
public function isAmazonCustomerByEmail()
{
return ($this->type === GranteeType::EMAIL);
}
/**
* Returns true if this grantee object represents a group by URL
*
* @return bool
*/
public function isGroup()
{
return ($this->type === GranteeType::GROUP);
}
/**
* Returns the value used in headers to specify this grantee
*
* @return string
*/
public function getHeaderValue()
{
$key = static::$headerMap[$this->type];
return "{$key}=\"{$this->id}\"";
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$result = array(
'Type' => $this->type
);
switch ($this->type) {
case GranteeType::USER:
$result['ID'] = $this->id;
$result['DisplayName'] = $this->displayName;
break;
case GranteeType::EMAIL:
$result['EmailAddress'] = $this->id;
break;
case GranteeType::GROUP:
$result['URI'] = $this->id;
}
return $result;
}
}