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