Skip to content

Commit

Permalink
Add admin setting page with users defaults
Browse files Browse the repository at this point in the history
Signed-off-by: Топонен Никита <Toponen@lanit.ru>
  • Loading branch information
natoponen committed Sep 5, 2022
1 parent b8995c1 commit d9c325a
Show file tree
Hide file tree
Showing 15 changed files with 427 additions and 1 deletion.
2 changes: 2 additions & 0 deletions appinfo/info.xml
Expand Up @@ -46,6 +46,8 @@
</commands>

<settings>
<admin>OCA\Notifications\Settings\Admin</admin>
<admin-section>OCA\Notifications\Settings\AdminSection</admin-section>
<personal>OCA\Notifications\Settings\Personal</personal>
<personal-section>OCA\Notifications\Settings\PersonalSection</personal-section>
</settings>
Expand Down
3 changes: 2 additions & 1 deletion appinfo/routes.php
Expand Up @@ -34,5 +34,6 @@
['name' => 'API#generateNotification', 'url' => '/api/{apiVersion}/admin_notifications/{userId}', 'verb' => 'POST', 'requirements' => ['apiVersion' => 'v(1|2)']],

['name' => 'Settings#personal', 'url' => '/api/{apiVersion}/settings', 'verb' => 'POST', 'requirements' => ['apiVersion' => 'v2']],
],
['name' => 'Settings#admin', 'url' => '/api/{apiVersion}/settings/admin', 'verb' => 'POST', 'requirements' => ['apiVersion' => 'v2']],
],
];
3 changes: 3 additions & 0 deletions js/notifications-adminSettings.js

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions js/notifications-adminSettings.js.LICENSE.txt
@@ -0,0 +1,23 @@
/*!
* The buffer module from node.js, for the browser.
*
* @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
* @license MIT
*/

/*!
* The buffer module from node.js, for the browser.
*
* @author Feross Aboukhadijeh <https://feross.org>
* @license MIT
*/

/*!
* Vue.js v2.7.4
* (c) 2014-2022 Evan You
* Released under the MIT License.
*/

/*! For license information please see CheckboxRadioSwitch.js.LICENSE.txt */

/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
1 change: 1 addition & 0 deletions js/notifications-adminSettings.js.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions lib/AppInfo/Application.php
Expand Up @@ -27,6 +27,8 @@
use OCA\Notifications\App;
use OCA\Notifications\Capabilities;
use OCA\Notifications\Listener\BeforeTemplateRenderedListener;
use OCA\Notifications\Listener\PostLoginListener;
use OCA\Notifications\Listener\UserCreatedListener;
use OCA\Notifications\Listener\UserDeletedListener;
use OCA\Notifications\Notifier\AdminNotifications;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand All @@ -36,6 +38,8 @@
use OCP\AppFramework\IAppContainer;
use OCP\Notification\IManager;
use OCP\User\Events\UserDeletedEvent;
use OCP\User\Events\UserCreatedEvent;
use OCP\User\Events\PostLoginEvent;

class Application extends \OCP\AppFramework\App implements IBootstrap {
public const APP_ID = 'notifications';
Expand All @@ -55,6 +59,8 @@ public function register(IRegistrationContext $context): void {

$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
$context->registerEventListener(UserCreatedEvent::class, UserCreatedListener::class);
$context->registerEventListener(PostLoginEvent::class, PostLoginListener::class);
}

public function boot(IBootContext $context): void {
Expand Down
12 changes: 12 additions & 0 deletions lib/Controller/SettingsController.php
Expand Up @@ -58,4 +58,16 @@ public function personal(int $batchSetting, string $soundNotification, string $s

return new DataResponse();
}

/**
* @PasswordConfirmationRequired
* @AuthorizedAdminSetting(settings=OCA\Notifications\Settings\Admin)
*/
public function admin(int $batchSetting, string $soundNotification, string $soundTalk): DataResponse {
$this->config->setAppValue(Application::APP_ID, 'setting_batchtime', $batchSetting);
$this->config->setAppValue(Application::APP_ID, 'sound_notification', $soundNotification !== 'no' ? 'yes' : 'no');
$this->config->setAppValue(Application::APP_ID, 'sound_talk', $soundTalk !== 'no' ? 'yes' : 'no');

return new DataResponse();
}
}
57 changes: 57 additions & 0 deletions lib/Listener/PostLoginListener.php
@@ -0,0 +1,57 @@
<?php

namespace OCA\Notifications\Listener;

use OCA\Notifications\AppInfo\Application;
use OCA\Notifications\Model\Settings;
use OCA\Notifications\Model\SettingsMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\User\Events\PostLoginEvent;
use OCP\EventDispatcher\IEventListener;
use OCP\EventDispatcher\Event;
use OCP\IUserManager;
use OCP\IConfig;

class PostLoginListener implements IEventListener {
/** @var IUserManager */
private $userManager;
/** @var SettingsMapper */
private $settingsMapper;
/** @var IConfig */
private $config;

public function __construct(IUserManager $userManager, SettingsMapper $settingsMapper, IConfig $config) {
$this->userManager = $userManager;
$this->settingsMapper = $settingsMapper;
$this->config = $config;
}

public function handle(Event $event): void {
if (!($event instanceof PostLoginEvent)) {
// Unrelated
return;
}

$userId = $event->getUser()->getUID();

$default_sound_notification = $this->config->getAppValue(Application::APP_ID, 'sound_notification') === 'yes' ? 'yes' : 'no';
$default_sound_talk = $this->config->getAppValue(Application::APP_ID, 'sound_talk') === 'yes' ? 'yes' : 'no';
$default_batchtime = $this->config->getAppValue(Application::APP_ID, 'setting_batchtime');

if ($default_batchtime != Settings::EMAIL_SEND_WEEKLY
&& $default_batchtime != Settings::EMAIL_SEND_DAILY
&& $default_batchtime != Settings::EMAIL_SEND_3HOURLY
&& $default_batchtime != Settings::EMAIL_SEND_HOURLY
&& $default_batchtime != Settings::EMAIL_SEND_OFF) {
$default_batchtime = Settings::EMAIL_SEND_3HOURLY;
}

try {
$this->settingsMapper->getSettingsByUser($userId);
} catch (DoesNotExistException $e) {
$this->config->setUserValue($userId, Application::APP_ID, 'sound_notification', $default_sound_notification);
$this->config->setUserValue($userId, Application::APP_ID, 'sound_talk', $default_sound_talk);
$this->settingsMapper->setBatchSettingForUser($userId, $default_batchtime);
}
}
}
53 changes: 53 additions & 0 deletions lib/Listener/UserCreatedListener.php
@@ -0,0 +1,53 @@
<?php

namespace OCA\Notifications\Listener;

use OCA\Notifications\AppInfo\Application;
use OCA\Notifications\Model\Settings;
use OCA\Notifications\Model\SettingsMapper;
use OCP\IUserManager;
use OCP\User\Events\UserCreatedEvent;
use OCP\EventDispatcher\IEventListener;
use OCP\EventDispatcher\Event;
use OCP\IConfig;

class UserCreatedListener implements IEventListener {
/** @var IUserManager */
private $userManager;
/** @var SettingsMapper */
private $settingsMapper;
/** @var IConfig */
private $config;


public function __construct(IUserManager $userManager, SettingsMapper $settingsMapper, IConfig $config) {
$this->userManager = $userManager;
$this->settingsMapper = $settingsMapper;
$this->config = $config;
}

public function handle(Event $event): void {
if (!($event instanceof UserCreatedEvent)) {
// Unrelated
return;
}

$userId = $event->getUser()->getUID();

$default_sound_notification = $this->config->getAppValue(Application::APP_ID, 'sound_notification') === 'yes' ? 'yes' : 'no';
$default_sound_talk = $this->config->getAppValue(Application::APP_ID, 'sound_talk') === 'yes' ? 'yes' : 'no';
$default_batchtime = $this->config->getAppValue(Application::APP_ID, 'setting_batchtime');

if ($default_batchtime != Settings::EMAIL_SEND_WEEKLY
&& $default_batchtime != Settings::EMAIL_SEND_DAILY
&& $default_batchtime != Settings::EMAIL_SEND_3HOURLY
&& $default_batchtime != Settings::EMAIL_SEND_HOURLY
&& $default_batchtime != Settings::EMAIL_SEND_OFF) {
$default_batchtime = Settings::EMAIL_SEND_3HOURLY;
}

$this->config->setUserValue($userId, Application::APP_ID, 'sound_notification', $default_sound_notification);
$this->config->setUserValue($userId, Application::APP_ID, 'sound_talk', $default_sound_talk);
$this->settingsMapper->setBatchSettingForUser($userId, $default_batchtime);
}
}
98 changes: 98 additions & 0 deletions lib/Settings/Admin.php
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace OCA\Notifications\Settings;

use OCA\Notifications\AppInfo\Application;
use OCA\Notifications\Model\Settings;
use OCA\Notifications\Model\SettingsMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUser;
use OCP\Settings\ISettings;
use OCP\IUserSession;
use OCP\Util;

class Admin implements ISettings
{
/** @var \OCP\IConfig */
protected $config;

/** @var \OCP\IL10N */
protected $l10n;

/** @var SettingsMapper */
private $settingsMapper;

/** @var IUserSession */
private $session;

/** @var IInitialState */
private $initialState;

public function __construct(IConfig $config,
IL10N $l10n,
IUserSession $session,
SettingsMapper $settingsMapper,
IInitialState $initialState)
{
$this->config = $config;
$this->l10n = $l10n;
$this->settingsMapper = $settingsMapper;
$this->session = $session;
$this->initialState = $initialState;
}

/**
* @return TemplateResponse
*/
public function getForm(): TemplateResponse
{
Util::addScript('notifications', 'notifications-adminSettings');

$default_sound_notification = $this->config->getAppValue(Application::APP_ID, 'sound_notification') === 'yes' ? 'yes' : 'no';
$default_sound_talk = $this->config->getAppValue(Application::APP_ID, 'sound_talk') === 'yes' ? 'yes' : 'no';
$default_batchtime = $this->config->getAppValue(Application::APP_ID, 'setting_batchtime');

if ($default_batchtime != Settings::EMAIL_SEND_WEEKLY
&& $default_batchtime != Settings::EMAIL_SEND_DAILY
&& $default_batchtime != Settings::EMAIL_SEND_3HOURLY
&& $default_batchtime != Settings::EMAIL_SEND_HOURLY
&& $default_batchtime != Settings::EMAIL_SEND_OFF) {
$default_batchtime = Settings::EMAIL_SEND_3HOURLY;
}

$this->initialState->provideInitialState('config', [
'setting' => 'admin',
'setting_batchtime' => $default_batchtime,
'sound_notification' => $default_sound_notification === 'yes',
'sound_talk' => $default_sound_talk === 'yes',
]);

return new TemplateResponse('notifications', 'settings/admin');
}

/**
* @return string the section ID, e.g. 'sharing'
*/
public function getSection(): string
{
return 'notifications';
}

/**
* @return int whether the form should be rather on the top or bottom of
* the admin section. The forms are arranged in ascending order of the
* priority values. It is required to return a value between 0 and 100.
*
* E.g.: 70
*/
public function getPriority(): int
{
return 20;
}
}
67 changes: 67 additions & 0 deletions lib/Settings/AdminSection.php
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace OCA\Notifications\Settings;

use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\Settings\IIconSection;

class AdminSection implements IIconSection {
/** @var IL10N */
private $l;

/** @var IURLGenerator */
private $url;

public function __construct(IURLGenerator $url, IL10N $l) {
$this->url = $url;
$this->l = $l;
}

/**
* returns the relative path to an 16*16 icon describing the section.
* e.g. '/core/img/places/files.svg'
*
* @returns string
* @since 12
*/
public function getIcon(): string {
return $this->url->imagePath('notifications', 'notifications-dark.svg');
}

/**
* returns the ID of the section. It is supposed to be a lower case string,
* e.g. 'ldap'
*
* @returns string
* @since 9.1
*/
public function getID(): string {
return 'notifications';
}

/**
* returns the translated name as it should be displayed, e.g. 'LDAP / AD
* integration'. Use the L10N service to translate it.
*
* @return string
* @since 9.1
*/
public function getName(): string {
return $this->l->t('Notifications');
}

/**
* @return int whether the form should be rather on the top or bottom of
* the settings navigation. The sections are arranged in ascending order of
* the priority values. It is required to return a value between 0 and 99.
*
* E.g.: 70
* @since 9.1
*/
public function getPriority(): int {
return 10;
}
}
10 changes: 10 additions & 0 deletions src/adminSettings.js
@@ -0,0 +1,10 @@
import Vue from 'vue'
import AdminSettings from './views/AdminSettings'

Vue.prototype.t = t
Vue.prototype.n = n

export default new Vue({
el: '#notifications-admin-settings',
render: h => h(AdminSettings),
})

0 comments on commit d9c325a

Please sign in to comment.