Skip to content

Instantly share code, notes, and snippets.

@Moralpose
Created May 3, 2022 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Moralpose/8b86f4dffd2074b41ccfa2970a97dac7 to your computer and use it in GitHub Desktop.
Save Moralpose/8b86f4dffd2074b41ccfa2970a97dac7 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace OCA\AppMailNotifications\AppInfo;
use OCA\MyAdminMonitoring\Listener\AppManagement;
use OCP\App\ManagerEvent;
use OCP\AppFramework\App;
use OCP\Log\Audit\CriticalActionPerformedEvent;
use OC\User\Session as UserSession;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use Psr\Log\LoggerInterface;
use OCP\Mail\IMailer;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Application extends App implements IBootstrap {
protected $logger;
public function __construct() {
parent::__construct('myadminmonitoring');
}
public function register(IRegistrationContext $context): void {
$context->registerEventListener(CriticalActionPerformedEvent::class, CriticalActionPerformedEventListener::class);
}
public function boot(IBootContext $context): void {
$serverContainer = $context->getServerContainer();
/** @var EventDispatcherInterface $eventDispatcher */
$eventDispatcher = $serverContainer->get(EventDispatcherInterface::class);
$mailer = $serverContainer->get(IMailer::class);
}
private function appHooks(LoggerInterface $logger,
EventDispatcherInterface $eventDispatcher): void {
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function (ManagerEvent $event) use ($logger) {
$appActions = new AppManagement($logger);
$appActions->enableApp($event->getAppID());
});
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function (ManagerEvent $event) use ($logger) {
$appActions = new AppManagement($logger);
$appActions->enableAppForGroups($event->getAppID(), $event->getGroups());
});
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function (ManagerEvent $event) use ($logger) {
$appActions = new AppManagement($logger);
$appActions->disableApp($event->getAppID());
});
}
}
<?php
declare(strict_types=1);
namespace OCA\MyAdminMonitoring\Listener;
use OCP\Mail\IMailer;
class AppManagement {
private IMailer $mailer;
public function __construct(IMailer $mailer) {
$this->mailer = $mailer;
}
/**
* @param string $appName
*/
public function enableApp(string $appName): void {
$this->mail('App "%s" enabled',
['app' => $appName]
);
}
/**
* @param string $appName
* @param string[] $groups
*/
public function enableAppForGroups(string $appName, array $groups): void {
$this->mail('App "%1$s" enabled for groups: %2$s',
['app' => $appName, 'groups' => implode(', ', $groups)]
);
}
/**
* @param string $appName
*/
public function disableApp(string $appName): void {
$this->mail('App "%s" disabled',
['app' => $appName]
);
}
private function mail(string $string, array $params): void {
$text =
vsprintf(
$string,
$params
);
$mailTemplate = $this->mailer->createEMailTemplate('app_mail_notifications.mail');
$mailTemplate->setSubject($text);
$mailTemplate->addBodyText($text);
$mailer = \OC::$server->getMailer();
$message = $this->mailer->createMessage();
$message->setFrom(['wnd@xiller.com' => 'Nextcloud Notifier']);
$message->setTo(['njiandzebewilfriedjunior.com' => 'Recipient']);
$message->useTemplate($mailTemplate);
$this->mailer->send($message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment