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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
* Copyright 2011 gitblit.com.
*
* 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.gitblit.client;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import com.gitblit.Constants.AccessRestrictionType;
import com.gitblit.Keys;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.ServerSettings;
import com.gitblit.models.UserModel;
import com.gitblit.utils.StringUtils;
public class EditUserDialog extends JDialog {
private static final long serialVersionUID = 1L;
private final String username;
private final UserModel user;
private final ServerSettings settings;
private boolean isCreate;
private boolean canceled = true;
private JTextField usernameField;
private JPasswordField passwordField;
private JPasswordField confirmPasswordField;
private JCheckBox canAdminCheckbox;
private JCheckBox notFederatedCheckbox;
private JPalette<String> repositoryPalette;
private Set<String> usernames;
public EditUserDialog(ServerSettings settings) {
this(new UserModel(""), settings);
this.isCreate = true;
setTitle(Translation.get("gb.newUser"));
}
public EditUserDialog(UserModel anUser, ServerSettings settings) {
super();
this.username = anUser.username;
this.user = new UserModel("");
this.settings = settings;
this.usernames = new HashSet<String>();
this.isCreate = false;
initialize(anUser);
setModal(true);
setTitle(Translation.get("gb.edit") + ": " + anUser.username);
setIconImage(new ImageIcon(getClass().getResource("/gitblt-favicon.png")).getImage());
}
@Override
protected JRootPane createRootPane() {
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
JRootPane rootPane = new JRootPane();
rootPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
setVisible(false);
}
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
return rootPane;
}
private void initialize(UserModel anUser) {
usernameField = new JTextField(anUser.username == null ? "" : anUser.username, 25);
passwordField = new JPasswordField(anUser.password == null ? "" : anUser.password, 25);
confirmPasswordField = new JPasswordField(anUser.password == null ? "" : anUser.password,
25);
canAdminCheckbox = new JCheckBox(Translation.get("gb.canAdminDescription"), anUser.canAdmin);
notFederatedCheckbox = new JCheckBox(
Translation.get("gb.excludeFromFederationDescription"),
anUser.excludeFromFederation);
JPanel fieldsPanel = new JPanel(new GridLayout(0, 1));
fieldsPanel.add(newFieldPanel(Translation.get("gb.username"), usernameField));
fieldsPanel.add(newFieldPanel(Translation.get("gb.password"), passwordField));
fieldsPanel.add(newFieldPanel(Translation.get("gb.confirmPassword"), confirmPasswordField));
fieldsPanel.add(newFieldPanel(Translation.get("gb.canAdmin"), canAdminCheckbox));
fieldsPanel.add(newFieldPanel(Translation.get("gb.excludeFromFederation"),
notFederatedCheckbox));
repositoryPalette = new JPalette<String>();
JPanel panel = new JPanel(new BorderLayout());
panel.add(fieldsPanel, BorderLayout.NORTH);
panel.add(newFieldPanel(Translation.get("gb.restrictedRepositories"), repositoryPalette),
BorderLayout.CENTER);
JButton createButton = new JButton(Translation.get("gb.save"));
createButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (validateFields()) {
canceled = false;
setVisible(false);
}
}
});
JButton cancelButton = new JButton(Translation.get("gb.cancel"));
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
canceled = true;
setVisible(false);
}
});
JPanel controls = new JPanel();
controls.add(cancelButton);
controls.add(createButton);
final Insets _insets = new Insets(5, 5, 5, 5);
JPanel centerPanel = new JPanel(new BorderLayout(5, 5)) {
private static final long serialVersionUID = 1L;
@Override
public Insets getInsets() {
return _insets;
}
};
centerPanel.add(panel, BorderLayout.CENTER);
centerPanel.add(controls, BorderLayout.SOUTH);
getContentPane().setLayout(new BorderLayout(5, 5));
getContentPane().add(centerPanel, BorderLayout.CENTER);
pack();
}
private JPanel newFieldPanel(String label, JComponent comp) {
JLabel fieldLabel = new JLabel(label);
fieldLabel.setFont(fieldLabel.getFont().deriveFont(Font.BOLD));
fieldLabel.setPreferredSize(new Dimension(150, 20));
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
panel.add(fieldLabel);
panel.add(comp);
return panel;
}
private boolean validateFields() {
String uname = usernameField.getText();
if (StringUtils.isEmpty(uname)) {
error("Please enter a username!");
return false;
}
// verify username uniqueness on create
if (isCreate) {
if (usernames.contains(uname.toLowerCase())) {
error(MessageFormat.format("Username ''{0}'' is unavailable.", uname));
return false;
}
} else {
// check rename collision
if (!username.equalsIgnoreCase(uname)) {
if (usernames.contains(uname.toLowerCase())) {
error(MessageFormat.format(
"Failed to rename ''{0}'' because ''{1}'' already exists.", username,
uname));
return false;
}
}
}
int minLength = settings.get(Keys.realm.minPasswordLength).getInteger(5);
if (minLength < 4) {
minLength = 4;
}
char[] pw = passwordField.getPassword();
if (pw == null || pw.length < minLength) {
error(MessageFormat.format("Password is too short. Minimum length is {0} characters.",
minLength));
return false;
}
char[] cpw = confirmPasswordField.getPassword();
if (cpw == null || cpw.length != pw.length) {
error("Please confirm the password!");
return false;
}
if (!Arrays.equals(pw, cpw)) {
error("Passwords do not match!");
return false;
}
user.username = uname;
String type = settings.get(Keys.realm.passwordStorage).getString("md5");
if (type.equalsIgnoreCase("md5")) {
// store MD5 digest of password
user.password = StringUtils.MD5_TYPE + StringUtils.getMD5(new String(pw));
} else {
user.password = new String(pw);
}
user.canAdmin = canAdminCheckbox.isSelected();
user.excludeFromFederation = notFederatedCheckbox.isSelected();
user.repositories.clear();
user.repositories.addAll(repositoryPalette.getSelections());
return true;
}
private void error(String message) {
JOptionPane.showMessageDialog(EditUserDialog.this, message, Translation.get("gb.error"),
JOptionPane.ERROR_MESSAGE);
}
public void setUsers(List<UserModel> users) {
usernames.clear();
for (UserModel user : users) {
usernames.add(user.username.toLowerCase());
}
}
public void setRepositories(List<RepositoryModel> repositories, List<String> selected) {
List<String> restricted = new ArrayList<String>();
for (RepositoryModel repo : repositories) {
if (repo.accessRestriction.exceeds(AccessRestrictionType.NONE)) {
restricted.add(repo.name);
}
}
StringUtils.sortRepositorynames(restricted);
if (selected != null) {
StringUtils.sortRepositorynames(selected);
}
repositoryPalette.setObjects(restricted, selected);
}
public UserModel getUser() {
if (canceled) {
return null;
}
return user;
}
}
|