Skip to content
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
5 changes: 4 additions & 1 deletion backend/src/Models/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ public static function slugify(string $name): string
/**
* Shared SELECT with entry/view counts and avatar/header image metadata.
* GROUP BY c.id required (tag join fans out).
* Only entries authored by the collection owner count toward entry_count,
* matching the claim join in Entry::COLLECTION_CLAIM_JOIN_SQL.
*/
private const COLLECTION_SELECT =
"SELECT c.*,
COUNT(DISTINCT et.entry_id) AS entry_count,
COUNT(DISTINCT ec.id) AS entry_count,
COALESCE(vc.view_count, 0) AS view_count,
av.filename AS avatar_filename,
av.user_id AS avatar_user_id,
Expand All @@ -68,6 +70,7 @@ public static function slugify(string $name): string
FROM " . self::TABLE . " c
LEFT JOIN trail_collection_tags ct ON ct.collection_id = c.id
LEFT JOIN trail_entry_tags et ON et.tag_id = ct.tag_id
LEFT JOIN trail_entries ec ON ec.id = et.entry_id AND ec.user_id = c.owner_user_id
LEFT JOIN trail_view_counts vc ON vc.target_type = 'collection' AND vc.target_id = c.id
LEFT JOIN trail_images av ON av.id = c.avatar_image_id
LEFT JOIN trail_images hd ON hd.id = c.header_image_id";
Expand Down
8 changes: 6 additions & 2 deletions backend/src/Models/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ class Entry
/**
* Shared claim join: each entry's claimed collection (lowest collection_id wins)
* plus the resolved collection columns. Used by getAll/searchAll/searchByTagSlug.
*
* Only the collection owner's own entries are claimed: the ownership check sits
* in the collection join so entries by other registered accounts resolve to
* col = NULL and keep their poster attribution.
*/
public const COLLECTION_CLAIM_JOIN_SQL = " LEFT JOIN (
SELECT et.entry_id, MIN(ct.collection_id) AS collection_id
FROM trail_entry_tags et
JOIN trail_collection_tags ct ON ct.tag_id = et.tag_id
GROUP BY et.entry_id
) claimed ON claimed.entry_id = e.id
LEFT JOIN trail_collections col ON col.id = claimed.collection_id";
LEFT JOIN trail_collections col ON col.id = claimed.collection_id AND col.owner_user_id = e.user_id";

public function __construct(PDO $db)
{
Expand Down Expand Up @@ -1005,7 +1009,7 @@ public function searchAll(?string $searchQuery = null, int $limit = 50, ?string
WHERE ct.collection_id = ?
GROUP BY et.entry_id
) claimed ON claimed.entry_id = e.id
LEFT JOIN trail_collections col ON col.id = claimed.collection_id";
INNER JOIN trail_collections col ON col.id = claimed.collection_id AND col.owner_user_id = e.user_id";
$params[] = $collectionId;
}

Expand Down
78 changes: 78 additions & 0 deletions backend/tests/Integration/CollectionsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,84 @@ public function testCollectionAttributionEndToEnd(): void
}
}

/**
* Tag-based attribution is scoped to the collection owner (the admin
* account). An entry posted by another registered account that carries the
* same tag must stay attributed to its poster and must not show up in the
* collection's entry list or count.
*
* Requires TRAIL_ADMIN_API_TOKEN (the collection owner) and
* TRAIL_USER_API_TOKEN (any non-admin account) in the env.
*/
public function testNonOwnerEntriesAreNotClaimed(): void
{
$adminToken = getenv('TRAIL_ADMIN_API_TOKEN') ?: null;
$userToken = getenv('TRAIL_USER_API_TOKEN') ?: null;
if ($adminToken === null || $userToken === null) {
$this->markTestSkipped('TRAIL_ADMIN_API_TOKEN and TRAIL_USER_API_TOKEN must both be set.');
}

$suffix = str_replace('.', '-', uniqid());
$tagName = 'it-nc-' . $suffix;
$slug = 'it-nc-' . $suffix;

$entryId = null;
$tagId = null;
$collectionId = null;

try {
// Non-admin posts and tags an entry
$create = $this->makeRequest('POST', '/api/entries', ['text' => 'Non-owner claim IT entry ' . $suffix], $userToken);
$this->assertEquals(201, $create['status']);
$entryId = (int) ($create['data']['id'] ?? 0);
$this->assertGreaterThan(0, $entryId);

$list = $this->makeRequest('GET', '/api/entries?limit=50');
$entry = $this->findEntry($list['data'], $entryId);
$this->assertNotNull($entry, 'Seeded entry not found in /api/entries');

$tagged = $this->makeRequest('PUT', "/api/entries/{$entry['hash_id']}/tags", ['tags' => [$tagName]], $userToken);
$this->assertEquals(200, $tagged['status']);
$tagId = (int) ($tagged['data']['tags'][0]['id'] ?? 0);
$this->assertGreaterThan(0, $tagId);

// Admin claims the same tag with a collection
$created = $this->makeRequest('POST', '/api/admin/collections', [
'name' => 'IT Non-Owner ' . $suffix,
'slug' => $slug,
'tag_ids' => [$tagId],
], $adminToken);
$this->assertEquals(201, $created['status']);
$collectionId = (int) ($created['data']['collection']['id'] ?? 0);
$this->assertGreaterThan(0, $collectionId);

// Landing feed / public API keep poster attribution
$api = $this->makeRequest('GET', '/api/entries?limit=50');
$unclaimed = $this->findEntry($api['data'], $entryId);
$this->assertNotNull($unclaimed);
$this->assertNull($unclaimed['collection'] ?? null, 'Non-owner entry was claimed by a collection');

// Collection page and count exclude it
$scoped = $this->makeRequest('GET', "/api/collections/{$slug}/entries?limit=50");
$this->assertEquals(200, $scoped['status']);
$this->assertNull($this->findEntry($scoped['data'], $entryId), 'Non-owner entry leaked into collection entries');

$detail = $this->makeRequest('GET', "/api/collections/{$slug}");
$this->assertEquals(200, $detail['status']);
$this->assertSame(0, (int) $detail['data']['collection']['entry_count']);
} finally {
if ($collectionId !== null) {
$this->makeRequest('DELETE', "/api/admin/collections/{$collectionId}", null, $adminToken);
}
if ($entryId !== null) {
$this->makeRequest('DELETE', "/api/admin/entries/{$entryId}", null, $adminToken);
}
if ($tagId !== null) {
$this->makeRequest('DELETE', "/api/admin/tags/{$tagId}", null, $adminToken);
}
}
}

/**
* Find an entry by id in a list response's entries array.
*/
Expand Down
Loading