Skip to content

Commit c5920fc

Browse files
Merge pull request #58061 from nextcloud/backport/57930/stable33
[stable33] fix(files_sharing): stop ignoring shares without a usergroup entry when filtering by path
2 parents 1cc6c82 + 0e67f6c commit c5920fc

File tree

1 file changed

+52
-9
lines changed

1 file changed

+52
-9
lines changed

lib/private/Share20/DefaultShareProvider.php

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,16 @@ private function _getSharedWith(
860860
/** @var Share[] $shares */
861861
$shares = [];
862862

863+
$nonChildPath = '/';
864+
if ($path !== null) {
865+
$path = str_replace('/' . $userId . '/files', '', $path);
866+
$path = rtrim($path, '/');
867+
868+
if ($path !== '') {
869+
$nonChildPath = $path;
870+
}
871+
}
872+
863873
if ($shareType === IShare::TYPE_USER) {
864874
//Get shares directly with this user
865875
$qb = $this->dbConn->getQueryBuilder();
@@ -893,9 +903,14 @@ private function _getSharedWith(
893903

894904
if ($path !== null) {
895905
if ($forChildren) {
896-
$qb->andWhere($qb->expr()->like('file_target', $qb->createNamedParameter($this->dbConn->escapeLikeParameter($path) . '_%')));
906+
$qb->andWhere(
907+
$qb->expr()->like(
908+
'file_target',
909+
$qb->createNamedParameter($this->dbConn->escapeLikeParameter($path) . '/_%', IQueryBuilder::PARAM_STR),
910+
),
911+
);
897912
} else {
898-
$qb->andWhere($qb->expr()->eq('file_target', $qb->createNamedParameter($path)));
913+
$qb->andWhere($qb->expr()->eq('file_target', $qb->createNamedParameter($nonChildPath, IQueryBuilder::PARAM_STR)));
899914
}
900915
}
901916

@@ -951,14 +966,34 @@ private function _getSharedWith(
951966
}
952967

953968
if ($path !== null) {
954-
$qb->leftJoin('s', 'share', 'sc', $qb->expr()->eq('sc.parent', 's.id'))
955-
->andWhere($qb->expr()->eq('sc.share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP)))
956-
->where($qb->expr()->eq('sc.share_with', $qb->createNamedParameter($userId)));
969+
$onClause = $qb->expr()->andX(
970+
$qb->expr()->eq('sc.parent', 's.id'),
971+
$qb->expr()->eq('sc.share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP)),
972+
$qb->expr()->eq('sc.share_with', $qb->createNamedParameter($userId)),
973+
);
974+
$qb->leftJoin('s', 'share', 'sc', $onClause);
957975

958976
if ($forChildren) {
959-
$qb->andWhere($qb->expr()->like('sc.file_target', $qb->createNamedParameter($this->dbConn->escapeLikeParameter($path) . '_%')));
977+
$childPathTemplate = $this->dbConn->escapeLikeParameter($path) . '/_%';
978+
$qb->andWhere(
979+
$qb->expr()->orX(
980+
$qb->expr()->like('sc.file_target', $qb->createNamedParameter($childPathTemplate)),
981+
$qb->expr()->andX(
982+
$qb->expr()->isNull('sc.file_target'),
983+
$qb->expr()->like('s.file_target', $qb->createNamedParameter($childPathTemplate))
984+
),
985+
),
986+
);
960987
} else {
961-
$qb->andWhere($qb->expr()->eq('sc.file_target', $qb->createNamedParameter($path)));
988+
$qb->andWhere(
989+
$qb->expr()->orX(
990+
$qb->expr()->eq('sc.file_target', $qb->createNamedParameter($nonChildPath, IQueryBuilder::PARAM_STR)),
991+
$qb->expr()->andX(
992+
$qb->expr()->isNull('sc.file_target'),
993+
$qb->expr()->eq('s.file_target', $qb->createNamedParameter($nonChildPath, IQueryBuilder::PARAM_STR)),
994+
),
995+
)
996+
);
962997
}
963998
}
964999

@@ -989,7 +1024,7 @@ private function _getSharedWith(
9891024
/*
9901025
* Resolve all group shares to user specific shares
9911026
*/
992-
$shares = $this->resolveGroupShares($shares2, $userId);
1027+
$shares = $this->resolveGroupShares($shares2, $userId, $path, $forChildren);
9931028
} else {
9941029
throw new BackendError('Invalid backend');
9951030
}
@@ -1105,7 +1140,7 @@ private function createShare($data) {
11051140
* @param $userId
11061141
* @return Share[] The updates shares if no update is found for a share return the original
11071142
*/
1108-
private function resolveGroupShares($shareMap, $userId) {
1143+
private function resolveGroupShares($shareMap, $userId, ?string $path = null, ?bool $forChildren = false) {
11091144
$qb = $this->dbConn->getQueryBuilder();
11101145
$query = $qb->select('*')
11111146
->from('share')
@@ -1119,6 +1154,14 @@ private function resolveGroupShares($shareMap, $userId) {
11191154
if (count($shareMap) === 1) {
11201155
$share = reset($shareMap);
11211156
$query->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())));
1157+
} elseif ($path !== null) {
1158+
if ($forChildren) {
1159+
$query->andWhere($qb->expr()->like('file_target',
1160+
$qb->createNamedParameter($this->dbConn->escapeLikeParameter($path) . '/_%')));
1161+
} else {
1162+
$nonChildPath = $path !== '' ? $path : '/';
1163+
$query->andWhere($qb->expr()->eq('file_target', $qb->createNamedParameter($nonChildPath)));
1164+
}
11221165
}
11231166

11241167
$stmt = $query->executeQuery();

0 commit comments

Comments
 (0)