Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

EditTeamPage.java 10KB

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