| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright 2015 Google Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
namespace Google\Cloud\Storage; |
| 19 |
|
| 20 |
use Google\Cloud\Storage\Connection\ConnectionInterface; |
| 21 |
use InvalidArgumentException; |
| 22 |
|
| 23 |
/** |
| 24 |
* Google Cloud Storage uses access control lists (ACLs) to manage bucket and |
| 25 |
* object access. ACLs are the mechanism you use to share objects with other |
| 26 |
* users and allow other users to access your buckets and objects. For more |
| 27 |
* information please see the overview on |
| 28 |
* [access-control](https://cloud.google.com/storage/docs/access-control). |
| 29 |
* |
| 30 |
* Example: |
| 31 |
* ``` |
| 32 |
* use Google\Cloud\Storage\StorageClient; |
| 33 |
* |
| 34 |
* $storage = new StorageClient(); |
| 35 |
* |
| 36 |
* $bucket = $storage->bucket('my-bucket'); |
| 37 |
* $acl = $bucket->acl(); |
| 38 |
* ``` |
| 39 |
*/ |
| 40 |
class Acl |
| 41 |
{ |
| 42 |
const ROLE_READER = 'READER'; |
| 43 |
const ROLE_WRITER = 'WRITER'; |
| 44 |
const ROLE_OWNER = 'OWNER'; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var ConnectionInterface Represents a connection to Cloud Storage. |
| 48 |
*/ |
| 49 |
protected $connection; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var array ACL specific options. |
| 53 |
*/ |
| 54 |
private $aclOptions; |
| 55 |
|
| 56 |
/** |
| 57 |
* @param ConnectionInterface $connection Represents a connection to |
| 58 |
* Cloud Storage. |
| 59 |
* @param string $type The type of access control this instance applies to. |
| 60 |
* @param array $identity Represents which bucket, file, or generation this |
| 61 |
* instance applies to. |
| 62 |
* @throws InvalidArgumentException Thrown when an invalid type is passed in. |
| 63 |
*/ |
| 64 |
public function __construct(ConnectionInterface $connection, $type, array $identity) |
| 65 |
{ |
| 66 |
$validTypes = [ |
| 67 |
'bucketAccessControls', |
| 68 |
'defaultObjectAccessControls', |
| 69 |
'objectAccessControls' |
| 70 |
]; |
| 71 |
|
| 72 |
if (!in_array($type, $validTypes)) { |
| 73 |
throw new InvalidArgumentException('type must be one of the following: ' . implode(', ', $validTypes)); |
| 74 |
} |
| 75 |
|
| 76 |
$this->connection = $connection; |
| 77 |
$this->aclOptions = $identity + ['type' => $type]; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Delete access controls on a {@see Google\Cloud\Storage\Bucket} or |
| 82 |
* {@see Google\Cloud\Storage\StorageObject} for a specified entity. |
| 83 |
* |
| 84 |
* Example: |
| 85 |
* ``` |
| 86 |
* $acl->delete('allAuthenticatedUsers'); |
| 87 |
* ``` |
| 88 |
* |
| 89 |
* @see https://cloud.google.com/storage/docs/json_api/v1/bucketAccessControls/delete BucketAccessControls delete |
| 90 |
* API documentation. |
| 91 |
* @see https://cloud.google.com/storage/docs/json_api/v1/defaultObjectAccessControls/delete |
| 92 |
* DefaultObjectAccessControls delete API documentation. |
| 93 |
* @see https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls/delete ObjectAccessControls delete |
| 94 |
* API documentation. |
| 95 |
* |
| 96 |
* @param string $entity The entity to delete. |
| 97 |
* @param array $options [optional] Configuration Options. |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
public function delete($entity, array $options = []) |
| 101 |
{ |
| 102 |
$aclOptions = $this->aclOptions + ['entity' => $entity]; |
| 103 |
$this->connection->deleteAcl($options + $aclOptions); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get access controls on a {@see Google\Cloud\Storage\Bucket} or |
| 108 |
* {@see Google\Cloud\Storage\StorageObject}. By default this will return all available |
| 109 |
* access controls. You may optionally specify a single entity to return |
| 110 |
* details for as well. |
| 111 |
* |
| 112 |
* Example: |
| 113 |
* ``` |
| 114 |
* $res = $acl->get(['entity' => 'allAuthenticatedUsers']); |
| 115 |
* ``` |
| 116 |
* |
| 117 |
* @see https://cloud.google.com/storage/docs/json_api/v1/bucketAccessControls/get BucketAccessControls get API |
| 118 |
* documentation. |
| 119 |
* @see https://cloud.google.com/storage/docs/json_api/v1/defaultObjectAccessControls/get |
| 120 |
* DefaultObjectAccessControls get API documentation. |
| 121 |
* @see https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls/get ObjectAccessControls get API |
| 122 |
* documentation. |
| 123 |
* |
| 124 |
* @param array $options [optional] { |
| 125 |
* Configuration options. |
| 126 |
* |
| 127 |
* @type string $entity The entity to fetch. |
| 128 |
* } |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
public function get(array $options = []) |
| 132 |
{ |
| 133 |
if (isset($options['entity'])) { |
| 134 |
return $this->connection->getAcl($options + $this->aclOptions); |
| 135 |
} |
| 136 |
|
| 137 |
$response = $this->connection->listAcl($options + $this->aclOptions); |
| 138 |
return $response['items']; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Add access controls on a {@see Google\Cloud\Storage\Bucket} or |
| 143 |
* {@see Google\Cloud\Storage\StorageObject}. |
| 144 |
* |
| 145 |
* Example: |
| 146 |
* ``` |
| 147 |
* $acl->add('allAuthenticatedUsers', 'WRITER'); |
| 148 |
* ``` |
| 149 |
* |
| 150 |
* @see https://cloud.google.com/storage/docs/json_api/v1/bucketAccessControls/insert BucketAccessControls insert |
| 151 |
* API documentation. |
| 152 |
* @see https://cloud.google.com/storage/docs/json_api/v1/defaultObjectAccessControls/insert |
| 153 |
* DefaultObjectAccessControls insert API documentation. |
| 154 |
* @see https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls/insert ObjectAccessControls insert |
| 155 |
* API documentation. |
| 156 |
* |
| 157 |
* @param string $entity The entity to add access controls to. |
| 158 |
* @param string $role The permissions to add for the specified entity. May |
| 159 |
* be one of 'OWNER', 'READER', or 'WRITER'. |
| 160 |
* @param array $options [optional] Configuration Options. |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
public function add($entity, $role, array $options = []) |
| 164 |
{ |
| 165 |
$aclOptions = $this->aclOptions + [ |
| 166 |
'entity' => $entity, |
| 167 |
'role' => $role |
| 168 |
]; |
| 169 |
|
| 170 |
return $this->connection->insertAcl($options + $aclOptions); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Update access controls on a {@see Google\Cloud\Storage\Bucket} or |
| 175 |
* {@see Google\Cloud\Storage\StorageObject}. |
| 176 |
* |
| 177 |
* Example: |
| 178 |
* ``` |
| 179 |
* $acl->update('allAuthenticatedUsers', 'READER'); |
| 180 |
* ``` |
| 181 |
* |
| 182 |
* @see https://cloud.google.com/storage/docs/json_api/v1/bucketAccessControls/patch BucketAccessControls patch API |
| 183 |
* documentation. |
| 184 |
* @see https://cloud.google.com/storage/docs/json_api/v1/defaultObjectAccessControls/patch |
| 185 |
* DefaultObjectAccessControls patch API documentation. |
| 186 |
* @see https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls/patch ObjectAccessControls patch |
| 187 |
* API documentation. |
| 188 |
* |
| 189 |
* @param string $entity The entity to update access controls for. |
| 190 |
* @param string $role The permissions to update for the specified entity. |
| 191 |
* May be one of 'OWNER', 'READER', or 'WRITER'. |
| 192 |
* @param array $options [optional] Configuration Options. |
| 193 |
* @return array |
| 194 |
*/ |
| 195 |
public function update($entity, $role, array $options = []) |
| 196 |
{ |
| 197 |
$aclOptions = $this->aclOptions + [ |
| 198 |
'entity' => $entity, |
| 199 |
'role' => $role |
| 200 |
]; |
| 201 |
|
| 202 |
return $this->connection->patchAcl($options + $aclOptions); |
| 203 |
} |
| 204 |
} |
| 205 |
|