001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2011-2016 ForgeRock AS.
016 */
017package org.opends.server.types;
018
019import static org.opends.server.types.AccountStatusNotificationProperty.*;
020import static org.opends.server.util.CollectionUtils.*;
021import static org.opends.server.util.StaticUtils.*;
022
023import java.util.ArrayList;
024import java.util.Date;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.forgerock.i18n.LocalizableMessage;
030import org.forgerock.opendj.ldap.ByteString;
031import org.forgerock.opendj.ldap.DN;
032import org.opends.server.core.PasswordPolicy;
033import org.opends.server.core.PasswordPolicyState;
034
035/**
036 * This class defines a data type for storing information associated
037 * with an account status notification.
038 */
039@org.opends.server.types.PublicAPI(
040     stability=org.opends.server.types.StabilityLevel.VOLATILE,
041     mayInstantiate=false,
042     mayExtend=false,
043     mayInvoke=true)
044public final class AccountStatusNotification
045{
046  /** The notification type for this account status notification. */
047  private AccountStatusNotificationType notificationType;
048
049  /** The entry for the user to whom this notification applies. */
050  private Entry userEntry;
051
052  /**
053   * A set of additional properties that may be useful for this
054   * notification.
055   */
056  private Map<AccountStatusNotificationProperty,List<String>>
057               notificationProperties;
058
059  /**
060   * A message that provides additional information for this account
061   * status notification.
062   */
063  private LocalizableMessage message;
064
065
066
067  /**
068   * Creates a new account status notification object with the
069   * provided information.
070   *
071   * @param  notificationType        The type for this account status
072   *                                 notification.
073   * @param  userEntry               The entry for the user to whom
074   *                                 this notification applies.
075   * @param  message                 The human-readable message for
076   *                                 this notification.
077   * @param  notificationProperties  A set of properties that may
078   *                                 include additional information
079   *                                 about this notification.
080   */
081  public AccountStatusNotification(
082              AccountStatusNotificationType notificationType,
083              Entry userEntry, LocalizableMessage message,
084              Map<AccountStatusNotificationProperty,List<String>>
085                   notificationProperties)
086  {
087    this.notificationType = notificationType;
088    this.userEntry        = userEntry;
089    this.message          = message;
090
091    if (notificationProperties == null)
092    {
093      this.notificationProperties = new HashMap<>(0);
094    }
095    else
096    {
097      this.notificationProperties = notificationProperties;
098    }
099  }
100
101
102
103  /**
104   * Retrieves the notification type for this account status
105   * notification.
106   *
107   * @return  The notification type for this account status
108   *          notification.
109   */
110  public AccountStatusNotificationType getNotificationType()
111  {
112    return notificationType;
113  }
114
115
116
117  /**
118   * Retrieves the DN of the user entry to which this notification
119   * applies.
120   *
121   * @return  The DN of the user entry to which this notification
122   *          applies.
123   */
124  public DN getUserDN()
125  {
126    return userEntry.getName();
127  }
128
129
130
131  /**
132   * Retrieves user entry for whom this notification applies.
133   *
134   * @return  The user entry for whom this notification applies.
135   */
136  public Entry getUserEntry()
137  {
138    return userEntry;
139  }
140
141
142
143  /**
144   * Retrieves a message that provides additional information for this
145   * account status notification.
146   *
147   * @return  A message that provides additional information for this
148   *          account status notification.
149   */
150  public LocalizableMessage getMessage()
151  {
152    return message;
153  }
154
155
156
157  /**
158   * Retrieves a set of properties that may provide additional
159   * information for this account status notification.
160   *
161   * @return  A set of properties that may provide additional
162   *          information for this account status notification.
163   */
164  public Map<AccountStatusNotificationProperty,List<String>>
165              getNotificationProperties()
166  {
167    return notificationProperties;
168  }
169
170
171
172  /**
173   * Retrieves the set of values for the specified account status
174   * notification property.
175   *
176   * @param  property  The account status notification property for
177   *                   which to retrieve the associated values.
178   *
179   * @return  The set of values for the specified account status
180   *          notification property, or {@code null} if the specified
181   *          property is not defined for this account status
182   *          notification.
183   */
184  public List<String> getNotificationProperty(
185                           AccountStatusNotificationProperty property)
186  {
187    return notificationProperties.get(property);
188  }
189
190
191
192  /**
193   * Creates a set of account status notification properties from the
194   * provided information.
195   *
196   * @param  pwPolicyState     The password policy state for the user
197   *                           associated with the notification.
198   * @param  tempLocked        Indicates whether the user's account
199   *                           has been temporarily locked.
200   * @param  timeToExpiration  The length of time in seconds until the
201   *                           user's password expires, or -1 if it's
202   *                           not about to expire.
203   * @param  oldPasswords      The set of old passwords for the user,
204   *                           or {@code null} if this is not
205   *                           applicable.
206   * @param  newPasswords      The set of new passwords for the user,
207   *                           or {@code null} if this is not
208   *                           applicable.
209   *
210   * @return  The created set of account status notification
211   *          properties.
212   */
213  @org.opends.server.types.PublicAPI(
214       stability=org.opends.server.types.StabilityLevel.PRIVATE,
215       mayInstantiate=false,
216       mayExtend=false,
217       mayInvoke=false)
218  public static Map<AccountStatusNotificationProperty,List<String>>
219                     createProperties(
220                          PasswordPolicyState pwPolicyState,
221                          boolean tempLocked, int timeToExpiration,
222                          List<ByteString> oldPasswords,
223                          List<ByteString> newPasswords)
224  {
225    HashMap<AccountStatusNotificationProperty,List<String>> props = new HashMap<>(4);
226
227    PasswordPolicy policy = pwPolicyState.getAuthenticationPolicy();
228    props.put(PASSWORD_POLICY_DN, newArrayList(policy.getDN().toString()));
229
230    if (tempLocked)
231    {
232      long secondsUntilUnlock = policy.getLockoutDuration();
233      if (secondsUntilUnlock > 0L)
234      {
235        props.put(SECONDS_UNTIL_UNLOCK, newArrayList(String.valueOf(secondsUntilUnlock)));
236
237        String string = secondsToTimeString(secondsUntilUnlock).toString();
238        props.put(TIME_UNTIL_UNLOCK, newArrayList(string));
239
240        long unlockTime = System.currentTimeMillis() + (1000 * secondsUntilUnlock);
241        props.put(ACCOUNT_UNLOCK_TIME, newArrayList(new Date(unlockTime).toString()));
242      }
243    }
244
245    if (timeToExpiration >= 0)
246    {
247      props.put(SECONDS_UNTIL_EXPIRATION, newArrayList(String.valueOf(timeToExpiration)));
248
249      String string = secondsToTimeString(timeToExpiration).toString();
250      props.put(TIME_UNTIL_EXPIRATION, newArrayList(string));
251
252      long expTime = System.currentTimeMillis() + (1000L * timeToExpiration);
253      props.put(PASSWORD_EXPIRATION_TIME, newArrayList(new Date(expTime).toString()));
254    }
255
256    if (oldPasswords != null && !oldPasswords.isEmpty())
257    {
258      props.put(OLD_PASSWORD, toStrings(oldPasswords));
259    }
260    if (newPasswords != null && !newPasswords.isEmpty())
261    {
262      props.put(NEW_PASSWORD, toStrings(newPasswords));
263    }
264
265    return props;
266  }
267
268  private static ArrayList<String> toStrings(List<ByteString> byteStrings)
269  {
270    ArrayList<String> results = new ArrayList<>(byteStrings.size());
271    for (ByteString v : byteStrings)
272    {
273      results.add(v.toString());
274    }
275    return results;
276  }
277
278
279
280  /**
281   * Retrieves a string representation of this account status
282   * notification.
283   *
284   * @return  A string representation of this account status
285   *          notification.
286   */
287  @Override
288  public String toString()
289  {
290    return "AccountStatusNotification(type=" +
291           notificationType.getName() + ",dn=" + userEntry.getName() +
292           ",message=" + message + ")";
293  }
294}