Skip to content

Commit

Permalink
detailed access list
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Jul 31, 2024
1 parent 6bf9fcf commit 6f2e329
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/Db/CoreQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public function limitToFileSource(int $nodeId): void {
* @param array $files
*/
public function limitToFileSourceArray(array $files): void {
$this->limitArray('file_source', $files);
$this->limitInArray('file_source', $files, type: IQueryBuilder::PARAM_INT_ARRAY);
}


Expand Down
10 changes: 7 additions & 3 deletions lib/Db/ShareWrapperRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,23 @@ public function getSharesByFileId(int $fileId, bool $getData = false): array {
* @return ShareWrapper[]
* @throws RequestBuilderException
*/
public function getSharesByFileIds(array $fileIds, bool $getData = false): array {
public function getSharesByFileIds(array $fileIds, bool $getData = false, bool $getChild = false): array {
$qb = $this->getShareSelectSql();
$qb->limitToFileSourceArray($fileIds);

if ($getData) {
$qb->setOptions([CoreQueryBuilder::SHARE], ['getData' => $getData]);
$qb->leftJoinCircle(CoreQueryBuilder::SHARE, null, 'share_with');
$qb->limitNull('parent', false);
}

if ($getChild) {
$qb->orderBy('parent', 'asc');
} else {
$qb->limitNull('parent', false);
}
return $this->getItemsFromRequest($qb);
}


/**
* @param FederatedUser $federatedUser
Expand Down
11 changes: 11 additions & 0 deletions lib/Model/ShareWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ShareWrapper extends ManagedModel implements IDeserializable, IQueryRow, J
private ?DateTime $expirationDate = null;
private string $shareOwner = '';
private int $shareType = 0;
private int $parent = 0;
private ?Circle $circle = null;
private int $childId = 0;
private string $childFileTarget = '';
Expand Down Expand Up @@ -232,6 +233,15 @@ public function getShareType(): int {
return $this->shareType;
}

public function setParent(int $parent): self {
$this->parent = $parent;
return $this;
}

public function getParent(): int {
return $this->parent;
}

public function setCircle(Circle $circle): self {
$this->circle = $circle;

Expand Down Expand Up @@ -454,6 +464,7 @@ public function import(array $data): IDeserializable {

$this->setId($this->get('id', $data))
->setShareType($this->getInt('shareType', $data))
->setParent($data['parent'] ?? 0)
->setPermissions($this->getInt('permissions', $data))
->setHideDownload($this->getBool('hideDownload', $data))
->setItemType($this->get('itemType', $data))
Expand Down
4 changes: 2 additions & 2 deletions lib/Service/ShareWrapperService.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ public function getSharesByFileId(int $fileId, bool $getData = false): array {
* @return ShareWrapper[]
* @throws RequestBuilderException
*/
public function getSharesByFileIds(array $fileIds, bool $getData = false): array {
return ($fileIds === []) ? [] : $this->shareWrapperRequest->getSharesByFileIds($fileIds, $getData);
public function getSharesByFileIds(array $fileIds, bool $getData = false, bool $getChild = false): array {
return ($fileIds === []) ? [] : $this->shareWrapperRequest->getSharesByFileIds($fileIds, $getData, $getChild);
}

/**
Expand Down
55 changes: 49 additions & 6 deletions lib/ShareByCircleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,55 @@ public function getAccessList($nodes, $currentAccess): array {
$ids[] = $node->getId();
}

$users = $remote = $mails = [];
$shares = $this->shareWrapperService->getSharesByFileIds($ids, true);

foreach($shares as $share) {
if (!$currentAccess) {
return $this->getAccessListShort($ids);
}

$knownIds = $users = $remote = $mails = [];
foreach ($this->shareWrapperService->getSharesByFileIds($ids, true, true) as $share) {
$circle = $share->getCircle();
foreach ($circle->getInheritedMembers() as $member) {
if ($share->getParent() > 0 && in_array($member->getSingleId(), $knownIds[$share->getFileSource()] ?? [])) {
continue;
}
$knownIds[$share->getFileSource()][] = $member->getSingleId();

switch ($member->getUserType()) {
case Member::TYPE_USER:
if ($member->isLocal()) {
$users[$member->getUserId()] = [
'node_id' => $share->getFileSource(),
'node_path' => $share->getFileTarget()
];
} else {
$remote[$member->getUserid() . '@' . $member->getInstance()] = [
'node_id' => $share->getFileSource(),
'token' => $share->getToken()
];
}
break;
case Member::TYPE_MAIL:
$mails[$member->getUserId()] = [
'node_id' => $share->getFileSource(),
'token' => $share->getToken()
];
break;
}
}
}

return [
'users' => $users,
'remote' => $remote,
'email' => $mails
];
}

private function getAccessListShort(array $ids): array {
$users = $mails = [];
$remote = false;
foreach ($this->shareWrapperService->getSharesByFileIds($ids, true) as $share) {
$circle = $share->getCircle();
foreach ($circle->getInheritedMembers() as $member) {
switch ($member->getUserType()) {
Expand All @@ -650,9 +695,7 @@ public function getAccessList($nodes, $currentAccess): array {
$users[] = $member->getUserId();
}
} else {
if (!in_array($member->getUserId(), $remote)) {
$remote[] = $member->getUserid() . '@' . $member->getInstance();
}
$remote = true;
}
break;
case Member::TYPE_MAIL:
Expand Down
17 changes: 11 additions & 6 deletions lib/Tools/Db/ExtendedQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,10 @@ public function limitArray(string $field, array $value, string $alias = '', bool
* @param string $field
* @param array $value
* @param string $alias
* @param int $type
*/
public function limitInArray(string $field, array $value, string $alias = ''): void {
$this->andWhere($this->exprLimitInArray($field, $value, $alias));
public function limitInArray(string $field, array $value, string $alias = '', int $type = IQueryBuilder::PARAM_STR_ARRAY): void {
$this->andWhere($this->exprLimitInArray($field, $value, $alias, $type));
}

/**
Expand Down Expand Up @@ -520,22 +521,26 @@ public function exprLimitArray(
return $andX;
}


/**
* @param string $field
* @param array $values
* @param string $alias
* @param int $type
*
* @return string
*/
public function exprLimitInArray(string $field, array $values, string $alias = ''): string {
public function exprLimitInArray(
string $field,
array $values,
string $alias = '',
int $type = IQueryBuilder::PARAM_STR_ARRAY
): string {
if ($this->getType() === DBALQueryBuilder::SELECT) {
$field = (($alias === '') ? $this->getDefaultSelectAlias() : $alias) . '.' . $field;
}

$expr = $this->expr();

return $expr->in($field, $this->createNamedParameter($values, IQueryBuilder::PARAM_STR_ARRAY));
return $expr->in($field, $this->createNamedParameter($values, $type));
}


Expand Down

0 comments on commit 6f2e329

Please sign in to comment.