Files
Toiminnanohjaus/app/Modules/Users/Infrastructure/Auth/LaravelPasswordLinkSender.php
T
petriandClaude Opus 5.5 8dfc02c4e5 End other sessions on password change and give admin links 72 hours
- AuthenticateSession in the web group logs out a user's other sessions
  when their password changes.
- Set-password links made by an admin use a new admin_links broker with
  its own password_setup_tokens table and a 72-hour expiry, so links
  handed over by hand don't expire in an hour. Forgot-password links
  keep 60 minutes. The reset page accepts both, and issuing or using a
  link ends the user's earlier links.
- Admin links are emailed with PasswordSetupLinkNotification; the email
  body is shared with the forgot-password email via PasswordLinkMail.
- Spec chapter 3 and CLAUDE.md describe the link lifetimes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-23 17:42:37 +02:00

69 lines
2.4 KiB
PHP

<?php
namespace App\Modules\Users\Infrastructure\Auth;
use App\Models\User;
use App\Modules\Users\Application\Contracts\PasswordLinkSender;
use App\Modules\Users\Application\DTOs\IssuedSetupLink;
use App\Modules\Users\Domain\ValueObjects\EmailAddress;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Support\Facades\Password;
use LogicException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
/**
* Uses Laravel's password reset tokens. Forgot-password links use the
* default broker (60 minutes); links made by an admin use the admin_links
* broker, which has its own table and a longer expiry.
*/
class LaravelPasswordLinkSender implements PasswordLinkSender
{
public const string ADMIN_BROKER = 'admin_links';
public function __construct(private PasswordBroker $passwords) {}
public static function adminLinkHours(): int
{
return intdiv((int) config('auth.passwords.'.self::ADMIN_BROKER.'.expire'), 60);
}
public function sendSetupLink(EmailAddress $email): bool
{
try {
return $this->passwords->sendResetLink(['email' => $email->value]) === PasswordBroker::RESET_LINK_SENT;
} catch (TransportExceptionInterface $exception) {
// The mail server is down or refused the message: log it and let
// the caller offer a resend instead of failing the whole action.
report($exception);
return false;
}
}
public function issueSetupLink(EmailAddress $email): IssuedSetupLink
{
$user = User::query()->where('email', $email->value)->first()
?? throw new LogicException('A set-password link was asked for an unknown user.');
// The new link replaces every earlier link of the user.
Password::broker()->deleteToken($user);
$token = Password::broker(self::ADMIN_BROKER)->createToken($user);
$url = route('password.reset', [
'token' => $token,
'email' => $user->getEmailForPasswordReset(),
]);
try {
$user->notify(new PasswordSetupLinkNotification($token));
$emailSent = true;
} catch (TransportExceptionInterface $exception) {
// The admin still gets the link to hand over another way.
report($exception);
$emailSent = false;
}
return new IssuedSetupLink($url, $emailSent);
}
}