Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layer permission save handling. #977

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package fi.nls.oskari.control.admin;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.cache.CacheManager;
import fi.nls.oskari.control.ActionException;
import fi.nls.oskari.control.ActionParameters;
import fi.nls.oskari.control.ActionParamsException;
import fi.nls.oskari.control.layer.GetMapLayerGroupsHandler;
import fi.nls.oskari.domain.Role;
import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.map.layer.OskariLayerService;
import fi.nls.oskari.service.OskariComponentManager;
import fi.nls.oskari.service.ServiceException;
Expand All @@ -15,6 +20,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.oskari.log.AuditLog;
import org.oskari.permissions.PermissionService;
import org.oskari.permissions.model.*;

Expand All @@ -41,6 +47,8 @@ public class LayerPermissionHandler extends AbstractLayerAdminHandler {
private static final String KEY_LAYERS = "layers";
private static final String KEY_PERMISSION = "permissions";

private final static Logger log = LogFactory.getLogger(LayerPermissionHandler.class);

@Override
public void init() {
super.init();
Expand Down Expand Up @@ -105,7 +113,78 @@ public void handleGet(ActionParameters params) throws ActionException {

@Override
public void handlePost(ActionParameters params) throws ActionException {
// TODO: basically SaveLayerPermissionHandler, but check if the syntax still makes sense
//only accept admins
params.requireAdminUser();

final JSONArray resources = parseJSONArray(params.getHttpParam(KEY_LAYERS));
final List<String> layerMappings = new ArrayList<>();

try {
for (int i = 0; i < resources.length(); i++) {
final JSONObject layerPermission = resources.getJSONObject(i);
final String layerMapping = new Integer(layerPermission.getInt("id")).toString();
final Optional<Resource> dbResource = permissionsService.findResource(ResourceType.maplayer, layerMapping);
if (!dbResource.isPresent()) {
throw new ActionParamsException("Resource not found: " + layerMapping);
}
Resource resource = dbResource.get();
final int roleId = Integer.parseInt(layerPermission.getString("roleId"));
JSONArray perm = layerPermission.getJSONArray("permissions");
final List<Permission> resourcePermissions = resource.getPermissions();


for (int n = 0; n < resourcePermissions.size(); n++) {
Permission permission = resourcePermissions.get(n);
boolean found = false;
String type = permission.getType();
for (int j = 0; j < perm.length(); j++) {
if (perm.getString(j).equals(type)) {
found = true;
}
}
if (!found) {
// permission was REMOVED
resource.removePermissionsOfType(type, PermissionExternalType.ROLE, roleId);
}
}
for (int j = 0; j < perm.length(); j++) {
String permissionType = perm.getString(j);

if (!resource.hasRolePermission(roleId, permissionType)) {
// permission was GRANTED
Permission permission = new Permission();
permission.setRoleId(roleId);
permission.setType(permissionType);
resource.addPermission(permission);
}
}
permissionsService.saveResource(resource);
AuditLog.user(params.getClientIp(), params.getUser())
.withParam("id", resource.getMapping())
.updated(AuditLog.ResourceType.MAPLAYER_PERMISSION);
layerMappings.add(resource.getMapping());
}
ResponseHelper.writeResponse(params, "success");
flushLayerListCache();
} catch (JSONException e) {
throw new ActionParamsException("Invalid input");
} finally {
log.info("Layer permissions updated by", params.getUser().getScreenname(), "Layers updated:", layerMappings);
}
}

private void flushLayerListCache() {
CacheManager.getCache(GetMapLayerGroupsHandler.CACHE_NAME).flush(true);
}

private JSONArray parseJSONArray(final String jsonArray) throws ActionParamsException {
try {
final JSONArray resources = new JSONArray(jsonArray);
log.debug(" permissions JSON ", resources);
return resources;
} catch (Exception e) {
throw new ActionParamsException("Unable to parse param JSON:\n" + jsonArray);
}
}

private Role[] getRoles() {
Expand Down