32703aa67fd8ece816cb4a54f26e74fa143ec22d
[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 rolefix = "";
16                         String role = details.getUsername();
17                         Collection<? extends GrantedAuthority> au = details.getAuthorities();
18                         for (GrantedAuthority ga : au) {
19                                 if (ga.getAuthority().equals("ROLE_LDAP_USER")) {
20                                         rolefix = "LDAP:";
21                                 }
22                         }
23                         return rolefix + role;
24                 }
25                 return principal.toString();
26         }
27
28         protected boolean isUserRole() {
29                 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
30                 if (principal instanceof UserDetails) {
31                         UserDetails details = (UserDetails) principal;
32                         Collection<? extends GrantedAuthority> au = details.getAuthorities();
33                         for (GrantedAuthority ga : au) {
34                                 if (ga.getAuthority().equals("ROLE_USER") || ga.getAuthority().equals("ROLE_LDAP_USER")) {
35                                         return true;
36                                 }
37                         }
38                         return false;
39                 }
40                 return false;
41         }
42
43         protected boolean isAdminRole() {
44                 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
45                 if (principal instanceof UserDetails) {
46                         UserDetails details = (UserDetails) principal;
47                         Collection<? extends GrantedAuthority> au = details.getAuthorities();
48                         for (GrantedAuthority ga : au) {
49                                 if (ga.getAuthority().equals("ROLE_ADMIN")) {
50                                         return true;
51                                 }
52                         }
53                         return false;
54                 }
55                 return false;
56         }
57         
58 }