Skip to content
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

Content policy: Adjust check in to not fetch from database. Search result: Adjust data for policy and route #2935

Merged
merged 5 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sourcecode/hub/app/Http/Requests/ContentFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ private function attachModel(array $hits, bool $forUser, bool $showDrafts): Coll
$eagerLoad = ['users'];
if ($showDrafts) {
$eagerLoad[] = 'latestVersion';
} else {
}
if (!$showDrafts || $forUser) {
$eagerLoad[] = 'latestPublishedVersion';
}

Expand All @@ -331,7 +332,7 @@ private function attachModel(array $hits, bool $forUser, bool $showDrafts): Coll
?? throw new NotFoundHttpException();

$canUse = Gate::allows('use', [$model, $version]);
$canEdit = Gate::allows('edit', $model);
$canEdit = Gate::allows('edit', [$model, $version]);
$canView = Gate::allows('view', $model);
$canDelete = $forUser && Gate::allows('delete', $model);
$canCopy = Gate::allows('copy', $model);
Expand All @@ -349,7 +350,7 @@ private function attachModel(array $hits, bool $forUser, bool $showDrafts): Coll
useUrl: $canUse ? route('content.use', [$model, $version]) : null,
editUrl: $canEdit ? route('content.edit', [$model, $version]) : null,
shareUrl: $canView ? route('content.share', [$model, SessionScope::TOKEN_PARAM => null]) : null,
copyUrl: $canCopy ? route('content.copy', [$model, $version]) : null,
copyUrl: $canCopy ? route('content.copy', [$model]) : null,
deleteUrl: $canDelete ? route('content.delete', [$model]) : null,
);
});
Expand Down
12 changes: 7 additions & 5 deletions sourcecode/hub/app/Policies/ContentPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ public function use(User|null $user, Content $content, ContentVersion $version):
return false;
}

if (!$version->content?->is($content)) {
return false;
}

if (!$version->published) {
return false;
}
Expand All @@ -151,7 +147,13 @@ public function manageRoles(User $user, Content $content): bool

private function ensureVersionBelongsToContent(Content $content, ContentVersion|null $version): void
{
if ($version && !$version->content?->is($content)) {
if ($version && (
$version->content_id !== $content->id ||
$version->exists === false ||
$content->exists === false ||
$version->getConnectionName() !== $content->getConnectionName()
)
) {
throw new LogicException('Version does not belong to content');
}
}
Expand Down