Add LDAP authentication and enabling Spring logging
[proteocache.git] / server / compbio / controllers / BasicController.java
1 package compbio.controllers;
2
3 import java.util.Collection;
4
5 import org.springframework.security.core.GrantedAuthority;
6 import org.springframework.security.core.context.SecurityContextHolder;
7 import org.springframework.security.core.userdetails.UserDetails;
8
9 public class BasicController {
10
11         protected String getPrincipalName() {
12                 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
13                 if (principal instanceof UserDetails) {
14                         UserDetails details = (UserDetails) principal;
15                         String ldapprefix = "";
16                         String role = details.getUsername();
17                         Collection<? extends GrantedAuthority> au = details.getAuthorities();
18                         for (GrantedAuthority ga : au) {
19                                 System.out.println("role -> " + ga.getAuthority());
20                                 if (ga.getAuthority().equals("ROLE_LDAP_USER")) {
21                                         ldapprefix = "LDAP:";
22                                 }
23                         }
24                         return ldapprefix + role;
25                 }
26                 return principal.toString();
27         }
28
29         protected boolean isUserRole() {
30                 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
31                 if (principal instanceof UserDetails) {
32                         UserDetails details = (UserDetails) principal;
33                         Collection<? extends GrantedAuthority> au = details.getAuthorities();
34                         for (GrantedAuthority ga : au) {
35                                 if (ga.getAuthority().equals("ROLE_USER") || ga.getAuthority().equals("ROLE_LDAP_USER")) {
36                                         return true;
37                                 }
38                         }
39                         return false;
40                 }
41                 return false;
42         }
43
44 }