1 package compbio.controllers;
3 import java.text.SimpleDateFormat;
4 import java.util.Calendar;
5 import java.util.Collection;
7 import org.springframework.security.core.GrantedAuthority;
8 import org.springframework.security.core.context.SecurityContextHolder;
9 import org.springframework.security.core.userdetails.UserDetails;
11 import compbio.cassandra.DateFormatter;
12 import compbio.cassandra.readers.CassandraReader;
15 * ProteoCache Basic controller. All other controllers should inherit this
16 * controller. Currently BasicController provides user role checks and some
17 * global dates for the system (current and the earliest date of executing job)
19 * @author Alexander Sherstnev
23 public class BasicController {
24 final protected SimpleDateFormat formaterDDMMYY = DateFormatter.getFormatDDMMYY();
25 final protected SimpleDateFormat formaterYYMMDD = DateFormatter.getFormatYYMMDD();
28 * give the user (principal) name
30 * @return the user name. If the user uses LDAP credentials LDAP prefix is
33 protected String getPrincipalName() {
34 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
35 if (principal instanceof UserDetails) {
36 UserDetails details = (UserDetails) principal;
38 String role = details.getUsername();
39 Collection<? extends GrantedAuthority> au = details.getAuthorities();
40 for (GrantedAuthority ga : au) {
41 if (ga.getAuthority().equals("ROLE_LDAP_USER")) {
45 return rolefix + role;
47 return principal.toString();
51 * check whether the current user has standard user permissions (ROLE_USER
54 * @return true if this is a ROLE_USER/ROLE_LDAP_USER user, false otherwise
56 protected boolean isUserRole() {
57 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
58 if (principal instanceof UserDetails) {
59 UserDetails details = (UserDetails) principal;
60 Collection<? extends GrantedAuthority> au = details.getAuthorities();
61 for (GrantedAuthority ga : au) {
62 if (ga.getAuthority().equals("ROLE_USER") || ga.getAuthority().equals("ROLE_LDAP_USER")) {
72 * check whether the current user has administrator permissions (ROLE_ADMIN)
74 * @return true if this is a ROLE_ADMIN user, false otherwise
76 protected boolean isAdminRole() {
77 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
78 if (principal instanceof UserDetails) {
79 UserDetails details = (UserDetails) principal;
80 Collection<? extends GrantedAuthority> au = details.getAuthorities();
81 for (GrantedAuthority ga : au) {
82 if (ga.getAuthority().equals("ROLE_ADMIN")) {
92 * check whether provided dates are ordered properly and within permitted
96 * early limit of the time range)
98 * * @param date2 late limit of the time range)
100 * * @param longDate1 date 1 in the numerical represenation (in
103 * * @param longDate2 date 2 in the numerical represenation (in
106 * @return true if these dates are correct, false otherwise
108 protected String checkDates(String date1, String date2, long longDate1, long longDate2) {
109 Calendar cal2 = Calendar.getInstance();
110 if (date1.equalsIgnoreCase("") || date2.equalsIgnoreCase(""))
111 return "The date can not be empty strinfs";
112 else if (!DateFormatter.isThisDateValid(date1, formaterYYMMDD) || !DateFormatter.isThisDateValid(date2, formaterYYMMDD))
113 return "The date format in invalid. The format yyyy/mm/dd should be used";
114 else if (longDate2 < CassandraReader.earliestDate())
115 return "The date2 is earlier than the earliest date in the system " + getEarliestDate();
116 else if (longDate1 > cal2.getTimeInMillis())
117 return "The date1 is later than the current date " + getCurrentDate();
118 else if (longDate1 > longDate2)
119 return "Wrong date range. The date1 is later than date2.";
125 * gives the current date in the form of string (yyyy/mm/dd)
127 * @return the current date
129 protected String getCurrentDate() {
130 Calendar cal = Calendar.getInstance();
131 String date = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
136 * gives the earliest date in the system (through a direct call of a
137 * Cassandra related class) in the form of string (yyyy/mm/dd)
139 * @return the current date
141 protected String getEarliestDate() {
142 return DateFormatter.DateLongToString(CassandraReader.earliestDate(), formaterYYMMDD);