|
22 | 22 | import com.stormpath.sdk.cache.Cache; |
23 | 23 | import com.stormpath.sdk.client.Client; |
24 | 24 | import com.stormpath.sdk.directory.AccountStore; |
| 25 | +import com.stormpath.sdk.directory.AccountStoreVisitor; |
25 | 26 | import com.stormpath.sdk.directory.AccountStoreVisitorAdapter; |
26 | 27 | import com.stormpath.sdk.directory.Directory; |
| 28 | +import com.stormpath.sdk.directory.PasswordStrength; |
| 29 | +import com.stormpath.sdk.group.Group; |
| 30 | +import com.stormpath.sdk.impl.resource.AbstractResource; |
27 | 31 | import com.stormpath.sdk.lang.Assert; |
| 32 | +import com.stormpath.sdk.lang.Strings; |
28 | 33 | import com.stormpath.sdk.organization.Organization; |
| 34 | +import com.stormpath.sdk.organization.OrganizationList; |
29 | 35 | import com.stormpath.sdk.servlet.account.event.impl.DefaultRegisteredAccountRequestEvent; |
30 | 36 | import com.stormpath.sdk.servlet.application.ApplicationResolver; |
31 | 37 | import com.stormpath.sdk.servlet.authc.impl.TransientAuthenticationResult; |
|
42 | 48 | import javax.servlet.http.HttpServletResponse; |
43 | 49 | import java.util.Arrays; |
44 | 50 | import java.util.Collections; |
| 51 | +import java.util.HashMap; |
45 | 52 | import java.util.LinkedHashMap; |
46 | 53 | import java.util.List; |
47 | 54 | import java.util.Map; |
@@ -154,6 +161,7 @@ protected void appendModel(HttpServletRequest request, HttpServletResponse respo |
154 | 161 | model.put("loginUri", loginUri); |
155 | 162 | } else { |
156 | 163 | model.put("accountStores", accountStoreModelFactory.getAccountStores(request)); |
| 164 | + model.put("passwordPolicy", getPasswordPolicy(request)); |
157 | 165 | } |
158 | 166 | } |
159 | 167 |
|
@@ -310,4 +318,98 @@ private Map<String, Object> getCustomData(HttpServletRequest request, Form form) |
310 | 318 |
|
311 | 319 | return result; |
312 | 320 | } |
| 321 | + |
| 322 | + private Map<String, Object> getPasswordPolicy(HttpServletRequest request) { |
| 323 | + |
| 324 | + String onk = request.getParameter("organizationNameKey"); |
| 325 | + |
| 326 | + PasswordStrength passwordStrength; |
| 327 | + |
| 328 | + if (Strings.hasText(onk)) { |
| 329 | + passwordStrength = findPasswordStrengthByOrganization(onk); |
| 330 | + } else { |
| 331 | + passwordStrength = getApplicationPasswordStrength(request); |
| 332 | + } |
| 333 | + |
| 334 | + if (passwordStrength == null) { |
| 335 | + return null; |
| 336 | + } |
| 337 | + |
| 338 | + return convertPasswordStrengthToMap(passwordStrength); |
| 339 | + } |
| 340 | + |
| 341 | + private PasswordStrength getApplicationPasswordStrength(HttpServletRequest request) { |
| 342 | + AccountStore defaultAccountStore = getApplication(request).getDefaultAccountStore(); |
| 343 | + |
| 344 | + if (defaultAccountStore == null) { |
| 345 | + return null; |
| 346 | + } |
| 347 | + |
| 348 | + final PasswordStrength[] passwordStrength = new PasswordStrength[1]; |
| 349 | + defaultAccountStore.accept(new AccountStoreVisitor() { |
| 350 | + @Override |
| 351 | + public void visit(Group group) { |
| 352 | + passwordStrength[0] = group.getDirectory().getPasswordPolicy().getStrength(); |
| 353 | + } |
| 354 | + |
| 355 | + @Override |
| 356 | + public void visit(Directory directory) { |
| 357 | + passwordStrength[0] = directory.getPasswordPolicy().getStrength(); |
| 358 | + } |
| 359 | + |
| 360 | + @Override |
| 361 | + public void visit(Organization organization) { |
| 362 | + passwordStrength[0] = getOrganizationPasswordStrength(organization); |
| 363 | + } |
| 364 | + }); |
| 365 | + |
| 366 | + return passwordStrength[0]; |
| 367 | + } |
| 368 | + |
| 369 | + private PasswordStrength findPasswordStrengthByOrganization(String onk) { |
| 370 | + HashMap<String, Object> query = new HashMap<>(); |
| 371 | + query.put("nameKey", onk); |
| 372 | + OrganizationList organizations = client.getOrganizations(query); |
| 373 | + |
| 374 | + if (organizations.getSize() != 1) { |
| 375 | + return null; |
| 376 | + } |
| 377 | + |
| 378 | + return getOrganizationPasswordStrength(organizations.single()); |
| 379 | + } |
| 380 | + |
| 381 | + private PasswordStrength getOrganizationPasswordStrength(Organization organization) { |
| 382 | + AccountStore organizationDefaultAccountStore = organization.getDefaultAccountStore(); |
| 383 | + |
| 384 | + if (organizationDefaultAccountStore == null) { |
| 385 | + return null; |
| 386 | + } |
| 387 | + |
| 388 | + final PasswordStrength[] passwordStrength = new PasswordStrength[1]; |
| 389 | + organizationDefaultAccountStore.accept(new AccountStoreVisitorAdapter() { |
| 390 | + @Override |
| 391 | + public void visit(Group group) { |
| 392 | + passwordStrength[0] = group.getDirectory().getPasswordPolicy().getStrength(); |
| 393 | + } |
| 394 | + |
| 395 | + @Override |
| 396 | + public void visit(Directory directory) { |
| 397 | + passwordStrength[0] = directory.getPasswordPolicy().getStrength(); |
| 398 | + } |
| 399 | + }); |
| 400 | + |
| 401 | + return passwordStrength[0]; |
| 402 | + } |
| 403 | + |
| 404 | + private Map<String, Object> convertPasswordStrengthToMap(PasswordStrength passwordStrength) { |
| 405 | + AbstractResource abstractResource = (AbstractResource) passwordStrength; |
| 406 | + |
| 407 | + Map<String, Object> strength = new HashMap<>(); |
| 408 | + |
| 409 | + for (String propertyName : abstractResource.getPropertyDescriptors().keySet()) { |
| 410 | + strength.put(propertyName, abstractResource.getProperty(propertyName)); |
| 411 | + } |
| 412 | + |
| 413 | + return strength; |
| 414 | + } |
313 | 415 | } |
0 commit comments