1 5 package org.exoplatform.faces.user.component; 6 7 import javax.faces.context.ExternalContext; 8 import javax.portlet.ActionResponse; 9 import org.exoplatform.faces.application.ExoFacesMessage; 10 import org.exoplatform.faces.core.component.InformationProvider; 11 import org.exoplatform.faces.core.component.UISimpleForm; 12 import org.exoplatform.faces.core.component.UIStringInput; 13 import org.exoplatform.faces.core.component.model.*; 14 import org.exoplatform.faces.core.event.ExoActionEvent; 15 import org.exoplatform.faces.core.event.ExoActionListener; 16 import org.exoplatform.container.SessionContainer ; 17 import org.exoplatform.portal.session.RequestInfo; 18 import org.exoplatform.services.portletcontainer.pci.Output; 19 import org.exoplatform.services.security.SecurityService; 20 25 public class UILogin extends UISimpleForm { 26 public static final String LOGIN_ACTION = "login"; 27 public static final String REGISTER_ACTION = "register"; 28 public static final String LOGOUT_ACTION = "logout"; 29 30 private UIStringInput userNameInput_; 31 private UIStringInput passwordInput_; 32 33 private String loginPath_; 34 private SecurityService securityService_; 35 36 public UILogin(SecurityService service) throws Exception { 37 super("loginForm", "post", null); 38 setId("UILogin"); 39 setRendererType("SimpleFormRenderer"); 40 setClazz("UILogin"); 41 securityService_ = service ; 42 RequestInfo rinfo = (RequestInfo)SessionContainer.getComponent(RequestInfo.class); 43 loginPath_ = rinfo.getContextPath() + "/faces/private/"; 44 45 userNameInput_ = new UIStringInput("userName", ""); 46 passwordInput_ = new UIStringInput("password", ""); 47 passwordInput_.setType(UIStringInput.PASSWORD); 48 add(new Row(). 49 add(new LabelCell("#{UILogin.label.user-name}")). 50 add(new ComponentCell(this, userNameInput_))); 51 add(new Row(). 52 add(new LabelCell("#{UILogin.label.password}")). 53 add(new ComponentCell(this, passwordInput_))); 54 add(new Row(). 55 add(new ListComponentCell(). 56 add(new FormButton("#{UILogin.button.login}", LOGIN_ACTION)). 57 addColspan("2").addAlign("center"))); 58 addActionListener(LoginActionListener.class , LOGIN_ACTION) ; 59 } 60 61 public String getLoginPath() { return loginPath_; } 62 63 static public class LoginActionListener extends ExoActionListener { 64 public void execute(ExoActionEvent event) throws Exception { 65 UILogin comp = (UILogin) event.getComponent() ; 66 String userName = comp.userNameInput_.getValue() ; 67 String password = comp.passwordInput_.getValue(); 68 InformationProvider iprovider = findInformationProvider(comp) ; 69 boolean hasError = false ; 70 if(userName == null || userName.length() == 0){ 71 iprovider.addMessage(new ExoFacesMessage("#{UILogin.msg.empty-login}")) ; 72 hasError = true ; 73 } 74 if(password == null || password.length() == 0){ 75 iprovider.addMessage(new ExoFacesMessage("#{UILogin.msg.empty-password}")) ; 76 hasError = true ; 77 } 78 if(hasError) return ; 79 if(comp.securityService_.authenticate(userName, password)) { 80 ExternalContext eContext =comp.getFacesContext().getExternalContext(); 81 ActionResponse response = (ActionResponse) eContext.getResponse(); 82 response.addProperty(Output.LOGIN, userName); 83 response.addProperty(Output.PASSWORD, password); 84 response.sendRedirect(comp.loginPath_ + userName); 85 } else { 86 iprovider.addMessage(new ExoFacesMessage("#{UILogin.msg.unknow-user}")) ; 87 } 88 } 89 } 90 } | Popular Tags |