Skip to content

Commit d24e8d8

Browse files
committed
Allow to set Timezone name
1 parent d86f113 commit d24e8d8

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace RESTAPI\Endpoints;
4+
5+
require_once 'RESTAPI/autoloader.inc';
6+
7+
use RESTAPI\Core\Endpoint;
8+
9+
/**
10+
* Defines an Endpoint for interacting with the SystemTimezone Model object at
11+
* /api/v2/system/timezone.
12+
*/
13+
class SystemTimezoneEndpoint extends Endpoint {
14+
public function __construct() {
15+
# Set Endpoint attributes
16+
$this->url = '/api/v2/system/timezone';
17+
$this->model_name = 'SystemTimezone';
18+
$this->request_method_options = ['GET', 'PATCH'];
19+
$this->get_help_text = 'Reads the current system timezone.';
20+
$this->patch_help_text = 'Updates the system timezone.';
21+
22+
# Construct the parent Endpoint object
23+
parent::__construct();
24+
}
25+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace RESTAPI\Models;
4+
5+
use RESTAPI\Core\Model;
6+
use RESTAPI\Fields\StringField;
7+
use RESTAPI\Responses\ValidationError;
8+
9+
/**
10+
* Defines a Model that represents the Timezone configuration on this system.
11+
*/
12+
class SystemTimezone extends Model {
13+
public StringField $timezone;
14+
15+
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], mixed ...$options) {
16+
# Set model attributes
17+
$this->config_path = 'system';
18+
$this->update_strategy = 'merge';
19+
$this->always_apply = true;
20+
21+
# Set model Fields
22+
$this->timezone = new StringField(
23+
default: 'Etc/GMT',
24+
allow_empty: false,
25+
allow_null: false,
26+
many: false,
27+
help_text: 'Set geographic region name (Continent/Location) to determine the timezone for the firewall.'
28+
);
29+
30+
parent::__construct($id, $parent_id, $data, ...$options);
31+
}
32+
33+
/**
34+
* Apply these Timezone changes to the system.
35+
*/
36+
public function apply() {
37+
system_timezone_configure();
38+
}
39+
40+
/*
41+
*
42+
*/
43+
public function validate_extra(): void {
44+
$tzlist = system_get_timezone_list();
45+
46+
$tz = $this->timezone->value;
47+
48+
if ( ! in_array($tz, $tzlist) ) {
49+
throw new ValidationError(
50+
message: "Unknown timezone={$tz}",
51+
response_id: 'TIMEZONE_UNKNOWN_TIMEZONE',
52+
);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)