summaryrefslogtreecommitdiffstats
path: root/gerrit-gwtui/src/main/java/com/google/gerrit/client/auth/userpass/UserPassSignInDialog.java
blob: a0ec038458d6f9e558a8346126022df0009ba1b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.client.auth.userpass;

import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.SignInDialog;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.ui.SmallHeading;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.common.auth.SignInMode;
import com.google.gerrit.common.auth.userpass.LoginResult;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwtexpui.globalkey.client.GlobalKey;
import com.google.gwtexpui.globalkey.client.NpTextBox;

public class UserPassSignInDialog extends SignInDialog {
  private final FlowPanel formBody;

  private FlowPanel errorLine;
  private InlineLabel errorMsg;

  private Button login;
  private Button close;
  private TextBox username;
  private TextBox password;

  public UserPassSignInDialog(final String initialErrorMsg) {
    super(SignInMode.SIGN_IN);

    formBody = new FlowPanel();
    formBody.setStyleName("gerrit-OpenID-loginform");
    add(formBody);

    createHeaderText();
    createErrorBox();
    createUsernameBox();
    if (initialErrorMsg != null) {
      showError(initialErrorMsg);
    }
  }

  @Override
  public void show() {
    super.show();
    DeferredCommand.addCommand(new Command() {
      @Override
      public void execute() {
        username.setFocus(true);
      }
    });
  }

  private void createHeaderText() {
    final FlowPanel headerText = new FlowPanel();
    final SmallHeading headerLabel = new SmallHeading();
    headerLabel.setText(Util.M.signInAt(Location.getHostName()));
    headerText.add(headerLabel);
    formBody.add(headerText);
  }

  private void createErrorBox() {
    errorLine = new FlowPanel();
    DOM.setStyleAttribute(errorLine.getElement(), "visibility", "hidden");
    errorLine.setStyleName("gerrit-OpenID-errorline");

    errorMsg = new InlineLabel();
    errorLine.add(errorMsg);
    formBody.add(errorLine);
  }

  private void showError(final String msgText) {
    errorMsg.setText(msgText);
    DOM.setStyleAttribute(errorLine.getElement(), "visibility", "");
  }

  private void hideError() {
    DOM.setStyleAttribute(errorLine.getElement(), "visibility", "hidden");
  }

  private void createUsernameBox() {
    username = new NpTextBox();
    username.setVisibleLength(25);
    username.addKeyPressHandler(new KeyPressHandler() {
      @Override
      public void onKeyPress(final KeyPressEvent event) {
        if (event.getCharCode() == KeyCodes.KEY_ENTER) {
          event.preventDefault();
          password.selectAll();
          password.setFocus(true);
        }
      }
    });

    password = new PasswordTextBox();
    password.setVisibleLength(25);
    password.addKeyPressHandler(GlobalKey.STOP_PROPAGATION);
    password.addKeyPressHandler(new KeyPressHandler() {
      @Override
      public void onKeyPress(final KeyPressEvent event) {
        if (event.getCharCode() == KeyCodes.KEY_ENTER) {
          event.preventDefault();
          onLogin();
        }
      }
    });

    final FlowPanel buttons = new FlowPanel();
    buttons.setStyleName("gerrit-ErrorDialog-Buttons");

    login = new Button();
    login.setText(Util.C.buttonSignIn());
    login.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(final ClickEvent event) {
        onLogin();
      }
    });
    buttons.add(login);

    close = new Button();
    close.setText(Gerrit.C.errorDialogClose());
    close.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        hide();
      }
    });
    buttons.add(close);

    final Grid formGrid = new Grid(3, 2);
    formGrid.setText(0, 0, Util.C.username());
    formGrid.setText(1, 0, Util.C.password());
    formGrid.setWidget(0, 1, username);
    formGrid.setWidget(1, 1, password);
    formGrid.setWidget(2, 1, buttons);
    formBody.add(formGrid);

    username.setTabIndex(1);
    password.setTabIndex(2);
    login.setTabIndex(3);
    close.setTabIndex(4);
  }

  private void enable(final boolean on) {
    username.setEnabled(on);
    password.setEnabled(on);
    login.setEnabled(on);
  }

  private void onLogin() {
    hideError();

    final String user = username.getText();
    if (user == null || user.equals("")) {
      showError(Util.C.usernameRequired());
      username.setFocus(true);
      return;
    }

    final String pass = password.getText();
    if (pass == null || pass.equals("")) {
      showError(Util.C.passwordRequired());
      password.setFocus(true);
      return;
    }

    enable(false);
    Util.SVC.authenticate(user, pass, new GerritCallback<LoginResult>() {
      public void onSuccess(final LoginResult result) {
        if (result.success) {
          String token = History.getToken();
          if (result.isNew && !token.startsWith(PageLinks.REGISTER + ",")) {
            token = PageLinks.REGISTER + "," + token;
          }

          // Unfortunately we no longer support updating the web UI when the
          // user signs in. Instead we must force a reload of the page, but
          // that isn't easy because we might need to change the anchor. So
          // we bounce through a little redirection servlet on the server.
          //
          Location.replace(Location.getPath() + "login/" + token);
        } else {
          showError(Util.C.invalidLogin());
          enable(true);
          password.selectAll();
          DeferredCommand.addCommand(new Command() {
            @Override
            public void execute() {
              password.setFocus(true);
            }
          });
        }
      }

      @Override
      public void onFailure(final Throwable caught) {
        super.onFailure(caught);
        enable(false);
      }
    });
  }
}