🚀 Refactor: Centralize isBoardMember logic in EinundzwanzigPleb model and replace redundant checks

This commit is contained in:
HolgerHatGarKeineNode
2026-05-18 22:04:45 +02:00
parent cb61d9d543
commit 3b855e9517
14 changed files with 86 additions and 75 deletions
+6 -1
View File
@@ -9,7 +9,7 @@ class NostrUser implements Authenticatable
{
protected string $pubkey;
protected ?object $pleb;
protected ?EinundzwanzigPleb $pleb;
public function __construct(string $pubkey)
{
@@ -63,4 +63,9 @@ class NostrUser implements Authenticatable
{
return $this->pleb;
}
public function isBoardMember(): bool
{
return $this->pleb?->isBoardMember() ?? false;
}
}
+2 -2
View File
@@ -37,7 +37,7 @@ trait WithNostrAuth
->where('pubkey', $pubkey)
->first();
if ($this->currentPleb && in_array($this->currentPleb->npub, config('einundzwanzig.config.current_board'), true)) {
if ($this->currentPleb && $this->currentPleb->isBoardMember()) {
$this->canEdit = true;
}
@@ -62,7 +62,7 @@ trait WithNostrAuth
$this->currentPleb = $user->getPleb();
$this->isAllowed = true;
if ($this->currentPleb && in_array($this->currentPleb->npub, config('einundzwanzig.config.current_board'), true)) {
if ($this->currentPleb && $this->currentPleb->isBoardMember()) {
$this->canEdit = true;
}
}
+14
View File
@@ -50,4 +50,18 @@ class EinundzwanzigPleb extends Authenticatable implements CipherSweetEncrypted
{
return $this->hasMany(PaymentEvent::class);
}
public function isBoardMember(): bool
{
return in_array($this->npub, config('einundzwanzig.config.current_board', []), true);
}
public function hasPaidMembership(?int $year = null): bool
{
return $this->association_status->value > 1
&& $this->paymentEvents()
->where('year', $year ?? (int) date('Y'))
->where('paid', true)
->exists();
}
}
+3 -14
View File
@@ -29,7 +29,7 @@ class ElectionPolicy
*/
public function create(NostrUser $user): bool
{
return $this->isBoardMember($user);
return $user->isBoardMember();
}
/**
@@ -38,7 +38,7 @@ class ElectionPolicy
*/
public function update(NostrUser $user, Election $election): bool
{
return $this->isBoardMember($user);
return $user->isBoardMember();
}
/**
@@ -47,7 +47,7 @@ class ElectionPolicy
*/
public function delete(NostrUser $user, Election $election): bool
{
return $this->isBoardMember($user);
return $user->isBoardMember();
}
/**
@@ -64,15 +64,4 @@ class ElectionPolicy
return $pleb->association_status->value >= 3;
}
private function isBoardMember(NostrUser $user): bool
{
$pleb = $user->getPleb();
if (! $pleb) {
return false;
}
return in_array($pleb->npub, config('einundzwanzig.config.current_board'), true);
}
}
+5 -15
View File
@@ -3,7 +3,6 @@
namespace App\Policies;
use App\Auth\NostrUser;
use App\Models\EinundzwanzigPleb;
use App\Models\ProjectProposal;
class ProjectProposalPolicy
@@ -26,7 +25,7 @@ class ProjectProposalPolicy
/**
* Determine whether the user can create project proposals.
* Requires: authenticated, association_status > 1, paid membership for current year.
* Allowed for: board members (always) OR active members with paid membership for the current year.
*/
public function create(NostrUser $user): bool
{
@@ -36,8 +35,7 @@ class ProjectProposalPolicy
return false;
}
return $pleb->association_status->value > 1
&& $pleb->paymentEvents()->where('year', date('Y'))->where('paid', true)->exists();
return $pleb->isBoardMember() || $pleb->hasPaidMembership();
}
/**
@@ -53,7 +51,7 @@ class ProjectProposalPolicy
}
return $pleb->id === $projectProposal->einundzwanzig_pleb_id
|| $this->isBoardMember($pleb);
|| $pleb->isBoardMember();
}
/**
@@ -69,7 +67,7 @@ class ProjectProposalPolicy
}
return $pleb->id === $projectProposal->einundzwanzig_pleb_id
|| $this->isBoardMember($pleb);
|| $pleb->isBoardMember();
}
/**
@@ -84,14 +82,6 @@ class ProjectProposalPolicy
return false;
}
return $this->isBoardMember($pleb);
}
/**
* @param EinundzwanzigPleb $pleb
*/
private function isBoardMember(object $pleb): bool
{
return in_array($pleb->npub, config('einundzwanzig.config.current_board'), true);
return $pleb->isBoardMember();
}
}