Package net.diegomaia.vraptor.saci.restriction

Examples of net.diegomaia.vraptor.saci.restriction.RestrictionResult


public class RestrictionValidator {

  public RestrictionResult validateRestrictions(
      RestrictionsWrapper resourceRestrictions,
      RestrictionsWrapper methodRestrictions, Profile profile) {
    RestrictionResult restrictionResult;
   
    restrictionResult = this.checkLogin(profile, resourceRestrictions.getOnAccessDenial(), methodRestrictions.getOnAccessDenial());
    if (!restrictionResult.isRestricted()){
      //User logged in
      restrictionResult = this.checkMethodRestrictions(profile, resourceRestrictions, methodRestrictions);
      if (!restrictionResult.isRestricted()){
        //Method (action) has no restrictions regarding the current user
        if (this.shouldInheritResourceRestrictions(resourceRestrictions, methodRestrictions))
          //Cascade restrictions - verify both method and resource restrictions
          restrictionResult = this.checkResourceRestrictions(profile, resourceRestrictions, methodRestrictions);
      }
View Full Code Here


  private RestrictionResult checkMethodRestrictions(Profile profile,
      RestrictionsWrapper resourceRestrictions,
      RestrictionsWrapper methodRestrictions) {
    List<Restriction> resourceRestrictionsList = methodRestrictions.getRestrictions();
    RestrictionResult restrictionResult = new RestrictionResult();
    for (Restriction restriction : resourceRestrictionsList){
      if (restriction instanceof AccessLevelRestriction){
        restrictionResult = this.checkAccessLevelRestriction(profile, (AccessLevelRestriction)restriction, methodRestrictions.getOnAccessDenial(), resourceRestrictions.getOnAccessDenial());
        if (restrictionResult.isRestricted()) break;
      }
      if (restriction instanceof RolesRestriction){
        restrictionResult = this.checkRolesRestriction(profile, (RolesRestriction)restriction, methodRestrictions.getOnAccessDenial(), resourceRestrictions.getOnAccessDenial());
      }
    }
View Full Code Here

 
  private RestrictionResult checkResourceRestrictions(Profile profile,
      RestrictionsWrapper resourceRestrictions,
      RestrictionsWrapper methodRestrictions) {
    List<Restriction> resourceRestrictionsList = resourceRestrictions.getRestrictions();
    RestrictionResult restrictionResult = new RestrictionResult();
    for (Restriction restriction : resourceRestrictionsList){
      if (restriction instanceof AccessLevelRestriction){
        restrictionResult = this.checkAccessLevelRestriction(profile, (AccessLevelRestriction)restriction, methodRestrictions.getOnAccessDenial(), resourceRestrictions.getOnAccessDenial());
        if (restrictionResult.isRestricted()) break;
      }
      if (restriction instanceof RolesRestriction){
        restrictionResult = this.checkRolesRestriction(profile, (RolesRestriction)restriction, methodRestrictions.getOnAccessDenial(), resourceRestrictions.getOnAccessDenial());
      }
    }
View Full Code Here

  }

  private RestrictionResult checkRolesRestriction(Profile profile,
      RolesRestriction restriction, OnAccessDenial onMethodAccessDenial,
      OnAccessDenial onResourceAccessDenial) {
    RestrictionResult restrictionResult = new RestrictionResult();
    List<Role> roles = restriction.getRoles();
    if (roles.size() == 0){
      throw new RestrictionAnnotationException("You must specify the roles in the 'roles' attribute within the @Roles annotation.");
    }
    switch (restriction.getPolicy()){
    case CONJUNCTION:
      if (!profile.getRoles().containsAll(restriction.getRolesAsStrings())){
        restrictionResult.setRestricted();
        restrictionResult.setRestrictionReason(RestrictionReason.ROLE_NOT_PLAYED_BY_USER);
        restrictionResult.setDestination(this.getDestination(onMethodAccessDenial, onResourceAccessDenial));
        restrictionResult.setHttp403(this.isHttp403(onMethodAccessDenial, onResourceAccessDenial));
      }
      break;
    case DISJUNCTION:
      boolean hasRole = false;
      for (Role role : roles){
        if (profile.getRoles().contains(role.getRole())){
          hasRole = true;
          break;
        }
      }
      if (!hasRole){
        restrictionResult.setRestricted();
        restrictionResult.setRestrictionReason(RestrictionReason.ROLE_NOT_PLAYED_BY_USER);
        restrictionResult.setDestination(this.getDestination(onMethodAccessDenial, onResourceAccessDenial));
        restrictionResult.setHttp403(this.isHttp403(onMethodAccessDenial, onResourceAccessDenial));
      }
      break;
    }
    return restrictionResult;
  }
View Full Code Here

  }

  private RestrictionResult checkAccessLevelRestriction(
      Profile profile, AccessLevelRestriction restriction,
      OnAccessDenial onMethodAccessDenial, OnAccessDenial onResourceAccessDenial) {
    RestrictionResult restrictionResult = new RestrictionResult();
    if (restriction.getMinimumAccessLevel() > restriction.getMaximumAccessLevel())
      throw new RestrictionAnnotationException("'minimumAccessLevel' cannot be greater than 'maximumAccessLevel'.");
    if (profile.getAccessLevel() < restriction.getMinimumAccessLevel() || profile.getAccessLevel() > restriction.getMaximumAccessLevel()){
      restrictionResult.setRestricted();
      restrictionResult.setRestrictionReason(RestrictionReason.ACCESS_LEVEL_OUT_OF_RANGE);
      restrictionResult.setDestination(this.getDestination(onMethodAccessDenial, onResourceAccessDenial));
      restrictionResult.setHttp403(this.isHttp403(onMethodAccessDenial, onResourceAccessDenial));
    }
    return restrictionResult;
  }
View Full Code Here

  }

  private RestrictionResult checkLogin(Profile profile,
      OnAccessDenial onResourceAccessDenial,
      OnAccessDenial onMethodAccessDenied) {
    RestrictionResult restrictionResult = new RestrictionResult();
    if (!profile.isLoggedIn()){
      String destination = "";
      restrictionResult.setRestricted();
      restrictionResult.setRestrictionReason(RestrictionReason.USER_NOT_LOGGED_IN);
      if (onMethodAccessDenied != null) {
        if (onMethodAccessDenied.forceHttp403()){
          restrictionResult.setHttp403(true);
        } else {
          if (!onMethodAccessDenied.loginPage().isEmpty()){
            destination = onMethodAccessDenied.loginPage();
          }
        }
      } else {
        if (onResourceAccessDenial != null) {
          if (onResourceAccessDenial.forceHttp403()) {
            restrictionResult.setHttp403(true);
          } else {
            if (!onResourceAccessDenial.loginPage().isEmpty()){
              destination = onResourceAccessDenial.loginPage();
            }
          }
        }
      }
      restrictionResult.setDestination(destination);
    }
    return restrictionResult;
  }
View Full Code Here

    this.restrictor = restrictor;
    this.result = result;
  }

  public RestrictionResult checkRestrictions(Method method, Profile profile, Boolean handleRedirection) {
    RestrictionResult restrictionResult = this.restrictor.checkRestriction(method, profile);
    this.confirmDestination(restrictionResult);
    if (restrictionResult.isRestricted() && handleRedirection) {
      this.handleRedirection(restrictionResult.isHttp403(), restrictionResult.getDestination());
      restrictionResult = null;
    }
    return restrictionResult;
  }
View Full Code Here

  public Restrictor(RestrictionValidator restrictionValidator) {
    this.restrictionValidator = restrictionValidator;
  }

  public RestrictionResult checkRestriction(Method method, Profile profile) {
    RestrictionResult restrictionResult;
    RestrictionsWrapper resourceRestrictions = this.getResourceRestriction(method.getDeclaringClass());
    RestrictionsWrapper methodRestrictions = this.getMethodRestrictions(method);
    restrictionResult = this.restrictionValidator.validateRestrictions(resourceRestrictions, methodRestrictions, profile);
    return restrictionResult;
  }
View Full Code Here

TOP

Related Classes of net.diegomaia.vraptor.saci.restriction.RestrictionResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.