commit | author | age
|
fe24a0
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
|
16 |
package com.gitblit.wicket.pages;
|
|
17 |
|
|
18 |
import java.text.MessageFormat;
|
|
19 |
import java.util.ArrayList;
|
|
20 |
import java.util.Collections;
|
37d5b7
|
21 |
import java.util.Comparator;
|
0b9119
|
22 |
import java.util.HashSet;
|
fe24a0
|
23 |
import java.util.Iterator;
|
JM |
24 |
import java.util.List;
|
0b9119
|
25 |
import java.util.Set;
|
fe24a0
|
26 |
|
JM |
27 |
import org.apache.wicket.PageParameters;
|
d3ca1c
|
28 |
import org.apache.wicket.behavior.SimpleAttributeModifier;
|
fe24a0
|
29 |
import org.apache.wicket.extensions.markup.html.form.palette.Palette;
|
JM |
30 |
import org.apache.wicket.markup.html.form.Button;
|
7f7051
|
31 |
import org.apache.wicket.markup.html.form.CheckBox;
|
37d5b7
|
32 |
import org.apache.wicket.markup.html.form.ChoiceRenderer;
|
fe24a0
|
33 |
import org.apache.wicket.markup.html.form.Form;
|
JM |
34 |
import org.apache.wicket.markup.html.form.TextField;
|
|
35 |
import org.apache.wicket.model.CompoundPropertyModel;
|
0b9119
|
36 |
import org.apache.wicket.model.IModel;
|
JM |
37 |
import org.apache.wicket.model.Model;
|
fe24a0
|
38 |
import org.apache.wicket.model.util.CollectionModel;
|
JM |
39 |
import org.apache.wicket.model.util.ListModel;
|
|
40 |
|
fd9d28
|
41 |
import com.gitblit.Constants.RegistrantType;
|
fe24a0
|
42 |
import com.gitblit.GitBlitException;
|
428b22
|
43 |
import com.gitblit.Keys;
|
822dfe
|
44 |
import com.gitblit.models.RegistrantAccessPermission;
|
fe24a0
|
45 |
import com.gitblit.models.TeamModel;
|
37d5b7
|
46 |
import com.gitblit.models.UserChoice;
|
AS |
47 |
import com.gitblit.models.UserModel;
|
fe24a0
|
48 |
import com.gitblit.utils.StringUtils;
|
JM |
49 |
import com.gitblit.wicket.RequiresAdminRole;
|
6fa6ab
|
50 |
import com.gitblit.wicket.StringChoiceRenderer;
|
fe24a0
|
51 |
import com.gitblit.wicket.WicketUtils;
|
d7905a
|
52 |
import com.gitblit.wicket.panels.BulletListPanel;
|
822dfe
|
53 |
import com.gitblit.wicket.panels.RegistrantPermissionsPanel;
|
fe24a0
|
54 |
|
JM |
55 |
@RequiresAdminRole
|
|
56 |
public class EditTeamPage extends RootSubPage {
|
|
57 |
|
|
58 |
private final boolean isCreate;
|
0b9119
|
59 |
|
JM |
60 |
private IModel<String> mailingLists;
|
fe24a0
|
61 |
|
JM |
62 |
public EditTeamPage() {
|
|
63 |
// create constructor
|
|
64 |
super();
|
|
65 |
isCreate = true;
|
|
66 |
setupPage(new TeamModel(""));
|
b0e164
|
67 |
setStatelessHint(false);
|
092f0a
|
68 |
setOutputMarkupId(true);
|
fe24a0
|
69 |
}
|
JM |
70 |
|
|
71 |
public EditTeamPage(PageParameters params) {
|
|
72 |
// edit constructor
|
|
73 |
super(params);
|
|
74 |
isCreate = false;
|
|
75 |
String name = WicketUtils.getTeamname(params);
|
99d0d4
|
76 |
TeamModel model = app().users().getTeamModel(name);
|
fe24a0
|
77 |
setupPage(model);
|
b0e164
|
78 |
setStatelessHint(false);
|
092f0a
|
79 |
setOutputMarkupId(true);
|
fe24a0
|
80 |
}
|
JM |
81 |
|
dd630f
|
82 |
@Override
|
JM |
83 |
protected boolean requiresPageMap() {
|
|
84 |
return true;
|
|
85 |
}
|
699e71
|
86 |
|
6ef8d7
|
87 |
@Override
|
JM |
88 |
protected Class<? extends BasePage> getRootNavPageClass() {
|
a8d988
|
89 |
return TeamsPage.class;
|
6ef8d7
|
90 |
}
|
dd630f
|
91 |
|
fe24a0
|
92 |
protected void setupPage(final TeamModel teamModel) {
|
JM |
93 |
if (isCreate) {
|
|
94 |
super.setupPage(getString("gb.newTeam"), "");
|
|
95 |
} else {
|
|
96 |
super.setupPage(getString("gb.edit"), teamModel.name);
|
|
97 |
}
|
d7905a
|
98 |
|
fe24a0
|
99 |
CompoundPropertyModel<TeamModel> model = new CompoundPropertyModel<TeamModel>(teamModel);
|
JM |
100 |
|
092f0a
|
101 |
List<String> repos = getAccessRestrictedRepositoryList(true, null);
|
d7905a
|
102 |
|
fe24a0
|
103 |
List<String> teamUsers = new ArrayList<String>(teamModel.users);
|
d7905a
|
104 |
List<String> preReceiveScripts = new ArrayList<String>();
|
JM |
105 |
List<String> postReceiveScripts = new ArrayList<String>();
|
|
106 |
|
fe24a0
|
107 |
final String oldName = teamModel.name;
|
822dfe
|
108 |
final List<RegistrantAccessPermission> permissions = teamModel.getRepositoryPermissions();
|
d7905a
|
109 |
|
JM |
110 |
// users palette
|
37d5b7
|
111 |
final Palette<UserChoice> users = new Palette<UserChoice>("users", new ListModel<UserChoice>(
|
AS |
112 |
getTeamUsers(teamUsers)), new CollectionModel<UserChoice>(sortByDisplayName(getTeamUsers(app().users().getAllUsernames()))), new ChoiceRenderer<UserChoice>(null, "userId"), 10, false);
|
d7905a
|
113 |
|
JM |
114 |
// pre-receive palette
|
|
115 |
if (teamModel.preReceiveScripts != null) {
|
|
116 |
preReceiveScripts.addAll(teamModel.preReceiveScripts);
|
|
117 |
}
|
|
118 |
final Palette<String> preReceivePalette = new Palette<String>("preReceiveScripts",
|
99d0d4
|
119 |
new ListModel<String>(preReceiveScripts), new CollectionModel<String>(app().repositories()
|
JM |
120 |
.getPreReceiveScriptsUnused(null)), new StringChoiceRenderer(),
|
6fa6ab
|
121 |
12, true);
|
d7905a
|
122 |
|
JM |
123 |
// post-receive palette
|
|
124 |
if (teamModel.postReceiveScripts != null) {
|
|
125 |
postReceiveScripts.addAll(teamModel.postReceiveScripts);
|
|
126 |
}
|
|
127 |
final Palette<String> postReceivePalette = new Palette<String>("postReceiveScripts",
|
99d0d4
|
128 |
new ListModel<String>(postReceiveScripts), new CollectionModel<String>(app().repositories()
|
JM |
129 |
.getPostReceiveScriptsUnused(null)), new StringChoiceRenderer(),
|
6fa6ab
|
130 |
12, true);
|
d7905a
|
131 |
|
fe24a0
|
132 |
Form<TeamModel> form = new Form<TeamModel>("editForm", model) {
|
JM |
133 |
|
|
134 |
private static final long serialVersionUID = 1L;
|
|
135 |
|
|
136 |
/*
|
|
137 |
* (non-Javadoc)
|
699e71
|
138 |
*
|
fe24a0
|
139 |
* @see org.apache.wicket.markup.html.form.Form#onSubmit()
|
JM |
140 |
*/
|
|
141 |
@Override
|
|
142 |
protected void onSubmit() {
|
|
143 |
String teamname = teamModel.name;
|
|
144 |
if (StringUtils.isEmpty(teamname)) {
|
6caa93
|
145 |
error(getString("gb.pleaseSetTeamName"));
|
fe24a0
|
146 |
return;
|
JM |
147 |
}
|
|
148 |
if (isCreate) {
|
99d0d4
|
149 |
TeamModel model = app().users().getTeamModel(teamname);
|
fe24a0
|
150 |
if (model != null) {
|
6caa93
|
151 |
error(MessageFormat.format(getString("gb.teamNameUnavailable"), teamname));
|
fe24a0
|
152 |
return;
|
JM |
153 |
}
|
|
154 |
}
|
b0e164
|
155 |
// update team permissions
|
822dfe
|
156 |
for (RegistrantAccessPermission repositoryPermission : permissions) {
|
JM |
157 |
teamModel.setRepositoryPermission(repositoryPermission.registrant, repositoryPermission.permission);
|
fe24a0
|
158 |
}
|
JM |
159 |
|
37d5b7
|
160 |
Iterator<UserChoice> selectedUsers = users.getSelectedChoices();
|
fe24a0
|
161 |
List<String> members = new ArrayList<String>();
|
JM |
162 |
while (selectedUsers.hasNext()) {
|
37d5b7
|
163 |
members.add(selectedUsers.next().getUserId().toLowerCase());
|
fe24a0
|
164 |
}
|
JM |
165 |
teamModel.users.clear();
|
|
166 |
teamModel.users.addAll(members);
|
|
167 |
|
0b9119
|
168 |
// set mailing lists
|
JM |
169 |
String ml = mailingLists.getObject();
|
|
170 |
if (!StringUtils.isEmpty(ml)) {
|
|
171 |
Set<String> list = new HashSet<String>();
|
|
172 |
for (String address : ml.split("(,|\\s)")) {
|
|
173 |
if (StringUtils.isEmpty(address)) {
|
|
174 |
continue;
|
|
175 |
}
|
|
176 |
list.add(address.toLowerCase());
|
|
177 |
}
|
|
178 |
teamModel.mailingLists.clear();
|
|
179 |
teamModel.mailingLists.addAll(list);
|
|
180 |
}
|
d7905a
|
181 |
|
JM |
182 |
// pre-receive scripts
|
|
183 |
List<String> preReceiveScripts = new ArrayList<String>();
|
|
184 |
Iterator<String> pres = preReceivePalette.getSelectedChoices();
|
|
185 |
while (pres.hasNext()) {
|
|
186 |
preReceiveScripts.add(pres.next());
|
|
187 |
}
|
|
188 |
teamModel.preReceiveScripts.clear();
|
|
189 |
teamModel.preReceiveScripts.addAll(preReceiveScripts);
|
|
190 |
|
|
191 |
// post-receive scripts
|
|
192 |
List<String> postReceiveScripts = new ArrayList<String>();
|
|
193 |
Iterator<String> post = postReceivePalette.getSelectedChoices();
|
|
194 |
while (post.hasNext()) {
|
|
195 |
postReceiveScripts.add(post.next());
|
|
196 |
}
|
|
197 |
teamModel.postReceiveScripts.clear();
|
|
198 |
teamModel.postReceiveScripts.addAll(postReceiveScripts);
|
|
199 |
|
fe24a0
|
200 |
try {
|
5ae529
|
201 |
if (isCreate) {
|
JM |
202 |
app().gitblit().addTeam(teamModel);
|
|
203 |
} else {
|
|
204 |
app().gitblit().reviseTeam(oldName, teamModel);
|
|
205 |
}
|
fe24a0
|
206 |
} catch (GitBlitException e) {
|
JM |
207 |
error(e.getMessage());
|
|
208 |
return;
|
|
209 |
}
|
|
210 |
setRedirect(false);
|
|
211 |
if (isCreate) {
|
|
212 |
// create another team
|
6caa93
|
213 |
info(MessageFormat.format(getString("gb.teamCreated"),
|
fe24a0
|
214 |
teamModel.name));
|
JM |
215 |
}
|
fe7c01
|
216 |
// back to users page
|
a8d988
|
217 |
setResponsePage(TeamsPage.class);
|
fe24a0
|
218 |
}
|
JM |
219 |
};
|
|
220 |
|
d3ca1c
|
221 |
// do not let the browser pre-populate these fields
|
JM |
222 |
form.add(new SimpleAttributeModifier("autocomplete", "off"));
|
|
223 |
|
6cca86
|
224 |
// not all user services support manipulating team memberships
|
04a985
|
225 |
boolean editMemberships = app().authentication().supportsTeamMembershipChanges(teamModel);
|
699e71
|
226 |
|
fe24a0
|
227 |
// field names reflective match TeamModel fields
|
JM |
228 |
form.add(new TextField<String>("name"));
|
7f7051
|
229 |
form.add(new CheckBox("canAdmin"));
|
99d0d4
|
230 |
form.add(new CheckBox("canFork").setEnabled(app().settings().getBoolean(Keys.web.allowForking, true)));
|
7f7051
|
231 |
form.add(new CheckBox("canCreate"));
|
6cca86
|
232 |
form.add(users.setEnabled(editMemberships));
|
0b9119
|
233 |
mailingLists = new Model<String>(teamModel.mailingLists == null ? ""
|
JM |
234 |
: StringUtils.flattenStrings(teamModel.mailingLists, " "));
|
|
235 |
form.add(new TextField<String>("mailingLists", mailingLists));
|
d7905a
|
236 |
|
092f0a
|
237 |
form.add(new RegistrantPermissionsPanel("repositories", RegistrantType.REPOSITORY,
|
JM |
238 |
repos, permissions, getAccessPermissions()));
|
d7905a
|
239 |
form.add(preReceivePalette);
|
99d0d4
|
240 |
form.add(new BulletListPanel("inheritedPreReceive", "inherited", app().repositories()
|
d7905a
|
241 |
.getPreReceiveScriptsInherited(null)));
|
JM |
242 |
form.add(postReceivePalette);
|
99d0d4
|
243 |
form.add(new BulletListPanel("inheritedPostReceive", "inherited", app().repositories()
|
d7905a
|
244 |
.getPostReceiveScriptsInherited(null)));
|
fe24a0
|
245 |
|
719798
|
246 |
form.add(new Button("save"));
|
JM |
247 |
Button cancel = new Button("cancel") {
|
fe24a0
|
248 |
private static final long serialVersionUID = 1L;
|
JM |
249 |
|
|
250 |
@Override
|
|
251 |
public void onSubmit() {
|
a8d988
|
252 |
setResponsePage(TeamsPage.class);
|
fe24a0
|
253 |
}
|
JM |
254 |
};
|
|
255 |
cancel.setDefaultFormProcessing(false);
|
|
256 |
form.add(cancel);
|
|
257 |
|
|
258 |
add(form);
|
|
259 |
}
|
37d5b7
|
260 |
|
AS |
261 |
private List<UserChoice> getTeamUsers(List<String> teamUserIds) {
|
|
262 |
List<UserChoice> teamUsers = new ArrayList<UserChoice>();
|
|
263 |
for (String teamUserId : teamUserIds) {
|
|
264 |
UserModel userModel = app().users().getUserModel(teamUserId);
|
|
265 |
if (userModel!=null) {
|
|
266 |
teamUsers.add(new UserChoice(userModel.displayName, userModel.username, userModel.emailAddress));
|
|
267 |
}
|
|
268 |
}
|
|
269 |
return sortByDisplayName(teamUsers);
|
|
270 |
}
|
|
271 |
|
|
272 |
private List<UserChoice> sortByDisplayName(List<UserChoice> teamUsers) {
|
|
273 |
Collections.sort(teamUsers, new Comparator<UserChoice>() {
|
|
274 |
|
|
275 |
@Override
|
|
276 |
public int compare(UserChoice o1, UserChoice o2) {
|
|
277 |
return o1.getDisplayNameOrUserId().compareTo(o2.getDisplayNameOrUserId());
|
|
278 |
}
|
|
279 |
});
|
|
280 |
return teamUsers;
|
|
281 |
}
|
fe24a0
|
282 |
}
|