Completed
Pull Request — master (#32371)
by Tom
21:30 queued 09:59
created

SetTimezoneController::set()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Tom Needham <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Settings\Controller;
23
24
use OC\AppFramework\Http;
25
use \OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http\JSONResponse;
27
use OCP\IConfig;
28
use OCP\IL10N;
29
use OCP\IRequest;
30
use OCP\IUserSession;
31
use OCP\PreConditionNotMetException;
32
33
/**
34
 * @package OC\Settings\Controller
35
 */
36
class SetTimezoneController extends Controller {
37
38
	/** @var IRequest */
39
	protected $request;
40
	/** @var IConfig  */
41
	protected $config;
42
	/** @var IL10N  */
43
	protected $l;
44
	/** @var IUserSession  */
45
	protected $session;
46
47
	public function __construct(
48
		$appName,
49
		IRequest $request,
50
		IConfig $config,
51
		IL10N $l,
52
		IUserSession $session) {
53
		parent::__construct($appName, $request);
54
		$this->request = $request;
55
		$this->config = $config;
56
		$this->l = $l;
57
		$this->session = $session;
58
	}
59
60
	/**
61
	 * @NoAdminRequired
62
	 */
63
	public function set() {
64
65
		$available = \DateTimeZone::listIdentifiers();
66
		if (!in_array($this->request->getParam('timezoneInput'), $available)) {
67
			return new JSONResponse(['message' => $this->l->t('Timezone not recognised')], Http::STATUS_BAD_REQUEST);
68
		}
69
		try {
70
			$this->config->setUserValue(
71
				$this->session->getUser()->getUID(),
72
				'core',
73
				'timezone',
74
				$this->request->getParam('timezoneInput'));
75
			return new JSONResponse(['message' => $this->l->t('Done')], Http::STATUS_ACCEPTED);
76
		} catch (PreConditionNotMetException $e) {
77
			return new JSONResponse(['message' => $this->l->t('Could not save configuration')], Http::STATUS_INTERNAL_SERVER_ERROR);
78
		}
79
	}
80
81
}
82