Skip to content

Commit 291439d

Browse files
committed
Settings for send mail on login
1 parent d7e31a3 commit 291439d

File tree

8 files changed

+57
-15
lines changed

8 files changed

+57
-15
lines changed

src/App/Controller/Account/SecurityController.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function load(ServerRequestInterface $request): ResponseInterface
3535
return new HtmlResponse(
3636
$this->template->render('account/security',
3737
[
38+
'account' => $this->accountService->findAccountById($account->getId()),
3839
'totpToken' => $this->securityService->generateTOTPSecret(),
3940
'twoFactors' => $this->securityService->getTwoFactorByAccountID($account->getId())
4041
]));
@@ -45,14 +46,21 @@ public function create(Account $account): void
4546

4647
if(isset($_POST['addTwoFactorModalSubmit'], $_POST['addTwoFactorModalTFAToken'], $_POST['addTwoFactorModalName'], $_POST['addTwoFactorModalTFACode']))
4748
{
48-
4949
$twoFactor = new TwoFactor();
5050
$twoFactor->setAccount($account->getId());
5151
$twoFactor->setType(TwoFactorType::TOTP_ID);
5252
$twoFactor->setName($_POST['addTwoFactorModalName']);
5353
$twoFactor->setToken($_POST['addTwoFactorModalTFAToken']);
5454

5555
$this->securityService->add($twoFactor, $_POST['addTwoFactorModalTFACode']);
56+
}
57+
58+
if(isset($_POST['securityBasicSettingsSave']))
59+
{
60+
61+
$this->accountService->setSendLoginEmail($account->getId(), isset($_POST['sendLoginEmailCheck']));
62+
63+
MESSAGES->add('success', 'account-settings-security-update-success');
5664

5765
}
5866

src/App/Controller/Authentication/LoginController.php

+16-12
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function login(ServerRequestInterface $request): ?ResponseInterface
6060
$account->setId($login['id']);
6161
$account->setActive((int)$login['active'] === 1);
6262
$account->setSetupComplete((int)$login['setupComplete'] === 1);
63+
$account->setSendMailUnknownLogin((int)$login['sendMailUnknownLogin'] === 1);
6364

6465
if ($account->isActive() === false) {
6566
MESSAGES->add('danger', 'login-account-disabled');
@@ -72,18 +73,21 @@ public function login(ServerRequestInterface $request): ?ResponseInterface
7273
MESSAGES->add('success', 'login-account-successful');
7374
$this->accountService->updateLastUserLogin($account);
7475

75-
$this->mailerService->configureMail(
76-
$account->getEmail(),
77-
'Neue Anmeldung erkannt',
78-
MailType::NEW_LOGIN_DETECTED_MAIL_ID,
79-
[
80-
'accountName' => $account->getName(),
81-
'browser' => $userInformation->configure($_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'])->getBrowser(),
82-
'country' => $userInformation->getCountry(),
83-
'ip' => $userInformation->getIP()
84-
],
85-
$account->getId()
86-
)->send();
76+
if($account->isSendMailUnknownLogin())
77+
{
78+
$this->mailerService->configureMail(
79+
$account->getEmail(),
80+
'Neue Anmeldung erkannt',
81+
MailType::NEW_LOGIN_DETECTED_MAIL_ID,
82+
[
83+
'accountName' => $account->getName(),
84+
'browser' => $userInformation->configure($_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'])->getBrowser(),
85+
'country' => $userInformation->getCountry(),
86+
'ip' => $userInformation->getIP()
87+
],
88+
$account->getId()
89+
)->send();
90+
}
8791

8892

8993
$_SESSION[Software::SESSION_USERID_NAME] = $account->getId();

src/App/Middleware/AuthenticationMiddleware.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
use Psr\Http\Server\MiddlewareInterface;
1414
use Psr\Http\Server\RequestHandlerInterface;
1515

16-
readonly class AuthenticationMiddleware implements MiddlewareInterface
16+
readonly class
17+
AuthenticationMiddleware implements MiddlewareInterface
1718
{
1819

1920
public function __construct(
@@ -43,6 +44,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4344
$account->setBusiness($accountData['business']);
4445
$account->setAdmin($accountData['isAdmin'] === 1);
4546
$account->setLevel($accountData['level']);
47+
$account->setSendMailUnknownLogin($accountData['sendMailUnknownLogin'] === 1);
4648

4749
return $handler->handle($request->withAttribute(Account::class, $account));
4850
}

src/App/Model/Authentication/Account.php

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Account
2121
private bool $setupComplete;
2222
private bool $support;
2323
private bool $admin;
24+
private bool $sendMailUnknownLogin;
2425

2526
public function getId(): int
2627
{
@@ -162,4 +163,14 @@ public function setAdmin(bool $admin): void
162163
$this->admin = $admin;
163164
}
164165

166+
public function isSendMailUnknownLogin(): bool
167+
{
168+
return $this->sendMailUnknownLogin;
169+
}
170+
171+
public function setSendMailUnknownLogin(bool $sendMailUnknownLogin): void
172+
{
173+
$this->sendMailUnknownLogin = $sendMailUnknownLogin;
174+
}
175+
165176
}

src/App/Service/Authentication/AccountService.php

+6
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,10 @@ public function getAccountList(): array
162162
return $this->accountTable->findAll();
163163
}
164164

165+
public function setSendLoginEmail(int $account, bool $active): void
166+
{
167+
$this->accountTable->updateAccountSendMailUnknownLogin($account, $active);
168+
}
169+
170+
165171
}

src/App/Table/Authentication/AccountTable.php

+6
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,10 @@ public function updateAccountActive(int $account, bool $active): int|string|bool
7979
)->execute();
8080
}
8181

82+
public function updateAccountSendMailUnknownLogin(int $account, bool $active): int|string|bool
83+
{
84+
return $this->query->update($this->getTableName())->where('id', $account)->set(['sendMailUnknownLogin' => $active ? 1 : 0]
85+
)->execute();
86+
}
87+
8288
}

template/account/security.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@
4040
<div class="col-md-6">
4141
<div class="mb-3">
4242
<div class="form-check form-switch">
43-
<input class="form-check-input" type="checkbox" role="switch" id="sendLoginEmailCheck" name="sendLoginEmailCheck" checked>
43+
<input class="form-check-input" type="checkbox" role="switch" id="sendLoginEmailCheck" name="sendLoginEmailCheck"
44+
<?= $account['sendMailUnknownLogin'] === 1 ? 'checked' : '' ?>>
4445
<label class="form-check-label" for="sendLoginEmailCheck">
4546
<?= $this->e($this->translate('account-settings-security-general-send-login-information-mail')) ?>
4647
</label>
4748
</div>
4849
</div>
4950
</div>
51+
<div class="col-md-6">
52+
<button type="submit" class="btn btn-primary float-end" name="securityBasicSettingsSave">Speichern</button>
53+
</div>
5054

5155
</form>
5256
</div>

translations/de.json

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
"account-settings-security-two-factor-failed-unk": "Der 2. Faktor konnte nicht in deinem Konto hinterlegt werden.",
123123
"account-settings-security-two-factor-failed-code-invalid": "Dein angegebener Code ist nicht korrekt.",
124124
"account-settings-security-two-factor-success": "Der 2. Faktor wurde erfolgreich in deinem Konto hinterlegt.",
125+
"account-settings-security-update-success": "Deine Sicherheitseinstellungen wurden aktualisiert.",
125126

126127
"admin-navigation-general-tab-title": "Allgemein",
127128
"admin-navigation-general-tab-user-management-title": "Kontoverwaltung",

0 commit comments

Comments
 (0)