View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.security.authz.expression;
17  
18  import org.kuali.mobility.security.user.api.User;
19  import org.kuali.mobility.security.user.entity.UserAttribute;
20  
21  import java.util.List;
22  
23  public class PersonAttributeExpression implements Expression {
24  
25  	private static final long serialVersionUID = 4461424607920854636L;
26  
27  	private String key;
28  
29  	private String value;
30  
31  	public PersonAttributeExpression(String key, String value) {
32  		this.key = key;
33  		this.value = value;
34  	}
35  
36  	public boolean evaluate(User user) {
37  		boolean success = false;
38  		List<UserAttribute> attributes = user.getAttribute(key);
39  		if (attributes != null) {
40  			for(UserAttribute attribute : attributes) {
41  				if (attribute.getAttributeValue() != null
42  					&& attribute.getAttributeValue().equalsIgnoreCase(value)) {
43  					success = true;
44  					break;
45  				}
46  			}
47  		}
48  		return success;
49  	}
50  
51  	public String getKey() {
52  		return key;
53  	}
54  
55  	public String getValue() {
56  		return value;
57  	}
58  
59  	@Override
60  	public boolean equals(Object obj) {
61  		if (obj instanceof PersonAttributeExpression) {
62  			PersonAttributeExpression other = (PersonAttributeExpression)obj;
63  			return key.equals(other.key) && value.equals(other.value);
64  		}
65  		return false;
66  	}
67  	
68  }