001    /*
002     * Databinder: a simple bridge from Wicket to Hibernate
003     * Copyright (C) 2006  Nathan Hamblen nathan@technically.us
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, write to the Free Software
017     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
018     */
019    package net.databinder.auth.components;
020    
021    import net.databinder.auth.AuthSession;
022    import net.databinder.auth.components.DataSignInPageBase.ReturnPage;
023    import net.databinder.components.NullPlug;
024    
025    import org.apache.wicket.AttributeModifier;
026    import org.apache.wicket.Component;
027    import org.apache.wicket.markup.html.border.Border;
028    import org.apache.wicket.markup.html.form.CheckBox;
029    import org.apache.wicket.markup.html.form.Form;
030    import org.apache.wicket.markup.html.form.RequiredTextField;
031    import org.apache.wicket.markup.html.form.SimpleFormComponentLabel;
032    import org.apache.wicket.markup.html.form.TextField;
033    import org.apache.wicket.markup.html.form.validation.FormComponentFeedbackBorder;
034    import org.apache.wicket.markup.html.panel.FeedbackPanel;
035    import org.apache.wicket.markup.html.panel.Panel;
036    import org.apache.wicket.model.Model;
037    import org.apache.wicket.model.ResourceModel;
038    
039    /**
040     * Displays username and password fields, along with optional "remember me" checkbox.
041     * Queries the IAuthSession upon a login attempt. Replaceable String resources: <pre>
042     * data.auth.username
043     * data.auth.password
044     * data.auth.remember
045     * data.auth.sign_in</pre>
046     * @see AuthSession
047     */
048    public class DataSignInPanel extends Panel {
049            private ReturnPage returnPage;
050            public DataSignInPanel(String id, ReturnPage returnPage) {
051                    super(id);
052                    this.returnPage = returnPage;
053                    add(new SignInForm("signInForm"));
054            }
055            
056            protected class SignInForm extends Form {
057                    private CheckBox rememberMe;
058                    private RequiredTextField username;
059                    private RSAPasswordTextField password;
060                    
061                    protected RequiredTextField getUsername() { return username; }
062                    protected RSAPasswordTextField getPassword() { return password; }
063                    protected CheckBox getRememberMe() { return rememberMe; }
064                    
065                    protected SignInForm(String id) {
066                            super(id);
067                            add(highFormSocket("highFormSocket"));
068                            add(feedbackBorder("username-border")
069                                    .add(username = new RequiredTextField("username", new Model())));
070                            username.setLabel(new ResourceModel("data.auth.username", "Username"));
071                            add(new SimpleFormComponentLabel("username-label", username));
072                            add(feedbackBorder("password-border")
073                                            .add(password = new RSAPasswordTextField("password", new Model(), this)));
074                            password.setRequired(true);
075                            password.setLabel(new ResourceModel("data.auth.password", "Password"));
076                            add(new SimpleFormComponentLabel("password-label", password));
077                            add(rememberMe = new CheckBox("rememberMe", new Model(Boolean.TRUE)));
078                            
079                            add(lowFormSocket("lowFormSocket"));
080                    }
081                    @Override
082                    protected void onSubmit() {
083                            if (DataSignInPageBase.getAuthSession().signIn((String)username.getModelObject(), (String)password.getModelObject(), 
084                                            (Boolean)rememberMe.getModelObject()))
085                            {
086                                    if (returnPage == null) {
087                                            if (!continueToOriginalDestination())
088                                                    setResponsePage(getApplication().getHomePage());
089                                    } else
090                                            setResponsePage(returnPage.get());
091                            } else
092                                    error(getLocalizer().getString("signInFailed", this, "Sorry, these credentials are not recognized."));
093                    }
094            }
095            
096            /** @return border to be added to each form component, base returns FormComponentFeedbackBorder */
097            protected Border feedbackBorder(String id) {
098                    return new FormComponentFeedbackBorder(id);
099            }
100    
101            /** @return content to appear above form, base return FeedbackPanel */
102            protected Component highFormSocket(String id) {
103                    return new FeedbackPanel(id)
104                            .add(new AttributeModifier("class", true, new Model("feedback")));
105            }
106    
107            /** @return content to appear below form, base return blank */
108            protected Component lowFormSocket(String id) {
109                    return new NullPlug(id);
110            }
111    }