diff --git a/appinfo/routes.php b/appinfo/routes.php index 6e65217..3676ca5 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -79,5 +79,10 @@ return [ 'providerId' => '1' ] ], + [ + 'name' => 'Timezone#setTimezone', + 'url' => '/config/timezone', + 'verb' => 'POST', + ], ], ]; diff --git a/js/timezone.js b/js/timezone.js new file mode 100644 index 0000000..e48e622 --- /dev/null +++ b/js/timezone.js @@ -0,0 +1,39 @@ +/* global $, jstz, OC */ + +/* + * @copyright 2019 Christoph Wurst + * + * @author 2019 Christoph Wurst + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see . + */ + +console.debug('updating timezone and offset for SAML user') + +$.ajax({ + type: 'POST', + url: OC.generateUrl('/apps/user_saml/config/timezone'), + data: { + timezone: jstz.determine().name(), + timezoneOffset: (-new Date().getTimezoneOffset() / 60) + }, + error: function(e) { + console.error('could not set timezone and offset for SAML user', e) + }, + success: function() { + console.info('timezone and offset udpated for SAML user') + } +}) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 126099d..3ab8181 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -52,6 +52,7 @@ class Application extends App { }); $container->registerMiddleWare('OnlyLoggedInMiddleware'); + $this->timezoneHandling(); } public function registerDavAuth() { @@ -63,4 +64,26 @@ class Application extends App { $event->getServer()->addPlugin($container->query(DavPlugin::class)); }); } + + private function timezoneHandling() { + $container = $this->getContainer(); + + $userSession = $container->getServer()->getUserSession(); + $session = $container->getServer()->getSession(); + $config = $container->getServer()->getConfig(); + + $dispatcher = $container->getServer()->getEventDispatcher(); + $dispatcher->addListener('OCA\Files::loadAdditionalScripts', function() use ($session, $config, $userSession) { + if (!$userSession->isLoggedIn()) { + return; + } + + $user = $userSession->getUser(); + $timezoneDB = $config->getUserValue($user->getUID(), 'core', 'timezone', ''); + + if ($timezoneDB === '' || !$session->exists('timezone')) { + \OCP\Util::addScript('user_saml', 'timezone'); + } + }); + } } diff --git a/lib/Controller/TimezoneController.php b/lib/Controller/TimezoneController.php new file mode 100644 index 0000000..46218c7 --- /dev/null +++ b/lib/Controller/TimezoneController.php @@ -0,0 +1,68 @@ + + * + * @author Roeland Jago Douma + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see . + * + */ + +namespace OCA\User_SAML\Controller; + +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\JSONResponse; +use OCP\IConfig; +use OCP\IRequest; +use OCP\ISession; + +class TimezoneController extends Controller { + + /** @var IConfig */ + private $config; + /** @var string */ + private $userId; + /** @var ISession */ + private $session; + + public function __construct($appName, + IRequest $request, + IConfig $config, + $userId, + ISession $session) { + parent::__construct($appName, $request); + $this->config = $config; + $this->userId = $userId; + $this->session = $session; + } + + /** + * @NoAdminRequired + * @UseSession + * + * @param string $timezone + * @param int $timezoneOffset + * @return JSONResponse + * @throws \OCP\PreConditionNotMetException + */ + public function setTimezone($timezone, $timezoneOffset) { + $this->config->setUserValue($this->userId, 'core', 'timezone', $timezone); + $this->session->set('timezone', $timezoneOffset); + + return new JSONResponse(); + } +} diff --git a/tests/unit/AppInfo/RoutesTest.php b/tests/unit/AppInfo/RoutesTest.php index b8efaba..e806fa9 100644 --- a/tests/unit/AppInfo/RoutesTest.php +++ b/tests/unit/AppInfo/RoutesTest.php @@ -85,6 +85,11 @@ class Test extends TestCase { 'providerId' => '1' ] ], + [ + 'name' => 'Timezone#setTimezone', + 'url' => '/config/timezone', + 'verb' => 'POST', + ], ], ]; $this->assertSame($expected, $routes);