001/**
002 * Copyright 2011-2013 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 */
015
016package org.kuali.mobility.security.authz.expression;
017
018import org.kuali.mobility.security.user.api.User;
019import org.kuali.mobility.security.user.entity.UserAttribute;
020
021import java.util.List;
022
023public class PersonAttributeExpression implements Expression {
024
025        private static final long serialVersionUID = 4461424607920854636L;
026
027        private String key;
028
029        private String value;
030
031        public PersonAttributeExpression(String key, String value) {
032                this.key = key;
033                this.value = value;
034        }
035
036        public boolean evaluate(User user) {
037                boolean success = false;
038                List<UserAttribute> attributes = user.getAttribute(key);
039                if (attributes != null) {
040                        for(UserAttribute attribute : attributes) {
041                                if (attribute.getAttributeValue() != null
042                                        && attribute.getAttributeValue().equalsIgnoreCase(value)) {
043                                        success = true;
044                                        break;
045                                }
046                        }
047                }
048                return success;
049        }
050
051        public String getKey() {
052                return key;
053        }
054
055        public String getValue() {
056                return value;
057        }
058
059        @Override
060        public boolean equals(Object obj) {
061                if (obj instanceof PersonAttributeExpression) {
062                        PersonAttributeExpression other = (PersonAttributeExpression)obj;
063                        return key.equals(other.key) && value.equals(other.value);
064                }
065                return false;
066        }
067        
068}