doortts doortts 2017-07-20
guest-user: Detect guest user who logged in with LDAP
@669b9d8ba6a22ca249d5a5b29d7d72a8bb3a41cc
app/controllers/Application.java
--- app/controllers/Application.java
+++ app/controllers/Application.java
@@ -34,6 +34,7 @@
     public static String PRIVATE_IS_DEFAULT = play.Configuration.root().getString("project.default.scope.when.create", "public");
     public static boolean HIDE_PROJECT_LISTING = play.Configuration.root().getBoolean("application.hide.project.listing", false);
     public static boolean SEND_YONA_USAGE =play.Configuration.root().getBoolean("application.send.yona.usage", true);
+    public static String GUEST_USER_LOGIN_ID_PREFIX  = play.Configuration.root().getString("application.guest.user.login.id.prefix ", "");
 
     @AnonymousCheck
     public static Result index() {
app/controllers/UserApp.java
--- app/controllers/UserApp.java
+++ app/controllers/UserApp.java
@@ -455,7 +455,6 @@
     }
 
     private static User createUserDelegate(CandidateUser candidateUser) {
-        // . is replaced with - because of BasicAuth parsing case with id
         String loginIdCandidate = candidateUser.getLoginId();
 
         User user = new User();
@@ -475,6 +474,7 @@
             user.password = candidateUser.getPassword();
         }
 
+        user.isGuest = candidateUser.isGuest();
         return createNewUser(user);
     }
 
@@ -1148,20 +1148,10 @@
         try {
             LdapUser ldapUser = ldapService.authenticate(loginIdOrEmail, password);
             play.Logger.debug("l: " + ldapUser);
+
             User localUserFoundByLdapLogin = User.findByEmail(ldapUser.getEmail());
             if (localUserFoundByLdapLogin.isAnonymous()) {
-                CandidateUser candidateUser = new CandidateUser(
-                        ldapUser.getDisplayName(),
-                        ldapUser.getEmail(),
-                        ldapUser.getUserLoginId(),
-                        password
-                );
-                User created = createUserDelegate(candidateUser);
-                if (created.state == UserState.LOCKED) {
-                    flash(Constants.INFO, "user.signup.requested");
-                    return User.anonymous;
-                }
-                return created;
+                return createNewUser(password, ldapUser);
             } else {
                 if(!localUserFoundByLdapLogin.isSamePassword(password)) {
                     User.resetPassword(localUserFoundByLdapLogin.loginId, password);
@@ -1169,6 +1159,7 @@
 
                 localUserFoundByLdapLogin.refresh();
                 localUserFoundByLdapLogin.name = ldapUser.getDisplayName();
+                localUserFoundByLdapLogin.isGuest = ldapUser.isGuestUser();
                 localUserFoundByLdapLogin.update();
                 return localUserFoundByLdapLogin;
             }
@@ -1191,6 +1182,22 @@
         }
     }
 
+    private static User createNewUser(String password, LdapUser ldapUser) {
+        CandidateUser candidateUser = new CandidateUser(
+                ldapUser.getDisplayName(),
+                ldapUser.getEmail(),
+                ldapUser.getUserLoginId(),
+                password,
+                ldapUser.isGuestUser()
+        );
+        User created = createUserDelegate(candidateUser);
+        if (created.state == UserState.LOCKED) {
+            flash(Constants.INFO, "user.signup.requested");
+            return User.anonymous;
+        }
+        return created;
+    }
+
     public static boolean isUsingSignUpConfirm(){
         Configuration config = play.Play.application().configuration();
         Boolean useSignUpConfirm = config.getBoolean("signup.require.admin.confirm");
app/models/CandidateUser.java
--- app/models/CandidateUser.java
+++ app/models/CandidateUser.java
@@ -16,17 +16,19 @@
     private String email;
     private String loginId;
     private String password;
+    private boolean isGuest;
 
     public CandidateUser(String name, String email) {
         this.name = name;
         this.email = email;
     }
 
-    public CandidateUser(String name, String email, String loginId, String password) {
+    public CandidateUser(String name, String email, String loginId, String password, boolean isGuest) {
         this.name = name;
         this.email = email;
         this.loginId = loginId;
         this.password = password;
+        this.isGuest = isGuest;
     }
 
     public String getName() {
@@ -64,6 +66,10 @@
         this.password = password;
     }
 
+    public boolean isGuest() {
+        return isGuest;
+    }
+
     @Override
     public String toString() {
         return "CandidateUser{" +
app/models/User.java
--- app/models/User.java
+++ app/models/User.java
@@ -182,6 +182,8 @@
     @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
     public List<OrganizationUser> organizationUsers;
 
+    public boolean isGuest = false;
+
     public User() {
     }
 
app/models/support/LdapUser.java
--- app/models/support/LdapUser.java
+++ app/models/support/LdapUser.java
@@ -6,10 +6,18 @@
  **/
 package models.support;
 
+import controllers.Application;
 import org.apache.commons.lang3.StringUtils;
 
 import javax.naming.NamingException;
 import javax.naming.directory.Attribute;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static controllers.Application.GUEST_USER_LOGIN_ID_PREFIX;
+import static org.apache.commons.lang3.StringUtils.isBlank;
+import static org.apache.commons.lang3.StringUtils.isNotBlank;
 
 public class LdapUser {
     private Attribute displayName;
@@ -25,7 +33,7 @@
     }
 
     public String getDisplayName() {
-        if (StringUtils.isNotBlank(getDepartment())) {
+        if (isNotBlank(getDepartment())) {
             return getString(this.displayName) + " [" + getDepartment() + "]";
         } else {
             return getString(this.displayName);
@@ -43,6 +51,29 @@
             e.printStackTrace();
             return "";
         }
+    }
+
+    public boolean isGuestUser() {
+        if(isBlank(GUEST_USER_LOGIN_ID_PREFIX)){
+            return false;
+        }
+        List<String> prefixes = new ArrayList<>();
+
+        for(String idPrefix: GUEST_USER_LOGIN_ID_PREFIX.replaceAll(" ", "")
+                .split(",")){
+            String prefix = StringUtils.defaultString(idPrefix, "").toLowerCase().trim();
+            if (isNotBlank(prefix)) {
+                prefixes.add(prefix);
+            }
+        }
+
+        for (String prefix : prefixes) {
+            if(this.getUserLoginId().toLowerCase().startsWith(prefix.toLowerCase())) {
+                return true;
+            }
+        }
+
+        return false;
     }
 
     public String getEmail() {
@@ -64,6 +95,7 @@
                 ", email='" + getEmail() + '\'' +
                 ", userId='" + getUserLoginId() + '\'' +
                 ", department='" + getDepartment() + '\'' +
+                ", isGuest='" + isGuestUser() + '\'' +
                 '}';
     }
 }
 
conf/evolutions/default/16.sql (added)
+++ conf/evolutions/default/16.sql
@@ -0,0 +1,7 @@
+# --- !Ups
+ALTER TABLE n4user ADD COLUMN is_guest tinyint(1) default 0;
+CREATE INDEX ix_n4user_is_guest ON n4user (is_guest);
+
+# --- !Downs
+DROP INDEX IF EXISTS ix_n4user_is_guest ON n4user;
+ALTER TABLE n4user DROP COLUMN is_guest;(파일 끝에 줄바꿈 문자 없음)
Add a comment
List