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 Matecat\SimpleS3\Commands\CommandHandler; |
16
|
|
|
|
17
|
|
|
class SetBucketPolicy extends CommandHandler |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Set policy for a bucket. |
21
|
|
|
* For a complete reference: |
22
|
|
|
* https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-policy.html?highlight=put%20policy |
23
|
|
|
* |
24
|
|
|
* @param mixed $params |
25
|
|
|
* |
26
|
|
|
* @return bool |
27
|
|
|
* @throws \Exception |
28
|
|
|
*/ |
29
|
|
|
public function handle($params = []) |
30
|
|
|
{ |
31
|
|
|
$bucketName = $params['bucket']; |
32
|
|
|
$policy = $params['policy']; |
33
|
|
|
|
34
|
|
|
$config = [ |
35
|
|
|
'Bucket' => $bucketName, |
36
|
|
|
'Policy' => $policy, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
if (isset($params['access'])) { |
40
|
|
|
$config['ConfirmRemoveSelfBucketAccess'] = $params['access']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (isset($params['md5'])) { |
44
|
|
|
$config['ContentMD5'] = $params['md5']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
$policy = $this->client->getConn()->putBucketPolicy($config); |
49
|
|
|
|
50
|
|
|
if (($policy instanceof ResultInterface) and $policy['@metadata']['statusCode'] === 204) { |
51
|
|
|
if (null !== $this->commandHandlerLogger) { |
52
|
|
|
$this->commandHandlerLogger->log($this, sprintf('Policy was successfully set for bucket \'%s\'', $bucketName)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (null !== $this->commandHandlerLogger) { |
59
|
|
|
$this->commandHandlerLogger->log($this, sprintf('Something went wrong while setting policy of bucket \'%s\'', $bucketName), 'warning'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return false; |
63
|
|
|
} catch (\Exception $e) { |
64
|
|
|
if (null !== $this->commandHandlerLogger) { |
65
|
|
|
$this->commandHandlerLogger->logExceptionAndReturnFalse($e); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
throw $e; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $params |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function validateParams($params = []) |
78
|
|
|
{ |
79
|
|
|
return ( |
80
|
|
|
isset($params['bucket']) and |
81
|
|
|
isset($params['policy']) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|