1 | package gov.va.med.edp.springframework.security.userdetails.memory;
|
---|
2 |
|
---|
3 | import org.springframework.security.userdetails.memory.UserAttributeEditor;
|
---|
4 | import org.springframework.security.userdetails.memory.UserAttribute;
|
---|
5 | import org.springframework.beans.propertyeditors.PropertiesEditor;
|
---|
6 | import org.springframework.util.StringUtils;
|
---|
7 |
|
---|
8 | import java.beans.PropertyEditorSupport;
|
---|
9 | import java.util.Properties;
|
---|
10 | import java.util.Iterator;
|
---|
11 | import java.util.Random;
|
---|
12 |
|
---|
13 | import gov.va.med.edp.springframework.security.userdetails.VistaUserDetails;
|
---|
14 | import gov.va.med.edp.springframework.security.userdetails.vistalink.VistaUser;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Property editor to assist with the setup of a {@link VistaUserMap}.<p>The format of entries should be:</p>
|
---|
18 | * <p><code> duz@stationNumber=access;verify,grantedAuthority[,grantedAuthority][,enabled|disabled] </code></p>
|
---|
19 | * At least one granted authority must be listed.</p>
|
---|
20 | * <p>The <code>duz@stationNumber</code> represents the key and duplicates are handled the same was as duplicates would be
|
---|
21 | * in Java <code>Properties</code> files.</p>
|
---|
22 | * <p>If the above requirements are not met, the invalid entry will be silently ignored.</p>
|
---|
23 | * <p>This editor always assumes each entry has a non-expired account and non-expired credentials. However, it
|
---|
24 | * does honour the user enabled/disabled flag as described above.</p>
|
---|
25 | */
|
---|
26 | public class VistaUserMapEditor extends PropertyEditorSupport {
|
---|
27 | public void setAsText(String s) throws IllegalArgumentException {
|
---|
28 | VistaUserMap userMap = new VistaUserMap();
|
---|
29 |
|
---|
30 | if ((s == null) || "".equals(s)) {
|
---|
31 | // Leave value in property editor null
|
---|
32 | } else {
|
---|
33 | // Use properties editor to tokenize the string
|
---|
34 | PropertiesEditor propertiesEditor = new PropertiesEditor();
|
---|
35 | propertiesEditor.setAsText(s);
|
---|
36 |
|
---|
37 | Properties props = (Properties) propertiesEditor.getValue();
|
---|
38 | addUsersFromProperties(userMap, props);
|
---|
39 | }
|
---|
40 |
|
---|
41 | setValue(userMap);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public static VistaUserMap addUsersFromProperties(VistaUserMap userMap, Properties props) {
|
---|
45 | // Now we have properties, process each one individually
|
---|
46 | UserAttributeEditor configAttribEd = new UserAttributeEditor();
|
---|
47 |
|
---|
48 | for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
|
---|
49 | String key = (String) iter.next();
|
---|
50 | String value = props.getProperty(key);
|
---|
51 |
|
---|
52 | // Convert value to a password, enabled setting, and list of granted authorities
|
---|
53 | configAttribEd.setAsText(value);
|
---|
54 |
|
---|
55 | UserAttribute attr = (UserAttribute) configAttribEd.getValue();
|
---|
56 |
|
---|
57 | // Make a user object, assuming the properties were properly provided
|
---|
58 | if (attr != null) {
|
---|
59 | String duz = StringUtils.split(key, "@")[0];
|
---|
60 | String stationNumber = StringUtils.split(key, "@")[1];
|
---|
61 | String access = StringUtils.split(attr.getPassword(),";")[0];
|
---|
62 | String verify = StringUtils.split(attr.getPassword(),";")[1];
|
---|
63 | VistaUserDetails user = new VistaUser(new Random().toString(), stationNumber, duz, access, verify, attr.isEnabled(), true, true, true, attr.getAuthorities());
|
---|
64 | userMap.addUser(user);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | return userMap;
|
---|
69 | }
|
---|
70 | }
|
---|