From 3b855e95177b9388cf315f3c73a085869ab01058 Mon Sep 17 00:00:00 2001
From: HolgerHatGarKeineNode
<123783602+HolgerHatGarKeineNode@users.noreply.github.com>
Date: Mon, 18 May 2026 22:04:45 +0200
Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Refactor:=20Centralize=20`isBoar?=
=?UTF-8?q?dMember`=20logic=20in=20`EinundzwanzigPleb`=20model=20and=20rep?=
=?UTF-8?q?lace=20redundant=20checks?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/Auth/NostrUser.php | 7 ++-
app/Livewire/Traits/WithNostrAuth.php | 4 +-
app/Models/EinundzwanzigPleb.php | 14 +++++
app/Policies/ElectionPolicy.php | 17 +-----
app/Policies/ProjectProposalPolicy.php | 20 ++-----
config/einundzwanzig/config.php | 2 +
phpunit.xml | 3 +-
.../views/components/project-card.blade.php | 2 +-
.../views/livewire/association/news.blade.php | 58 ++++++++-----------
.../project-support/form/create.blade.php | 7 +--
.../project-support/index.blade.php | 2 +-
.../project-support/show.blade.php | 4 +-
.../Feature/Livewire/Association/NewsTest.php | 12 ++++
.../Policies/ProjectProposalPolicyTest.php | 9 +++
14 files changed, 86 insertions(+), 75 deletions(-)
diff --git a/app/Auth/NostrUser.php b/app/Auth/NostrUser.php
index 8d563c5..1c4f431 100644
--- a/app/Auth/NostrUser.php
+++ b/app/Auth/NostrUser.php
@@ -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;
+ }
}
diff --git a/app/Livewire/Traits/WithNostrAuth.php b/app/Livewire/Traits/WithNostrAuth.php
index 6c00a75..dd44469 100644
--- a/app/Livewire/Traits/WithNostrAuth.php
+++ b/app/Livewire/Traits/WithNostrAuth.php
@@ -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;
}
}
diff --git a/app/Models/EinundzwanzigPleb.php b/app/Models/EinundzwanzigPleb.php
index d012b46..aef7faf 100644
--- a/app/Models/EinundzwanzigPleb.php
+++ b/app/Models/EinundzwanzigPleb.php
@@ -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();
+ }
}
diff --git a/app/Policies/ElectionPolicy.php b/app/Policies/ElectionPolicy.php
index 351fc9d..e5178fb 100644
--- a/app/Policies/ElectionPolicy.php
+++ b/app/Policies/ElectionPolicy.php
@@ -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);
- }
}
diff --git a/app/Policies/ProjectProposalPolicy.php b/app/Policies/ProjectProposalPolicy.php
index 00ef9ad..9ece835 100644
--- a/app/Policies/ProjectProposalPolicy.php
+++ b/app/Policies/ProjectProposalPolicy.php
@@ -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();
}
}
diff --git a/config/einundzwanzig/config.php b/config/einundzwanzig/config.php
index 655660a..210b7bc 100644
--- a/config/einundzwanzig/config.php
+++ b/config/einundzwanzig/config.php
@@ -7,5 +7,7 @@ return [
'npub10t8npnmqhpwx9w8k232kess7gqtdlr6kqjemdzf8jnughwqd0gwsez0924',
'npub1r8343wqpra05l3jnc4jud4xz7vlnyeslf7gfsty7ahpf92rhfmpsmqwym8',
'npub17fqtu2mgf7zueq2kdusgzwr2lqwhgfl2scjsez77ddag2qx8vxaq3vnr8y',
+ 'npub1v4lgwjv7qfn3t7qjscpsgz9vqvspf6hecdp2ckgp0dz89uqn5slsgrhw3p',
+ 'npub14r770s5wrqpm8jmzur5arnm9aum9x0wasaxwczael54xhjggl7ws5lygc6',
],
];
diff --git a/phpunit.xml b/phpunit.xml
index 567e65c..61c031c 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -22,7 +22,8 @@
- {{ $currentPleb->profile?->name ?? str($currentPleb->npub)->limit(32) }}
-
+ {{ $currentPleb->profile?->name ?? str($currentPleb->npub)->limit(32) }}
+