Skip to content

Commit 6e3c6e6

Browse files
committed
add flag to override public access on smart-albums
1 parent c21c9e1 commit 6e3c6e6

4 files changed

Lines changed: 111 additions & 21 deletions

File tree

app/Policies/PhotoQueryPolicy.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,34 @@ public function applySearchabilityFilter(FixedQueryBuilder $query, ?Album $origi
130130
});
131131
}
132132

133+
/**
134+
* Restricts the photo query to only non sensitive photos.
135+
*
136+
* @param FixedQueryBuilder $query
137+
* @param Album|null $origin
138+
*
139+
* @return FixedQueryBuilder
140+
*/
141+
public function applySensitivityFilter(FixedQueryBuilder $query, ?Album $origin = null, bool $include_nsfw = true): FixedQueryBuilder
142+
{
143+
if ($include_nsfw) {
144+
return $query;
145+
}
146+
147+
$this->prepareModelQueryOrFail($query, true, false);
148+
149+
// If origin is set, also restrict the search result for admin
150+
// to photos which are in albums below origin.
151+
// This is not a security filter, but simply functional.
152+
if ($origin !== null) {
153+
$query
154+
->where('albums._lft', '>=', $origin->_lft)
155+
->where('albums._rgt', '<=', $origin->_rgt);
156+
}
157+
158+
return $query->where(fn (Builder $query) => $this->appendSensitivityConditions($query->getQuery(), $origin?->_lft, $origin?->_rgt));
159+
}
160+
133161
/**
134162
* Adds the conditions of _searchable_ photos to the query.
135163
*

app/Relations/HasManyPhotosByTag.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,33 @@ public function addEagerConstraints(array $albums): void
6969
$album = $albums[0];
7070
$tags = $album->show_tags;
7171

72-
$this->photo_query_policy
73-
->applySearchabilityFilter(
74-
$this->getRelationQuery(),
75-
origin: null,
76-
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_smart_albums')
77-
)
78-
->where(function (Builder $q) use ($tags): void {
79-
// Filter for requested tags
80-
foreach ($tags as $tag) {
81-
$q->where('tags', 'like', '%' . trim($tag) . '%');
82-
}
83-
});
72+
if (Configs::getValueAsBool('TA_override_visibility')) {
73+
$this->photo_query_policy
74+
->applySensitivityFilter(
75+
$this->getRelationQuery(),
76+
origin: null,
77+
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_smart_albums')
78+
)
79+
->where(function (Builder $q) use ($tags): void {
80+
// Filter for requested tags
81+
foreach ($tags as $tag) {
82+
$q->where('tags', 'like', '%' . trim($tag) . '%');
83+
}
84+
});
85+
} else {
86+
$this->photo_query_policy
87+
->applySearchabilityFilter(
88+
$this->getRelationQuery(),
89+
origin: null,
90+
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_smart_albums')
91+
)
92+
->where(function (Builder $q) use ($tags): void {
93+
// Filter for requested tags
94+
foreach ($tags as $tag) {
95+
$q->where('tags', 'like', '%' . trim($tag) . '%');
96+
}
97+
});
98+
}
8499
}
85100

86101
/**

app/SmartAlbums/BaseSmartAlbum.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,18 @@ public function get_photos(): Collection
113113
*/
114114
public function photos(): Builder
115115
{
116-
$query = $this->photo_query_policy
117-
->applySearchabilityFilter(
118-
query: Photo::query()->with(['album', 'size_variants', 'statistics']),
119-
origin: null,
120-
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_smart_albums')
121-
)->where($this->smart_photo_condition);
122-
123-
return $query;
116+
$base_query = Photo::query()->with(['album', 'size_variants', 'statistics']);
117+
118+
if (!Configs::getValueAsBool('SA_override_visibility')) {
119+
return $this->photo_query_policy
120+
->applySearchabilityFilter(query: $base_query, origin: null, include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_smart_albums'))
121+
->where($this->smart_photo_condition);
122+
}
123+
124+
// If the smart album visibility override is enabled, we do not need to apply any security filter, as all photos are visible
125+
// in this smart album. We still need to apply the smart album condition, though.
126+
return $this->photo_query_policy->applySensitivityFilter(query: $base_query, origin: null, include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_smart_albums'))
127+
->where($this->smart_photo_condition);
124128
}
125129

126130
/**
@@ -215,4 +219,4 @@ public function setPrivate(): void
215219
$this->public_permissions = null;
216220
$perm->delete();
217221
}
218-
}
222+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
9+
use App\Models\Extensions\BaseConfigMigration;
10+
11+
return new class() extends BaseConfigMigration {
12+
public const CAT = 'Smart Albums';
13+
14+
public function getConfigs(): array
15+
{
16+
return [
17+
[
18+
'key' => 'SA_override_visibility',
19+
'value' => '0',
20+
'cat' => self::CAT,
21+
'type_range' => self::BOOL,
22+
'description' => 'Smart album visibility overrides the photo visibility.',
23+
'details' => '<span class="pi pi-exclamation-triangle text-orange-500"></span> This will make any photos matching the smart album condition visible.',
24+
'is_expert' => true,
25+
'is_secret' => false,
26+
'level' => 0,
27+
'order' => 10,
28+
],
29+
[
30+
'key' => 'TA_override_visibility',
31+
'value' => '0',
32+
'cat' => self::CAT,
33+
'type_range' => self::BOOL,
34+
'description' => 'Tag album visibility overrides the photo visibility.',
35+
'details' => '<span class="pi pi-exclamation-triangle text-orange-500"></span> This will make any photos matching the tag album condition visible.',
36+
'is_expert' => true,
37+
'is_secret' => false,
38+
'level' => 0,
39+
'order' => 11,
40+
],
41+
];
42+
}
43+
};

0 commit comments

Comments
 (0)