View Javadoc
1   /*
2    * Copyright 2019 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.dccon.repository.ldap.transcoder;
18  
19  import java.util.Optional;
20  import lombok.extern.slf4j.Slf4j;
21  import org.ldaptive.io.AbstractStringValueTranscoder;
22  import org.springframework.util.StringUtils;
23  
24  /**
25   * The user account control value transcoder.
26   *
27   * @author Christian Bremer
28   */
29  @Slf4j
30  public class UserAccountControlValueTranscoder extends AbstractStringValueTranscoder<Integer> {
31  
32    /**
33     * The bit map value of a disabled account.
34     */
35    static final int ACCOUNTDISABLE = 1 << 1;
36  
37    /**
38     * The bit map value of a normal account.
39     */
40    static final int NORMAL_ACCOUNT = 1 << 9;
41  
42    /**
43     * The bit map value a password that doesn't expire.
44     */
45    static final int DONT_EXPIRE_PASSWORD = 1 << 16;
46  
47    /**
48     * Gets user account control value.
49     *
50     * @param enabled the enabled
51     * @param existingValue the existing value
52     * @return the user account control value
53     */
54    public static int getUserAccountControlValue(Boolean enabled, Integer existingValue) {
55      int value = Optional.ofNullable(existingValue).orElse(0);
56      if ((value & NORMAL_ACCOUNT) != NORMAL_ACCOUNT) {
57        value = value + NORMAL_ACCOUNT;
58      }
59      if ((value & DONT_EXPIRE_PASSWORD) != DONT_EXPIRE_PASSWORD) {
60        value = value + DONT_EXPIRE_PASSWORD;
61      }
62      if (Boolean.FALSE.equals(enabled)) {
63        if ((value & ACCOUNTDISABLE) != ACCOUNTDISABLE) {
64          value = value + ACCOUNTDISABLE;
65        }
66      } else {
67        if ((value & ACCOUNTDISABLE) == ACCOUNTDISABLE) {
68          value = value - ACCOUNTDISABLE;
69        }
70      }
71      return value;
72    }
73  
74    /**
75     * Is user account enabled boolean.
76     *
77     * @param userAccountControlValue the user account control value
78     * @return the boolean
79     */
80    public static boolean isUserAccountEnabled(Integer userAccountControlValue) {
81      return userAccountControlValue != null
82          && ((userAccountControlValue & ACCOUNTDISABLE) != ACCOUNTDISABLE);
83    }
84  
85    @Override
86    public Integer decodeStringValue(String value) {
87      return StringUtils.hasText(value)
88          ? Integer.parseInt(value)
89          : getUserAccountControlValue(true, 0);
90    }
91  
92    @Override
93    public String encodeStringValue(Integer value) {
94      return Optional.ofNullable(value)
95          .map(String::valueOf)
96          .orElseGet(() -> String.valueOf(getUserAccountControlValue(true, 0)));
97    }
98  
99    @Override
100   public Class<Integer> getType() {
101     return Integer.class;
102   }
103 }