Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from owncloud/feat/notifications-user-disable
Browse files Browse the repository at this point in the history
feat: send notification when license limit was exceeded
  • Loading branch information
wkloucek authored May 3, 2021
2 parents d3d2af9 + e133b75 commit 586fc88
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 9 deletions.
4 changes: 4 additions & 0 deletions appinfo/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

$app = new \OCA\QNAP\Application();
$app->registerNotifier();
46 changes: 46 additions & 0 deletions lib/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @author Vincent Petry <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\QNAP;

use OCP\AppFramework\App;

class Application extends App {
public function __construct(array $urlParams = []) {
parent::__construct('qnap', $urlParams);
}

/**
* Registers the notifier
*/
public function registerNotifier() {
$manager = $this->getContainer()->getServer()->getNotificationManager();
$manager->registerNotifier(function () {
return $this->getContainer()->query(Notifier::class);
}, function () {
$l = \OC::$server->getL10N('qnap');
return [
'id' => 'qnap',
'name' => $l->t('QNAP'),
];
});
}
}
40 changes: 33 additions & 7 deletions lib/Command/CheckActiveUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
namespace OCA\QNAP\Command;

use OCA\QNAP\QnapLicense;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Mail\IMailer;
use OCP\Notification\IManager;
use OCP\Template;
use OCP\Util;
use Symfony\Component\Console\Command\Command;
Expand All @@ -54,18 +55,24 @@ class CheckActiveUsers extends Command {

/** @var int */
private $numberOfActiveUsers = 0;

/**
* @var IManager
*/
private $notificationManager;
/**
* @var IConfig
* @var IURLGenerator
*/
private $config;
private $urlGenerator;

public function __construct(IUserManager $userManager, IMailer $mailer, IL10N $l10n, IGroupManager $groupManager, IConfig $config) {
public function __construct(IUserManager $userManager, IMailer $mailer, IL10N $l10n, IGroupManager $groupManager, IManager $notificationManager, IURLGenerator $urlGenerator) {
parent::__construct();
$this->userManager = $userManager;
$this->mailer = $mailer;
$this->l10n = $l10n;
$this->groupManager = $groupManager;
$this->config = $config;
$this->notificationManager = $notificationManager;
$this->urlGenerator = $urlGenerator;
}

protected function configure() {
Expand All @@ -86,6 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
}
return 0;
}
$this->sendNotification($output);
$this->sendEMailToAdmin($output);
$this->disableExceededUsers($output, $licensedUsers);

Expand All @@ -108,11 +116,11 @@ private function sendEMailToAdmin(OutputInterface $output): void {
// send it out now
$message = $this->mailer->createMessage();
$message->setTo($recipients);
$message->setSubject((string) $this->l10n->t('Action Required: Your ownCloud User Licenses are exceeded'));
$message->setSubject((string) $this->l10n->t('Action Required: Your ownCloud licenses\' user limit was exceeded'));
$message->setPlainBody($plainBody);
$message->setFrom([
Util::getDefaultEmailAddress('qnap-noreply') =>
(string)$this->l10n->t('[Dummy] License Control'),
(string)$this->l10n->t('ownCloud on QNAP'),
]);
try {
$this->mailer->send($message);
Expand Down Expand Up @@ -168,4 +176,22 @@ private function buildRecipients(): array {

return $recipients;
}

private function sendNotification(OutputInterface $output): void {
$link = $this->urlGenerator->linkTo('', 'index.php/settings/admin?sectionid=general');

$time = \time();
foreach ($this->adminUsers as $a) {
$rnd = \random_int(PHP_INT_MIN, PHP_INT_MAX);
$notification = $this->notificationManager->createNotification();
$notification->setApp('qnap');
$notification->setObject('qnap', "$time-$rnd");
$notification->setUser($a->getUserName());
$notification->setLink($link);
$notification->setSubject('qnap-license-exceeded');
$notification->setDateTime(new \DateTime());

$this->notificationManager->notify($notification);
}
}
}
38 changes: 38 additions & 0 deletions lib/Notifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace OCA\QNAP;

use OCP\L10N\IFactory;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;

class Notifier implements INotifier {

/**
* @var IFactory
*/
private $l10NFactory;

public function __construct(IFactory $l10NFactory) {
$this->l10NFactory = $l10NFactory;
}

public function prepare(INotification $notification, $languageCode) {
if ($notification->getApp() !== 'qnap') {
throw new \InvalidArgumentException();
}

if ($notification->getSubject() !== 'qnap-license-exceeded') {
return $notification;
}
$l = $this->l10NFactory->get('qnap', $languageCode);

$message = (string)$l->t('The maximum user limit was exceeded. ');
$message .= (string)$l->t('To add more users, purchase a license from https://software.qnap.com/owncloud.html');

$notification->setParsedSubject($l->t('Action Required: Your ownCloud licenses\' user limit was exceeded'));
$notification->setParsedMessage($message);

return $notification;
}
}
4 changes: 2 additions & 2 deletions templates/mail/exceeded/plain.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
p($l->t('Dear ownCloud - Admin,'));
print_unescaped("\n");
print_unescaped("\n");
p($l->t('unfortunately, you exceeded the maximum number of users within your current license.'));
p($l->t('The maximum user limit was exceeded.'));
print_unescaped("\n");
p($l->t('To upgrade the number of users please visit: https://software.qnap.com/owncloud.html'));
p($l->t('To add more users, purchase a license from https://software.qnap.com/owncloud.html'));
print_unescaped("\n");
print_unescaped("\n");
// TRANSLATORS term at the end of a mail
Expand Down

0 comments on commit 586fc88

Please sign in to comment.