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.

RegistrantPermissionsPanel.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2012 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.panels;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import org.apache.wicket.ajax.AjaxRequestTarget;
  23. import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
  24. import org.apache.wicket.ajax.markup.html.form.AjaxButton;
  25. import org.apache.wicket.markup.html.basic.Label;
  26. import org.apache.wicket.markup.html.form.DropDownChoice;
  27. import org.apache.wicket.markup.html.form.Form;
  28. import org.apache.wicket.markup.html.form.IChoiceRenderer;
  29. import org.apache.wicket.markup.repeater.Item;
  30. import org.apache.wicket.markup.repeater.OddEvenItem;
  31. import org.apache.wicket.markup.repeater.RefreshingView;
  32. import org.apache.wicket.markup.repeater.util.ModelIteratorAdapter;
  33. import org.apache.wicket.model.CompoundPropertyModel;
  34. import org.apache.wicket.model.IModel;
  35. import com.gitblit.Constants.AccessPermission;
  36. import com.gitblit.Constants.PermissionType;
  37. import com.gitblit.Constants.RegistrantType;
  38. import com.gitblit.models.RegistrantAccessPermission;
  39. import com.gitblit.utils.DeepCopier;
  40. import com.gitblit.utils.StringUtils;
  41. import com.gitblit.wicket.WicketUtils;
  42. /**
  43. * Allows user to manipulate registrant access permissions.
  44. *
  45. * @author James Moger
  46. *
  47. */
  48. public class RegistrantPermissionsPanel extends BasePanel {
  49. private static final long serialVersionUID = 1L;
  50. public RegistrantPermissionsPanel(String wicketId, RegistrantType registrantType, List<String> allRegistrants, final List<RegistrantAccessPermission> permissions, final Map<AccessPermission, String> translations) {
  51. super(wicketId);
  52. setOutputMarkupId(true);
  53. // update existing permissions repeater
  54. RefreshingView<RegistrantAccessPermission> dataView = new RefreshingView<RegistrantAccessPermission>("permissionRow") {
  55. private static final long serialVersionUID = 1L;
  56. @Override
  57. protected Iterator<IModel<RegistrantAccessPermission>> getItemModels() {
  58. // the iterator returns RepositoryPermission objects, but we need it to
  59. // return models
  60. return new ModelIteratorAdapter<RegistrantAccessPermission>(permissions.iterator()) {
  61. @Override
  62. protected IModel<RegistrantAccessPermission> model(RegistrantAccessPermission permission) {
  63. return new CompoundPropertyModel<RegistrantAccessPermission>(permission);
  64. }
  65. };
  66. }
  67. @Override
  68. protected Item<RegistrantAccessPermission> newItem(String id, int index, IModel<RegistrantAccessPermission> model) {
  69. // this item sets markup class attribute to either 'odd' or
  70. // 'even' for decoration
  71. return new OddEvenItem<RegistrantAccessPermission>(id, index, model);
  72. }
  73. public void populateItem(final Item<RegistrantAccessPermission> item) {
  74. final RegistrantAccessPermission entry = item.getModelObject();
  75. if (RegistrantType.REPOSITORY.equals(entry.registrantType)) {
  76. String repoName = StringUtils.stripDotGit(entry.registrant);
  77. if (StringUtils.findInvalidCharacter(repoName) == null) {
  78. // repository, strip .git and show swatch
  79. Label registrant = new Label("registrant", repoName);
  80. WicketUtils.setCssClass(registrant, "repositorySwatch");
  81. WicketUtils.setCssBackground(registrant, repoName);
  82. item.add(registrant);
  83. } else {
  84. // likely a regex
  85. Label label = new Label("registrant", entry.registrant);
  86. WicketUtils.setCssStyle(label, "font-weight: bold;");
  87. item.add(label);
  88. }
  89. } else {
  90. // user or team
  91. Label label = new Label("registrant", entry.registrant);
  92. WicketUtils.setCssStyle(label, "font-weight: bold;");
  93. item.add(label);
  94. }
  95. switch (entry.permissionType) {
  96. case OWNER:
  97. Label owner = new Label("pType", "owner");
  98. WicketUtils.setHtmlTooltip(owner, getString("gb.ownerPermission"));
  99. item.add(owner);
  100. break;
  101. case REGEX:
  102. Label regex = new Label("pType", "regex");
  103. WicketUtils.setHtmlTooltip(regex, getString("gb.regexPermission"));
  104. item.add(regex);
  105. break;
  106. default:
  107. item.add(new Label("pType", "").setVisible(false));
  108. break;
  109. }
  110. // use ajax to get immediate update of permission level change
  111. // otherwise we can lose it if they change levels and then add
  112. // a new repository permission
  113. final DropDownChoice<AccessPermission> permissionChoice = new DropDownChoice<AccessPermission>(
  114. "permission", Arrays.asList(AccessPermission.values()), new AccessPermissionRenderer(translations));
  115. // only allow changing an explicitly defined permission
  116. // this is designed to prevent changing a regex permission in
  117. // a repository
  118. permissionChoice.setEnabled(entry.isEditable);
  119. permissionChoice.setOutputMarkupId(true);
  120. if (entry.isEditable) {
  121. permissionChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
  122. private static final long serialVersionUID = 1L;
  123. protected void onUpdate(AjaxRequestTarget target) {
  124. target.addComponent(permissionChoice);
  125. }
  126. });
  127. }
  128. item.add(permissionChoice);
  129. }
  130. };
  131. add(dataView);
  132. setOutputMarkupId(true);
  133. // filter out registrants we already have permissions for
  134. final List<String> registrants = new ArrayList<String>(allRegistrants);
  135. for (RegistrantAccessPermission rp : permissions) {
  136. if (rp.isEditable) {
  137. // only remove editable duplicates
  138. // this allows for specifying an explicit permission
  139. registrants.remove(rp.registrant);
  140. }
  141. }
  142. // add new permission form
  143. IModel<RegistrantAccessPermission> addPermissionModel = new CompoundPropertyModel<RegistrantAccessPermission>(new RegistrantAccessPermission(registrantType));
  144. Form<RegistrantAccessPermission> addPermissionForm = new Form<RegistrantAccessPermission>("addPermissionForm", addPermissionModel);
  145. addPermissionForm.add(new DropDownChoice<String>("registrant", registrants));
  146. addPermissionForm.add(new DropDownChoice<AccessPermission>("permission", Arrays
  147. .asList(AccessPermission.NEWPERMISSIONS), new AccessPermissionRenderer(translations)));
  148. AjaxButton button = new AjaxButton("addPermissionButton", addPermissionForm) {
  149. private static final long serialVersionUID = 1L;
  150. @Override
  151. protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
  152. // add permission to our list
  153. RegistrantAccessPermission rp = (RegistrantAccessPermission) form.getModel().getObject();
  154. if (rp.permission == null) {
  155. return;
  156. }
  157. RegistrantAccessPermission copy = DeepCopier.copy(rp);
  158. if (StringUtils.findInvalidCharacter(copy.registrant) != null) {
  159. copy.permissionType = PermissionType.REGEX;
  160. }
  161. permissions.add(copy);
  162. // remove registrant from available choices
  163. registrants.remove(rp.registrant);
  164. // force the panel to refresh
  165. target.addComponent(RegistrantPermissionsPanel.this);
  166. }
  167. };
  168. addPermissionForm.add(button);
  169. // only show add permission form if we have a registrant choice
  170. add(addPermissionForm.setVisible(registrants.size() > 0));
  171. }
  172. protected boolean getStatelessHint()
  173. {
  174. return false;
  175. }
  176. private class AccessPermissionRenderer implements IChoiceRenderer<AccessPermission> {
  177. private static final long serialVersionUID = 1L;
  178. private final Map<AccessPermission, String> map;
  179. public AccessPermissionRenderer(Map<AccessPermission, String> map) {
  180. this.map = map;
  181. }
  182. @Override
  183. public String getDisplayValue(AccessPermission type) {
  184. return map.get(type);
  185. }
  186. @Override
  187. public String getIdValue(AccessPermission type, int index) {
  188. return Integer.toString(index);
  189. }
  190. }
  191. }