+
Hallo {$user->getFirstName()},
+
+
Ihr Administrator hat einen Account für Sie in myCRM erstellt.
+
+
E-Mail: {$user->getEmail()}
+
+
Bitte klicken Sie auf den folgenden Link, um Ihr Passwort einzurichten und Ihren Account zu aktivieren:
+
+
+
+
Oder kopieren Sie diesen Link in Ihren Browser:
+
+ {$setupUrl}
+
+
+
Wichtig: Dieser Link ist 24 Stunden lang gültig.
+
+
Sollten Sie Probleme haben, wenden Sie sich bitte an Ihren Administrator.
+
+
+
+
+HTML;
+ }
+}
diff --git a/src/State/UserPasswordHasher.php b/src/State/UserPasswordHasher.php
index d61c6c3..1406ee8 100644
--- a/src/State/UserPasswordHasher.php
+++ b/src/State/UserPasswordHasher.php
@@ -5,13 +5,15 @@ namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\User;
+use App\Service\PasswordSetupService;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class UserPasswordHasher implements ProcessorInterface
{
public function __construct(
private ProcessorInterface $processor,
- private UserPasswordHasherInterface $passwordHasher
+ private UserPasswordHasherInterface $passwordHasher,
+ private PasswordSetupService $passwordSetupService
) {
}
@@ -21,16 +23,37 @@ class UserPasswordHasher implements ProcessorInterface
return $this->processor->process($data, $operation, $uriVariables, $context);
}
+ $isNewUser = !$data->getId();
+ $hasPlainPassword = !empty($data->getPlainPassword());
+
// Hash plain password if provided
- if ($data->getPlainPassword()) {
+ if ($hasPlainPassword) {
$hashedPassword = $this->passwordHasher->hashPassword(
$data,
$data->getPlainPassword()
);
$data->setPassword($hashedPassword);
$data->eraseCredentials();
+ } elseif ($isNewUser) {
+ // New user without password - set temporary random password
+ $tempPassword = bin2hex(random_bytes(16));
+ $hashedPassword = $this->passwordHasher->hashPassword($data, $tempPassword);
+ $data->setPassword($hashedPassword);
}
- return $this->processor->process($data, $operation, $uriVariables, $context);
+ // Process the user
+ $result = $this->processor->process($data, $operation, $uriVariables, $context);
+
+ // Send password setup email for new users without password
+ if ($isNewUser && !$hasPlainPassword) {
+ try {
+ $this->passwordSetupService->sendPasswordSetupEmail($data);
+ } catch (\Exception $e) {
+ // Log error but don't fail user creation
+ error_log('Failed to send password setup email: ' . $e->getMessage());
+ }
+ }
+
+ return $result;
}
}
diff --git a/templates/security/login.html.twig b/templates/security/login.html.twig
index 0721fb8..940a29b 100644
--- a/templates/security/login.html.twig
+++ b/templates/security/login.html.twig
@@ -159,6 +159,13 @@