Some tiny technical changes to controllers
authorSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Wed, 18 Dec 2013 08:59:23 +0000 (08:59 +0000)
committerSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Wed, 18 Dec 2013 08:59:23 +0000 (08:59 +0000)
server/compbio/controllers/BasicController.java
server/compbio/controllers/MainController.java
server/compbio/controllers/UserController.java
server/compbio/spring/security/LDAPAuthorityMapper.java
server/compbio/spring/security/PCacheLDAPAuthority.java
webapp/view/fragments/mainmenu.jsp

index 389a0f7..32703aa 100644 (file)
@@ -12,16 +12,15 @@ public class BasicController {
                Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                if (principal instanceof UserDetails) {
                        UserDetails details = (UserDetails) principal;
-                       String ldapprefix = "";
+                       String rolefix = "";
                        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:";
+                                       rolefix = "LDAP:";
                                }
                        }
-                       return ldapprefix + role;
+                       return rolefix + role;
                }
                return principal.toString();
        }
@@ -41,4 +40,19 @@ public class BasicController {
                return false;
        }
 
+       protected boolean isAdminRole() {
+               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_ADMIN")) {
+                                       return true;
+                               }
+                       }
+                       return false;
+               }
+               return false;
+       }
+       
 }
index a945e25..d5bef44 100644 (file)
@@ -10,7 +10,8 @@ public class MainController extends BasicController {
 
        @RequestMapping(value = "/index", method = RequestMethod.GET)
        public String printPublicHome(ModelMap model) {
-               model.addAttribute("message", "Spring Security Custom Form example");
+               model.addAttribute("username", getPrincipalName());
+               if (isUserRole()) return "home";
                return "public";
        }
 
index a00bd15..56b7087 100644 (file)
@@ -35,7 +35,7 @@ public class UserController {
        // JavaMailSender mailSender;
        private final Pattern EMAIL = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}");
 
-       @RequestMapping(value = "/register/query", method = RequestMethod.POST)
+       @RequestMapping(value = "/register/query", method = RequestMethod.GET)
        public String RegisterForm(ModelMap model) {
                User user = new User();
                model.addAttribute(user);
@@ -91,7 +91,7 @@ public class UserController {
                }
 
                user.setRegistrationDate(new Date());
-               UserManager cm = new CassandraUserManager();
+               CassandraUserManager cm = new CassandraUserManager();
                try {
                        cm.addUser(user);
                } catch (DataIntegrityViolationException e) {
index 061a3c6..4be363a 100644 (file)
@@ -11,10 +11,17 @@ import org.apache.commons.logging.LogFactory;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
-import org.springframework.security.ldap.authentication.LdapAuthenticationProvider;
 
 /**
- * Maps groups defined in LDAP to roles for a specific user.
+ * The class maps LDAP groups defined in the LDAP service to the ProteoCache
+ * roles for a specific user. The class implements the standard Spring Security
+ * GrantedAuthoritiesMapper interface and should replace the standard
+ * Authorities Mapper in security configuration XML file.
+ * 
+ * @version 1.0 January 2014
+ * 
+ * @author Alexaner Sherstnev
+ * 
  */
 @Resource
 public class LDAPAuthorityMapper implements GrantedAuthoritiesMapper {
@@ -23,29 +30,19 @@ public class LDAPAuthorityMapper implements GrantedAuthoritiesMapper {
 
        public LDAPAuthorityMapper() {
        }
-/*
-       public Collection<? extends GrantedAuthority> mapAuthorities(final Collection<? extends GrantedAuthority> authorities) {
 
-               Set<PCacheLDAPAuthority> roles = EnumSet.noneOf(PCacheLDAPAuthority.class);
-               roles.add(PCacheLDAPAuthority.ROLE_LDAP_USER);
-               logger.info("LDAPAuthorityMapper: set new role ROLE_LDAP_USER");
-               *
-                * for (GrantedAuthority authority : authorities) { if
-                * (ROLE_CUSTOMER_SERVICE_OFFICER.equals(authority.getAuthority())) {
-                * roles.add(PCacheLDAPAuthority.ROLE_USER); } else if
-                * (ROLE_ADMIN.equals(authority.getAuthority())) {
-                * roles.add(PCacheLDAPAuthority.ROLE_ADMIN); } }
-                *
-               return roles;
-       }*/
-       
-       
+       /**
+        * the real mapping method. Currently, all users who are able to
+        * authenticate are assigned with the same ROLE_LDAP_USER.
+        * 
+        * @param authorities a set of original granted authorities assigned by ldapAuthProvider
+        * @return a new set of granted authorities greated in the method
+        */
        public Collection<? extends GrantedAuthority> mapAuthorities(final Collection<? extends GrantedAuthority> authorities) {
                SimpleGrantedAuthority sa = new SimpleGrantedAuthority("ROLE_LDAP_USER");
                Set<GrantedAuthority> roles = new HashSet<GrantedAuthority>();
                roles.add(sa);
                logger.info("LDAPAuthorityMapper: set new role ROLE_LDAP_USER");
-
                return roles;
        }
 }
index dde6d87..aee0964 100644 (file)
@@ -4,12 +4,14 @@ import org.springframework.security.core.GrantedAuthority;
 import org.springframework.util.Assert;
 
 /**
- * Maps groups defined in LDAP to roles for a specific user.
- */
-/*
- * public enum PCacheLDAPAuthority implements GrantedAuthority { ROLE_LDAP_USER;
- * public String getAuthority() { return name(); } }
+ * The class implements the standard granted authority for Spring Security Not
+ * used in the current version of ProteoCache
+ * 
+ * @version 1.0 January 2014
+ * 
+ * @author Alexaner Sherstnev
  */
+
 public final class PCacheLDAPAuthority implements GrantedAuthority {
 
        private static final long serialVersionUID = 1;
@@ -17,7 +19,7 @@ public final class PCacheLDAPAuthority implements GrantedAuthority {
        private String role = "ROLE_LDAP_USER";
 
        public PCacheLDAPAuthority(String role) {
-               Assert.hasText(role, "A granted authority textual representation is required");
+               Assert.hasText(role, "A text representation of the granted authority is required");
                this.role = role;
        }
 
index 8f8e403..adaf956 100644 (file)
                        <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-search"></span> Search Servlets <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                        <li><a href="<spring:url value="/sequence/query" htmlEscape="true" />">Protein Sequence</a></li>
+                                       <li><a href="<spring:url value="/features/query" htmlEscape="true" />">Protein Secondary Structure Features</a></li>
+                                       <!-- 
                                        <li><a href="<spring:url value="/job/query" htmlEscape="true" />">Job</a></li>
                                        <li><a href="<spring:url value="/admin/ip/query" htmlEscape="true" />">IP</a></li>
+                                       -->
                                </ul>
                        </li>
                        <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-stats"></span> Statistics Servlets<b class="caret"></b></a>
@@ -24,7 +27,6 @@
                                        <li><a href="<spring:url value="/stat/exectime/query" htmlEscape="true" />">Job Time Execution</a></li>
                                        <li><a href="<spring:url value="/admin/ip/counts/query" htmlEscape="true" />">IPs by Job count</a></li>
                                        <li><a href="<spring:url value="/sequence/counts/query" htmlEscape="true" />">Proteins by Job count</a></li>
-                                       <li><a href="<spring:url value="/features/query" htmlEscape="true" />">Proteins by SS Features</a></li>
                                </ul>
                        </li>
 <sec:authorize access="hasRole('ROLE_ADMIN')">