Skip to content
Open
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
2 changes: 2 additions & 0 deletions app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public function vote(Request $request, $projectId, $itemId)

$item = $project->items()->visibleForCurrentUser()->findOrfail($itemId);

abort_if($item->board?->block_votes, 403);

$item->toggleUpvote();

return redirect()->back();
Expand Down
3 changes: 3 additions & 0 deletions app/Livewire/Item/VoteButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Livewire\Item;

use App\Models\Item;
use App\Models\Vote;
use Livewire\Component;
use Illuminate\Support\Collection;
Expand All @@ -24,6 +25,8 @@ public function mount(bool $hideSubscribeOption = false)

public function toggleUpvote()
{
abort_if($this->model instanceof Item && $this->model->board?->block_votes, 403);

$this->model->toggleUpvote();
$this->refreshModel();
}
Expand Down
2 changes: 2 additions & 0 deletions app/Livewire/Project/ItemCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function mount()

public function toggleUpvote()
{
abort_if($this->item->board?->block_votes, 403);

$this->item->toggleUpvote();
$this->item = $this->item->refresh();
}
Expand Down
16 changes: 15 additions & 1 deletion app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Support\Str;
use App\Enums\InboxWorkflow;
use App\Settings\GeneralSettings;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
Expand All @@ -22,7 +24,10 @@

class Item extends Model
{
use HasFactory, Sluggable, HasOgImage, HasUpvote, HasTags;
use HasFactory, Sluggable, HasOgImage, HasTags;
use HasUpvote {
toggleUpvote as private toggleUpvoteWithoutBoardGuard;
}

public $fillable = [
'slug',
Expand Down Expand Up @@ -88,6 +93,15 @@ public function board(): BelongsTo
return $this->belongsTo(Board::class);
}

public function toggleUpvote(?User $user = null): Vote|Model|RedirectResponse|bool|Redirector
{
if ($this->board?->block_votes) {
return false;
}

return $this->toggleUpvoteWithoutBoardGuard($user);
}

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
Expand Down
16 changes: 9 additions & 7 deletions tests/Feature/Item/VoteOnItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,24 @@
$this->assertTrue($user->hasAdminAccess());
});

test('A user cant vote on an item that belongs to a board which disables voting', function () {
test('A user cannot vote on an item that belongs to a board which disables voting', function () {
$user = User::first();

$board = Board::factory()->create(['block_votes' => true]);
$project = Project::factory()->create();
$board = Board::factory()->for($project)->create(['block_votes' => true]);
$item = Item::factory()->create([
'project_id' => $board->getAttributeValue('project_id'),
'board_id' => $board->getAttributeValue('id'),
]);
'project_id' => $project->id,
'board_id' => $board->id,
]);

$this->assertEquals(0, $item->votes()->count());

$this->actingAs($user)
->post(route('projects.items.vote', [
'item' => $item->getKey(),
'project' => $board->getKey(),
]));
'project' => $project->getKey(),
]))
->assertForbidden();

$this->assertAuthenticatedAs($user);
$this->assertEquals(0, $item->votes()->count());
Expand Down
Loading