Add LDAP authentication and enabling Spring logging
[proteocache.git] / server / compbio / controllers / BasicController.java
index d4be834..389a0f7 100644 (file)
@@ -1,21 +1,44 @@
 package compbio.controllers;
 
+import java.util.Collection;
+
+import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.security.core.userdetails.UserDetails;
-/*
-import org.springframework.stereotype.Controller;
-import org.springframework.ui.ModelMap;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-*/
 
 public class BasicController {
 
        protected String getPrincipalName() {
                Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                if (principal instanceof UserDetails) {
-                       return ((UserDetails) principal).getUsername();
-               } 
+                       UserDetails details = (UserDetails) principal;
+                       String ldapprefix = "";
+                       String role = details.getUsername();
+                       Collection<? extends GrantedAuthority> au = details.getAuthorities();
+                       for (GrantedAuthority ga : au) {
+                               System.out.println("role -> " + ga.getAuthority());
+                               if (ga.getAuthority().equals("ROLE_LDAP_USER")) {
+                                       ldapprefix = "LDAP:";
+                               }
+                       }
+                       return ldapprefix + role;
+               }
                return principal.toString();
        }
+
+       protected boolean isUserRole() {
+               Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+               if (principal instanceof UserDetails) {
+                       UserDetails details = (UserDetails) principal;
+                       Collection<? extends GrantedAuthority> au = details.getAuthorities();
+                       for (GrantedAuthority ga : au) {
+                               if (ga.getAuthority().equals("ROLE_USER") || ga.getAuthority().equals("ROLE_LDAP_USER")) {
+                                       return true;
+                               }
+                       }
+                       return false;
+               }
+               return false;
+       }
+
 }