Files
projectsend/app/Modules/Groups/Http/Controllers/MembershipRequestsController.php
T
denkfabrik-li 5242169bb0 Deny a membership request once, as approve() already does
approve() refuses a request that is not pending:

    abort_unless($group !== null && $client !== null
        && $membershipRequest->status === MembershipRequest::STATUS_PENDING, 404);

deny(), one method below, checks nothing. Denying is not idempotent, so
repeating it is not a no-op:

  - denied_at is stamped again, and that is what the client's re-request
    cooldown counts from (MyGroupsController::inDenyCooldown). Repeating
    the request keeps one client out of one group for as long as somebody
    cares to keep asking, without a single new decision being made.
  - a second GroupMembershipDenied entry goes into the activity log, for
    a denial that did not happen.
  - a second "your request was declined" mail goes to the client.

The queue lists only pending requests, so nothing on the screen offers
this; it takes asking for the route directly. It needs
approve_groups_memberships_requests, so it is not a stranger's move.

The guard is the same one, answering the same 404, placed where deny()
can reach it. deny() keeps tolerating a vanished group or client -- that
tolerance is deliberate and separate: the denied row persists for the
cooldown even when the group it named is gone, and index() already
filters those rows out with whereHas.

Not in this change: deny() writes the status, the log entry and the
notification without a shared transaction. approve() has exactly the same
shape, so fixing one alone would replace a symmetry with a difference,
and doing both means also deciding where the mail sits relative to the
commit -- which is the question #1691 answers for file bytes, and worth
answering on its own rather than inside a state-machine fix.
2026-08-26 06:09:49 +02:00

122 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Groups\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Audit\Action;
use App\Modules\Audit\ActivityLogger;
use App\Modules\Groups\Models\MembershipRequest;
use App\Modules\Groups\Notifications\GroupMembershipDeniedNotification;
use App\Modules\Notifications\Notifier;
use App\Modules\Platform\Settings\Setting;
use App\Modules\Platform\Settings\Settings;
use App\Support\Pagination;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
/**
* The staff queue for group membership requests: approve joins the
* client to the group, deny discards the request. Both survive in the
* activity log.
*/
class MembershipRequestsController extends Controller
{
public function __construct(
private readonly ActivityLogger $activity,
private readonly Settings $settings,
private readonly Notifier $notifier,
) {}
public function index(Request $request): Response
{
$validated = $request->validate([
'search' => ['nullable', 'string', 'max:255'],
]);
$filters = ['search' => $validated['search'] ?? null];
$requests = MembershipRequest::query()
->pending()
// A request whose client or group vanished is dead weight; excluding
// it in SQL (not after fetching) keeps pagination counts honest.
->whereHas('user')
->whereHas('group')
->with(['group', 'user'])
->when($filters['search'], fn (Builder $query, string $search) => $query->where(fn (Builder $q) => $q
->whereHas('user', fn (Builder $u) => $u->where('name', 'like', "%{$search}%")->orWhere('email', 'like', "%{$search}%"))
->orWhereHas('group', fn (Builder $g) => $g->where('name', 'like', "%{$search}%"))))
->orderBy('created_at')
->paginate(25)
->withQueryString()
->through(fn (MembershipRequest $membershipRequest): array => [
'id' => $membershipRequest->id,
'client_name' => $membershipRequest->user?->name,
'client_email' => $membershipRequest->user?->email,
'group_name' => $membershipRequest->group?->name,
'created_at' => $membershipRequest->created_at?->toIso8601String(),
]);
return Inertia::render('groups/membership-requests', [
'requests' => $requests->items(),
'pagination' => Pagination::meta($requests),
'filters' => $filters,
]);
}
public function approve(MembershipRequest $membershipRequest): RedirectResponse
{
$group = $membershipRequest->group;
$client = $membershipRequest->user;
abort_unless($group !== null && $client !== null && $membershipRequest->status === MembershipRequest::STATUS_PENDING, 404);
$group->members()->syncWithoutDetaching([$client->id]);
$membershipRequest->delete();
$this->activity->log(Action::GroupMembershipApproved, subject: $group, context: ['member' => $client->name]);
$this->notifier->send('group.membership_approved', [$client], subject: $group, data: ['groupName' => $group->name]);
return back()->with('success', __('Membership request approved.'));
}
public function deny(MembershipRequest $membershipRequest): RedirectResponse
{
// The half of approve()'s guard that applies here. A request that
// has already been denied is not a decision left to make, and
// taking it again re-stamps denied_at -- which is what the
// client's re-request cooldown counts from, so the same request
// repeated keeps a client out of a group indefinitely -- while
// writing a second log entry and sending a second "your request
// was declined" mail for one decision. The queue only ever lists
// pending requests, so this is not reachable through the screen;
// it is reachable by asking for the route directly.
abort_unless($membershipRequest->status === MembershipRequest::STATUS_PENDING, 404);
$group = $membershipRequest->group;
$client = $membershipRequest->user;
// The denied row persists: the client sees the outcome, and it
// enforces the re-request cooldown.
$membershipRequest->forceFill([
'status' => MembershipRequest::STATUS_DENIED,
'denied_at' => now(),
])->save();
if ($group !== null && $client !== null) {
$this->activity->log(Action::GroupMembershipDenied, subject: $group, context: ['member' => $client->name]);
if ($this->settings->get(Setting::EmailNotificationsEnabled) === true) {
$client->notify(new GroupMembershipDeniedNotification($group->name));
}
}
return back()->with('success', __('Membership request denied.'));
}
}