Skip to content

Commit 40c929c

Browse files
committed
User timezone #1012
1 parent ccbcba2 commit 40c929c

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

content/modules/security/pages/authentication.adoc

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ You can customize the prefix for resource role authorities using the standard Sp
2828
2929
Similarly, you can adjust the prefix for row-level role authorities using the `jmix.security.default-row-level-role-prefix` application property.
3030
====
31-
* `getLocale()` and `getTimeZone()` return the locale and time zone of the current user.
31+
32+
* `getLocale()` returns the locale of the current user.
33+
34+
* `getTimeZone()` returns the time zone of the current user. The <<time-zones,next section>> explains how the user time zone is determined and how it is used by the framework.
3235

3336
* `isSet()` returns `true` if the current execution thread is authenticated, that is contains information about the user. If it's not, `getUser()`, `getLocale()` and `getTimeZone()` methods described above will throw the `IllegalStateException`.
3437

@@ -46,6 +49,50 @@ include::example$/security-ex1/src/main/java/com/company/demo/view/authtest/Auth
4649
For example, you can use `DelegatingSecurityContextRunnable` to propagate authentication to new threads as described in {spring-security-doc}/servlet/integrations/concurrency.html[Spring Security documentation^].
4750
====
4851

52+
[[time-zones]]
53+
=== Using Time Zones
54+
55+
Jmix automatically converts values in UI components between server and user time zones for entity attributes of the following types:
56+
57+
* `OffsetDateTime`
58+
* `java.util.Date` with `@Temporal(TemporalType.TIMESTAMP)`, which corresponds to the `DateTime` attribute type in Studio.
59+
60+
Below we explain how to specify the user and server time zones.
61+
62+
By default, the `User` entity implements the `HasTimeZone` interface with two methods:
63+
64+
* `getTimeZoneId()` is implemented in the `User` entity and returns a time zone set for the user in the database.
65+
66+
* `isAutoTimeZone()` method returns `false` by default for all users. You can implement this method in the `User` entity and return `true` for all users, or map it to a database column as it is done for `getTimeZoneId()` to have different values for different users.
67+
68+
The time zone of the current user returned by `CurrentAuthentication.getTimeZone()` is determined in the following order:
69+
70+
* A non-empty value returned by the `User.getTimeZoneId()` method is used to construct the `TimeZone` object.
71+
72+
* If the `User.isAutoTimeZone()` method returns `true`, the framework tries to obtain the time zone from the user's web browser through a `DeviceTimeZoneProvider` implementation.
73+
74+
* If both previous steps were unsuccessful, the server default time zone is used.
75+
76+
The server time zone is defined by the server operating system. You can set it explicitly using the `user.timezone` Java system property or `TimeZone` class. For example:
77+
78+
[source,java]
79+
----
80+
@SpringBootApplication
81+
public class DemoApplication {
82+
83+
public static void main(String[] args) {
84+
System.setProperty("user.timezone", "UTC");
85+
SpringApplication.run(DemoApplication.class, args);
86+
}
87+
----
88+
89+
We recommend also setting the same time zone for the database. For example, in PostgreSQL you can do it using the following command:
90+
91+
[source,sql]
92+
----
93+
ALTER DATABASE demo SET timezone TO 'UTC'
94+
----
95+
4996
[[client]]
5097
== Client Authentication
5198

0 commit comments

Comments
 (0)