-
Notifications
You must be signed in to change notification settings - Fork 87
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
ACL Cache #883
Draft
Deltachaos
wants to merge
3
commits into
nextcloud:master
Choose a base branch
from
netzbegruenung:cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
ACL Cache #883
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
<?php | ||
|
||
namespace OCA\GroupFolders\ACL; | ||
|
||
use OCA\GroupFolders\ACL\UserMapping\IUserMapping; | ||
use OCA\GroupFolders\ACL\UserMapping\IUserMappingManager; | ||
use OCA\GroupFolders\ACL\UserMapping\UserMapping; | ||
use OCP\ICache; | ||
use OCP\ICacheFactory; | ||
use OCP\IUser; | ||
|
||
class ACLRuleCache | ||
{ | ||
const DERIVATION = 0.1; | ||
|
||
const DEFAULT_TTL = 3600 * 24; | ||
|
||
const INDEX_TTL = (3600 * 24) + 3600; | ||
|
||
/** | ||
* @var ICache | ||
*/ | ||
private $ruleCache; | ||
|
||
/** | ||
* @var IUserMappingManager | ||
*/ | ||
protected $userMappingManager; | ||
|
||
public function __construct(ICacheFactory $cacheFactory, IUserMappingManager $userMappingManager) { | ||
$this->userMappingManager = $userMappingManager; | ||
$this->ruleCache = $cacheFactory->createDistributed('acl'); | ||
} | ||
|
||
protected function set($key, $value, $ttl = null) : void | ||
{ | ||
if ($ttl === null) { | ||
$ttl = self::DEFAULT_TTL; | ||
} | ||
|
||
$ttl = rand($ttl, round($ttl * self::DERIVATION)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks wrong, this currently select a ttl between 0.1 days and 1 days, while I would expect a derivation of 0.1 to mean something like 0.9 days to 1 day |
||
|
||
$this->ruleCache->set($key, $value, $ttl); | ||
} | ||
|
||
protected function has($key) : bool | ||
{ | ||
return $this->ruleCache->hasKey($key); | ||
} | ||
|
||
protected function get($key) | ||
{ | ||
return $this->ruleCache->get($key); | ||
} | ||
|
||
protected function remove($key) : void | ||
{ | ||
$this->ruleCache->remove($key); | ||
} | ||
|
||
protected function keyPath(int $storageId, string $path) : string | ||
{ | ||
return 'path_' . $storageId . '_' . $path; | ||
} | ||
|
||
protected function keyFileId(int $fileId) : string | ||
{ | ||
return 'file_' . $fileId; | ||
} | ||
|
||
protected function keyPathMapping(int $storageId, string $path, IUserMapping $mapping) : string | ||
{ | ||
return $this->keyPath($storageId, $path) . '_' . $mapping->getType() . '_' . $mapping->getId(); | ||
} | ||
|
||
public function clearByPathKey(string $pathKey) : void | ||
{ | ||
$keys = $this->get($pathKey); | ||
if (!isset($keys['rule_path_keys'])) { | ||
$keys['rule_path_keys'] = []; | ||
} | ||
if (!isset($keys['rule_id_keys'])) { | ||
$keys['rule_id_keys'] = []; | ||
} | ||
|
||
foreach (array_merge($keys['rule_path_keys'], $keys['rule_id_keys']) as $key) { | ||
$this->remove($key); | ||
} | ||
} | ||
|
||
public function clearByPath(int $storageId, string $path) : void | ||
{ | ||
$pathKey = $this->keyPath($storageId, $path); | ||
if (!$this->has($pathKey)) { | ||
return; | ||
} | ||
|
||
$this->clearByPathKey($pathKey); | ||
} | ||
|
||
public function clearByFileId(int $fileId) : void | ||
{ | ||
$fileIdKey = $this->keyFileId($fileId); | ||
if (!$this->has($fileIdKey)) { | ||
return; | ||
} | ||
|
||
$this->clearByPathKey($this->get($fileIdKey)); | ||
} | ||
|
||
/** | ||
* @return Rule[]|null | ||
*/ | ||
public function getByPath(IUser $user, int $storageId, string $path) : ?array | ||
{ | ||
return $this->getByPathKey($user, $this->keyPath($storageId, $path)); | ||
} | ||
|
||
/** | ||
* @param string[] $paths | ||
* @return (Rule[]|null)[] | ||
*/ | ||
public function getByPaths(IUser $user, int $storageId, array $paths) : array | ||
{ | ||
$rulePaths = []; | ||
|
||
foreach ($paths as $path) { | ||
$cached = $this->getByPath($user, $storageId, $path); | ||
if ($cached !== null) { | ||
$rulePaths[$path] = $cached; | ||
} | ||
} | ||
|
||
return $rulePaths; | ||
} | ||
|
||
/** | ||
* @return Rule[]|null | ||
*/ | ||
public function getByFileId(IUser $user, int $fileId) : ?array | ||
{ | ||
$fileIdKey = $this->keyFileId($fileId); | ||
if (!$this->has($fileIdKey)) { | ||
return null; | ||
} | ||
|
||
$cached = $this->getByPathKey($user, $this->get($fileIdKey)); | ||
if ($cached === null) { | ||
return null; | ||
} | ||
|
||
$rules = []; | ||
|
||
foreach ($cached as $rule) { | ||
if ($rule->getFileId() == $fileIdKey) { | ||
$rules[] = $rule; | ||
} | ||
} | ||
|
||
return $rules; | ||
} | ||
|
||
/** | ||
* @param (Rule[])[] $paths | ||
*/ | ||
|
||
public function cachePath(int $storageId, array $paths) : void | ||
{ | ||
foreach ($paths as $path => $rules) { | ||
$this->cacheRules($storageId, $path, $rules); | ||
} | ||
} | ||
|
||
/** | ||
* @param Rule[] $rules | ||
*/ | ||
public function cacheRules(int $storageId, string $path, array $rules) : void | ||
{ | ||
$rulePathKeys = []; | ||
$fileIdKeys = []; | ||
|
||
foreach ($rules as $rule) { | ||
$rulePathKeys[] = $rulePathKey = $this->keyPathMapping($storageId, $path, $rule->getUserMapping()); | ||
$fileIdKeys[] = $ruleFileIdKey = $this->keyFileId($rule->getFileId()); | ||
$this->set($rulePathKey, array_merge($rule->jsonSerialize(), [ | ||
'file_id' => $rule->getFileId() | ||
])); | ||
} | ||
|
||
|
||
$pathKey = $this->keyPath($storageId, $path); | ||
$this->set($pathKey, [ | ||
'rule_path_keys' => $rulePathKeys, | ||
'rule_id_keys' => $fileIdKeys | ||
], self::INDEX_TTL); | ||
foreach ($fileIdKeys as $fileIdKey) { | ||
$this->set($fileIdKey, $pathKey, self::INDEX_TTL); | ||
} | ||
} | ||
|
||
/** | ||
* @param IUserMapping[] $userMappings | ||
* @param Rule $rule | ||
* @return bool | ||
*/ | ||
protected function checkMappingInRule(array $userMappings, Rule $rule) : bool | ||
{ | ||
foreach ($userMappings as $userMapping) { | ||
if ($userMapping->getId() == $rule->getUserMapping()->getId() && | ||
$userMapping->getType() == $rule->getUserMapping()->getType()) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param IUser $user | ||
* @param string $pathKey | ||
* @return Rule[]|null | ||
*/ | ||
protected function getByPathKey(IUser $user, string $pathKey) : ?array | ||
{ | ||
if (!$this->has($pathKey)) { | ||
return null; | ||
} | ||
|
||
$keys = $this->get($pathKey); | ||
if (!isset($keys['rule_path_keys'])) { | ||
return null; | ||
} | ||
|
||
$userMappings = $this->userMappingManager->getMappingsForUser($user); | ||
|
||
$rules = []; | ||
|
||
foreach ($keys['rule_path_keys'] as $key) { | ||
if (!$this->has($key)) { | ||
return null; | ||
} | ||
|
||
$ruleData = $this->get($key); | ||
|
||
$rule = new Rule( | ||
new UserMapping($ruleData['mapping']['type'], $ruleData['mapping']['id']), | ||
$ruleData['file_id'], | ||
$ruleData['mask'], | ||
$ruleData['permissions'] | ||
); | ||
|
||
if ($this->checkMappingInRule($userMappings, $rule)) { | ||
$rules[] = $rule; | ||
} | ||
} | ||
|
||
return $rules; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a bit safer in terms of naming collisions.