mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
18.31.2018 c94933076eed54cfb5baf72d3cf532440a5a9b81
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package de.micromata.borgbutler.server.user;
 
import java.util.Locale;
 
public class UserUtils {
    private static final ThreadLocal<UserInfo> threadUserInfo = new ThreadLocal<UserInfo>();
 
    /**
     * Gets the current user from ThreadLocal.
     * @return
     */
    public static UserData getUser() {
        UserInfo user = threadUserInfo.get();
        if (user == null) {
            return null;
        }
        return user.userData;
    }
 
    public static Locale getUserLocale() {
        UserInfo userInfo = threadUserInfo.get();
        if (userInfo == null) return null;
        UserData user = userInfo.userData;
        Locale locale = user.getLocale();
        if (locale == null) {
            locale = userInfo.requestLocale;
        }
        return locale;
    }
 
    public static String getUserDateFormat() {
        UserData user = getUser();
        return user != null ? user.getDateFormat() : null;
    }
 
    static void setUser(UserData user, Locale requestLocale) {
        threadUserInfo.set(new UserInfo(user, requestLocale));
    }
 
    static void removeUser() {
        threadUserInfo.remove();
    }
 
    private static class UserInfo {
        private UserData userData;
        private Locale requestLocale;
 
        private UserInfo(UserData userData, Locale requestLocale) {
            this.userData = userData;
            this.requestLocale = requestLocale;
        }
    }
}