Skip to content

Commit

Permalink
fix: interaction policy
Browse files Browse the repository at this point in the history
  • Loading branch information
abourtnik committed Jan 20, 2024
1 parent 069becb commit c1241de
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions app/Http/Controllers/InteractionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ private function perform (InteractionRequest $request, $status) : JsonResponse {

$likeable = $model::findOrFail($id);

$this->authorize('interact', $likeable);

$interaction = $likeable->interactions()->whereRelation('user', 'id', Auth::user()->id)->first();

if ($interaction) {
Expand Down
7 changes: 2 additions & 5 deletions app/Http/Requests/Interaction/InteractionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Models\Comment;
use App\Models\Video;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class InteractionRequest extends FormRequest
Expand Down Expand Up @@ -38,10 +37,8 @@ public function rules() : array
'id' => [
'required',
'numeric',
Rule::exists((new $model())->getTable(), 'id')->where(function ($query) use ($model) {
return (new $model)->scopePublic($query)->orWhere('user_id', Auth::user()->id);
})
],
'exists:'.$model.',id',
]
];
}
}
7 changes: 7 additions & 0 deletions app/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ protected function parsedContent(): Attribute
);
}

protected function isActive(): Attribute
{
return Attribute::make(
get: fn () => !$this->is_banned
);
}

/**
* -------------------- SCOPES --------------------
*/
Expand Down
14 changes: 14 additions & 0 deletions app/Policies/CommentPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ public function report(User $user, Comment $comment): Response|bool
: Response::denyWithStatus(403, 'You are not authorized to report this comment');
}

/**
* Determine whether the user can like/dislike the comment.
*
* @param User $user
* @param Comment $comment
* @return Response|bool
*/
public function interact(User $user, Comment $comment): Response|bool
{
return $comment->is_active || $comment->user->is($user)
? Response::allow()
: Response::denyWithStatus(403, 'You are not authorized to interact with this comment');
}

/**
* Determine whether the user can pin the model.
*
Expand Down
14 changes: 14 additions & 0 deletions app/Policies/VideoPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,18 @@ public function unpin(User $user, Video $video): Response|bool
? Response::allow()
: Response::denyWithStatus(403);
}

/**
* Determine whether the user can like/dislike video.
*
* @param User $user
* @param Video $video
* @return Response|bool
*/
public function interact(User $user, Video $video): Response|bool
{
return $video->is_active || $video->user->is($user)
? Response::allow()
: Response::denyWithStatus(403, 'You are not authorized to interact with this video');
}
}

0 comments on commit c1241de

Please sign in to comment.