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;
|
0b9119
|
21 |
import java.util.HashSet;
|
fe24a0
|
22 |
import java.util.Iterator;
|
JM |
23 |
import java.util.List;
|
0b9119
|
24 |
import java.util.Set;
|
fe24a0
|
25 |
|
JM |
26 |
import org.apache.wicket.PageParameters;
|
d3ca1c
|
27 |
import org.apache.wicket.behavior.SimpleAttributeModifier;
|
fe24a0
|
28 |
import org.apache.wicket.extensions.markup.html.form.palette.Palette;
|
JM |
29 |
import org.apache.wicket.markup.html.form.Button;
|
|
30 |
import org.apache.wicket.markup.html.form.Form;
|
|
31 |
import org.apache.wicket.markup.html.form.TextField;
|
|
32 |
import org.apache.wicket.model.CompoundPropertyModel;
|
0b9119
|
33 |
import org.apache.wicket.model.IModel;
|
JM |
34 |
import org.apache.wicket.model.Model;
|
fe24a0
|
35 |
import org.apache.wicket.model.util.CollectionModel;
|
JM |
36 |
import org.apache.wicket.model.util.ListModel;
|
|
37 |
|
|
38 |
import com.gitblit.Constants.AccessRestrictionType;
|
|
39 |
import com.gitblit.GitBlit;
|
|
40 |
import com.gitblit.GitBlitException;
|
|
41 |
import com.gitblit.models.RepositoryModel;
|
|
42 |
import com.gitblit.models.TeamModel;
|
|
43 |
import com.gitblit.utils.StringUtils;
|
|
44 |
import com.gitblit.wicket.RequiresAdminRole;
|
6fa6ab
|
45 |
import com.gitblit.wicket.StringChoiceRenderer;
|
fe24a0
|
46 |
import com.gitblit.wicket.WicketUtils;
|
d7905a
|
47 |
import com.gitblit.wicket.panels.BulletListPanel;
|
fe24a0
|
48 |
|
JM |
49 |
@RequiresAdminRole
|
|
50 |
public class EditTeamPage extends RootSubPage {
|
|
51 |
|
|
52 |
private final boolean isCreate;
|
0b9119
|
53 |
|
JM |
54 |
private IModel<String> mailingLists;
|
fe24a0
|
55 |
|
JM |
56 |
public EditTeamPage() {
|
|
57 |
// create constructor
|
|
58 |
super();
|
|
59 |
isCreate = true;
|
|
60 |
setupPage(new TeamModel(""));
|
|
61 |
}
|
|
62 |
|
|
63 |
public EditTeamPage(PageParameters params) {
|
|
64 |
// edit constructor
|
|
65 |
super(params);
|
|
66 |
isCreate = false;
|
|
67 |
String name = WicketUtils.getTeamname(params);
|
|
68 |
TeamModel model = GitBlit.self().getTeamModel(name);
|
|
69 |
setupPage(model);
|
|
70 |
}
|
|
71 |
|
|
72 |
protected void setupPage(final TeamModel teamModel) {
|
|
73 |
if (isCreate) {
|
|
74 |
super.setupPage(getString("gb.newTeam"), "");
|
|
75 |
} else {
|
|
76 |
super.setupPage(getString("gb.edit"), teamModel.name);
|
|
77 |
}
|
d7905a
|
78 |
|
fe24a0
|
79 |
CompoundPropertyModel<TeamModel> model = new CompoundPropertyModel<TeamModel>(teamModel);
|
JM |
80 |
|
|
81 |
List<String> repos = new ArrayList<String>();
|
|
82 |
for (String repo : GitBlit.self().getRepositoryList()) {
|
|
83 |
RepositoryModel repositoryModel = GitBlit.self().getRepositoryModel(repo);
|
|
84 |
if (repositoryModel.accessRestriction.exceeds(AccessRestrictionType.NONE)) {
|
|
85 |
repos.add(repo);
|
|
86 |
}
|
|
87 |
}
|
|
88 |
StringUtils.sortRepositorynames(repos);
|
d7905a
|
89 |
|
fe24a0
|
90 |
List<String> teamUsers = new ArrayList<String>(teamModel.users);
|
JM |
91 |
Collections.sort(teamUsers);
|
d7905a
|
92 |
List<String> preReceiveScripts = new ArrayList<String>();
|
JM |
93 |
List<String> postReceiveScripts = new ArrayList<String>();
|
|
94 |
|
fe24a0
|
95 |
final String oldName = teamModel.name;
|
d7905a
|
96 |
|
JM |
97 |
// repositories palette
|
fe24a0
|
98 |
final Palette<String> repositories = new Palette<String>("repositories",
|
JM |
99 |
new ListModel<String>(new ArrayList<String>(teamModel.repositories)),
|
6fa6ab
|
100 |
new CollectionModel<String>(repos), new StringChoiceRenderer(), 10, false);
|
d7905a
|
101 |
|
JM |
102 |
// users palette
|
fe24a0
|
103 |
final Palette<String> users = new Palette<String>("users", new ListModel<String>(
|
JM |
104 |
new ArrayList<String>(teamUsers)), new CollectionModel<String>(GitBlit.self()
|
6fa6ab
|
105 |
.getAllUsernames()), new StringChoiceRenderer(), 10, false);
|
d7905a
|
106 |
|
JM |
107 |
// pre-receive palette
|
|
108 |
if (teamModel.preReceiveScripts != null) {
|
|
109 |
preReceiveScripts.addAll(teamModel.preReceiveScripts);
|
|
110 |
}
|
|
111 |
final Palette<String> preReceivePalette = new Palette<String>("preReceiveScripts",
|
|
112 |
new ListModel<String>(preReceiveScripts), new CollectionModel<String>(GitBlit
|
6fa6ab
|
113 |
.self().getPreReceiveScriptsUnused(null)), new StringChoiceRenderer(),
|
JM |
114 |
12, true);
|
d7905a
|
115 |
|
JM |
116 |
// post-receive palette
|
|
117 |
if (teamModel.postReceiveScripts != null) {
|
|
118 |
postReceiveScripts.addAll(teamModel.postReceiveScripts);
|
|
119 |
}
|
|
120 |
final Palette<String> postReceivePalette = new Palette<String>("postReceiveScripts",
|
|
121 |
new ListModel<String>(postReceiveScripts), new CollectionModel<String>(GitBlit
|
6fa6ab
|
122 |
.self().getPostReceiveScriptsUnused(null)), new StringChoiceRenderer(),
|
JM |
123 |
12, true);
|
d7905a
|
124 |
|
fe24a0
|
125 |
Form<TeamModel> form = new Form<TeamModel>("editForm", model) {
|
JM |
126 |
|
|
127 |
private static final long serialVersionUID = 1L;
|
|
128 |
|
|
129 |
/*
|
|
130 |
* (non-Javadoc)
|
|
131 |
*
|
|
132 |
* @see org.apache.wicket.markup.html.form.Form#onSubmit()
|
|
133 |
*/
|
|
134 |
@Override
|
|
135 |
protected void onSubmit() {
|
|
136 |
String teamname = teamModel.name;
|
|
137 |
if (StringUtils.isEmpty(teamname)) {
|
6caa93
|
138 |
error(getString("gb.pleaseSetTeamName"));
|
fe24a0
|
139 |
return;
|
JM |
140 |
}
|
|
141 |
if (isCreate) {
|
|
142 |
TeamModel model = GitBlit.self().getTeamModel(teamname);
|
|
143 |
if (model != null) {
|
6caa93
|
144 |
error(MessageFormat.format(getString("gb.teamNameUnavailable"), teamname));
|
fe24a0
|
145 |
return;
|
JM |
146 |
}
|
|
147 |
}
|
|
148 |
Iterator<String> selectedRepositories = repositories.getSelectedChoices();
|
|
149 |
List<String> repos = new ArrayList<String>();
|
|
150 |
while (selectedRepositories.hasNext()) {
|
|
151 |
repos.add(selectedRepositories.next().toLowerCase());
|
|
152 |
}
|
2afc31
|
153 |
if (repos.size() == 0) {
|
6caa93
|
154 |
error(getString("gb.teamMustSpecifyRepository"));
|
2afc31
|
155 |
return;
|
JM |
156 |
}
|
fe24a0
|
157 |
teamModel.repositories.clear();
|
JM |
158 |
teamModel.repositories.addAll(repos);
|
|
159 |
|
|
160 |
Iterator<String> selectedUsers = users.getSelectedChoices();
|
|
161 |
List<String> members = new ArrayList<String>();
|
|
162 |
while (selectedUsers.hasNext()) {
|
|
163 |
members.add(selectedUsers.next().toLowerCase());
|
|
164 |
}
|
|
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 {
|
JM |
201 |
GitBlit.self().updateTeamModel(oldName, teamModel, isCreate);
|
|
202 |
} catch (GitBlitException e) {
|
|
203 |
error(e.getMessage());
|
|
204 |
return;
|
|
205 |
}
|
|
206 |
setRedirect(false);
|
|
207 |
if (isCreate) {
|
|
208 |
// create another team
|
6caa93
|
209 |
info(MessageFormat.format(getString("gb.teamCreated"),
|
fe24a0
|
210 |
teamModel.name));
|
JM |
211 |
}
|
fe7c01
|
212 |
// back to users page
|
JM |
213 |
setResponsePage(UsersPage.class);
|
fe24a0
|
214 |
}
|
JM |
215 |
};
|
|
216 |
|
d3ca1c
|
217 |
// do not let the browser pre-populate these fields
|
JM |
218 |
form.add(new SimpleAttributeModifier("autocomplete", "off"));
|
|
219 |
|
6cca86
|
220 |
// not all user services support manipulating team memberships
|
JM |
221 |
boolean editMemberships = GitBlit.self().supportsTeamMembershipChanges();
|
|
222 |
|
fe24a0
|
223 |
// field names reflective match TeamModel fields
|
JM |
224 |
form.add(new TextField<String>("name"));
|
6cca86
|
225 |
form.add(users.setEnabled(editMemberships));
|
0b9119
|
226 |
mailingLists = new Model<String>(teamModel.mailingLists == null ? ""
|
JM |
227 |
: StringUtils.flattenStrings(teamModel.mailingLists, " "));
|
|
228 |
form.add(new TextField<String>("mailingLists", mailingLists));
|
d7905a
|
229 |
|
0b9119
|
230 |
form.add(repositories);
|
d7905a
|
231 |
form.add(preReceivePalette);
|
JM |
232 |
form.add(new BulletListPanel("inheritedPreReceive", "inherited", GitBlit.self()
|
|
233 |
.getPreReceiveScriptsInherited(null)));
|
|
234 |
form.add(postReceivePalette);
|
|
235 |
form.add(new BulletListPanel("inheritedPostReceive", "inherited", GitBlit.self()
|
|
236 |
.getPostReceiveScriptsInherited(null)));
|
fe24a0
|
237 |
|
719798
|
238 |
form.add(new Button("save"));
|
JM |
239 |
Button cancel = new Button("cancel") {
|
fe24a0
|
240 |
private static final long serialVersionUID = 1L;
|
JM |
241 |
|
|
242 |
@Override
|
|
243 |
public void onSubmit() {
|
|
244 |
setResponsePage(UsersPage.class);
|
|
245 |
}
|
|
246 |
};
|
|
247 |
cancel.setDefaultFormProcessing(false);
|
|
248 |
form.add(cancel);
|
|
249 |
|
|
250 |
add(form);
|
|
251 |
}
|
|
252 |
}
|