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