Load a timezone file if no timezone is set

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2019-05-24 11:55:03 +02:00
parent ab468f0399
commit 1365bf820d
No known key found for this signature in database
GPG Key ID: F941078878347C0C
5 changed files with 140 additions and 0 deletions

View File

@ -79,5 +79,10 @@ return [
'providerId' => '1'
]
],
[
'name' => 'Timezone#setTimezone',
'url' => '/config/timezone',
'verb' => 'POST',
],
],
];

39
js/timezone.js Normal file
View File

@ -0,0 +1,39 @@
/* global $, jstz, OC */
/*
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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 <http://www.gnu.org/licenses/>.
*/
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')
}
})

View File

@ -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');
}
});
}
}

View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
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();
}
}

View File

@ -85,6 +85,11 @@ class Test extends TestCase {
'providerId' => '1'
]
],
[
'name' => 'Timezone#setTimezone',
'url' => '/config/timezone',
'verb' => 'POST',
],
],
];
$this->assertSame($expected, $routes);