Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better object ACL support #57

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor/cleanup.
  • Loading branch information
mgriego committed May 4, 2017
commit 5c2ffaa4b43c4b12356f5369343f0e028c39a31a
50 changes: 30 additions & 20 deletions src/GoogleStorageAdapter.php
Original file line number Diff line number Diff line change
@@ -222,38 +222,48 @@ protected function simplifyAcl($acl)
return array_combine(array_column($acl, 'entity'), array_column($acl, 'role'));
}

/**
* Update the ACL of the target object to match the source object.
*
* @param $sourceObject
* @param $targetObject
*/
protected function copyAcl($sourceObject, $targetObject)
{
$sourceAcl = $this->simplifyAcl($sourceObject->acl()->get());

$targetAcl = $this->simplifyAcl($targetObject->acl()->get());

foreach (array_keys($targetAcl) as $entity) {
if (!isset($sourceAcl[$entity])) {
$targetObject->acl()->delete($entity);
}
}

foreach ($sourceAcl as $entity => $role) {
if (!isset($targetAcl[$entity])) {
$targetObject->acl()->add($entity, $role);
} elseif ($targetAcl[$entity] != $role) {
$targetObject->acl()->update($entity, $role);
}
}
}

/**
* {@inheritdoc}
*/
public function copy($path, $newpath)
{
$newpath = $this->applyPathPrefix($newpath);

$originalObject = $this->getObject($path);
$originalAcl = $this->simplifyAcl($originalObject->acl()->get());
$sourceObject = $this->getObject($path);

$options = [
'name' => $newpath,
];
$newObject = $originalObject->copy($this->bucket, $options);
$targetObject = $sourceObject->copy($this->bucket, $options);

// Get the ACL that was applied to the new object so that we can modify
// it as needed to match the ACL of the original object.
$newAcl = $this->simplifyAcl($newObject->acl()->get());

foreach (array_keys($newAcl) as $entity) {
if (!isset($originalAcl[$entity])) {
$newObject->acl()->delete($entity);
}
}

foreach ($originalAcl as $entity => $role) {
if (!isset($newAcl[$entity])) {
$newObject->acl()->add($entity, $role);
} elseif ($newAcl[$entity] != $role) {
$newObject->acl()->update($entity, $role);
}
}
$this->copyAcl($sourceObject, $targetObject);

return true;
}