Acl.php 7.3 KB

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