2022-04-17 19:41:18 -04:00
|
|
|
<?php
|
|
|
|
namespace PonePaste\Helpers;
|
|
|
|
|
|
|
|
use PonePaste\Models\User;
|
|
|
|
use PonePaste\Models\Paste;
|
|
|
|
|
|
|
|
class AbilityHelper {
|
|
|
|
private const DESTRUCTIVE_ACTIONS = [
|
|
|
|
'edit', 'delete'
|
|
|
|
];
|
|
|
|
|
|
|
|
private User | null $user;
|
|
|
|
|
|
|
|
public function __construct(User | null $user) {
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function can(string $action, mixed $subject) : bool {
|
|
|
|
$is_destructive = in_array($action, self::DESTRUCTIVE_ACTIONS);
|
|
|
|
|
2022-04-18 14:09:02 -04:00
|
|
|
if (is_a($subject, 'PonePaste\\Models\\Paste')) {
|
|
|
|
if (((int) $subject->visible === Paste::VISIBILITY_PRIVATE) || $is_destructive) {
|
2022-04-18 14:05:26 -04:00
|
|
|
return $this->user !== null && $subject->user_id === $this->user->id;
|
2022-04-17 19:41:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-18 14:09:02 -04:00
|
|
|
if (is_a($subject, 'PonePaste\\Models\\User')) {
|
2022-04-18 14:05:26 -04:00
|
|
|
return !$is_destructive || ($this->user !== null && $subject->id === $this->user->id);
|
2022-04-17 19:41:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|