You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditTeamDialog.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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.client;
  17. import java.awt.BorderLayout;
  18. import java.awt.Dimension;
  19. import java.awt.FlowLayout;
  20. import java.awt.Font;
  21. import java.awt.GridLayout;
  22. import java.awt.Insets;
  23. import java.awt.event.ActionEvent;
  24. import java.awt.event.ActionListener;
  25. import java.awt.event.KeyEvent;
  26. import java.text.MessageFormat;
  27. import java.util.ArrayList;
  28. import java.util.Collections;
  29. import java.util.HashSet;
  30. import java.util.List;
  31. import java.util.Set;
  32. import javax.swing.ImageIcon;
  33. import javax.swing.JButton;
  34. import javax.swing.JCheckBox;
  35. import javax.swing.JComponent;
  36. import javax.swing.JDialog;
  37. import javax.swing.JLabel;
  38. import javax.swing.JOptionPane;
  39. import javax.swing.JPanel;
  40. import javax.swing.JRootPane;
  41. import javax.swing.JTabbedPane;
  42. import javax.swing.JTextField;
  43. import javax.swing.KeyStroke;
  44. import com.gitblit.Constants.AccessRestrictionType;
  45. import com.gitblit.Constants.AuthorizationControl;
  46. import com.gitblit.Constants.RegistrantType;
  47. import com.gitblit.models.RegistrantAccessPermission;
  48. import com.gitblit.models.RepositoryModel;
  49. import com.gitblit.models.ServerSettings;
  50. import com.gitblit.models.TeamModel;
  51. import com.gitblit.utils.ModelUtils;
  52. import com.gitblit.utils.StringUtils;
  53. public class EditTeamDialog extends JDialog {
  54. private static final long serialVersionUID = 1L;
  55. private final String teamname;
  56. private final TeamModel team;
  57. private final ServerSettings settings;
  58. private boolean isCreate;
  59. private boolean canceled = true;
  60. private JTextField teamnameField;
  61. private JCheckBox canAdminCheckbox;
  62. private JCheckBox canForkCheckbox;
  63. private JCheckBox canCreateCheckbox;
  64. private JTextField mailingListsField;
  65. private RegistrantPermissionsPanel repositoryPalette;
  66. private JPalette<String> userPalette;
  67. private JPalette<String> preReceivePalette;
  68. private JLabel preReceiveInherited;
  69. private JPalette<String> postReceivePalette;
  70. private JLabel postReceiveInherited;
  71. private Set<String> teamnames;
  72. public EditTeamDialog(int protocolVersion, ServerSettings settings) {
  73. this(protocolVersion, new TeamModel(""), settings);
  74. this.isCreate = true;
  75. setTitle(Translation.get("gb.newTeam"));
  76. }
  77. public EditTeamDialog(int protocolVersion, TeamModel aTeam, ServerSettings settings) {
  78. super();
  79. this.teamname = aTeam.name;
  80. this.team = new TeamModel("");
  81. this.settings = settings;
  82. this.teamnames = new HashSet<String>();
  83. this.isCreate = false;
  84. initialize(protocolVersion, aTeam);
  85. setModal(true);
  86. setTitle(Translation.get("gb.edit") + ": " + aTeam.name);
  87. setIconImage(new ImageIcon(getClass().getResource("/gitblt-favicon.png")).getImage());
  88. }
  89. @Override
  90. protected JRootPane createRootPane() {
  91. KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
  92. JRootPane rootPane = new JRootPane();
  93. rootPane.registerKeyboardAction(new ActionListener() {
  94. public void actionPerformed(ActionEvent actionEvent) {
  95. setVisible(false);
  96. }
  97. }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
  98. return rootPane;
  99. }
  100. private void initialize(int protocolVersion, TeamModel aTeam) {
  101. teamnameField = new JTextField(aTeam.name == null ? "" : aTeam.name, 25);
  102. canAdminCheckbox = new JCheckBox(Translation.get("gb.canAdminDescription"), aTeam.canAdmin);
  103. canForkCheckbox = new JCheckBox(Translation.get("gb.canForkDescription"), aTeam.canFork);
  104. canCreateCheckbox = new JCheckBox(Translation.get("gb.canCreateDescription"), aTeam.canCreate);
  105. mailingListsField = new JTextField(aTeam.mailingLists == null ? ""
  106. : StringUtils.flattenStrings(aTeam.mailingLists, " "), 50);
  107. JPanel fieldsPanel = new JPanel(new GridLayout(0, 1));
  108. fieldsPanel.add(newFieldPanel(Translation.get("gb.teamName"), teamnameField));
  109. fieldsPanel.add(newFieldPanel(Translation.get("gb.canAdmin"), canAdminCheckbox));
  110. fieldsPanel.add(newFieldPanel(Translation.get("gb.canFork"), canForkCheckbox));
  111. fieldsPanel.add(newFieldPanel(Translation.get("gb.canCreate"), canCreateCheckbox));
  112. fieldsPanel.add(newFieldPanel(Translation.get("gb.mailingLists"), mailingListsField));
  113. final Insets _insets = new Insets(5, 5, 5, 5);
  114. repositoryPalette = new RegistrantPermissionsPanel(RegistrantType.REPOSITORY);
  115. userPalette = new JPalette<String>();
  116. userPalette.setEnabled(settings.supportsTeamMembershipChanges);
  117. JPanel fieldsPanelTop = new JPanel(new BorderLayout());
  118. fieldsPanelTop.add(fieldsPanel, BorderLayout.NORTH);
  119. JPanel repositoriesPanel = new JPanel(new BorderLayout()) {
  120. private static final long serialVersionUID = 1L;
  121. public Insets getInsets() {
  122. return _insets;
  123. }
  124. };
  125. repositoriesPanel.add(repositoryPalette, BorderLayout.CENTER);
  126. JPanel usersPanel = new JPanel(new BorderLayout()) {
  127. private static final long serialVersionUID = 1L;
  128. public Insets getInsets() {
  129. return _insets;
  130. }
  131. };
  132. usersPanel.add(userPalette, BorderLayout.CENTER);
  133. preReceivePalette = new JPalette<String>(true);
  134. preReceiveInherited = new JLabel();
  135. JPanel preReceivePanel = new JPanel(new BorderLayout(5, 5));
  136. preReceivePanel.add(preReceivePalette, BorderLayout.CENTER);
  137. preReceivePanel.add(preReceiveInherited, BorderLayout.WEST);
  138. postReceivePalette = new JPalette<String>(true);
  139. postReceiveInherited = new JLabel();
  140. JPanel postReceivePanel = new JPanel(new BorderLayout(5, 5));
  141. postReceivePanel.add(postReceivePalette, BorderLayout.CENTER);
  142. postReceivePanel.add(postReceiveInherited, BorderLayout.WEST);
  143. JTabbedPane panel = new JTabbedPane(JTabbedPane.TOP);
  144. panel.addTab(Translation.get("gb.general"), fieldsPanelTop);
  145. panel.addTab(Translation.get("gb.teamMembers"), usersPanel);
  146. panel.addTab(Translation.get("gb.restrictedRepositories"), repositoriesPanel);
  147. panel.addTab(Translation.get("gb.preReceiveScripts"), preReceivePanel);
  148. panel.addTab(Translation.get("gb.postReceiveScripts"), postReceivePanel);
  149. JButton createButton = new JButton(Translation.get("gb.save"));
  150. createButton.addActionListener(new ActionListener() {
  151. public void actionPerformed(ActionEvent event) {
  152. if (validateFields()) {
  153. canceled = false;
  154. setVisible(false);
  155. }
  156. }
  157. });
  158. JButton cancelButton = new JButton(Translation.get("gb.cancel"));
  159. cancelButton.addActionListener(new ActionListener() {
  160. public void actionPerformed(ActionEvent event) {
  161. canceled = true;
  162. setVisible(false);
  163. }
  164. });
  165. JPanel controls = new JPanel();
  166. controls.add(cancelButton);
  167. controls.add(createButton);
  168. JPanel centerPanel = new JPanel(new BorderLayout(5, 5)) {
  169. private static final long serialVersionUID = 1L;
  170. @Override
  171. public Insets getInsets() {
  172. return _insets;
  173. }
  174. };
  175. centerPanel.add(panel, BorderLayout.CENTER);
  176. centerPanel.add(controls, BorderLayout.SOUTH);
  177. getContentPane().setLayout(new BorderLayout(5, 5));
  178. getContentPane().add(centerPanel, BorderLayout.CENTER);
  179. pack();
  180. }
  181. private JPanel newFieldPanel(String label, JComponent comp) {
  182. JLabel fieldLabel = new JLabel(label);
  183. fieldLabel.setFont(fieldLabel.getFont().deriveFont(Font.BOLD));
  184. fieldLabel.setPreferredSize(new Dimension(150, 20));
  185. JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
  186. panel.add(fieldLabel);
  187. panel.add(comp);
  188. return panel;
  189. }
  190. private boolean validateFields() {
  191. String tname = teamnameField.getText();
  192. if (StringUtils.isEmpty(tname)) {
  193. error("Please enter a team name!");
  194. return false;
  195. }
  196. boolean rename = false;
  197. // verify teamname uniqueness on create
  198. if (isCreate) {
  199. if (teamnames.contains(tname.toLowerCase())) {
  200. error(MessageFormat.format("Team name ''{0}'' is unavailable.", tname));
  201. return false;
  202. }
  203. } else {
  204. // check rename collision
  205. rename = !StringUtils.isEmpty(teamname) && !teamname.equalsIgnoreCase(tname);
  206. if (rename) {
  207. if (teamnames.contains(tname.toLowerCase())) {
  208. error(MessageFormat.format(
  209. "Failed to rename ''{0}'' because ''{1}'' already exists.", teamname,
  210. tname));
  211. return false;
  212. }
  213. }
  214. }
  215. team.name = tname;
  216. team.canAdmin = canAdminCheckbox.isSelected();
  217. team.canFork = canForkCheckbox.isSelected();
  218. team.canCreate = canCreateCheckbox.isSelected();
  219. String ml = mailingListsField.getText();
  220. if (!StringUtils.isEmpty(ml)) {
  221. Set<String> list = new HashSet<String>();
  222. for (String address : ml.split("(,|\\s)")) {
  223. if (StringUtils.isEmpty(address)) {
  224. continue;
  225. }
  226. list.add(address.toLowerCase());
  227. }
  228. team.mailingLists.clear();
  229. team.mailingLists.addAll(list);
  230. }
  231. for (RegistrantAccessPermission rp : repositoryPalette.getPermissions()) {
  232. team.setRepositoryPermission(rp.registrant, rp.permission);
  233. }
  234. team.users.clear();
  235. team.users.addAll(userPalette.getSelections());
  236. team.preReceiveScripts.clear();
  237. team.preReceiveScripts.addAll(preReceivePalette.getSelections());
  238. team.postReceiveScripts.clear();
  239. team.postReceiveScripts.addAll(postReceivePalette.getSelections());
  240. return true;
  241. }
  242. private void error(String message) {
  243. JOptionPane.showMessageDialog(EditTeamDialog.this, message, Translation.get("gb.error"),
  244. JOptionPane.ERROR_MESSAGE);
  245. }
  246. public void setTeams(List<TeamModel> teams) {
  247. teamnames.clear();
  248. for (TeamModel team : teams) {
  249. teamnames.add(team.name.toLowerCase());
  250. }
  251. }
  252. public void setRepositories(List<RepositoryModel> repositories, List<RegistrantAccessPermission> permissions) {
  253. List<String> restricted = new ArrayList<String>();
  254. for (RepositoryModel repo : repositories) {
  255. if (repo.accessRestriction.exceeds(AccessRestrictionType.NONE)
  256. && repo.authorizationControl.equals(AuthorizationControl.NAMED)) {
  257. restricted.add(repo.name);
  258. }
  259. }
  260. StringUtils.sortRepositorynames(restricted);
  261. List<String> list = new ArrayList<String>();
  262. // repositories
  263. list.add(".*");
  264. // all repositories excluding personal repositories
  265. if (ModelUtils.getUserRepoPrefix().length() == 1) list.add("[^" + ModelUtils.getUserRepoPrefix() +"].*");
  266. String lastProject = null;
  267. for (String repo : restricted) {
  268. String projectPath = StringUtils.getFirstPathElement(repo);
  269. if (lastProject == null || !lastProject.equalsIgnoreCase(projectPath)) {
  270. lastProject = projectPath;
  271. if (!StringUtils.isEmpty(projectPath)) {
  272. // regex for all repositories within a project
  273. list.add(projectPath + "/.*");
  274. }
  275. list.add(repo);
  276. }
  277. }
  278. // remove repositories for which user already has a permission
  279. if (permissions == null) {
  280. permissions = new ArrayList<RegistrantAccessPermission>();
  281. } else {
  282. for (RegistrantAccessPermission rp : permissions) {
  283. list.remove(rp.registrant);
  284. }
  285. }
  286. repositoryPalette.setObjects(list, permissions);
  287. }
  288. public void setUsers(List<String> users, List<String> selected) {
  289. Collections.sort(users);
  290. if (selected != null) {
  291. Collections.sort(selected);
  292. }
  293. userPalette.setObjects(users, selected);
  294. }
  295. public void setPreReceiveScripts(List<String> unused, List<String> inherited,
  296. List<String> selected) {
  297. Collections.sort(unused);
  298. if (selected != null) {
  299. Collections.sort(selected);
  300. }
  301. preReceivePalette.setObjects(unused, selected);
  302. showInherited(inherited, preReceiveInherited);
  303. }
  304. public void setPostReceiveScripts(List<String> unused, List<String> inherited,
  305. List<String> selected) {
  306. Collections.sort(unused);
  307. if (selected != null) {
  308. Collections.sort(selected);
  309. }
  310. postReceivePalette.setObjects(unused, selected);
  311. showInherited(inherited, postReceiveInherited);
  312. }
  313. private void showInherited(List<String> list, JLabel label) {
  314. StringBuilder sb = new StringBuilder();
  315. if (list != null && list.size() > 0) {
  316. sb.append("<html><body><b>INHERITED</b><ul style=\"margin-left:5px;list-style-type: none;\">");
  317. for (String script : list) {
  318. sb.append("<li>").append(script).append("</li>");
  319. }
  320. sb.append("</ul></body></html>");
  321. }
  322. label.setText(sb.toString());
  323. }
  324. public TeamModel getTeam() {
  325. if (canceled) {
  326. return null;
  327. }
  328. return team;
  329. }
  330. }