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