1 | package org.expeditee.auth.tags;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Map;
|
---|
6 | import java.util.Optional;
|
---|
7 |
|
---|
8 | import org.expeditee.items.Text;
|
---|
9 |
|
---|
10 | public enum AuthenticationTag {
|
---|
11 | Username ("txtUsername", System.getProperty("user.name")), // Should use Browser.getExpediteeUserName()
|
---|
12 | Password ("txtPassword", System.getProperty("user.password")),
|
---|
13 | PasswordAgain ("txtPasswordAgain", System.getProperty("user.password.again")),
|
---|
14 | NewPassword ("txtNewPassword", System.getProperty("user.new.password")),
|
---|
15 | NewPasswordAgain ("txtNewPasswordAgain", System.getProperty("user.new.password.again")),
|
---|
16 | Email ("txtEmail", System.getProperty("user.email")),
|
---|
17 | EmailAgain ("txtEmailAgain", System.getProperty("user.email.again")),
|
---|
18 | TrustedUserOne ("txtTrustedUserOne", System.getProperty("user.colleague.one")),
|
---|
19 | TrustedUserTwo ("txtTrustedUserTwo", System.getProperty("user.colleague.two")),
|
---|
20 | Frameset ("txtFrameset", System.getProperty("user.toshare.frameset")),
|
---|
21 | IntergalacticNumber ("txtIntergalacticNumber", System.getProperty("user.intergalacticnumber")),
|
---|
22 | PasswordSliceOne ("txtPasswordPieceOne", System.getProperty("user.password.piece.one")),
|
---|
23 | PasswordSliceTwo ("txtPasswordPieceTwo", System.getProperty("user.password.piece.two"));
|
---|
24 |
|
---|
25 | public String val;
|
---|
26 | public String def;
|
---|
27 |
|
---|
28 | private AuthenticationTag(String tag, String def) {
|
---|
29 | this.val = tag;
|
---|
30 | this.def = def;
|
---|
31 | }
|
---|
32 |
|
---|
33 | public static Optional<Map<AuthenticationTag, String>> fetchUserData(Collection<Text> from, boolean useDefaults, AuthenticationTag... tags) {
|
---|
34 | final Map<AuthenticationTag, String> fetchedData = new HashMap<AuthenticationTag, String>();
|
---|
35 |
|
---|
36 | for (AuthenticationTag tag: tags) {
|
---|
37 | for (Text canditate: from) {
|
---|
38 | // use the canditate if it exists
|
---|
39 | if (canditate.hasData(tag.val)) {
|
---|
40 | if (tag.val.equals("txtUsername")) {
|
---|
41 | fetchedData.put(tag, canditate.getText().toLowerCase());
|
---|
42 | } else {
|
---|
43 | fetchedData.put(tag, canditate.getText());
|
---|
44 | }
|
---|
45 | break;
|
---|
46 | }
|
---|
47 | // otherwise try use the default
|
---|
48 | String property = tag.def;
|
---|
49 | if (property != null && !property.isEmpty()) {
|
---|
50 | fetchedData.put(tag, useDefaults ? property : null);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | if (fetchedData.entrySet().size() == tags.length) {
|
---|
56 | return Optional.of(fetchedData);
|
---|
57 | } else {
|
---|
58 | return Optional.empty();
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|