-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathAssetQuery.php
More file actions
232 lines (192 loc) · 7 KB
/
Copy pathAssetQuery.php
File metadata and controls
232 lines (192 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
namespace CraftCms\Cms\Element\Queries;
use CraftCms\Cms\Asset\Elements\Asset;
use CraftCms\Cms\Database\Table;
use CraftCms\Cms\Element\Contracts\ElementInterface;
use CraftCms\Cms\Element\Queries\Concerns\Asset\EagerloadsTransforms;
use CraftCms\Cms\Element\Queries\Concerns\Asset\QueriesAlt;
use CraftCms\Cms\Element\Queries\Concerns\Asset\QueriesAssetLocation;
use CraftCms\Cms\Element\Queries\Concerns\Asset\QueriesAssetProperties;
use CraftCms\Cms\Element\Queries\Concerns\Asset\QueriesSizes;
use CraftCms\Cms\Element\Queries\Exceptions\QueryAbortedException;
use CraftCms\Cms\Support\Arr;
use CraftCms\Cms\Support\Facades\Volumes;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Override;
use Tpetry\QueryExpressions\Language\Alias;
/**
* @extends ElementQuery<Asset>
*/
class AssetQuery extends ElementQuery
{
use EagerloadsTransforms;
use QueriesAlt;
use QueriesAssetLocation;
use QueriesAssetProperties;
use QueriesSizes;
#[Override]
protected string $table = Table::ASSETS;
#[Override]
protected array $defaultOrderBy = [
'assets.dateCreated' => SORT_DESC,
'assets.id' => SORT_DESC,
];
/**
* @var bool|null Whether to only return assets that the user has permission to view.
*
* @used-by editable()
*/
public ?bool $editable = null;
/**
* @var bool|null Whether to only return entries that the user has permission to save.
*
* @used-by savable()
*/
public ?bool $savable = null;
public function __construct(array $config = [])
{
parent::__construct(Asset::class, $config);
$this->query->addSelect([
'assets.volumeId as volumeId',
'assets.folderId as folderId',
'assets.uploaderId as uploaderId',
'assets.filename as filename',
'assets.kind as kind',
'assets.width as width',
'assets.height as height',
'assets.size as size',
'assets.focalPoint as focalPoint',
'assets.keptFile as keptFile',
'assets.dateModified as dateModified',
'assets.mimeType as mimeType',
'assets_sites.alt as siteAlt',
'volumeFolders.path as folderPath',
]);
$this->beforeQuery(function (self $elementQuery) {
$elementQuery->query->leftJoin(new Alias(Table::ASSETS_SITES, 'assets_sites'), function (JoinClause $join) {
$join->on('assets_sites.assetId', '=', 'assets.id')
->whereColumn('assets_sites.siteId', '=', 'elements_sites.siteId');
});
$elementQuery->applyAuthParam($elementQuery->editable, 'viewAssets', 'viewPeerAssets');
$elementQuery->applyAuthParam($elementQuery->savable, 'saveAssets', 'savePeerAssets');
});
}
/**
* Sets the [[$editable]] property.
*
* @uses $editable
*/
public function editable(?bool $value = true): self
{
$this->editable = $value;
return $this;
}
/**
* Sets the [[$savable]] property.
*
* @uses $savable
*/
public function savable(?bool $value = true): self
{
$this->savable = $value;
return $this;
}
private function applyAuthParam(?bool $value, string $permissionPrefix, string $peerPermissionPrefix): void
{
if ($value === null) {
return;
}
$user = Auth::craftUser();
if (! $user) {
throw new QueryAbortedException;
}
$fullyAuthorizedVolumeIds = [];
$partiallyAuthorizedVolumeIds = [];
$unauthorizedVolumeIds = [];
foreach (Volumes::getAllVolumes() as $volume) {
if ($user->can("$peerPermissionPrefix:$volume->uid")) {
$fullyAuthorizedVolumeIds[] = $volume->id;
} elseif ($user->can("$permissionPrefix:$volume->uid")) {
$partiallyAuthorizedVolumeIds[] = $volume->id;
} else {
$unauthorizedVolumeIds[] = $volume->id;
}
}
if ($value) {
if (! $fullyAuthorizedVolumeIds && ! $partiallyAuthorizedVolumeIds) {
throw new QueryAbortedException;
}
$userId = $user->getCraftUserId();
$this->where(function (Builder $query) use ($userId, $fullyAuthorizedVolumeIds, $partiallyAuthorizedVolumeIds) {
if ($fullyAuthorizedVolumeIds) {
$query->orWhereIn('assets.volumeId', $fullyAuthorizedVolumeIds);
}
if ($partiallyAuthorizedVolumeIds) {
$query->orWhere(fn (Builder $query) => $query
->whereIn('assets.volumeId', $partiallyAuthorizedVolumeIds)
->where('assets.uploaderId', $userId),
);
}
});
return;
}
if (! $unauthorizedVolumeIds && ! $partiallyAuthorizedVolumeIds) {
throw new QueryAbortedException;
}
$userId = $user->getCraftUserId();
$this->where(function (Builder $query) use ($userId, $unauthorizedVolumeIds, $partiallyAuthorizedVolumeIds) {
if ($unauthorizedVolumeIds) {
$query->orWhereIn('assets.volumeId', $unauthorizedVolumeIds);
}
if ($partiallyAuthorizedVolumeIds) {
$query->orWhere(function (Builder $query) use ($userId, $partiallyAuthorizedVolumeIds) {
$query->whereIn('assets.volumeId', $partiallyAuthorizedVolumeIds)
->where(function (Builder $query) use ($userId) {
$query->where('assets.uploaderId', '!=', $userId)
->orWhereNull('assets.uploaderId');
});
});
}
});
}
#[Override]
public function createElement(array $row): ElementInterface
{
// Use the site-specific alt text
$siteAlt = Arr::pull($row, 'siteAlt');
$row['alt'] = $siteAlt;
return parent::createElement($row);
}
#[Override]
protected function cacheTags(): array
{
$tags = [];
if ($this->volumeId && $this->volumeId !== ':empty:') {
foreach ($this->volumeId as $volumeId) {
$tags[] = "volume:$volumeId";
}
}
return $tags;
}
#[Override]
protected function fieldLayouts(): Collection
{
if (! $this->volumeId) {
return parent::fieldLayouts();
}
if ($this->volumeId === ':empty:') {
return parent::fieldLayouts();
}
$fieldLayouts = [];
foreach (Arr::wrap($this->volumeId) as $volumeId) {
if ($volume = Volumes::getVolumeById((int) $volumeId)) {
$fieldLayouts[] = $volume->getFieldLayout();
}
}
return new Collection($fieldLayouts);
}
}