Skip to content

Commit 03d5476

Browse files
feat: [Storage] samples for printing bucket ACLs (GoogleCloudPlatform#1616)
1 parent 533a5d1 commit 03d5476

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
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+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_print_bucket_acl_for_user]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Print an entity role for a bucket ACL.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* @param string $entity The entity for which to query access controls.
34+
*/
35+
function print_bucket_acl_for_user(
36+
string $bucketName,
37+
string $entity
38+
): void {
39+
// $bucketName = 'my-bucket';
40+
// $entity = '[email protected]';
41+
42+
$storage = new StorageClient();
43+
$bucket = $storage->bucket($bucketName);
44+
$acl = $bucket->acl();
45+
46+
$item = $acl->get(['entity' => $entity]);
47+
printf('%s: %s' . PHP_EOL, $item['entity'], $item['role']);
48+
}
49+
# [END storage_print_bucket_acl_for_user]
50+
51+
// The following 2 lines are only needed to run the samples
52+
require_once __DIR__ . '/../../testing/sample_helpers.php';
53+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
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+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_print_file_acl_for_user]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Print an entity role for a file ACL.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* @param string $objectName The name of your Cloud Storage object.
34+
* @param string $entity The entity for which to query access controls.
35+
*/
36+
function print_file_acl_for_user(
37+
string $bucketName,
38+
string $objectName,
39+
string $entity
40+
): void {
41+
// $bucketName = 'my-bucket';
42+
// $objectName = 'my-object';
43+
// $entity = '[email protected]';
44+
45+
$storage = new StorageClient();
46+
$bucket = $storage->bucket($bucketName);
47+
$object = $bucket->object($objectName);
48+
$acl = $object->acl();
49+
$item = $acl->get(['entity' => $entity]);
50+
printf('%s: %s' . PHP_EOL, $item['entity'], $item['role']);
51+
}
52+
# [END storage_print_file_acl_for_user]
53+
54+
// The following 2 lines are only needed to run the samples
55+
require_once __DIR__ . '/../../testing/sample_helpers.php';
56+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/test/ObjectAclTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,72 @@ public function testManageObjectAcl()
9797
allAuthenticatedUsers: READER
9898
Deleted allAuthenticatedUsers from $objectUrl ACL
9999
100+
EOF;
101+
$this->assertEquals($output, $outputString);
102+
}
103+
104+
public function testPrintFileAclForUser()
105+
{
106+
$objectName = $this->requireEnv('GOOGLE_STORAGE_OBJECT');
107+
108+
$bucket = self::$storage->bucket(self::$bucketName);
109+
$object = $bucket->object($objectName);
110+
$acl = $object->acl();
111+
112+
$output = self::runFunctionSnippet('add_object_acl', [
113+
self::$bucketName,
114+
$objectName,
115+
'allAuthenticatedUsers',
116+
'READER',
117+
]);
118+
119+
$aclInfo = $acl->get(['entity' => 'allAuthenticatedUsers']);
120+
$this->assertArrayHasKey('role', $aclInfo);
121+
$this->assertEquals('READER', $aclInfo['role']);
122+
123+
$output .= self::runFunctionSnippet('print_file_acl_for_user', [
124+
self::$bucketName,
125+
$objectName,
126+
'allAuthenticatedUsers',
127+
]);
128+
129+
$objectUrl = sprintf('gs://%s/%s', self::$bucketName, $objectName);
130+
$outputString = <<<EOF
131+
Added allAuthenticatedUsers (READER) to $objectUrl ACL
132+
allAuthenticatedUsers: READER
133+
134+
EOF;
135+
$this->assertEquals($output, $outputString);
136+
}
137+
138+
public function testPrintBucketAclForUser()
139+
{
140+
$objectName = $this->requireEnv('GOOGLE_STORAGE_OBJECT');
141+
142+
$bucket = self::$storage->bucket(self::$bucketName);
143+
$object = $bucket->object($objectName);
144+
$acl = $object->acl();
145+
146+
$output = self::runFunctionSnippet('add_bucket_acl', [
147+
self::$bucketName,
148+
'allAuthenticatedUsers',
149+
'READER',
150+
]);
151+
152+
$aclInfo = $acl->get(['entity' => 'allAuthenticatedUsers']);
153+
$this->assertArrayHasKey('role', $aclInfo);
154+
$this->assertEquals('READER', $aclInfo['role']);
155+
156+
$output .= self::runFunctionSnippet('print_bucket_acl_for_user', [
157+
self::$bucketName,
158+
'allAuthenticatedUsers',
159+
]);
160+
161+
$bucketUrl = sprintf('gs://%s', self::$bucketName);
162+
$outputString = <<<EOF
163+
Added allAuthenticatedUsers (READER) to $bucketUrl ACL
164+
allAuthenticatedUsers: READER
165+
100166
EOF;
101167
$this->assertEquals($output, $outputString);
102168
}

0 commit comments

Comments
 (0)