Example usage for javax.servlet.http HttpServletRequest isUserInRole

List of usage examples for javax.servlet.http HttpServletRequest isUserInRole

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest isUserInRole.

Prototype

public boolean isUserInRole(String role);

Source Link

Document

Returns a boolean indicating whether the authenticated user is included in the specified logical "role".

Usage

From source file:org.energyos.espi.datacustodian.web.DefaultController.java

@RequestMapping(Routes.DEFAULT)
public String defaultAfterLogin(HttpServletRequest request, Principal principal) {
    if (request.isUserInRole(RetailCustomer.ROLE_CUSTODIAN)) {
        return "redirect:/custodian/home";
    } else if (request.isUserInRole(RetailCustomer.ROLE_USER)) {
        return "redirect:/RetailCustomer/" + currentCustomer(principal).getId() + "/home";
    }/*  w ww  .j av a 2s  .co  m*/
    return "redirect:/home";
}

From source file:org.energyos.espi.datacustodian.web.DefaultControllerTest.java

@Test
public void whenRoleUser_redirectsToRetailCustomerHome() {
    DefaultController controller = new DefaultController();
    RetailCustomer customer = new RetailCustomer();
    customer.setId(99L);//from w  ww . j  a v a  2 s  .co  m
    HttpServletRequest request = mock(HttpServletRequest.class);

    when(request.isUserInRole(RetailCustomer.ROLE_USER)).thenReturn(true);
    when(request.isUserInRole(RetailCustomer.ROLE_CUSTODIAN)).thenReturn(false);

    Authentication principal = mock(Authentication.class);
    when(principal.getPrincipal()).thenReturn(customer);

    assertEquals("redirect:/RetailCustomer/" + customer.getId() + "/home",
            controller.defaultAfterLogin(request, principal));
}

From source file:org.apache.struts.taglib.tiles.InsertTag.java

/**
 * Parse the list of roles and return <code>true</code> or <code>false</code> based on whether
 * the user has that role or not.//from   www . jav  a  2  s  .c  o m
 * @param role Comma-delimited list of roles.
 * @param request The request.
 */
static public boolean userHasRole(HttpServletRequest request, String role) {
    StringTokenizer st = new StringTokenizer(role, ",");
    while (st.hasMoreTokens()) {
        if (request.isUserInRole(st.nextToken())) {
            return true;
        }
    }

    return false;
}

From source file:mx.edu.um.mateo.general.web.InicioController.java

@RequestMapping({ "/", "/inicio" })
public String inicio(HttpServletRequest request) {
    log.debug("Cargando pagina de inicio");
    if (request.isUserInRole("ROLE_JEFE")) {
        return "redirect:/jefe";
    }/* w  ww .j  a v  a2  s. co m*/
    return "inicio/index";
}

From source file:org.jia.ptrack.web.Visit.java

public boolean isUserInRole(RoleType roleType) {
    // pia-lab-returning-false-stub(acegi-web)
    HttpServletRequest request = getRequest();
    boolean result = request.isUserInRole(roleType.getValue());
    return result;
}

From source file:org.apache.tiles.jsp.taglib.RoleSecurityTagSupport.java

/**
 * Checks if the user is inside the specified role.
 *
 * @return <code>true</code> if the user is allowed to have the tag
 * rendered./*  w w w  . j av  a  2s  .com*/
 */
protected boolean isAccessAllowed() {
    HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
    return (role == null || req.isUserInRole(role));
}

From source file:de.whs.poodle.controllers.GlobalControllerAdvice.java

@ModelAttribute
public void populateModel(Model model, Principal principal, HttpServletRequest request) {
    // various info about login etc.
    boolean isStudent = request.isUserInRole("ROLE_STUDENT");
    boolean isInstructor = request.isUserInRole("ROLE_INSTRUCTOR");
    boolean isAdmin = request.isUserInRole("ROLE_ADMIN");
    boolean isSwitched = request.isUserInRole("ROLE_PREVIOUS_ADMINISTRATOR");
    boolean isInStudentMode = request.isUserInRole("ROLE_FAKE_STUDENT");

    model.addAttribute("isStudent", isStudent);
    model.addAttribute("isInstructor", isInstructor);
    model.addAttribute("isAdmin", isAdmin);
    model.addAttribute("isSwitched", isSwitched);
    model.addAttribute("isInStudentMode", isInStudentMode);
    model.addAttribute("isLoggedIn", isStudent || isInstructor);

    if (isStudent) {
        // add the student object and the courseTerms he is enrolled to (for the navigation)
        Student student = studentRepo.getByUsername(principal.getName());
        List<CourseTerm> courseTerms = courseTermRepo.getEnrolledForStudent(student.getId());

        model.addAttribute("student", student);
        model.addAttribute("username", student.getUsername());
        model.addAttribute("globalCourseTerms", courseTerms);
    } else if (isInstructor) {
        //add the instructor object and his courses (for the navigation)
        Instructor instructor = instructorRepo.getByUsername(principal.getName());
        List<Course> course = courseRepo.getAllForInstructor(instructor.getId());

        model.addAttribute("instructor", instructor);
        model.addAttribute("username", instructor.getUsername());
        model.addAttribute("globalCourses", course);
    }/*  w  w w.  j a  v a  2  s  .co  m*/
}

From source file:com.ocs.dynamo.service.impl.DefaultUserDetailsServiceImpl.java

@Override
public boolean isUserInRole(String role) {
    try {//  w w  w.  j  ava2 s  .  c  o m
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes()).getRequest();
        return request.isUserInRole(role);
    } catch (Exception ex) {
        return false;
    }
}

From source file:org.apache.struts.chain.servlet.AuthorizeAction.java

protected boolean isAuthorized(Context context, String[] roles, ActionConfig mapping) throws Exception {

    // Identify the HTTP request object
    ServletWebContext swcontext = (ServletWebContext) context;
    HttpServletRequest request = swcontext.getRequest();

    // Check the current user against the list of required roles
    for (int i = 0; i < roles.length; i++) {
        if (request.isUserInRole(roles[i])) {
            return (true);
        }/*  w ww.  j a va2  s . c om*/
    }

    // Default to unauthorized
    return (false);

}

From source file:com.ocs.dynamo.service.impl.DefaultUserDetailsServiceImpl.java

@Override
public boolean isUserInRole(String... roles) {
    try {//w  w w  .j  a  v  a  2  s . com
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes()).getRequest();
        for (String r : roles) {
            if (request.isUserInRole(r)) {
                return true;
            }
        }
        return false;
    } catch (Exception ex) {
        return false;
    }
}