Skip to content

Commit

Permalink
Merge pull request #28 from nickv-nextcloud/notificationManager
Browse files Browse the repository at this point in the history
Add NotificationManager
  • Loading branch information
ChristophWurst committed Oct 23, 2018
2 parents d27f8e3 + f732f73 commit 65b8ad7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 37 deletions.
21 changes: 0 additions & 21 deletions lib/Provider/NotificationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,22 @@
use OCP\Authentication\TwoFactorAuth\IProvider;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
use OCP\Notification\IManager;
use OCP\Template;

class NotificationProvider implements IProvider {

/** @var IL10N */
private $l10n;
/** @var IManager */
private $notificationManager;
/** @var IRequest */
private $request;
/** @var IConfig */
private $config;
/** @var TokenManager */
private $tokenManager;

public function __construct(IL10N $l10n,
IRequest $request,
IManager $notificationManager,
IConfig $config,
TokenManager $tokenManager) {
$this->l10n = $l10n;
$this->request = $request;
$this->notificationManager = $notificationManager;
$this->config = $config;
$this->tokenManager = $tokenManager;
}
Expand Down Expand Up @@ -97,17 +87,6 @@ public function getDescription(): string {
public function getTemplate(IUser $user): Template {
$token = $this->tokenManager->generate($user->getUID());

//Send notificcation
$notification = $this->notificationManager->createNotification();
$notification->setApp(Application::APP_ID)
->setSubject('login_attempt', [
'ip' => $this->request->getRemoteAddress(),
])
->setObject('2fa_id', $token->getId())
->setUser($user->getUID())
->setDateTime(new \DateTime());
$this->notificationManager->notify($notification);

$tmpl = new Template(Application::APP_ID, 'challenge');
$tmpl->assign('token', $token->getToken());

Expand Down
64 changes: 64 additions & 0 deletions lib/Service/NotificationManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\TwoFactorNextcloudNotification\Service;

use OCA\TwoFactorNextcloudNotification\AppInfo\Application;
use OCA\TwoFactorNextcloudNotification\Db\Token;
use OCP\IRequest;
use OCP\Notification\IManager;

class NotificationManager {

/** @var IManager */
private $manager;

/** @var IRequest */
private $request;

public function __construct(IManager $manager, IRequest $request) {
$this->manager = $manager;
$this->request = $request;
}

public function clearNotification(Token $token) {
$notification = $this->manager->createNotification();
$notification->setApp(Application::APP_ID)
->setSubject('login_attempt')
->setObject('2fa_id', $token->getId());
$this->manager->markProcessed($notification);
}

public function newNotification(Token $token) {
$notification = $this->manager->createNotification();
$notification->setApp(Application::APP_ID)
->setSubject('login_attempt', [
'ip' => $this->request->getRemoteAddress(),
])
->setObject('2fa_id', $token->getId())
->setUser($token->getUserId())
->setDateTime(new \DateTime());
$this->manager->notify($notification);
}
}
21 changes: 5 additions & 16 deletions lib/Service/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@

namespace OCA\TwoFactorNextcloudNotification\Service;

use OCA\TwoFactorNextcloudNotification\AppInfo\Application;
use OCA\TwoFactorNextcloudNotification\Db\Token;
use OCA\TwoFactorNextcloudNotification\Db\TokenMapper;
use OCA\TwoFactorNextcloudNotification\Exception\TokenExpireException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Notification\IManager as NotificationManager;

class TokenManager {

Expand Down Expand Up @@ -75,7 +73,7 @@ public function getByToken(string $token): Token {
* @param Token $token
*/
public function delete(Token $token) {
$this->clearNotification($token);
$this->notificationManager->clearNotification($token);
$this->mapper->delete($token);
}

Expand All @@ -84,7 +82,7 @@ public function delete(Token $token) {
* @return Token
*/
public function update(Token $token): Token {
$this->clearNotification($token);
$this->notificationManager->clearNotification($token);
return $this->mapper->update($token);
}

Expand All @@ -97,18 +95,9 @@ public function cleanupTokens() {
}

public function generate(string $userId): Token {
return $this->mapper->generate($userId);
}

/**
* @param Token $token
*/
protected function clearNotification(Token $token) {
$notification = $this->notificationManager->createNotification();
$notification->setApp(Application::APP_ID)
->setSubject('login_attempt')
->setObject('2fa_id', $token->getId());
$this->notificationManager->markProcessed($notification);
$token = $this->mapper->generate($userId);
$this->notificationManager->newNotification($token);
return $token;
}

/**
Expand Down

0 comments on commit 65b8ad7

Please sign in to comment.