Make MailService strict

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-07-14 16:56:36 +02:00
parent 2d71c46cad
commit fc0d42f30d
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
2 changed files with 19 additions and 20 deletions

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net> * @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
* @copyright Copyright (c) 2017 Pellaeon Lin <pellaeon@hs.ntnu.edu.tw> * @copyright Copyright (c) 2017 Pellaeon Lin <pellaeon@hs.ntnu.edu.tw>
@ -27,7 +30,6 @@ namespace OCA\Registration\Service;
use OCA\Registration\Db\Registration; use OCA\Registration\Db\Registration;
use OCP\Defaults; use OCP\Defaults;
use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IL10N; use OCP\IL10N;
use OCP\ILogger; use OCP\ILogger;
@ -45,19 +47,21 @@ class MailService {
private $defaults; private $defaults;
/** @var IL10N */ /** @var IL10N */
private $l10n; private $l10n;
/** @var IConfig */
private $config;
/** @var IGroupManager */ /** @var IGroupManager */
private $groupManager; private $groupManager;
/** @var ILogger */ /** @var ILogger */
private $logger; private $logger;
public function __construct(IURLGenerator $urlGenerator, IMailer $mailer, Defaults $defaults, IL10N $l10n, IConfig $config, IGroupManager $groupManager, ILogger $logger) { public function __construct(IURLGenerator $urlGenerator,
IMailer $mailer,
Defaults $defaults,
IL10N $l10n,
IGroupManager $groupManager,
ILogger $logger) {
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->mailer = $mailer; $this->mailer = $mailer;
$this->defaults = $defaults; $this->defaults = $defaults;
$this->l10n = $l10n; $this->l10n = $l10n;
$this->config = $config;
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
$this->logger = $logger; $this->logger = $logger;
} }
@ -66,7 +70,7 @@ class MailService {
* @param string $email * @param string $email
* @throws RegistrationException * @throws RegistrationException
*/ */
public function validateEmail($email) { public function validateEmail(string $email): void {
if (!$this->mailer->validateMailAddress($email)) { if (!$this->mailer->validateMailAddress($email)) {
throw new RegistrationException($this->l10n->t('The email address you entered is not valid')); throw new RegistrationException($this->l10n->t('The email address you entered is not valid'));
} }
@ -76,7 +80,7 @@ class MailService {
* @param Registration $registration * @param Registration $registration
* @throws RegistrationException * @throws RegistrationException
*/ */
public function sendTokenByMail(Registration $registration) { public function sendTokenByMail(Registration $registration): void {
$link = $this->urlGenerator->linkToRouteAbsolute('registration.register.verifyToken', ['token' => $registration->getToken()]); $link = $this->urlGenerator->linkToRouteAbsolute('registration.register.verifyToken', ['token' => $registration->getToken()]);
$subject = $this->l10n->t('Verify your %s registration request', [$this->defaults->getName()]); $subject = $this->l10n->t('Verify your %s registration request', [$this->defaults->getName()]);
@ -113,22 +117,17 @@ class MailService {
} }
} }
/** public function notifyAdmins(string $userId, bool $userIsEnabled, string $userGroupId): void {
* @param string $userId
* @param string $userGroupId
* @param bool $userIsEnabled
*/
public function notifyAdmins($userId, $userIsEnabled, $userGroupId) {
// Notify admin // Notify admin
$admin_users = $this->groupManager->get('admin')->getUsers(); $admin_users = $this->groupManager->get('admin')->getUsers();
// if the user is disabled and belongs to a group // if the user is disabled and belongs to a group
// add subadmins of this group to notification list // add subadmins of this group to notification list
if (!$userIsEnabled and $userGroupId) { if (!$userIsEnabled && $userGroupId) {
$group = $this->groupManager->get($userGroupId); $group = $this->groupManager->get($userGroupId);
$subadmin_users = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($group); $subadmin_users = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($group);
foreach ($subadmin_users as $user) { foreach ($subadmin_users as $user) {
if (!in_array($user, $admin_users)) { if (!in_array($user, $admin_users, true)) {
$admin_users[] = $user; $admin_users[] = $user;
} }
} }
@ -142,20 +141,21 @@ class MailService {
} }
} }
try { try {
$this->sendNewUserNotifEmail($to_arr, $userId, $userIsEnabled); $this->sendNewUserNotifyEmail($to_arr, $userId, $userIsEnabled);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Sending admin notification email failed: '. $e->getMessage()); $this->logger->error('Sending admin notification email failed: '. $e->getMessage());
} }
} }
/** /**
* Sends new user notification email to admin * Sends new user notification email to given user list
*
* @param array $to * @param array $to
* @param string $username the new user * @param string $username the new user
* @param bool $userIsEnabled the new user account is enabled * @param bool $userIsEnabled the new user account is enabled
* @throws \Exception * @throws \Exception
*/ */
private function sendNewUserNotifEmail(array $to, $username, $userIsEnabled) { private function sendNewUserNotifyEmail(array $to, string $username, bool $userIsEnabled): void {
$link = $this->urlGenerator->linkToRouteAbsolute('settings.Users.usersListByGroup', [ $link = $this->urlGenerator->linkToRouteAbsolute('settings.Users.usersListByGroup', [
'group' => 'disabled', 'group' => 'disabled',
]); ]);

View File

@ -121,8 +121,7 @@ class RegisterControllerTest extends TestCase {
->with("registration", 'allowed_domains', '') ->with("registration", 'allowed_domains', '')
->willReturn(''); ->willReturn('');
$this->mailService->expects($this->once()) $this->mailService->expects($this->once())
->method('sendTokenByMail') ->method('sendTokenByMail');
->willReturn(true);
$this->assertEquals($this->registrationService->validateEmail($email), true); $this->assertEquals($this->registrationService->validateEmail($email), true);