migrate logging and password expiration notifications

This commit is contained in:
binsky 2021-03-13 18:41:27 +01:00
parent 6a103d8c8c
commit 3358d5de8b
7 changed files with 71 additions and 64 deletions

View file

@ -45,10 +45,10 @@ use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\ILogger;
use OCP\Notification\IManager;
use OCP\Util;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
class Application extends App implements IBootstrap {
public const APP_ID = 'passman';
@ -101,7 +101,7 @@ class Application extends App implements IBootstrap {
$context->registerService('CronService', function (ContainerInterface $c) {
return new CronService(
$c->get(CredentialService::class),
$c->get(ILogger::class),
$c->get(LoggerInterface::class),
$c->get(Utils::class),
$c->get(NotificationService::class),
$c->get(ActivityService::class),

View file

@ -24,7 +24,7 @@
namespace OCA\Passman\BackgroundJob;
use OC\BackgroundJob\TimedJob;
use \OCA\Passman\AppInfo\Application;
use OCA\Passman\Service\CronService;
use OCP\IConfig;
/**
@ -33,21 +33,23 @@ use OCP\IConfig;
* @package OCA\Passman\BackgroundJob
*/
class ExpireCredentials extends TimedJob {
/** @var IConfig */
protected $config;
protected IConfig $config;
private CronService $cronService;
/**
* ExpireCredentials constructor.
* @param IConfig $config
* @param CronService $cronService
*/
public function __construct(IConfig $config) {
public function __construct(IConfig $config, CronService $cronService) {
// Run once per minute
$this->setInterval(60);
$this->config = $config;
$this->cronService = $cronService;
}
protected function run($argument) {
$app = new Application();
$container = $app->getContainer();
$container->query('CronService')->expireCredentials();
$this->cronService->expireCredentials();
}
}

View file

@ -22,14 +22,16 @@
*/
namespace OCA\Passman;
use OCP\L10N\IFactory;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
class Notifier implements INotifier {
protected $factory;
protected IFactory $factory;
public function __construct(\OCP\L10N\IFactory $factory) {
public function __construct(IFactory $factory) {
$this->factory = $factory;
}
@ -37,7 +39,7 @@ class Notifier implements INotifier {
* @param INotification $notification
* @param string $languageCode The code of the language that should be used to prepare the notification
*/
public function prepare(INotification $notification, string $languageCode): INotification {
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== 'passman') {
// Not my app => throw
throw new \InvalidArgumentException();
@ -50,7 +52,7 @@ public function prepare(INotification $notification, string $languageCode): INot
// Deal with known subjects
case 'credential_expired':
$notification->setParsedSubject(
(string) $l->t('Your credential "%s" expired, click here to update the credential.', $notification->getSubjectParameters())
(string)$l->t('Your credential "%s" expired, click here to update the credential.', $notification->getSubjectParameters())
);
// Deal with the actions for a known subject
@ -58,13 +60,13 @@ public function prepare(INotification $notification, string $languageCode): INot
switch ($action->getLabel()) {
case 'remind':
$action->setParsedLabel(
(string) $l->t('Remind me later')
(string)$l->t('Remind me later')
);
break;
case 'ignore':
$action->setParsedLabel(
(string) $l->t('Ignore')
(string)$l->t('Ignore')
);
break;
}
@ -76,7 +78,7 @@ public function prepare(INotification $notification, string $languageCode): INot
case 'credential_shared':
$notification->setParsedSubject(
(string) $l->t('%s shared "%s" with you. Click here to accept', $notification->getSubjectParameters())
(string)$l->t('%s shared "%s" with you. Click here to accept', $notification->getSubjectParameters())
);
// Deal with the actions for a known subject
@ -84,7 +86,7 @@ public function prepare(INotification $notification, string $languageCode): INot
switch ($action->getLabel()) {
case 'decline':
$action->setParsedLabel(
(string) $l->t('Decline')
(string)$l->t('Decline')
);
break;
}
@ -95,13 +97,13 @@ public function prepare(INotification $notification, string $languageCode): INot
case 'credential_share_denied':
$notification->setParsedSubject(
(string) $l->t('%s has declined your share request for "%s".', $notification->getSubjectParameters())
(string)$l->t('%s has declined your share request for "%s".', $notification->getSubjectParameters())
);
return $notification;
case 'credential_share_accepted':
$notification->setParsedSubject(
(string) $l->t('%s has accepted your share request for "%s".', $notification->getSubjectParameters())
(string)$l->t('%s has accepted your share request for "%s".', $notification->getSubjectParameters())
);
return $notification;
default:
@ -118,6 +120,7 @@ public function prepare(INotification $notification, string $languageCode): INot
public function getID(): string {
return 'passman';
}
/**
* Human readable name describing the notifier
*

View file

@ -78,7 +78,7 @@ class CredentialService {
*/
public function upd(Credential $credential) {
$credential = $this->encryptService->encryptCredential($credential);
return $this->credentialMapper->updateCredential($credential);
return $this->credentialMapper->updateCredential($credential->jsonSerialize(), false);
}
/**
@ -179,4 +179,4 @@ class CredentialService {
$credential = $this->credentialMapper->getCredentialByGUID($credential_guid, $user_id);
return $this->encryptService->decryptCredential($credential);
}
}
}

View file

@ -23,21 +23,22 @@
namespace OCA\Passman\Service;
use OCP\IConfig;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\ILogger;
use OCA\Passman\Utility\Utils;
use OCA\Passman\Activity;
use OCA\Passman\Utility\Utils;
use OCP\DB\Exception;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;
class CronService {
private $credentialService;
private $logger;
private $utils;
private $notificationService;
private $activityService;
private $db;
public function __construct(CredentialService $credentialService, ILogger $logger, Utils $utils, NotificationService $notificationService, ActivityService $activityService, IDBConnection $db) {
private CredentialService $credentialService;
private LoggerInterface $logger;
private Utils $utils;
private NotificationService $notificationService;
private ActivityService $activityService;
private IDBConnection $db;
public function __construct(CredentialService $credentialService, LoggerInterface $logger, Utils $utils, NotificationService $notificationService, ActivityService $activityService, IDBConnection $db) {
$this->credentialService = $credentialService;
$this->logger = $logger;
$this->utils = $utils;
@ -49,21 +50,25 @@ class CronService {
public function expireCredentials() {
$expired_credentials = $this->credentialService->getExpiredCredentials($this->utils->getTime());
foreach($expired_credentials as $credential){
foreach ($expired_credentials as $credential) {
$link = ''; // @TODO create direct link to credential
$sql = 'SELECT count(*) as `rows` from `*PREFIX*notifications` WHERE `subject`= \'credential_expired\' AND object_id=?';
$id = $credential->getId();
$result = $this->db->executeQuery($sql, array($id));
$this->logger->debug($credential->getLabel() .' is expired, checking notifications!', array('app' => 'passman'));
$notifications = intval($result->fetch()['rows']);
if($notifications === 0) {
$this->logger->debug($credential->getLabel() .' is expired, adding notification!', array('app' => 'passman'));
$this->activityService->add(
Activity::SUBJECT_ITEM_EXPIRED, array($credential->getLabel(), $credential->getUserId()),
'', array(),
$link, $credential->getUserId(), Activity::TYPE_ITEM_EXPIRED);
$this->notificationService->credentialExpiredNotification($credential);
try {
$result = $this->db->executeQuery($sql, array($id));
$this->logger->debug($credential->getLabel() . ' is expired, checking notifications!', array('app' => 'passman'));
$notifications = intval($result->fetch()['rows']);
if ($notifications === 0) {
$this->logger->debug($credential->getLabel() . ' is expired, adding notification!', array('app' => 'passman'));
$this->activityService->add(
Activity::SUBJECT_ITEM_EXPIRED, array($credential->getLabel(), $credential->getUserId()),
'', array(),
$link, $credential->getUserId(), Activity::TYPE_ITEM_EXPIRED);
$this->notificationService->credentialExpiredNotification($credential);
}
} catch (Exception $exception) {
$this->logger->error('Error while creating a notification: ' . $exception->getMessage(), array('app' => 'passman'));
}
}
}

View file

@ -23,32 +23,29 @@
namespace OCA\Passman\Service;
use OCP\IConfig;
use OCP\AppFramework\Db\DoesNotExistException;
use OCA\Passman\Db\FileMapper;
use OCP\Notification\IManager;
class NotificationService {
private $manager;
private IManager $manager;
public function __construct() {
$this->manager = \OC::$server->getNotificationManager();
}
function credentialExpiredNotification($credential){
function credentialExpiredNotification($credential) {
$urlGenerator = \OC::$server->getURLGenerator();
$link = $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('','index.php/apps/passman/#/vault/'. $credential->getVaultId() .'/edit/'. $credential->getId()));
$link = $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'index.php/apps/passman/#/vault/' . $credential->getVaultId() . '/edit/' . $credential->getId()));
$api = $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'index.php/apps/passman'));
$notification = $this->manager->createNotification();
$remindAction = $notification->createAction();
$remindAction->setLabel('remind')
->setLink($api. '/api/internal/notifications/remind/'. $credential->getId() , 'POST');
->setLink($api . '/api/internal/notifications/remind/' . $credential->getId(), 'POST');
$declineAction = $notification->createAction();
$declineAction->setLabel('ignore')
->setLink($api . '/api/internal/notifications/read/'. $credential->getId(), 'DELETE');
->setLink($api . '/api/internal/notifications/read/' . $credential->getId(), 'DELETE');
$notification->setApp('passman')
->setUser($credential->getUserId())
@ -63,15 +60,15 @@ class NotificationService {
}
function credentialSharedNotification($data){
function credentialSharedNotification($data) {
$urlGenerator = \OC::$server->getURLGenerator();
$link = $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('','index.php/apps/passman/#/'));
$link = $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'index.php/apps/passman/#/'));
$api = $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'index.php/apps/passman'));
$notification = $this->manager->createNotification();
$declineAction = $notification->createAction();
$declineAction->setLabel('decline')
->setLink($api . '/api/v2/sharing/decline/'. $data['req_id'], 'DELETE');
->setLink($api . '/api/v2/sharing/decline/' . $data['req_id'], 'DELETE');
$notification->setApp('passman')
->setUser($data['target_user'])
@ -85,7 +82,7 @@ class NotificationService {
}
function credentialDeclinedSharedNotification($data){
function credentialDeclinedSharedNotification($data) {
$notification = $this->manager->createNotification();
$notification->setApp('passman')
->setUser($data['target_user'])
@ -96,7 +93,7 @@ class NotificationService {
}
function credentialAcceptedSharedNotification($data){
function credentialAcceptedSharedNotification($data) {
$notification = $this->manager->createNotification();
$notification->setApp('passman')
->setUser($data['target_user'])
@ -106,4 +103,4 @@ class NotificationService {
$this->manager->notify($notification);
}
}
}

View file

@ -30,9 +30,9 @@ use OCA\Passman\Service\CredentialService;
use OCA\Passman\Service\EncryptService;
use OCA\Passman\Service\FileService;
use OCP\IDBConnection;
use OCP\ILogger;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use Psr\Log\LoggerInterface;
class ServerSideEncryption implements IRepairStep {
@ -46,7 +46,7 @@ class ServerSideEncryption implements IRepairStep {
/** @var string */
private $installedVersion;
/** @var ILogger */
/** @var LoggerInterface */
private $logger;
/** @var CredentialService */
@ -58,8 +58,8 @@ class ServerSideEncryption implements IRepairStep {
/** @var FileService */
private $fileService;
public function __construct(EncryptService $encryptService, IDBConnection $db, ILogger $logger, CredentialService $credentialService, CredentialRevisionService $revisionService,
FileService $fileService) {
public function __construct(EncryptService $encryptService, IDBConnection $db, LoggerInterface $logger, CredentialService $credentialService, CredentialRevisionService $revisionService,
FileService $fileService) {
$this->encryptService = $encryptService;
$this->db = $db;
$this->logger = $logger;
@ -83,7 +83,7 @@ class ServerSideEncryption implements IRepairStep {
}
}
private function fetchAll($sql){
private function fetchAll($sql) {
return $this->db->executeQuery($sql)->fetchAll();
}