Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions dotCMS/src/main/java/com/dotcms/rendering/engine/ScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.Map;

/**
Expand Down Expand Up @@ -40,4 +41,18 @@ default Object eval (final HttpServletRequest request, final HttpServletResponse
*/
Object eval (final HttpServletRequest request, final HttpServletResponse response,
final Reader scriptReader, final Map<String, Object> contextParams);


/**
* Execute a single function (does not include any context rather than the bindings)
* @param functionName function name to execute
* @param script actual js script
* @param bindings binding for the context
* @param args arguments for the function
* @return Object
*/
Object executeFunction(String functionName, String script, Map<String, Object> bindings, final Object... args);



} // E:O:F:ScriptEngine.
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public Object eval(final HttpServletRequest request,
return new HashMap<>(Map.of("output", evalResult, "dotJSON", context.get("dotJSON")));
}

@Override
public Object executeFunction(String functionName, String stringReader, Map<String, Object> bindings, final Object... args) {
throw new UnsupportedOperationException("Not supported yet.");
}

}
28 changes: 28 additions & 0 deletions dotCMS/src/main/java/com/dotcms/rendering/js/JsEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,34 @@ public Object eval(final HttpServletRequest request,
}
}

@Override
public Object executeFunction(final String functionName, final String script,
final Map<String, Object> bindings, final Object... args) {
try (Context context = buildContext()) {

context.eval(ENGINE_JS, script);

final Value bindingsValue = context.getBindings(ENGINE_JS);
bindings.entrySet().forEach(entry -> bindingsValue.putMember(entry.getKey(), entry.getValue()));
bindingsValue.putMember("dotlogger", jsDotLogger);
final Value functionValue = context.getBindings(ENGINE_JS).getMember(functionName);

Value result = null;
if (functionValue.canExecute()) {

result = functionValue.execute(args);
}

checkRejected (result);

return asValue(result, new DotJSON());
} catch (final Exception e) {

Logger.error(this, e.getMessage(), e);
throw new JsEngineException(e);
}
}

private Object asValue (final Value eval, final DotJSON dotJSON) {

if (eval.isHostObject()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.dotcms.rendering.js.proxy;

import com.dotmarketing.business.APILocator;
import com.dotmarketing.util.Logger;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.proxy.ProxyArray;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
* View tool to retrieve on the execute function the roles in a lazy approach
* @author jsanca
*/
public class JsRolesFetcherByUser implements Serializable {

private final String userId;

public JsRolesFetcherByUser(final String userId) {
this.userId = userId;
}

@HostAccess.Export
public Object asArray() {
try {
Logger.debug(this, ()-> "Getting the roles of userId: " + userId);
return ProxyArray.fromArray(APILocator.getRoleAPI().loadRolesForUser(
userId).stream().map(JsRole::new).toArray(JsRole[]::new));
} catch (Exception e) {

Logger.error(e.getMessage(), e);
}

return new JsRole [] {};
}

@HostAccess.Export
public Object asList() {
try {
Logger.debug(this, ()-> "Getting the roles of userId");
return JsProxyFactory.createProxy(
APILocator.getRoleAPI().loadRolesForUser(
userId).stream().map(JsRole::new).collect(Collectors.toList()));
} catch (Exception e) {

Logger.error(e.getMessage(), e);
}

return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,45 @@
* ResponseEntityView for List<User> responses.
* Provides proper type information for OpenAPI/Swagger documentation.
*/
public class ResponseEntityListUserView extends ResponseEntityView<List<User>> {
public class ResponseEntityListUserView extends ResponseEntityView<List<UserView>> {

public ResponseEntityListUserView(List<User> entity) {
public ResponseEntityListUserView(List<UserView> entity) {
super(entity);
}

public ResponseEntityListUserView(List<User> entity, Pagination pagination) {
public ResponseEntityListUserView(List<UserView> entity, Pagination pagination) {
super(entity, pagination);
}

public ResponseEntityListUserView(List<User> entity, String... permissions) {
public ResponseEntityListUserView(List<UserView> entity, String... permissions) {
super(entity, permissions);
}

public ResponseEntityListUserView(List<User> entity, Map<String, String> i18nMessagesMap) {
public ResponseEntityListUserView(List<UserView> entity, Map<String, String> i18nMessagesMap) {
super(entity, i18nMessagesMap);
}

public ResponseEntityListUserView(List<User> entity, List<MessageEntity> messages) {
public ResponseEntityListUserView(List<UserView> entity, List<MessageEntity> messages) {
super(entity, messages);
}

public ResponseEntityListUserView(List<User> entity, List<MessageEntity> messages, Map<String, String> i18nMessagesMap) {
public ResponseEntityListUserView(List<UserView> entity, List<MessageEntity> messages, Map<String, String> i18nMessagesMap) {
super(entity, messages, i18nMessagesMap);
}

public ResponseEntityListUserView(List<User> entity, List<ErrorEntity> errors, List<MessageEntity> messages) {
public ResponseEntityListUserView(List<UserView> entity, List<ErrorEntity> errors, List<MessageEntity> messages) {
super(entity, errors, messages);
}

public ResponseEntityListUserView(List<User> entity, List<ErrorEntity> errors, List<MessageEntity> messages, Map<String, String> i18nMessagesMap) {
public ResponseEntityListUserView(List<UserView> entity, List<ErrorEntity> errors, List<MessageEntity> messages, Map<String, String> i18nMessagesMap) {
super(entity, errors, messages, i18nMessagesMap);
}

public ResponseEntityListUserView(List<User> entity, List<ErrorEntity> errors, List<MessageEntity> messages, Map<String, String> i18nMessagesMap, List<String> permissions) {
public ResponseEntityListUserView(List<UserView> entity, List<ErrorEntity> errors, List<MessageEntity> messages, Map<String, String> i18nMessagesMap, List<String> permissions) {
super(entity, errors, messages, i18nMessagesMap, permissions);
}

public ResponseEntityListUserView(List<User> entity, List<ErrorEntity> errors, List<MessageEntity> messages, Map<String, String> i18nMessagesMap, List<String> permissions, Pagination pagination) {
public ResponseEntityListUserView(List<UserView> entity, List<ErrorEntity> errors, List<MessageEntity> messages, Map<String, String> i18nMessagesMap, List<String> permissions, Pagination pagination) {
super(entity, errors, messages, i18nMessagesMap, permissions, pagination);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import com.dotcms.rest.ResponseEntityView;

import java.util.Map;

/**
* Returns a map such as
* userID -> {userId}
* user -> {userMap}
*/
public class ResponseUserMapEntityView extends ResponseEntityView <Map<String, Object>> {
public ResponseUserMapEntityView(Map<String, Object> entity) {
public class ResponseUserEntityView extends ResponseEntityView <UserView> {
public ResponseUserEntityView(UserView entity) {
super(entity);
}
}
Loading
Loading