|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Simple S3 package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Matecat\SimpleS3\Commands\Handlers; |
|
13
|
|
|
|
|
14
|
|
|
use Aws\ResultInterface; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use Matecat\SimpleS3\Commands\CommandHandler; |
|
17
|
|
|
|
|
18
|
|
|
class SetBucketPolicy extends CommandHandler |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Set policy for a bucket. |
|
22
|
|
|
* For a complete reference: |
|
23
|
|
|
* https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-policy.html?highlight=put%20policy |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $params |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
* @throws Exception |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function handle(array $params = []): bool |
|
31
|
|
|
{ |
|
32
|
1 |
|
$bucketName = $params[ 'bucket' ]; |
|
33
|
1 |
|
$policy = $params[ 'policy' ]; |
|
34
|
|
|
|
|
35
|
1 |
|
$config = [ |
|
36
|
1 |
|
'Bucket' => $bucketName, |
|
37
|
1 |
|
'Policy' => $policy, |
|
38
|
1 |
|
]; |
|
39
|
|
|
|
|
40
|
1 |
|
if (isset($params[ 'access' ])) { |
|
41
|
|
|
$config[ 'ConfirmRemoveSelfBucketAccess' ] = $params[ 'access' ]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
if (isset($params[ 'md5' ])) { |
|
45
|
|
|
$config[ 'ContentMD5' ] = $params[ 'md5' ]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
1 |
|
$policy = $this->client->getConn()->putBucketPolicy($config); |
|
50
|
|
|
|
|
51
|
1 |
|
if (($policy instanceof ResultInterface) and $policy[ '@metadata' ][ 'statusCode' ] === 204) { |
|
52
|
1 |
|
$this->commandHandlerLogger?->log($this, sprintf('Policy was successfully set for bucket \'%s\'', $bucketName)); |
|
53
|
|
|
|
|
54
|
1 |
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->commandHandlerLogger?->log($this, sprintf('Something went wrong while setting policy of bucket \'%s\'', $bucketName), 'warning'); |
|
58
|
|
|
|
|
59
|
|
|
return false; |
|
60
|
|
|
} catch (Exception $e) { |
|
61
|
|
|
$this->commandHandlerLogger?->logExceptionAndReturnFalse($e); |
|
62
|
|
|
|
|
63
|
|
|
throw $e; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array $params |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function validateParams(array $params = []): bool |
|
73
|
|
|
{ |
|
74
|
1 |
|
return ( |
|
75
|
1 |
|
isset($params[ 'bucket' ]) and |
|
76
|
1 |
|
isset($params[ 'policy' ]) |
|
77
|
1 |
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|