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;
|
6e3481
|
42 |
import com.gitblit.Constants.Role;
|
fe24a0
|
43 |
import com.gitblit.GitBlitException;
|
428b22
|
44 |
import com.gitblit.Keys;
|
822dfe
|
45 |
import com.gitblit.models.RegistrantAccessPermission;
|
fe24a0
|
46 |
import com.gitblit.models.TeamModel;
|
37d5b7
|
47 |
import com.gitblit.models.UserChoice;
|
AS |
48 |
import com.gitblit.models.UserModel;
|
fe24a0
|
49 |
import com.gitblit.utils.StringUtils;
|
JM |
50 |
import com.gitblit.wicket.RequiresAdminRole;
|
6fa6ab
|
51 |
import com.gitblit.wicket.StringChoiceRenderer;
|
fe24a0
|
52 |
import com.gitblit.wicket.WicketUtils;
|
d7905a
|
53 |
import com.gitblit.wicket.panels.BulletListPanel;
|
822dfe
|
54 |
import com.gitblit.wicket.panels.RegistrantPermissionsPanel;
|
fe24a0
|
55 |
|
JM |
56 |
@RequiresAdminRole
|
|
57 |
public class EditTeamPage extends RootSubPage {
|
|
58 |
|
|
59 |
private final boolean isCreate;
|
0b9119
|
60 |
|
JM |
61 |
private IModel<String> mailingLists;
|
fe24a0
|
62 |
|
JM |
63 |
public EditTeamPage() {
|
|
64 |
// create constructor
|
|
65 |
super();
|
|
66 |
isCreate = true;
|
|
67 |
setupPage(new TeamModel(""));
|
b0e164
|
68 |
setStatelessHint(false);
|
092f0a
|
69 |
setOutputMarkupId(true);
|
fe24a0
|
70 |
}
|
JM |
71 |
|
|
72 |
public EditTeamPage(PageParameters params) {
|
|
73 |
// edit constructor
|
|
74 |
super(params);
|
|
75 |
isCreate = false;
|
|
76 |
String name = WicketUtils.getTeamname(params);
|
99d0d4
|
77 |
TeamModel model = app().users().getTeamModel(name);
|
fe24a0
|
78 |
setupPage(model);
|
b0e164
|
79 |
setStatelessHint(false);
|
092f0a
|
80 |
setOutputMarkupId(true);
|
fe24a0
|
81 |
}
|
JM |
82 |
|
dd630f
|
83 |
@Override
|
JM |
84 |
protected boolean requiresPageMap() {
|
|
85 |
return true;
|
|
86 |
}
|
699e71
|
87 |
|
6ef8d7
|
88 |
@Override
|
JM |
89 |
protected Class<? extends BasePage> getRootNavPageClass() {
|
a8d988
|
90 |
return TeamsPage.class;
|
6ef8d7
|
91 |
}
|
dd630f
|
92 |
|
fe24a0
|
93 |
protected void setupPage(final TeamModel teamModel) {
|
JM |
94 |
if (isCreate) {
|
|
95 |
super.setupPage(getString("gb.newTeam"), "");
|
|
96 |
} else {
|
|
97 |
super.setupPage(getString("gb.edit"), teamModel.name);
|
|
98 |
}
|
d7905a
|
99 |
|
fe24a0
|
100 |
CompoundPropertyModel<TeamModel> model = new CompoundPropertyModel<TeamModel>(teamModel);
|
JM |
101 |
|
092f0a
|
102 |
List<String> repos = getAccessRestrictedRepositoryList(true, null);
|
d7905a
|
103 |
|
fe24a0
|
104 |
List<String> teamUsers = new ArrayList<String>(teamModel.users);
|
d7905a
|
105 |
List<String> preReceiveScripts = new ArrayList<String>();
|
JM |
106 |
List<String> postReceiveScripts = new ArrayList<String>();
|
|
107 |
|
fe24a0
|
108 |
final String oldName = teamModel.name;
|
822dfe
|
109 |
final List<RegistrantAccessPermission> permissions = teamModel.getRepositoryPermissions();
|
d7905a
|
110 |
|
JM |
111 |
// users palette
|
37d5b7
|
112 |
final Palette<UserChoice> users = new Palette<UserChoice>("users", new ListModel<UserChoice>(
|
AS |
113 |
getTeamUsers(teamUsers)), new CollectionModel<UserChoice>(sortByDisplayName(getTeamUsers(app().users().getAllUsernames()))), new ChoiceRenderer<UserChoice>(null, "userId"), 10, false);
|
d7905a
|
114 |
|
JM |
115 |
// pre-receive palette
|
|
116 |
if (teamModel.preReceiveScripts != null) {
|
|
117 |
preReceiveScripts.addAll(teamModel.preReceiveScripts);
|
|
118 |
}
|
|
119 |
final Palette<String> preReceivePalette = new Palette<String>("preReceiveScripts",
|
99d0d4
|
120 |
new ListModel<String>(preReceiveScripts), new CollectionModel<String>(app().repositories()
|
JM |
121 |
.getPreReceiveScriptsUnused(null)), new StringChoiceRenderer(),
|
6fa6ab
|
122 |
12, true);
|
d7905a
|
123 |
|
JM |
124 |
// post-receive palette
|
|
125 |
if (teamModel.postReceiveScripts != null) {
|
|
126 |
postReceiveScripts.addAll(teamModel.postReceiveScripts);
|
|
127 |
}
|
|
128 |
final Palette<String> postReceivePalette = new Palette<String>("postReceiveScripts",
|
99d0d4
|
129 |
new ListModel<String>(postReceiveScripts), new CollectionModel<String>(app().repositories()
|
JM |
130 |
.getPostReceiveScriptsUnused(null)), new StringChoiceRenderer(),
|
6fa6ab
|
131 |
12, true);
|
d7905a
|
132 |
|
fe24a0
|
133 |
Form<TeamModel> form = new Form<TeamModel>("editForm", model) {
|
JM |
134 |
|
|
135 |
private static final long serialVersionUID = 1L;
|
|
136 |
|
|
137 |
/*
|
|
138 |
* (non-Javadoc)
|
699e71
|
139 |
*
|
fe24a0
|
140 |
* @see org.apache.wicket.markup.html.form.Form#onSubmit()
|
JM |
141 |
*/
|
|
142 |
@Override
|
|
143 |
protected void onSubmit() {
|
|
144 |
String teamname = teamModel.name;
|
|
145 |
if (StringUtils.isEmpty(teamname)) {
|
6caa93
|
146 |
error(getString("gb.pleaseSetTeamName"));
|
fe24a0
|
147 |
return;
|
JM |
148 |
}
|
|
149 |
if (isCreate) {
|
99d0d4
|
150 |
TeamModel model = app().users().getTeamModel(teamname);
|
fe24a0
|
151 |
if (model != null) {
|
6caa93
|
152 |
error(MessageFormat.format(getString("gb.teamNameUnavailable"), teamname));
|
fe24a0
|
153 |
return;
|
JM |
154 |
}
|
|
155 |
}
|
b0e164
|
156 |
// update team permissions
|
822dfe
|
157 |
for (RegistrantAccessPermission repositoryPermission : permissions) {
|
JM |
158 |
teamModel.setRepositoryPermission(repositoryPermission.registrant, repositoryPermission.permission);
|
fe24a0
|
159 |
}
|
JM |
160 |
|
37d5b7
|
161 |
Iterator<UserChoice> selectedUsers = users.getSelectedChoices();
|
fe24a0
|
162 |
List<String> members = new ArrayList<String>();
|
JM |
163 |
while (selectedUsers.hasNext()) {
|
37d5b7
|
164 |
members.add(selectedUsers.next().getUserId().toLowerCase());
|
fe24a0
|
165 |
}
|
JM |
166 |
teamModel.users.clear();
|
|
167 |
teamModel.users.addAll(members);
|
|
168 |
|
0b9119
|
169 |
// set mailing lists
|
JM |
170 |
String ml = mailingLists.getObject();
|
|
171 |
if (!StringUtils.isEmpty(ml)) {
|
|
172 |
Set<String> list = new HashSet<String>();
|
|
173 |
for (String address : ml.split("(,|\\s)")) {
|
|
174 |
if (StringUtils.isEmpty(address)) {
|
|
175 |
continue;
|
|
176 |
}
|
|
177 |
list.add(address.toLowerCase());
|
|
178 |
}
|
|
179 |
teamModel.mailingLists.clear();
|
|
180 |
teamModel.mailingLists.addAll(list);
|
|
181 |
}
|
d7905a
|
182 |
|
JM |
183 |
// pre-receive scripts
|
|
184 |
List<String> preReceiveScripts = new ArrayList<String>();
|
|
185 |
Iterator<String> pres = preReceivePalette.getSelectedChoices();
|
|
186 |
while (pres.hasNext()) {
|
|
187 |
preReceiveScripts.add(pres.next());
|
|
188 |
}
|
|
189 |
teamModel.preReceiveScripts.clear();
|
|
190 |
teamModel.preReceiveScripts.addAll(preReceiveScripts);
|
|
191 |
|
|
192 |
// post-receive scripts
|
|
193 |
List<String> postReceiveScripts = new ArrayList<String>();
|
|
194 |
Iterator<String> post = postReceivePalette.getSelectedChoices();
|
|
195 |
while (post.hasNext()) {
|
|
196 |
postReceiveScripts.add(post.next());
|
|
197 |
}
|
|
198 |
teamModel.postReceiveScripts.clear();
|
|
199 |
teamModel.postReceiveScripts.addAll(postReceiveScripts);
|
|
200 |
|
fe24a0
|
201 |
try {
|
5ae529
|
202 |
if (isCreate) {
|
JM |
203 |
app().gitblit().addTeam(teamModel);
|
|
204 |
} else {
|
|
205 |
app().gitblit().reviseTeam(oldName, teamModel);
|
|
206 |
}
|
fe24a0
|
207 |
} catch (GitBlitException e) {
|
JM |
208 |
error(e.getMessage());
|
|
209 |
return;
|
|
210 |
}
|
|
211 |
setRedirect(false);
|
|
212 |
if (isCreate) {
|
|
213 |
// create another team
|
6caa93
|
214 |
info(MessageFormat.format(getString("gb.teamCreated"),
|
fe24a0
|
215 |
teamModel.name));
|
JM |
216 |
}
|
fe7c01
|
217 |
// back to users page
|
a8d988
|
218 |
setResponsePage(TeamsPage.class);
|
fe24a0
|
219 |
}
|
JM |
220 |
};
|
|
221 |
|
d3ca1c
|
222 |
// do not let the browser pre-populate these fields
|
JM |
223 |
form.add(new SimpleAttributeModifier("autocomplete", "off"));
|
|
224 |
|
6e3481
|
225 |
// not all user providers support manipulating team memberships
|
04a985
|
226 |
boolean editMemberships = app().authentication().supportsTeamMembershipChanges(teamModel);
|
6e3481
|
227 |
|
JM |
228 |
// not all user providers support manipulating the admin role
|
|
229 |
boolean changeAdminRole = app().authentication().supportsRoleChanges(teamModel, Role.ADMIN);
|
|
230 |
|
|
231 |
// not all user providers support manipulating the create role
|
|
232 |
boolean changeCreateRole = app().authentication().supportsRoleChanges(teamModel, Role.CREATE);
|
|
233 |
|
|
234 |
// not all user providers support manipulating the fork role
|
|
235 |
boolean changeForkRole = app().authentication().supportsRoleChanges(teamModel, Role.FORK);
|
699e71
|
236 |
|
fe24a0
|
237 |
// field names reflective match TeamModel fields
|
JM |
238 |
form.add(new TextField<String>("name"));
|
6e3481
|
239 |
form.add(new CheckBox("canAdmin").setEnabled(changeAdminRole));
|
JM |
240 |
form.add(new CheckBox("canFork").setEnabled(app().settings().getBoolean(Keys.web.allowForking, true) && changeForkRole));
|
|
241 |
form.add(new CheckBox("canCreate").setEnabled(changeCreateRole));
|
6cca86
|
242 |
form.add(users.setEnabled(editMemberships));
|
0b9119
|
243 |
mailingLists = new Model<String>(teamModel.mailingLists == null ? ""
|
JM |
244 |
: StringUtils.flattenStrings(teamModel.mailingLists, " "));
|
|
245 |
form.add(new TextField<String>("mailingLists", mailingLists));
|
d7905a
|
246 |
|
092f0a
|
247 |
form.add(new RegistrantPermissionsPanel("repositories", RegistrantType.REPOSITORY,
|
JM |
248 |
repos, permissions, getAccessPermissions()));
|
d7905a
|
249 |
form.add(preReceivePalette);
|
99d0d4
|
250 |
form.add(new BulletListPanel("inheritedPreReceive", "inherited", app().repositories()
|
d7905a
|
251 |
.getPreReceiveScriptsInherited(null)));
|
JM |
252 |
form.add(postReceivePalette);
|
99d0d4
|
253 |
form.add(new BulletListPanel("inheritedPostReceive", "inherited", app().repositories()
|
d7905a
|
254 |
.getPostReceiveScriptsInherited(null)));
|
fe24a0
|
255 |
|
719798
|
256 |
form.add(new Button("save"));
|
JM |
257 |
Button cancel = new Button("cancel") {
|
fe24a0
|
258 |
private static final long serialVersionUID = 1L;
|
JM |
259 |
|
|
260 |
@Override
|
|
261 |
public void onSubmit() {
|
a8d988
|
262 |
setResponsePage(TeamsPage.class);
|
fe24a0
|
263 |
}
|
JM |
264 |
};
|
|
265 |
cancel.setDefaultFormProcessing(false);
|
|
266 |
form.add(cancel);
|
|
267 |
|
|
268 |
add(form);
|
|
269 |
}
|
37d5b7
|
270 |
|
AS |
271 |
private List<UserChoice> getTeamUsers(List<String> teamUserIds) {
|
|
272 |
List<UserChoice> teamUsers = new ArrayList<UserChoice>();
|
|
273 |
for (String teamUserId : teamUserIds) {
|
|
274 |
UserModel userModel = app().users().getUserModel(teamUserId);
|
|
275 |
if (userModel!=null) {
|
|
276 |
teamUsers.add(new UserChoice(userModel.displayName, userModel.username, userModel.emailAddress));
|
|
277 |
}
|
|
278 |
}
|
|
279 |
return sortByDisplayName(teamUsers);
|
|
280 |
}
|
|
281 |
|
|
282 |
private List<UserChoice> sortByDisplayName(List<UserChoice> teamUsers) {
|
|
283 |
Collections.sort(teamUsers, new Comparator<UserChoice>() {
|
|
284 |
|
|
285 |
@Override
|
|
286 |
public int compare(UserChoice o1, UserChoice o2) {
|
|
287 |
return o1.getDisplayNameOrUserId().compareTo(o2.getDisplayNameOrUserId());
|
|
288 |
}
|
|
289 |
});
|
|
290 |
return teamUsers;
|
|
291 |
}
|
fe24a0
|
292 |
}
|