[Notice] Announcing the End of Demo Server [Read me]
guest-user: Detect guest user who logged in with LDAP
@669b9d8ba6a22ca249d5a5b29d7d72a8bb3a41cc
--- app/controllers/Application.java
+++ app/controllers/Application.java
... | ... | @@ -34,6 +34,7 @@ |
34 | 34 |
public static String PRIVATE_IS_DEFAULT = play.Configuration.root().getString("project.default.scope.when.create", "public"); |
35 | 35 |
public static boolean HIDE_PROJECT_LISTING = play.Configuration.root().getBoolean("application.hide.project.listing", false); |
36 | 36 |
public static boolean SEND_YONA_USAGE =play.Configuration.root().getBoolean("application.send.yona.usage", true); |
37 |
+ public static String GUEST_USER_LOGIN_ID_PREFIX = play.Configuration.root().getString("application.guest.user.login.id.prefix ", ""); |
|
37 | 38 |
|
38 | 39 |
@AnonymousCheck |
39 | 40 |
public static Result index() { |
--- app/controllers/UserApp.java
+++ app/controllers/UserApp.java
... | ... | @@ -455,7 +455,6 @@ |
455 | 455 |
} |
456 | 456 |
|
457 | 457 |
private static User createUserDelegate(CandidateUser candidateUser) { |
458 |
- // . is replaced with - because of BasicAuth parsing case with id |
|
459 | 458 |
String loginIdCandidate = candidateUser.getLoginId(); |
460 | 459 |
|
461 | 460 |
User user = new User(); |
... | ... | @@ -475,6 +474,7 @@ |
475 | 474 |
user.password = candidateUser.getPassword(); |
476 | 475 |
} |
477 | 476 |
|
477 |
+ user.isGuest = candidateUser.isGuest(); |
|
478 | 478 |
return createNewUser(user); |
479 | 479 |
} |
480 | 480 |
|
... | ... | @@ -1148,20 +1148,10 @@ |
1148 | 1148 |
try { |
1149 | 1149 |
LdapUser ldapUser = ldapService.authenticate(loginIdOrEmail, password); |
1150 | 1150 |
play.Logger.debug("l: " + ldapUser); |
1151 |
+ |
|
1151 | 1152 |
User localUserFoundByLdapLogin = User.findByEmail(ldapUser.getEmail()); |
1152 | 1153 |
if (localUserFoundByLdapLogin.isAnonymous()) { |
1153 |
- CandidateUser candidateUser = new CandidateUser( |
|
1154 |
- ldapUser.getDisplayName(), |
|
1155 |
- ldapUser.getEmail(), |
|
1156 |
- ldapUser.getUserLoginId(), |
|
1157 |
- password |
|
1158 |
- ); |
|
1159 |
- User created = createUserDelegate(candidateUser); |
|
1160 |
- if (created.state == UserState.LOCKED) { |
|
1161 |
- flash(Constants.INFO, "user.signup.requested"); |
|
1162 |
- return User.anonymous; |
|
1163 |
- } |
|
1164 |
- return created; |
|
1154 |
+ return createNewUser(password, ldapUser); |
|
1165 | 1155 |
} else { |
1166 | 1156 |
if(!localUserFoundByLdapLogin.isSamePassword(password)) { |
1167 | 1157 |
User.resetPassword(localUserFoundByLdapLogin.loginId, password); |
... | ... | @@ -1169,6 +1159,7 @@ |
1169 | 1159 |
|
1170 | 1160 |
localUserFoundByLdapLogin.refresh(); |
1171 | 1161 |
localUserFoundByLdapLogin.name = ldapUser.getDisplayName(); |
1162 |
+ localUserFoundByLdapLogin.isGuest = ldapUser.isGuestUser(); |
|
1172 | 1163 |
localUserFoundByLdapLogin.update(); |
1173 | 1164 |
return localUserFoundByLdapLogin; |
1174 | 1165 |
} |
... | ... | @@ -1191,6 +1182,22 @@ |
1191 | 1182 |
} |
1192 | 1183 |
} |
1193 | 1184 |
|
1185 |
+ private static User createNewUser(String password, LdapUser ldapUser) { |
|
1186 |
+ CandidateUser candidateUser = new CandidateUser( |
|
1187 |
+ ldapUser.getDisplayName(), |
|
1188 |
+ ldapUser.getEmail(), |
|
1189 |
+ ldapUser.getUserLoginId(), |
|
1190 |
+ password, |
|
1191 |
+ ldapUser.isGuestUser() |
|
1192 |
+ ); |
|
1193 |
+ User created = createUserDelegate(candidateUser); |
|
1194 |
+ if (created.state == UserState.LOCKED) { |
|
1195 |
+ flash(Constants.INFO, "user.signup.requested"); |
|
1196 |
+ return User.anonymous; |
|
1197 |
+ } |
|
1198 |
+ return created; |
|
1199 |
+ } |
|
1200 |
+ |
|
1194 | 1201 |
public static boolean isUsingSignUpConfirm(){ |
1195 | 1202 |
Configuration config = play.Play.application().configuration(); |
1196 | 1203 |
Boolean useSignUpConfirm = config.getBoolean("signup.require.admin.confirm"); |
--- app/models/CandidateUser.java
+++ app/models/CandidateUser.java
... | ... | @@ -16,17 +16,19 @@ |
16 | 16 |
private String email; |
17 | 17 |
private String loginId; |
18 | 18 |
private String password; |
19 |
+ private boolean isGuest; |
|
19 | 20 |
|
20 | 21 |
public CandidateUser(String name, String email) { |
21 | 22 |
this.name = name; |
22 | 23 |
this.email = email; |
23 | 24 |
} |
24 | 25 |
|
25 |
- public CandidateUser(String name, String email, String loginId, String password) { |
|
26 |
+ public CandidateUser(String name, String email, String loginId, String password, boolean isGuest) { |
|
26 | 27 |
this.name = name; |
27 | 28 |
this.email = email; |
28 | 29 |
this.loginId = loginId; |
29 | 30 |
this.password = password; |
31 |
+ this.isGuest = isGuest; |
|
30 | 32 |
} |
31 | 33 |
|
32 | 34 |
public String getName() { |
... | ... | @@ -64,6 +66,10 @@ |
64 | 66 |
this.password = password; |
65 | 67 |
} |
66 | 68 |
|
69 |
+ public boolean isGuest() { |
|
70 |
+ return isGuest; |
|
71 |
+ } |
|
72 |
+ |
|
67 | 73 |
@Override |
68 | 74 |
public String toString() { |
69 | 75 |
return "CandidateUser{" + |
--- app/models/User.java
+++ app/models/User.java
... | ... | @@ -182,6 +182,8 @@ |
182 | 182 |
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL) |
183 | 183 |
public List<OrganizationUser> organizationUsers; |
184 | 184 |
|
185 |
+ public boolean isGuest = false; |
|
186 |
+ |
|
185 | 187 |
public User() { |
186 | 188 |
} |
187 | 189 |
|
--- app/models/support/LdapUser.java
+++ app/models/support/LdapUser.java
... | ... | @@ -6,10 +6,18 @@ |
6 | 6 |
**/ |
7 | 7 |
package models.support; |
8 | 8 |
|
9 |
+import controllers.Application; |
|
9 | 10 |
import org.apache.commons.lang3.StringUtils; |
10 | 11 |
|
11 | 12 |
import javax.naming.NamingException; |
12 | 13 |
import javax.naming.directory.Attribute; |
14 |
+ |
|
15 |
+import java.util.ArrayList; |
|
16 |
+import java.util.List; |
|
17 |
+ |
|
18 |
+import static controllers.Application.GUEST_USER_LOGIN_ID_PREFIX; |
|
19 |
+import static org.apache.commons.lang3.StringUtils.isBlank; |
|
20 |
+import static org.apache.commons.lang3.StringUtils.isNotBlank; |
|
13 | 21 |
|
14 | 22 |
public class LdapUser { |
15 | 23 |
private Attribute displayName; |
... | ... | @@ -25,7 +33,7 @@ |
25 | 33 |
} |
26 | 34 |
|
27 | 35 |
public String getDisplayName() { |
28 |
- if (StringUtils.isNotBlank(getDepartment())) { |
|
36 |
+ if (isNotBlank(getDepartment())) { |
|
29 | 37 |
return getString(this.displayName) + " [" + getDepartment() + "]"; |
30 | 38 |
} else { |
31 | 39 |
return getString(this.displayName); |
... | ... | @@ -43,6 +51,29 @@ |
43 | 51 |
e.printStackTrace(); |
44 | 52 |
return ""; |
45 | 53 |
} |
54 |
+ } |
|
55 |
+ |
|
56 |
+ public boolean isGuestUser() { |
|
57 |
+ if(isBlank(GUEST_USER_LOGIN_ID_PREFIX)){ |
|
58 |
+ return false; |
|
59 |
+ } |
|
60 |
+ List<String> prefixes = new ArrayList<>(); |
|
61 |
+ |
|
62 |
+ for(String idPrefix: GUEST_USER_LOGIN_ID_PREFIX.replaceAll(" ", "") |
|
63 |
+ .split(",")){ |
|
64 |
+ String prefix = StringUtils.defaultString(idPrefix, "").toLowerCase().trim(); |
|
65 |
+ if (isNotBlank(prefix)) { |
|
66 |
+ prefixes.add(prefix); |
|
67 |
+ } |
|
68 |
+ } |
|
69 |
+ |
|
70 |
+ for (String prefix : prefixes) { |
|
71 |
+ if(this.getUserLoginId().toLowerCase().startsWith(prefix.toLowerCase())) { |
|
72 |
+ return true; |
|
73 |
+ } |
|
74 |
+ } |
|
75 |
+ |
|
76 |
+ return false; |
|
46 | 77 |
} |
47 | 78 |
|
48 | 79 |
public String getEmail() { |
... | ... | @@ -64,6 +95,7 @@ |
64 | 95 |
", email='" + getEmail() + '\'' + |
65 | 96 |
", userId='" + getUserLoginId() + '\'' + |
66 | 97 |
", department='" + getDepartment() + '\'' + |
98 |
+ ", isGuest='" + isGuestUser() + '\'' + |
|
67 | 99 |
'}'; |
68 | 100 |
} |
69 | 101 |
} |
+++ conf/evolutions/default/16.sql
... | ... | @@ -0,0 +1,7 @@ |
1 | +# --- !Ups | |
2 | +ALTER TABLE n4user ADD COLUMN is_guest tinyint(1) default 0; | |
3 | +CREATE INDEX ix_n4user_is_guest ON n4user (is_guest); | |
4 | + | |
5 | +# --- !Downs | |
6 | +DROP INDEX IF EXISTS ix_n4user_is_guest ON n4user; | |
7 | +ALTER TABLE n4user DROP COLUMN is_guest;(No newline at end of file) |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?