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