001    package railo.runtime.tag;
002    
003    import javax.servlet.jsp.tagext.Tag;
004    
005    import railo.commons.io.res.Resource;
006    import railo.runtime.exp.PageException;
007    import railo.runtime.ext.tag.TagImpl;
008    import railo.runtime.security.CredentialImpl;
009    import railo.runtime.type.KeyImpl;
010    import railo.runtime.type.Scope;
011    import railo.runtime.util.ApplicationContext;
012    
013    /**
014     * 
015     */
016    public final class Loginuser extends TagImpl {
017        private String name;
018        private String password;
019        private String[] roles;
020    
021            /**
022            * @see javax.servlet.jsp.tagext.Tag#release()
023            */
024            public void release()   {
025                    super.release();
026                    name=null;
027                    password=null;
028                    roles=null;
029            }
030    
031        /**
032         * @param name The name to set.
033         */
034        public void setName(String name) {
035            this.name = name;
036        }
037        /**
038         * @param password The password to set.
039         */
040        public void setPassword(String password) {
041            this.password = password;
042        }
043        /**
044         * @param oRoles The roles to set.
045         * @throws PageException
046         */
047        public void setRoles(Object oRoles) throws PageException {
048            roles=CredentialImpl.toRole(oRoles);
049        }
050        
051            /**
052            * @throws PageException
053             * @see javax.servlet.jsp.tagext.Tag#doStartTag()
054            */
055            public int doStartTag() throws PageException    {
056                    Resource rolesDir = pageContext.getConfig().getConfigDir().getRealResource("roles");
057                CredentialImpl login = new CredentialImpl(name,password,roles,rolesDir);
058                pageContext.setRemoteUser(login);
059                
060                Tag parent=getParent();
061                    while(parent!=null && !(parent instanceof Login)) {
062                            parent=parent.getParent();
063                    }
064                    ApplicationContext appContext = pageContext.getApplicationContext();
065                    if(parent!=null) {
066                        int loginStorage = appContext.getLoginStorage();
067                        String name=Login.getApplicationName(appContext);
068                        
069                        if(loginStorage==Scope.SCOPE_SESSION && pageContext.getApplicationContext().isSetSessionManagement())
070                            pageContext.sessionScope().set(KeyImpl.init(name),login.encode());
071                        else  {//if(loginStorage==Scope.SCOPE_COOKIE)
072                            pageContext.cookieScope().setCookie(KeyImpl.init(name),login.encode(),
073                                            -1,false,"/",Login.getCookieDomain(appContext));
074                        }
075                    }
076                    
077                
078                    return SKIP_BODY;
079            }
080    
081            /**
082            * @see javax.servlet.jsp.tagext.Tag#doEndTag()
083            */
084            public int doEndTag()   {
085                    return EVAL_PAGE;
086            }
087    }