Skip to content

Commit

Permalink
Merge pull request #390 from wttech/script-root-paths
Browse files Browse the repository at this point in the history
Script root paths
  • Loading branch information
dprzybyl authored Feb 26, 2023
2 parents f1f96b0 + c366055 commit 5066e56
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private static String getPathFromOriginalFileName(String savePath, String origin
path = savePath + (subPath.startsWith("/") ? "" : "/") + subPath;
}
}
if (!path.startsWith(SCRIPT_PATH)) {
if (!path.startsWith("/")) {
path = SCRIPT_PATH + (path.startsWith("/") ? "" : "/") + path;
}
return path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import com.cognifide.apm.api.scripts.LaunchEnvironment;
import com.cognifide.apm.api.scripts.LaunchMode;
import com.cognifide.apm.api.scripts.MutableScript;
import com.cognifide.apm.api.services.ExecutionMode;
import com.cognifide.apm.api.services.ScriptManager;
import com.cognifide.apm.core.Apm;
import com.cognifide.apm.core.utils.PathUtils;
import com.cognifide.apm.core.utils.ResourceMixinUtil;
import com.cognifide.apm.core.utils.RuntimeUtils;
import com.day.cq.commons.jcr.JcrConstants;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
Expand All @@ -34,14 +37,18 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -56,6 +63,10 @@ public class ScriptModel implements MutableScript {
@Self
private Resource resource;

@Inject
@OSGiService
private ScriptManager scriptManager;

@Inject
@Named(ScriptNode.APM_LAUNCH_ENABLED)
private Boolean launchEnabled;
Expand Down Expand Up @@ -106,6 +117,17 @@ public ScriptModel(Resource resource) {
this.path = resource.getPath();
}

@PostConstruct
private void afterCreated() {
if (verified == null) {
try {
scriptManager.process(this, ExecutionMode.VALIDATION, resource.getResourceResolver());
} catch (RepositoryException | PersistenceException e) {
LOGGER.error("", e);
}
}
}

@Override
public boolean isValid() {
return BooleanUtils.toBoolean(verified);
Expand Down Expand Up @@ -206,12 +228,14 @@ public void setLastExecuted(Date date) throws PersistenceException {
}

private void setProperty(String name, Object value) throws PersistenceException {
if (!PathUtils.isAppsOrLibsPath(path)) {
ResourceResolver resolver = resource.getResourceResolver();
boolean compositeNodeStore = RuntimeUtils.determineCompositeNodeStore(resolver);
if (!compositeNodeStore || !PathUtils.isAppsOrLibsPath(path)) {
ModifiableValueMap vm = resource.adaptTo(ModifiableValueMap.class);
ResourceMixinUtil.addMixin(vm, ScriptNode.APM_SCRIPT);
vm.put(name, convertValue(value));

resource.getResourceResolver().commit();
resolver.commit();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*-
* ========================LICENSE_START=================================
* AEM Permission Management
* %%
* Copyright (C) 2013 Wunderman Thompson Technology
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/
package com.cognifide.apm.core.services;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceDecorator;
import org.apache.sling.api.resource.ResourceWrapper;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;


@Component(service = {ScriptRootPathsProvider.class, ResourceDecorator.class}, immediate = true)
@Designate(ocd = ScriptRootPathsProvider.Configuration.class)
public class ScriptRootPathsProvider implements ResourceDecorator {

private static final String RESOURCE_TYPE = "wcm/commons/ui/shell/datasources/breadcrumbs";

private static final String RESOURCE_PATH = "/apps/apm/views/scripts/jcr:content/breadcrumbs";

private static final String DEFAULT_SCRIPT_PATH = "/conf/apm/scripts";

private Set<String> rootPaths;

@Activate
public void activate(Configuration config) {
this.rootPaths = new HashSet<>();
this.rootPaths.add(DEFAULT_SCRIPT_PATH);
this.rootPaths.addAll(Arrays.asList(config.rootPaths()));
}

@Override
public Resource decorate(Resource resource) {
Resource result = resource;
if (isAllowed(resource)) {
ValueMap valueMap = new ValueMapDecorator(new HashMap<>());
valueMap.putAll(resource.getValueMap());
valueMap.put("rootPath", "/");
result = new ResourceWrapper(resource) {
@Override
public ValueMap getValueMap() {
return valueMap;
}
};
}
return result;
}

@Override
public Resource decorate(Resource resource, HttpServletRequest request) {
return decorate(resource);
}

public boolean isValidPath(String path) {
return rootPaths.stream()
.anyMatch(rootPath -> path.startsWith(rootPath) || rootPath.startsWith(path));
}

private boolean isAllowed(Resource resource) {
return StringUtils.equals(resource.getResourceType(), RESOURCE_TYPE)
&& StringUtils.equals(resource.getPath(), RESOURCE_PATH)
&& rootPaths.size() > 1;
}

@ObjectClassDefinition(name = "AEM Permission Management - Script Root Paths Provider")
public @interface Configuration {

@AttributeDefinition(name = "Additional Script Root Paths")
String[] rootPaths();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.adobe.granite.ui.components.ds.SimpleDataSource;
import com.cognifide.apm.core.Property;
import com.cognifide.apm.core.scripts.ScriptModel;
import com.cognifide.apm.core.services.ScriptRootPathsProvider;
import com.cognifide.apm.core.ui.models.ScriptsRowModel;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -36,6 +37,7 @@
import org.apache.sling.api.resource.ResourceWrapper;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(
service = Servlet.class,
Expand All @@ -48,13 +50,17 @@
)
public class ScriptsDatasourceServlet extends SlingSafeMethodsServlet {

@Reference
private ScriptRootPathsProvider scriptRootPathsProvider;

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
String path = request.getRequestPathInfo().getSuffix();
List<Resource> scripts = new ArrayList<>();
Resource resource = request.getResourceResolver().getResource(path);
for (Resource child : resource.getChildren()) {
if (ScriptsRowModel.isFolder(child) || ScriptModel.isScript(child)) {
if ((ScriptsRowModel.isFolder(child) || ScriptModel.isScript(child))
&& scriptRootPathsProvider.isValidPath(child.getPath())) {
scripts.add(new ResourceTypeWrapper(child));
}
}
Expand Down

0 comments on commit 5066e56

Please sign in to comment.