X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=server%2Fcompbio%2Fcontrollers%2FBasicController.java;h=5c202367cd1203acef7bc2a2b5de70009465d9d4;hb=5637d461a2a42327d160f4de7b29ca6e6e1f32ab;hp=389a0f71ba3030311472ce0cd3cd297cd2cbaa89;hpb=c1ae48e93c766434005e5d5201af8ad23bc0a59b;p=proteocache.git diff --git a/server/compbio/controllers/BasicController.java b/server/compbio/controllers/BasicController.java index 389a0f7..5c20236 100644 --- a/server/compbio/controllers/BasicController.java +++ b/server/compbio/controllers/BasicController.java @@ -1,31 +1,58 @@ package compbio.controllers; +import java.text.SimpleDateFormat; +import java.util.Calendar; 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 compbio.cassandra.DateFormatter; +import compbio.cassandra.readers.CassandraReader; + +/** + * ProteoCache Basic controller. All other controllers should inherit this + * controller. Currently BasicController provides user role checks and some + * global dates for the system (current and the earliest date of executing job) + * + * @author Alexander Sherstnev + * @version 1.0 + * @since Dec 2013 + */ public class BasicController { + final protected SimpleDateFormat formaterDDMMYY = DateFormatter.getFormatDDMMYY(); + final protected SimpleDateFormat formaterYYMMDD = DateFormatter.getFormatYYMMDD(); + /** + * give the user (principal) name + * + * @return the user name. If the user uses LDAP credentials LDAP prefix is + * set + */ protected String getPrincipalName() { Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (principal instanceof UserDetails) { UserDetails details = (UserDetails) principal; - String ldapprefix = ""; + String rolefix = ""; String role = details.getUsername(); Collection au = details.getAuthorities(); for (GrantedAuthority ga : au) { - System.out.println("role -> " + ga.getAuthority()); if (ga.getAuthority().equals("ROLE_LDAP_USER")) { - ldapprefix = "LDAP:"; + rolefix = "LDAP:"; } } - return ldapprefix + role; + return rolefix + role; } return principal.toString(); } + /** + * check whether the current user has standard user permissions (ROLE_USER + * or ROLE_LDAP_USER) + * + * @return true if this is a ROLE_USER/ROLE_LDAP_USER user, false otherwise + */ protected boolean isUserRole() { Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (principal instanceof UserDetails) { @@ -41,4 +68,77 @@ public class BasicController { return false; } + /** + * check whether the current user has administrator permissions (ROLE_ADMIN) + * + * @return true if this is a ROLE_ADMIN user, false otherwise + */ + protected boolean isAdminRole() { + Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + if (principal instanceof UserDetails) { + UserDetails details = (UserDetails) principal; + Collection au = details.getAuthorities(); + for (GrantedAuthority ga : au) { + if (ga.getAuthority().equals("ROLE_ADMIN")) { + return true; + } + } + return false; + } + return false; + } + + /** + * check whether provided dates are ordered properly and within permitted + * time range + * + * @param date1 + * early limit of the time range) + * + * * @param date2 late limit of the time range) + * + * * @param longDate1 date 1 in the numerical represenation (in + * milliseconds) + * + * * @param longDate2 date 2 in the numerical represenation (in + * milliseconds) + * + * @return true if these dates are correct, false otherwise + */ + protected String checkDates(String date1, String date2, long longDate1, long longDate2) { + Calendar cal2 = Calendar.getInstance(); + if (date1.equalsIgnoreCase("") || date2.equalsIgnoreCase("")) + return "The date can not be empty strinfs"; + else if (!DateFormatter.isThisDateValid(date1, formaterYYMMDD) || !DateFormatter.isThisDateValid(date2, formaterYYMMDD)) + return "The date format in invalid. The format yyyy/mm/dd should be used"; + else if (longDate2 < CassandraReader.earliestDate()) + return "The date2 is earlier than the earliest date in the system " + getEarliestDate(); + else if (longDate1 > cal2.getTimeInMillis()) + return "The date1 is later than the current date " + getCurrentDate(); + else if (longDate1 > longDate2) + return "Wrong date range. The date1 is later than date2."; + + return null; + } + + /** + * gives the current date in the form of string (yyyy/mm/dd) + * + * @return the current date + */ + protected String getCurrentDate() { + Calendar cal = Calendar.getInstance(); + String date = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH); + return date; + } + + /** + * gives the earliest date in the system (through a direct call of a + * Cassandra related class) in the form of string (yyyy/mm/dd) + * + * @return the current date + */ + protected String getEarliestDate() { + return DateFormatter.DateLongToString(CassandraReader.earliestDate(), formaterYYMMDD); + } }