Guest User

Untitled

a guest
Aug 16th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. /*
  2.  * <copyright>
  3.  * Copyright (c) 2003-2013 SETECH Inc.  All Rights Reserved.
  4.  * 903 Industrial Drive, Murfreesboro, TN 37129.
  5.  *
  6.  * This file is part of SETECH MROVelocity Hub
  7.  * </copyright>
  8.  *
  9.  * $Id$
  10.  */
  11. package com.setech.mrovelocityhub.services.security.model;
  12.  
  13. import java.util.List;
  14.  
  15. import org.apache.commons.lang3.builder.ToStringBuilder;
  16. import org.springframework.security.core.GrantedAuthority;
  17.  
  18. import com.setech.mrovelocityhub.persistence.security.model.AuthorizationValue;
  19.  
  20. /**
  21.  *
  22.  * @version $Revision$
  23.  * @since   7.0.0
  24.  */
  25. public class SessionUserGrantedAuthority implements GrantedAuthority {
  26.  
  27.     private static final long serialVersionUID = -3937521692967089258L;
  28.    
  29.     private String      authority;
  30.     private List<Long>  values;
  31.    
  32.     /**
  33.      * @param authority
  34.      */
  35.     public SessionUserGrantedAuthority(String authority, List<Long> values) {
  36.         this.authority = authority;
  37.         this.values    = values;
  38.     }
  39.    
  40.     /* (non-Javadoc)
  41.      * @see org.springframework.security.core.GrantedAuthority#getAuthority()
  42.      */
  43.     @Override
  44.     public String getAuthority() {
  45.         return authority;
  46.     }
  47.    
  48.     /**
  49.      * @return
  50.      */
  51.     public List<Long> getValues() {
  52.         return values;
  53.     }
  54.    
  55.     /**
  56.      * @param value
  57.      * @return
  58.      */
  59.     public boolean hasValue(Long value) {
  60.         // No value specified and no values provided for the role,
  61.         // then the user is permitted with no restrictions.
  62.         if(value == null && values.size() == 0)
  63.             return true;
  64.         // if value is null but there are values to restrict the
  65.         // authority, then indicate that the match failed.
  66.         if(value == null)
  67.             return false;
  68.         // If the values contains the ALL indicator, no need to then
  69.         // check the specified value, allow it.
  70.         if(values.contains(AuthorizationValue.ALL))
  71.             return true;
  72.         // check if the value exists in the collection.
  73.         return values.contains((Long)value);       
  74.     }
  75.    
  76.     /**
  77.      * @param values
  78.      * @return
  79.      */
  80.     public boolean hasAllValues(List<Long> values) {
  81.         // if the input is null but values contain values,
  82.         // then indicate that specified values were not
  83.         // found.
  84.         if(values == null && this.values.size() != 0)
  85.             return false;
  86.         // if the input contains the ALL indicator
  87.         if(values.contains(AuthorizationValue.ALL))
  88.             return true;
  89.         // check all specific values match
  90.         for(Long value : values) {
  91.             if(!hasValue(value))
  92.                 return false;
  93.         }
  94.         return true;
  95.     }
  96.    
  97.     /* (non-Javadoc)
  98.      * @see java.lang.Object#toString()
  99.      */
  100.     @Override
  101.     public String toString() {
  102.         return new ToStringBuilder(this)
  103.             .append("authority", authority)
  104.             .append("values", values)
  105.             .toString();
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment