001/* 002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Arnaud Kervern 016 */ 017 018package org.nuxeo.ecm.platform.shibboleth.computedgroups; 019 020import javax.el.ELException; 021import javax.el.PropertyNotFoundException; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.jboss.el.ExpressionFactoryImpl; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.impl.DocumentModelImpl; 028import org.nuxeo.ecm.platform.el.ExpressionContext; 029import org.nuxeo.ecm.platform.el.ExpressionEvaluator; 030import org.nuxeo.ecm.platform.shibboleth.ShibbolethConstants; 031 032/** 033 * Helper to provide an easy way to execute the expression language defined in a Shibb Group doc 034 * 035 * @author Arnaud Kervern 036 */ 037public class ELGroupComputerHelper { 038 039 private static final Log log = LogFactory.getLog(ELGroupComputerHelper.class); 040 041 protected static final ExpressionContext ec = new ExpressionContext(); 042 043 protected static final ExpressionEvaluator ee = new ExpressionEvaluator(new ExpressionFactoryImpl()); 044 045 public static boolean isUserInGroup(DocumentModel user, String el) { 046 if (el == null || el.equals("")) { 047 return false; 048 } 049 050 ee.bindValue(ec, ShibbolethConstants.EL_CURRENT_USER_NAME, user); 051 return ee.evaluateExpression(ec, "${" + el + "}", Boolean.class); 052 } 053 054 public static boolean isValidEL(String el) { 055 if (el == null || el.equals("")) { 056 return false; 057 } 058 059 try { 060 ee.bindValue(ec, ShibbolethConstants.EL_CURRENT_USER_NAME, new DocumentModelImpl("user")); 061 ee.evaluateExpression(ec, "${" + el + "}", Boolean.class); 062 } catch (PropertyNotFoundException e) { 063 return false; 064 } catch (ELException e) { 065 log.error(e, e); 066 return false; 067 } 068 return true; 069 } 070}