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 **/
019package lucee.runtime.tag;
020
021import javax.servlet.jsp.tagext.Tag;
022
023import lucee.commons.io.res.Resource;
024import lucee.runtime.exp.PageException;
025import lucee.runtime.ext.tag.TagImpl;
026import lucee.runtime.listener.ApplicationContext;
027import lucee.runtime.security.CredentialImpl;
028import lucee.runtime.type.KeyImpl;
029import lucee.runtime.type.scope.Scope;
030
031/**
032 * 
033 */
034public final class Loginuser extends TagImpl {
035    private String name;
036    private String password;
037    private String[] roles;
038
039        @Override
040        public void release()   {
041                super.release();
042                name=null;
043                password=null;
044                roles=null;
045        }
046
047    /**
048     * @param name The name to set.
049     */
050    public void setName(String name) {
051        this.name = name;
052    }
053    /**
054     * @param password The password to set.
055     */
056    public void setPassword(String password) {
057        this.password = password;
058    }
059    /**
060     * @param oRoles The roles to set.
061     * @throws PageException
062     */
063    public void setRoles(Object oRoles) throws PageException {
064        roles=CredentialImpl.toRole(oRoles);
065    }
066    
067        @Override
068        public int doStartTag() throws PageException    {
069                Resource rolesDir = pageContext.getConfig().getConfigDir().getRealResource("roles");
070            CredentialImpl login = new CredentialImpl(name,password,roles,rolesDir);
071            pageContext.setRemoteUser(login);
072            
073            Tag parent=getParent();
074                while(parent!=null && !(parent instanceof Login)) {
075                        parent=parent.getParent();
076                }
077                ApplicationContext appContext = pageContext.getApplicationContext();
078                if(parent!=null) {
079                    int loginStorage = appContext.getLoginStorage();
080                    String name=Login.getApplicationName(appContext);
081                    
082                    if(loginStorage==Scope.SCOPE_SESSION && pageContext.getApplicationContext().isSetSessionManagement())
083                        pageContext.sessionScope().set(KeyImpl.init(name),login.encode());
084                    else  {//if(loginStorage==Scope.SCOPE_COOKIE)
085                        pageContext.cookieScope().setCookie(KeyImpl.init(name),login.encode(),
086                                        -1,false,"/",Login.getCookieDomain(appContext));
087                    }
088                }
089                
090            
091                return SKIP_BODY;
092        }
093
094        @Override
095        public int doEndTag()   {
096                return EVAL_PAGE;
097        }
098}