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.

AccessPolicyPanel.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2014 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.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import org.apache.wicket.ajax.form.AjaxFormChoiceComponentUpdatingBehavior;
  21. import org.apache.wicket.markup.html.basic.Label;
  22. import org.apache.wicket.markup.html.form.Radio;
  23. import org.apache.wicket.markup.html.form.RadioGroup;
  24. import org.apache.wicket.markup.html.list.ListItem;
  25. import org.apache.wicket.markup.html.list.ListView;
  26. import org.apache.wicket.markup.html.panel.Fragment;
  27. import org.apache.wicket.model.IModel;
  28. import org.apache.wicket.model.Model;
  29. import com.gitblit.Constants.AccessRestrictionType;
  30. import com.gitblit.Constants.AuthorizationControl;
  31. import com.gitblit.Keys;
  32. import com.gitblit.models.RepositoryModel;
  33. import com.gitblit.wicket.WicketUtils;
  34. /**
  35. * A radio group panel of the 5 available authorization/access restriction combinations.
  36. *
  37. * @author James Moger
  38. *
  39. */
  40. public class AccessPolicyPanel extends BasePanel {
  41. private static final long serialVersionUID = 1L;
  42. private final RepositoryModel repository;
  43. private final AjaxFormChoiceComponentUpdatingBehavior callback;
  44. private RadioGroup<AccessPolicy> policiesGroup;
  45. private IModel<Boolean> allowForks;
  46. public AccessPolicyPanel(String wicketId, RepositoryModel repository) {
  47. this(wicketId, repository, null);
  48. }
  49. public AccessPolicyPanel(String wicketId, RepositoryModel repository, AjaxFormChoiceComponentUpdatingBehavior callback) {
  50. super(wicketId);
  51. this.repository = repository;
  52. this.callback = callback;
  53. }
  54. @Override
  55. protected void onInitialize() {
  56. super.onInitialize();
  57. AccessPolicy anonymousPolicy = new AccessPolicy(getString("gb.anonymousPolicy"),
  58. getString("gb.anonymousPolicyDescription"),
  59. "blank.png",
  60. AuthorizationControl.AUTHENTICATED,
  61. AccessRestrictionType.NONE);
  62. AccessPolicy authenticatedPushPolicy = new AccessPolicy(getString("gb.authenticatedPushPolicy"),
  63. getString("gb.authenticatedPushPolicyDescription"),
  64. "lock_go_16x16.png",
  65. AuthorizationControl.AUTHENTICATED,
  66. AccessRestrictionType.PUSH);
  67. AccessPolicy namedPushPolicy = new AccessPolicy(getString("gb.namedPushPolicy"),
  68. getString("gb.namedPushPolicyDescription"),
  69. "lock_go_16x16.png",
  70. AuthorizationControl.NAMED,
  71. AccessRestrictionType.PUSH);
  72. AccessPolicy clonePolicy = new AccessPolicy(getString("gb.clonePolicy"),
  73. getString("gb.clonePolicyDescription"),
  74. "lock_pull_16x16.png",
  75. AuthorizationControl.NAMED,
  76. AccessRestrictionType.CLONE);
  77. AccessPolicy viewPolicy = new AccessPolicy(getString("gb.viewPolicy"),
  78. getString("gb.viewPolicyDescription"),
  79. "shield_16x16.png",
  80. AuthorizationControl.NAMED,
  81. AccessRestrictionType.VIEW);
  82. List<AccessPolicy> policies = new ArrayList<AccessPolicy>();
  83. if (app().settings().getBoolean(Keys.git.allowAnonymousPushes, false)) {
  84. policies.add(anonymousPolicy);
  85. }
  86. policies.add(authenticatedPushPolicy);
  87. policies.add(namedPushPolicy);
  88. policies.add(clonePolicy);
  89. policies.add(viewPolicy);
  90. AccessRestrictionType defaultRestriction = repository.accessRestriction;
  91. if (defaultRestriction == null) {
  92. defaultRestriction = AccessRestrictionType.fromName(app().settings().getString(Keys.git.defaultAccessRestriction,
  93. AccessRestrictionType.PUSH.name()));
  94. }
  95. AuthorizationControl defaultControl = repository.authorizationControl;
  96. if (defaultControl == null) {
  97. defaultControl = AuthorizationControl.fromName(app().settings().getString(Keys.git.defaultAuthorizationControl,
  98. AuthorizationControl.NAMED.name()));
  99. }
  100. AccessPolicy defaultPolicy = namedPushPolicy;
  101. for (AccessPolicy policy : policies) {
  102. if (policy.type == defaultRestriction && policy.control == defaultControl) {
  103. defaultPolicy = policy;
  104. }
  105. }
  106. policiesGroup = new RadioGroup<>("policiesGroup", new Model<AccessPolicy>(defaultPolicy));
  107. ListView<AccessPolicy> policiesList = new ListView<AccessPolicy>("policies", policies) {
  108. private static final long serialVersionUID = 1L;
  109. @Override
  110. protected void populateItem(ListItem<AccessPolicy> item) {
  111. AccessPolicy p = item.getModelObject();
  112. item.add(new Radio<AccessPolicy>("radio", item.getModel()));
  113. item.add(WicketUtils.newImage("image", p.image));
  114. item.add(new Label("name", p.name));
  115. item.add(new Label("description", p.description));
  116. }
  117. };
  118. policiesGroup.add(policiesList);
  119. if (callback != null) {
  120. policiesGroup.add(callback);
  121. policiesGroup.setOutputMarkupId(true);
  122. }
  123. add(policiesGroup);
  124. allowForks = Model.of(app().settings().getBoolean(Keys.web.allowForking, true));
  125. if (allowForks.getObject()) {
  126. Fragment fragment = new Fragment("allowForks", "allowForksFragment", this);
  127. fragment.add(new CheckboxOption("allowForks",
  128. getString("gb.allowForks"),
  129. getString("gb.allowForksDescription"),
  130. allowForks));
  131. add(fragment);
  132. } else {
  133. add(new Label("allowForks").setVisible(false));
  134. }
  135. setOutputMarkupId(true);
  136. }
  137. public void updateModel(RepositoryModel repository) {
  138. AccessPolicy policy = policiesGroup.getModelObject();
  139. repository.authorizationControl = policy.control;
  140. repository.accessRestriction = policy.type;
  141. repository.allowForks = allowForks.getObject();
  142. }
  143. @Override
  144. protected boolean getStatelessHint() {
  145. return false;
  146. }
  147. public static class AccessPolicy implements Serializable {
  148. private static final long serialVersionUID = 1L;
  149. final String name;
  150. final String description;
  151. final String image;
  152. final AuthorizationControl control;
  153. final AccessRestrictionType type;
  154. AccessPolicy(String name, String description, String img, AuthorizationControl control, AccessRestrictionType type) {
  155. this.name = name;
  156. this.description = description;
  157. this.image = img;
  158. this.control = control;
  159. this.type = type;
  160. }
  161. @Override
  162. public String toString() {
  163. return name;
  164. }
  165. }
  166. }