Skip to content

Commit d9c325a

Browse files
committed
Add admin setting page with users defaults
Signed-off-by: Топонен Никита <Toponen@lanit.ru>
1 parent b8995c1 commit d9c325a

15 files changed

+427
-1
lines changed

appinfo/info.xml

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
</commands>
4747

4848
<settings>
49+
<admin>OCA\Notifications\Settings\Admin</admin>
50+
<admin-section>OCA\Notifications\Settings\AdminSection</admin-section>
4951
<personal>OCA\Notifications\Settings\Personal</personal>
5052
<personal-section>OCA\Notifications\Settings\PersonalSection</personal-section>
5153
</settings>

appinfo/routes.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
['name' => 'API#generateNotification', 'url' => '/api/{apiVersion}/admin_notifications/{userId}', 'verb' => 'POST', 'requirements' => ['apiVersion' => 'v(1|2)']],
3535

3636
['name' => 'Settings#personal', 'url' => '/api/{apiVersion}/settings', 'verb' => 'POST', 'requirements' => ['apiVersion' => 'v2']],
37-
],
37+
['name' => 'Settings#admin', 'url' => '/api/{apiVersion}/settings/admin', 'verb' => 'POST', 'requirements' => ['apiVersion' => 'v2']],
38+
],
3839
];

js/notifications-adminSettings.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*!
2+
* The buffer module from node.js, for the browser.
3+
*
4+
* @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
5+
* @license MIT
6+
*/
7+
8+
/*!
9+
* The buffer module from node.js, for the browser.
10+
*
11+
* @author Feross Aboukhadijeh <https://feross.org>
12+
* @license MIT
13+
*/
14+
15+
/*!
16+
* Vue.js v2.7.4
17+
* (c) 2014-2022 Evan You
18+
* Released under the MIT License.
19+
*/
20+
21+
/*! For license information please see CheckboxRadioSwitch.js.LICENSE.txt */
22+
23+
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */

js/notifications-adminSettings.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/AppInfo/Application.php

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use OCA\Notifications\App;
2828
use OCA\Notifications\Capabilities;
2929
use OCA\Notifications\Listener\BeforeTemplateRenderedListener;
30+
use OCA\Notifications\Listener\PostLoginListener;
31+
use OCA\Notifications\Listener\UserCreatedListener;
3032
use OCA\Notifications\Listener\UserDeletedListener;
3133
use OCA\Notifications\Notifier\AdminNotifications;
3234
use OCP\AppFramework\Bootstrap\IBootContext;
@@ -36,6 +38,8 @@
3638
use OCP\AppFramework\IAppContainer;
3739
use OCP\Notification\IManager;
3840
use OCP\User\Events\UserDeletedEvent;
41+
use OCP\User\Events\UserCreatedEvent;
42+
use OCP\User\Events\PostLoginEvent;
3943

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

5660
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
5761
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
62+
$context->registerEventListener(UserCreatedEvent::class, UserCreatedListener::class);
63+
$context->registerEventListener(PostLoginEvent::class, PostLoginListener::class);
5864
}
5965

6066
public function boot(IBootContext $context): void {

lib/Controller/SettingsController.php

+12
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,16 @@ public function personal(int $batchSetting, string $soundNotification, string $s
5858

5959
return new DataResponse();
6060
}
61+
62+
/**
63+
* @PasswordConfirmationRequired
64+
* @AuthorizedAdminSetting(settings=OCA\Notifications\Settings\Admin)
65+
*/
66+
public function admin(int $batchSetting, string $soundNotification, string $soundTalk): DataResponse {
67+
$this->config->setAppValue(Application::APP_ID, 'setting_batchtime', $batchSetting);
68+
$this->config->setAppValue(Application::APP_ID, 'sound_notification', $soundNotification !== 'no' ? 'yes' : 'no');
69+
$this->config->setAppValue(Application::APP_ID, 'sound_talk', $soundTalk !== 'no' ? 'yes' : 'no');
70+
71+
return new DataResponse();
72+
}
6173
}

lib/Listener/PostLoginListener.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace OCA\Notifications\Listener;
4+
5+
use OCA\Notifications\AppInfo\Application;
6+
use OCA\Notifications\Model\Settings;
7+
use OCA\Notifications\Model\SettingsMapper;
8+
use OCP\AppFramework\Db\DoesNotExistException;
9+
use OCP\User\Events\PostLoginEvent;
10+
use OCP\EventDispatcher\IEventListener;
11+
use OCP\EventDispatcher\Event;
12+
use OCP\IUserManager;
13+
use OCP\IConfig;
14+
15+
class PostLoginListener implements IEventListener {
16+
/** @var IUserManager */
17+
private $userManager;
18+
/** @var SettingsMapper */
19+
private $settingsMapper;
20+
/** @var IConfig */
21+
private $config;
22+
23+
public function __construct(IUserManager $userManager, SettingsMapper $settingsMapper, IConfig $config) {
24+
$this->userManager = $userManager;
25+
$this->settingsMapper = $settingsMapper;
26+
$this->config = $config;
27+
}
28+
29+
public function handle(Event $event): void {
30+
if (!($event instanceof PostLoginEvent)) {
31+
// Unrelated
32+
return;
33+
}
34+
35+
$userId = $event->getUser()->getUID();
36+
37+
$default_sound_notification = $this->config->getAppValue(Application::APP_ID, 'sound_notification') === 'yes' ? 'yes' : 'no';
38+
$default_sound_talk = $this->config->getAppValue(Application::APP_ID, 'sound_talk') === 'yes' ? 'yes' : 'no';
39+
$default_batchtime = $this->config->getAppValue(Application::APP_ID, 'setting_batchtime');
40+
41+
if ($default_batchtime != Settings::EMAIL_SEND_WEEKLY
42+
&& $default_batchtime != Settings::EMAIL_SEND_DAILY
43+
&& $default_batchtime != Settings::EMAIL_SEND_3HOURLY
44+
&& $default_batchtime != Settings::EMAIL_SEND_HOURLY
45+
&& $default_batchtime != Settings::EMAIL_SEND_OFF) {
46+
$default_batchtime = Settings::EMAIL_SEND_3HOURLY;
47+
}
48+
49+
try {
50+
$this->settingsMapper->getSettingsByUser($userId);
51+
} catch (DoesNotExistException $e) {
52+
$this->config->setUserValue($userId, Application::APP_ID, 'sound_notification', $default_sound_notification);
53+
$this->config->setUserValue($userId, Application::APP_ID, 'sound_talk', $default_sound_talk);
54+
$this->settingsMapper->setBatchSettingForUser($userId, $default_batchtime);
55+
}
56+
}
57+
}

lib/Listener/UserCreatedListener.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace OCA\Notifications\Listener;
4+
5+
use OCA\Notifications\AppInfo\Application;
6+
use OCA\Notifications\Model\Settings;
7+
use OCA\Notifications\Model\SettingsMapper;
8+
use OCP\IUserManager;
9+
use OCP\User\Events\UserCreatedEvent;
10+
use OCP\EventDispatcher\IEventListener;
11+
use OCP\EventDispatcher\Event;
12+
use OCP\IConfig;
13+
14+
class UserCreatedListener implements IEventListener {
15+
/** @var IUserManager */
16+
private $userManager;
17+
/** @var SettingsMapper */
18+
private $settingsMapper;
19+
/** @var IConfig */
20+
private $config;
21+
22+
23+
public function __construct(IUserManager $userManager, SettingsMapper $settingsMapper, IConfig $config) {
24+
$this->userManager = $userManager;
25+
$this->settingsMapper = $settingsMapper;
26+
$this->config = $config;
27+
}
28+
29+
public function handle(Event $event): void {
30+
if (!($event instanceof UserCreatedEvent)) {
31+
// Unrelated
32+
return;
33+
}
34+
35+
$userId = $event->getUser()->getUID();
36+
37+
$default_sound_notification = $this->config->getAppValue(Application::APP_ID, 'sound_notification') === 'yes' ? 'yes' : 'no';
38+
$default_sound_talk = $this->config->getAppValue(Application::APP_ID, 'sound_talk') === 'yes' ? 'yes' : 'no';
39+
$default_batchtime = $this->config->getAppValue(Application::APP_ID, 'setting_batchtime');
40+
41+
if ($default_batchtime != Settings::EMAIL_SEND_WEEKLY
42+
&& $default_batchtime != Settings::EMAIL_SEND_DAILY
43+
&& $default_batchtime != Settings::EMAIL_SEND_3HOURLY
44+
&& $default_batchtime != Settings::EMAIL_SEND_HOURLY
45+
&& $default_batchtime != Settings::EMAIL_SEND_OFF) {
46+
$default_batchtime = Settings::EMAIL_SEND_3HOURLY;
47+
}
48+
49+
$this->config->setUserValue($userId, Application::APP_ID, 'sound_notification', $default_sound_notification);
50+
$this->config->setUserValue($userId, Application::APP_ID, 'sound_talk', $default_sound_talk);
51+
$this->settingsMapper->setBatchSettingForUser($userId, $default_batchtime);
52+
}
53+
}

lib/Settings/Admin.php

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OCA\Notifications\Settings;
6+
7+
use OCA\Notifications\AppInfo\Application;
8+
use OCA\Notifications\Model\Settings;
9+
use OCA\Notifications\Model\SettingsMapper;
10+
use OCP\AppFramework\Db\DoesNotExistException;
11+
use OCP\AppFramework\Http\TemplateResponse;
12+
use OCP\AppFramework\Services\IInitialState;
13+
use OCP\IConfig;
14+
use OCP\IL10N;
15+
use OCP\IUser;
16+
use OCP\Settings\ISettings;
17+
use OCP\IUserSession;
18+
use OCP\Util;
19+
20+
class Admin implements ISettings
21+
{
22+
/** @var \OCP\IConfig */
23+
protected $config;
24+
25+
/** @var \OCP\IL10N */
26+
protected $l10n;
27+
28+
/** @var SettingsMapper */
29+
private $settingsMapper;
30+
31+
/** @var IUserSession */
32+
private $session;
33+
34+
/** @var IInitialState */
35+
private $initialState;
36+
37+
public function __construct(IConfig $config,
38+
IL10N $l10n,
39+
IUserSession $session,
40+
SettingsMapper $settingsMapper,
41+
IInitialState $initialState)
42+
{
43+
$this->config = $config;
44+
$this->l10n = $l10n;
45+
$this->settingsMapper = $settingsMapper;
46+
$this->session = $session;
47+
$this->initialState = $initialState;
48+
}
49+
50+
/**
51+
* @return TemplateResponse
52+
*/
53+
public function getForm(): TemplateResponse
54+
{
55+
Util::addScript('notifications', 'notifications-adminSettings');
56+
57+
$default_sound_notification = $this->config->getAppValue(Application::APP_ID, 'sound_notification') === 'yes' ? 'yes' : 'no';
58+
$default_sound_talk = $this->config->getAppValue(Application::APP_ID, 'sound_talk') === 'yes' ? 'yes' : 'no';
59+
$default_batchtime = $this->config->getAppValue(Application::APP_ID, 'setting_batchtime');
60+
61+
if ($default_batchtime != Settings::EMAIL_SEND_WEEKLY
62+
&& $default_batchtime != Settings::EMAIL_SEND_DAILY
63+
&& $default_batchtime != Settings::EMAIL_SEND_3HOURLY
64+
&& $default_batchtime != Settings::EMAIL_SEND_HOURLY
65+
&& $default_batchtime != Settings::EMAIL_SEND_OFF) {
66+
$default_batchtime = Settings::EMAIL_SEND_3HOURLY;
67+
}
68+
69+
$this->initialState->provideInitialState('config', [
70+
'setting' => 'admin',
71+
'setting_batchtime' => $default_batchtime,
72+
'sound_notification' => $default_sound_notification === 'yes',
73+
'sound_talk' => $default_sound_talk === 'yes',
74+
]);
75+
76+
return new TemplateResponse('notifications', 'settings/admin');
77+
}
78+
79+
/**
80+
* @return string the section ID, e.g. 'sharing'
81+
*/
82+
public function getSection(): string
83+
{
84+
return 'notifications';
85+
}
86+
87+
/**
88+
* @return int whether the form should be rather on the top or bottom of
89+
* the admin section. The forms are arranged in ascending order of the
90+
* priority values. It is required to return a value between 0 and 100.
91+
*
92+
* E.g.: 70
93+
*/
94+
public function getPriority(): int
95+
{
96+
return 20;
97+
}
98+
}

lib/Settings/AdminSection.php

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OCA\Notifications\Settings;
6+
7+
use OCP\IL10N;
8+
use OCP\IURLGenerator;
9+
use OCP\Settings\IIconSection;
10+
11+
class AdminSection implements IIconSection {
12+
/** @var IL10N */
13+
private $l;
14+
15+
/** @var IURLGenerator */
16+
private $url;
17+
18+
public function __construct(IURLGenerator $url, IL10N $l) {
19+
$this->url = $url;
20+
$this->l = $l;
21+
}
22+
23+
/**
24+
* returns the relative path to an 16*16 icon describing the section.
25+
* e.g. '/core/img/places/files.svg'
26+
*
27+
* @returns string
28+
* @since 12
29+
*/
30+
public function getIcon(): string {
31+
return $this->url->imagePath('notifications', 'notifications-dark.svg');
32+
}
33+
34+
/**
35+
* returns the ID of the section. It is supposed to be a lower case string,
36+
* e.g. 'ldap'
37+
*
38+
* @returns string
39+
* @since 9.1
40+
*/
41+
public function getID(): string {
42+
return 'notifications';
43+
}
44+
45+
/**
46+
* returns the translated name as it should be displayed, e.g. 'LDAP / AD
47+
* integration'. Use the L10N service to translate it.
48+
*
49+
* @return string
50+
* @since 9.1
51+
*/
52+
public function getName(): string {
53+
return $this->l->t('Notifications');
54+
}
55+
56+
/**
57+
* @return int whether the form should be rather on the top or bottom of
58+
* the settings navigation. The sections are arranged in ascending order of
59+
* the priority values. It is required to return a value between 0 and 99.
60+
*
61+
* E.g.: 70
62+
* @since 9.1
63+
*/
64+
public function getPriority(): int {
65+
return 10;
66+
}
67+
}

src/adminSettings.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Vue from 'vue'
2+
import AdminSettings from './views/AdminSettings'
3+
4+
Vue.prototype.t = t
5+
Vue.prototype.n = n
6+
7+
export default new Vue({
8+
el: '#notifications-admin-settings',
9+
render: h => h(AdminSettings),
10+
})

0 commit comments

Comments
 (0)