1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.data.ldaptive.transcoder;
18
19 import java.util.Optional;
20 import lombok.ToString;
21 import lombok.extern.slf4j.Slf4j;
22 import org.ldaptive.transcode.AbstractStringValueTranscoder;
23 import org.springframework.util.StringUtils;
24
25
26
27
28
29
30 @ToString
31 @Slf4j
32 public class UserAccountControlValueTranscoder extends AbstractStringValueTranscoder<Integer> {
33
34
35
36
37 public static final String ATTRIBUTE_NAME = "userAccountControl";
38
39
40
41
42 public static final int ACCOUNT_DISABLED = 1 << 1;
43
44
45
46
47 public static final int NORMAL_ACCOUNT = 1 << 9;
48
49
50
51
52 public static final int DONT_EXPIRE_PASSWORD = 1 << 16;
53
54
55
56
57
58
59
60
61 public static int getUserAccountControlValue(Boolean enabled, Integer existingValue) {
62 int value = Optional.ofNullable(existingValue).orElse(0);
63 if ((value & NORMAL_ACCOUNT) != NORMAL_ACCOUNT) {
64 value = value + NORMAL_ACCOUNT;
65 }
66 if ((value & DONT_EXPIRE_PASSWORD) != DONT_EXPIRE_PASSWORD) {
67 value = value + DONT_EXPIRE_PASSWORD;
68 }
69 if (Boolean.FALSE.equals(enabled)) {
70 if ((value & ACCOUNT_DISABLED) != ACCOUNT_DISABLED) {
71 value = value + ACCOUNT_DISABLED;
72 }
73 } else {
74 if ((value & ACCOUNT_DISABLED) == ACCOUNT_DISABLED) {
75 value = value - ACCOUNT_DISABLED;
76 }
77 }
78 return value;
79 }
80
81
82
83
84
85
86
87
88 public static boolean isUserAccountEnabled(Integer userAccountControlValue) {
89 return isUserAccountEnabled(userAccountControlValue, true);
90 }
91
92
93
94
95
96
97
98
99 public static boolean isUserAccountEnabled(Integer userAccountControlValue, boolean defaultValue) {
100 return userAccountControlValue == null
101 ? defaultValue
102 : ((userAccountControlValue & ACCOUNT_DISABLED) != ACCOUNT_DISABLED);
103 }
104
105 @Override
106 public Integer decodeStringValue(String value) {
107 return StringUtils.hasText(value)
108 ? Integer.parseInt(value)
109 : getUserAccountControlValue(true, 0);
110 }
111
112 @Override
113 public String encodeStringValue(Integer value) {
114 return Optional.ofNullable(value)
115 .map(String::valueOf)
116 .orElseGet(() -> String.valueOf(getUserAccountControlValue(true, 0)));
117 }
118
119 @Override
120 public Class<Integer> getType() {
121 return Integer.class;
122 }
123 }