View Javadoc
1   /*
2    * Copyright 2024 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.ldaptive.transcoder;
18  
19  import static java.util.Objects.isNull;
20  
21  import lombok.EqualsAndHashCode;
22  import lombok.Getter;
23  import lombok.ToString;
24  
25  /**
26   * The domain user's account control.
27   *
28   * @author Christian Bremer
29   */
30  @EqualsAndHashCode
31  @ToString
32  @Getter
33  public class UserAccountControl {
34  
35    /**
36     * The bit map value of a disabled account.
37     */
38    static final int DISABLED_ACCOUNT = 1 << 1;
39  
40    /**
41     * The bit map value of a normal account.
42     */
43    static final int NORMAL_ACCOUNT = 1 << 9;
44  
45    /**
46     * The bit map value of a password that doesn't expire.
47     */
48    static final int DONT_EXPIRE_PASSWORD = 1 << 16;
49  
50    private int value;
51  
52    /**
53     * Instantiates a new user account control.
54     */
55    public UserAccountControl() {
56      this(null);
57    }
58  
59    /**
60     * Instantiates a new user account control.
61     *
62     * @param value the value
63     */
64    public UserAccountControl(Integer value) {
65      if (isNull(value) || value < 0) {
66        this.value = NORMAL_ACCOUNT;
67        setEnabled(true);
68        setPasswordExpirationEnabled(false);
69      } else {
70        this.value = value;
71      }
72    }
73  
74    /**
75     * Is normal account boolean.
76     *
77     * @return the boolean
78     */
79    public boolean isNormalAccount() {
80      return (value & NORMAL_ACCOUNT) == NORMAL_ACCOUNT;
81    }
82  
83    /**
84     * Sets normal account.
85     *
86     * @param isNormalAccount the is normal account
87     */
88    public void setNormalAccount(boolean isNormalAccount) {
89      if (isNormalAccount && !isNormalAccount()) {
90        value = value + NORMAL_ACCOUNT;
91      } else if (!isNormalAccount && isNormalAccount()) {
92        value = value - NORMAL_ACCOUNT;
93      }
94    }
95  
96    /**
97     * Is enabled boolean.
98     *
99     * @return the boolean
100    */
101   public boolean isEnabled() {
102     return (value & DISABLED_ACCOUNT) != DISABLED_ACCOUNT;
103   }
104 
105   /**
106    * Sets enabled.
107    *
108    * @param enabled the enabled
109    */
110   public void setEnabled(boolean enabled) {
111     if (enabled && !isEnabled()) {
112       value = value - DISABLED_ACCOUNT;
113     } else if (!enabled && isEnabled()) {
114       value = value + DISABLED_ACCOUNT;
115     }
116   }
117 
118   /**
119    * Is password expiration enabled boolean.
120    *
121    * @return the boolean
122    */
123   public boolean isPasswordExpirationEnabled() {
124     return (value & DONT_EXPIRE_PASSWORD) != DONT_EXPIRE_PASSWORD;
125   }
126 
127   /**
128    * Sets password expiration enabled.
129    *
130    * @param enabled the enabled
131    */
132   public void setPasswordExpirationEnabled(boolean enabled) {
133     if (enabled && !isPasswordExpirationEnabled()) {
134       value = value - DONT_EXPIRE_PASSWORD;
135     } else if (!enabled && isPasswordExpirationEnabled()) {
136       value = value + DONT_EXPIRE_PASSWORD;
137     }
138   }
139 
140 }