001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/**
020 * Implements the CFML Function isuserinrole
021 */
022package lucee.runtime.functions.decision;
023
024import lucee.commons.lang.StringUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.exp.PageException;
027import lucee.runtime.ext.function.Function;
028import lucee.runtime.security.Credential;
029import lucee.runtime.type.util.ListUtil;
030
031public final class IsUserInAnyRole implements Function {
032        public static boolean call(PageContext pc) throws PageException {
033                return call(pc, null);
034        }
035        public static boolean call(PageContext pc, String strRoles) throws PageException {
036                if(StringUtil.isEmpty(strRoles)){
037                        Credential ru = pc.getRemoteUser();
038                        if(ru==null) return false;
039                    return ru.getRoles().length>0;
040                }
041                
042                String[] roles = ListUtil.trimItems(ListUtil.listToStringArray(strRoles, ','));
043                for(int i=0;i<roles.length;i++) {
044                        if(IsUserInRole.call(pc, roles[i])) return true;
045                }
046                return false;
047        }
048}