001/* 002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Arnaud Kervern 018 */ 019 020package org.nuxeo.ecm.platform.shibboleth.computedgroups; 021 022import javax.el.ELException; 023import javax.el.PropertyNotFoundException; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.jboss.el.ExpressionFactoryImpl; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.impl.DocumentModelImpl; 030import org.nuxeo.ecm.platform.el.ExpressionContext; 031import org.nuxeo.ecm.platform.el.ExpressionEvaluator; 032import org.nuxeo.ecm.platform.shibboleth.ShibbolethConstants; 033 034/** 035 * Helper to provide an easy way to execute the expression language defined in a Shibb Group doc 036 * 037 * @author Arnaud Kervern 038 */ 039public class ELGroupComputerHelper { 040 041 private static final Log log = LogFactory.getLog(ELGroupComputerHelper.class); 042 043 protected static final ExpressionContext ec = new ExpressionContext(); 044 045 protected static final ExpressionEvaluator ee = new ExpressionEvaluator(new ExpressionFactoryImpl()); 046 047 public static boolean isUserInGroup(DocumentModel user, String el) { 048 if (el == null || el.equals("")) { 049 return false; 050 } 051 052 ee.bindValue(ec, ShibbolethConstants.EL_CURRENT_USER_NAME, user); 053 return ee.evaluateExpression(ec, "${" + el + "}", Boolean.class); 054 } 055 056 public static boolean isValidEL(String el) { 057 if (el == null || el.equals("")) { 058 return false; 059 } 060 061 try { 062 ee.bindValue(ec, ShibbolethConstants.EL_CURRENT_USER_NAME, new DocumentModelImpl("user")); 063 ee.evaluateExpression(ec, "${" + el + "}", Boolean.class); 064 } catch (PropertyNotFoundException e) { 065 return false; 066 } catch (ELException e) { 067 log.error(e, e); 068 return false; 069 } 070 return true; 071 } 072}