Commit a64b3899 authored by etj's avatar etj
Browse files

Merge pull request #1380 resolving conflicts.

parents 87624931 31df56ba
......@@ -139,7 +139,7 @@
<artifactId>prototype</artifactId>
<version>1.4.7</version>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.geoserver</groupId>
......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.apache.org/">
<body>
<wicket:extend>
<form wicket:id="form">
<ul>
<li>
<fieldset>
<ul>
<li>
<label for="priority"><wicket:message key="priority"></wicket:message></label>
<input id="priority" wicket:id="priority" type="text" class="text" />
</li>
<li>
<label for="roleName"><wicket:message key="roleName"></wicket:message></label>
<select wicket:id="roleName" id="roleName" />
</li>
<li>
<label for="userName"><wicket:message key="username"></wicket:message></label>
<select wicket:id="userName" id="userName" />
</li>
<li>
<label for="workspace"><wicket:message key="workspace"></wicket:message></label>
<select wicket:id="workspace" id="workspace" />
</li>
<li>
<label for="access"><wicket:message key="access"></wicket:message></label>
<select wicket:id="access" id="access" />
</li>
</ul>
</fieldset>
</li>
</ul>
<div class="button-group selfclear">
<a href="#" wicket:id="save"><wicket:message key="save">Save</wicket:message></a>
<a href="#" wicket:id="cancel"><wicket:message key="cancel">Cancel</wicket:message></a>
</div>
</form>
</wicket:extend>
</body>
</html>
\ No newline at end of file
/* (c) 2015 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.geofence.web;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
import org.apache.wicket.markup.html.form.*;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.geoserver.catalog.WorkspaceInfo;
import org.geoserver.geofence.core.model.enums.AdminGrantType;
import org.geoserver.geofence.core.model.enums.GrantType;
import org.geoserver.geofence.services.dto.ShortAdminRule;
import org.geoserver.security.GeoServerRoleService;
import org.geoserver.security.GeoServerSecurityManager;
import org.geoserver.security.impl.GeoServerRole;
import org.geoserver.security.impl.GeoServerUser;
import org.geoserver.web.GeoServerApplication;
import org.geoserver.web.GeoServerSecuredPage;
import org.geoserver.web.wicket.ParamResourceModel;
import org.springframework.dao.DuplicateKeyException;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
public class GeofenceAdminRulePage extends GeoServerSecuredPage {
protected DropDownChoice<String> userChoice, roleChoice, workspaceChoice;
protected DropDownChoice<AdminGrantType> grantTypeChoice;
public GeofenceAdminRulePage(final ShortAdminRule rule, final GeofenceAdminRulesModel rules) {
final Form<ShortAdminRule> form = new Form<>("form", new CompoundPropertyModel<ShortAdminRule>(rule));
add(form);
form.add(new TextField<Integer>("priority").setRequired(true));
form.add(roleChoice = new DropDownChoice<>("roleName", getRoleNames()));
roleChoice.add(new OnChangeAjaxBehavior() {
@Override
protected void onUpdate(AjaxRequestTarget target) {
userChoice.setChoices(getUserNames(roleChoice.getConvertedInput()));
form.getModelObject().setUserName(null);
userChoice.modelChanged();
target.addComponent(userChoice);
}
});
roleChoice.setNullValid(true);
form.add(userChoice = new DropDownChoice<>("userName", getUserNames(rule.getRoleName())));
userChoice.setOutputMarkupId(true);
userChoice.setNullValid(true);
form.add(workspaceChoice = new DropDownChoice<>("workspace", getWorkspaceNames()));
workspaceChoice.setNullValid(true);
form.add(grantTypeChoice = new DropDownChoice<>("access", Arrays.asList(AdminGrantType.values()), new AdminGrantTypeRenderer()));
grantTypeChoice.setRequired(true);
form.add(new SubmitLink("save") {
@Override
public void onSubmit() {
ShortAdminRule rule = (ShortAdminRule) getForm().getModelObject();
try {
rules.save(rule);
doReturn(GeofenceServerAdminPage.class);
} catch (DuplicateKeyException e) {
error(new ResourceModel("GeofenceRulePage.duplicate").getObject());
} catch (Exception exception) {
error(exception);
}
}
});
form.add(new BookmarkablePageLink<ShortAdminRule>("cancel", GeofenceServerPage.class));
}
protected List<String> getWorkspaceNames() {
SortedSet<String> resultSet = new TreeSet<String>();
for (WorkspaceInfo ws : getCatalog().getFacade().getWorkspaces()) {
resultSet.add(ws.getName());
}
return new ArrayList<>(resultSet);
}
protected List<String> getRoleNames() {
SortedSet<String> resultSet = new TreeSet<>();
try {
for (GeoServerRole role : securityManager().getRolesForAccessControl()) {
resultSet.add(role.getAuthority());
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
}
return new ArrayList<>(resultSet);
}
protected List<String> getUserNames(String roleName) {
SortedSet<String> resultSet = new TreeSet<>();
GeoServerSecurityManager securityManager = securityManager();
try {
if (roleName == null) {
for (String serviceName : securityManager.listUserGroupServices()) {
for (GeoServerUser user : securityManager.loadUserGroupService(serviceName).getUsers()) {
resultSet.add(user.getUsername());
}
}
} else {
for (String serviceName : securityManager.listRoleServices()) {
GeoServerRoleService roleService = securityManager.loadRoleService(serviceName);
GeoServerRole role = roleService.getRoleByName(roleName);
if (role != null) {
resultSet.addAll(roleService.getUserNamesForRole(role));
}
}
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
}
return new ArrayList<>(resultSet);
}
protected class AdminGrantTypeRenderer implements IChoiceRenderer<AdminGrantType> {
public Object getDisplayValue(AdminGrantType object) {
return new ParamResourceModel(object.name(), getPage()).getObject();
}
public String getIdValue(AdminGrantType object, int index) {
return object.name();
}
}
protected GeoServerSecurityManager securityManager() {
return GeoServerApplication.get().getSecurityManager();
}
}
/* (c) 2015 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.geofence.web;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.geoserver.geofence.core.model.AdminRule;
import org.geoserver.geofence.core.model.enums.AdminGrantType;
import org.geoserver.geofence.services.AdminRuleAdminService;
import org.geoserver.geofence.services.dto.ShortAdminRule;
import org.geoserver.web.GeoServerApplication;
import org.geoserver.web.wicket.GeoServerDataProvider;
import java.util.*;
@SuppressWarnings("serial")
public class GeofenceAdminRulesModel extends GeoServerDataProvider<ShortAdminRule> {
public static class RuleBeanProperty<T> extends BeanProperty<T> {
public RuleBeanProperty(String key, String propertyPath) {
super(key, propertyPath);
}
@Override
public Comparator<T> getComparator() {
return null;
}
public IModel getModel(IModel itemModel) {
return new PropertyModel<Object>(itemModel, getPropertyPath()) {
@Override
public Object getObject() {
Object o = super.getObject();
return o == null ? "*" : o;
}
};
}
}
public static final Property<ShortAdminRule> PRIORITY = new BeanProperty<>("priority", "priority");
public static final Property<ShortAdminRule> ROLE = new RuleBeanProperty<>("roleName", "roleName");
public static final Property<ShortAdminRule> USER = new RuleBeanProperty<>("userName", "userName");
public static final Property<ShortAdminRule> WORKSPACE = new RuleBeanProperty<>("workspace", "workspace");
public static final Property<ShortAdminRule> ACCESS = new RuleBeanProperty<>("access", "access");
public static final Property<ShortAdminRule> BUTTONS = new PropertyPlaceholder<>("buttons");
private static AdminRuleAdminService adminService() {
return (AdminRuleAdminService) GeoServerApplication.get().getBean("adminRuleAdminService");
}
protected List<ShortAdminRule> rules;
public GeofenceAdminRulesModel() {
rules = adminService().getAll();
setSort("priority", true);
}
@Override
protected Comparator<ShortAdminRule> getComparator(SortParam sort) {
return null;
}
@Override
public void setSort(SortParam param) {
super.setSort(param);
Collections.sort(rules, super.getComparator(param));
}
@Override
protected List<Property<ShortAdminRule>> getProperties() {
return Arrays.asList(PRIORITY, ROLE, USER, WORKSPACE, ACCESS, BUTTONS);
}
@Override
protected List<ShortAdminRule> getItems() {
return rules;
}
public void save(ShortAdminRule rule) {
if (rule.getId() == null) {
shiftIfNecessary(rule.getPriority(), rule);
int i = 0;
while (i < rules.size() && rules.get(i).getPriority() < rule.getPriority()) {
i++;
}
rules.add(i, rule);
AdminRule bigRule = new AdminRule();
syncRule(rule, bigRule);
rule.setId(adminService().insert(bigRule));
} else {
AdminRule bigRule = adminService().get(rule.getId());
if (bigRule.getPriority() != rule.getPriority()) {
shiftIfNecessary(rule.getPriority(), rule);
}
syncRule(rule, bigRule);
adminService().update(bigRule);
}
}
public void remove(Collection<ShortAdminRule> selected) {
rules.removeAll(selected);
for (ShortAdminRule rule : selected) {
adminService().delete(rule.getId());
}
}
public boolean canUp(ShortAdminRule rule) {
return rules.indexOf(rule) > 0;
}
public void moveUp(ShortAdminRule rule) {
int index = rules.indexOf(rule);
if (index > 0) {
swap(rule, rules.get(index - 1));
rules.remove(index);
rules.add(index - 1, rule);
}
}
public boolean canDown(ShortAdminRule rule) {
return rules.indexOf(rule) < rules.size() - 1;
}
public void moveDown(ShortAdminRule rule) {
int index = rules.indexOf(rule);
if (index < rules.size() - 1) {
swap(rule, rules.get(index + 1));
rules.remove(index);
rules.add(index + 1, rule);
}
}
protected void swap(ShortAdminRule rule, ShortAdminRule otherRule) {
long p = otherRule.getPriority();
otherRule.setPriority(rule.getPriority());
rule.setPriority(p);
adminService().swap(rule.getId(), otherRule.getId());
}
protected void shiftIfNecessary(long priority, ShortAdminRule keep) {
boolean necessary = false;
for (ShortAdminRule rule : rules) {
if (rule.getPriority() == priority) {
necessary = true;
continue;
}
}
if (necessary) {
for (ShortAdminRule rule : rules) {
if (rule.getPriority() >= priority && rule != keep) {
rule.setPriority(rule.getPriority() + 1);
}
}
adminService().shift(priority, 1);
}
}
public ShortAdminRule newRule() {
ShortAdminRule rule = new ShortAdminRule();
rule.setAccess(AdminGrantType.USER);
rule.setPriority(0);
return rule;
}
protected static void syncRule(ShortAdminRule shortRule, AdminRule rule) {
rule.setPriority(shortRule.getPriority());
rule.setUsername(shortRule.getUserName());
rule.setRolename(shortRule.getRoleName());
rule.setWorkspace(shortRule.getWorkspace());
rule.setAccess(shortRule.getAccess());
}
}
\ No newline at end of file
<html>
<body>
<wicket:panel>
<span wicket:id="up"></span>
<span wicket:id="down"></span>
<span wicket:id="edit"></span>
</wicket:panel>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org/" >
<body>
<wicket:extend>
<ul>
<li><a class="add-link" href="#" wicket:id="addNew"><wicket:message key="addNew" /></a></li>
<li><a class="remove-link" href="#" wicket:id="removeSelected"><wicket:message key="removeSelected" /></a></li>
</ul>
<div wicket:id="rulesPanel"></div>
</wicket:extend>
</body>
</html>
/* (c) 2015 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.geofence.web;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.geoserver.geofence.services.dto.ShortAdminRule;
import org.geoserver.web.GeoServerSecuredPage;
import org.geoserver.web.wicket.GeoServerDataProvider.Property;
import org.geoserver.web.wicket.GeoServerTablePanel;
import org.geoserver.web.wicket.ImageAjaxLink;
import org.geoserver.web.wicket.ParamResourceModel;
import wicketdnd.*;
import wicketdnd.theme.WebTheme;
@SuppressWarnings("serial")
public class GeofenceServerAdminPage extends GeoServerSecuredPage {
private GeofenceAdminRulesModel rulesModel;
private GeoServerTablePanel<ShortAdminRule> rulesPanel;
private AjaxLink<Object> remove;
public GeofenceServerAdminPage() {
add(new AjaxLink<Object>("addNew") {
@Override
public void onClick(AjaxRequestTarget target) {
setResponsePage(new GeofenceAdminRulePage(rulesModel.newRule(), rulesModel));
}
});
add(remove = new AjaxLink<Object>("removeSelected") {
@Override
public void onClick(AjaxRequestTarget target) {
rulesModel.remove(rulesPanel.getSelection());
target.addComponent(rulesPanel);
}
});
remove.setOutputMarkupId(true);
remove.setEnabled(false);
add(rulesPanel = new GeoServerTablePanel<ShortAdminRule>("rulesPanel",
rulesModel = new GeofenceAdminRulesModel(), true) {
@Override
protected Component getComponentForProperty(String id, IModel itemModel, Property<ShortAdminRule> property) {
if (property == GeofenceAdminRulesModel.BUTTONS) {
return new ButtonPanel(id, (ShortAdminRule) itemModel.getObject());
}
return null;
}
@Override
protected void onSelectionUpdate(AjaxRequestTarget target) {
remove.setEnabled(rulesPanel.getSelection().size() > 0);
target.addComponent(remove);
}
});
rulesPanel.add(CSSPackageResource.getHeaderContribution(new WebTheme()));
rulesPanel.add(new DragSource(Operation.MOVE).drag("tr"));
rulesPanel.add(new DropTarget(Operation.MOVE) {
public void onDrop(AjaxRequestTarget target, Transfer transfer, Location location) {
if (location == null || !(location.getComponent().getDefaultModel().getObject() instanceof ShortAdminRule)) {
return;
}
ShortAdminRule movedRule = transfer.getData();
ShortAdminRule targetRule = (ShortAdminRule) location.getComponent().getDefaultModel().getObject();
if (movedRule.getId().equals(targetRule.getId())) {
return;
}
if (movedRule.getPriority() < targetRule.getPriority()) {
movedRule.setPriority(targetRule.getPriority() + 1);
} else {
movedRule.setPriority(targetRule.getPriority());
}
rulesModel.save(movedRule);
doReturn(GeofenceServerAdminPage.class);
}
}.dropCenter("tr"));
rulesPanel.setOutputMarkupId(true);
}
private class ButtonPanel extends Panel {
private ImageAjaxLink upLink;
private ImageAjaxLink downLink;
public ButtonPanel(String id, final ShortAdminRule rule) {
super(id);
this.setOutputMarkupId(true);
upLink = new ImageAjaxLink("up", new ResourceReference(getClass(), "img/arrow_up.png")) {
private static final long serialVersionUID = -8179503447106596760L;
@Override
protected void onClick(AjaxRequestTarget target) {
rulesModel.moveUp(rule);
target.addComponent(rulesPanel);
}
@Override
protected void onComponentTag(ComponentTag tag) {
if (rulesModel.canUp(rule)) {
tag.put("style", "visibility:visible");
} else {
tag.put("style", "visibility:hidden");
}
}
};
upLink.getImage().add(new AttributeModifier("alt", true, new ParamResourceModel("GeofenceServerAdminPage.up", upLink)));
upLink.setOutputMarkupId(true);
add(upLink);
downLink = new ImageAjaxLink("down", new ResourceReference(getClass(), "img/arrow_down.png")) {
private static final long serialVersionUID = 4640187752303674221L;
@Override
protected void onClick(AjaxRequestTarget target) {
rulesModel.moveDown(rule);
target.addComponent(rulesPanel);
}
@Override
protected void onComponentTag(ComponentTag tag) {
if (rulesModel.canDown(rule)) {
tag.put("style", "visibility:visible");
} else {
tag.put("style", "visibility:hidden");
}
}
};
downLink.getImage().add(new AttributeModifier("alt", true, new ParamResourceModel("GeofenceServerAdminPage.down", downLink)));
downLink.setOutputMarkupId(true);
add(downLink);
ImageAjaxLink editLink = new ImageAjaxLink("edit", new ResourceReference(getClass(), "img/edit.png")) {