Search in sources :

Example 1 with ActionResult

use of com.cognifide.cq.cqsm.api.actions.ActionResult in project APM by Cognifide.

the class ScriptManagerImpl method execute.

private Progress execute(Script script, final Mode mode, Map<String, String> customDefinitions, ResourceResolver resolver) throws ExecutionException, RepositoryException {
    if (script == null) {
        throw new ExecutionException("Script is not specified");
    }
    if (mode == null) {
        throw new ExecutionException("Execution mode is not specified");
    }
    final String path = script.getPath();
    actionFactory.update();
    LOG.info(String.format("Script execution started: %s [%s]", path, mode));
    Progress progress = new ProgressImpl(resolver.getUserID());
    final List<ActionDescriptor> descriptors = parseAllDescriptors(script, customDefinitions, resolver);
    final ActionExecutor actionExecutor = createExecutor(mode, resolver);
    final Context context = actionExecutor.getContext();
    final SessionSavingPolicy savingPolicy = context.getSavingPolicy();
    eventManager.trigger(Event.BEFORE_EXECUTE, script, mode, progress);
    for (ActionDescriptor descriptor : descriptors) {
        ActionResult result = actionExecutor.execute(descriptor);
        progress.addEntry(descriptor, result);
        if ((Status.ERROR == result.getStatus()) && (Mode.DRY_RUN != mode)) {
            eventManager.trigger(Event.AFTER_EXECUTE, script, mode, progress);
            return progress;
        }
        savingPolicy.save(context.getSession(), SessionSavingMode.EVERY_ACTION);
    }
    savingPolicy.save(context.getSession(), SessionSavingMode.SINGLE);
    eventManager.trigger(Event.AFTER_EXECUTE, script, mode, progress);
    return progress;
}
Also used : Context(com.cognifide.cq.cqsm.api.executors.Context) ProgressImpl(com.cognifide.cq.cqsm.core.progress.ProgressImpl) ActionExecutor(com.cognifide.cq.cqsm.core.actions.executor.ActionExecutor) Progress(com.cognifide.cq.cqsm.api.logger.Progress) ActionResult(com.cognifide.cq.cqsm.api.actions.ActionResult) ActionDescriptor(com.cognifide.cq.cqsm.api.actions.ActionDescriptor) SessionSavingPolicy(com.cognifide.cq.cqsm.core.sessions.SessionSavingPolicy) ExecutionException(com.cognifide.cq.cqsm.api.exceptions.ExecutionException)

Example 2 with ActionResult

use of com.cognifide.cq.cqsm.api.actions.ActionResult in project APM by Cognifide.

the class AddToGroup method process.

private ActionResult process(final Context context, boolean execute) {
    ActionResult actionResult = new ActionResult();
    List<String> errors = new ArrayList<>();
    Authorizable authorizable = null;
    try {
        authorizable = context.getCurrentAuthorizable();
        actionResult.setAuthorizable(authorizable.getID());
    } catch (RepositoryException | ActionExecutionException e) {
        actionResult.logError(MessagingUtils.createMessage(e));
        return actionResult;
    }
    for (String id : groupIds) {
        try {
            Group group = AuthorizablesUtils.getGroup(context, id);
            if (authorizable.isGroup()) {
                ActionUtils.checkCyclicRelations(group, (Group) authorizable);
            }
            LOGGER.info(String.format("Adding Authorizable with id = %s to group with id = %s", authorizable.getID(), group.getID()));
            if (execute) {
                group.addMember(authorizable);
            }
            actionResult.logMessage(MessagingUtils.addedToGroup(authorizable.getID(), id));
        } catch (RepositoryException | ActionExecutionException e) {
            errors.add(MessagingUtils.createMessage(e));
        }
    }
    if (!errors.isEmpty()) {
        ActionUtils.logErrors(errors, actionResult);
        actionResult.logError("Execution interrupted");
    }
    return actionResult;
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) ActionResult(com.cognifide.cq.cqsm.api.actions.ActionResult) ArrayList(java.util.ArrayList) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) RepositoryException(javax.jcr.RepositoryException) ActionExecutionException(com.cognifide.cq.cqsm.api.exceptions.ActionExecutionException)

Example 3 with ActionResult

use of com.cognifide.cq.cqsm.api.actions.ActionResult in project APM by Cognifide.

the class Allow method process.

private ActionResult process(final Context context, boolean simulate) {
    ActionResult actionResult = new ActionResult();
    try {
        Authorizable authorizable = context.getCurrentAuthorizable();
        actionResult.setAuthorizable(authorizable.getID());
        context.getSession().getNode(path);
        final PermissionActionHelper permissionActionHelper = new PermissionActionHelper(context.getValueFactory(), path, glob, permissions);
        LOGGER.info(String.format("Adding permissions %s for authorizable with id = %s for path = %s %s", permissions.toString(), context.getCurrentAuthorizable().getID(), path, StringUtils.isEmpty(glob) ? "" : ("glob = " + glob)));
        if (simulate) {
            permissionActionHelper.checkPermissions(context.getAccessControlManager());
        } else {
            permissionActionHelper.applyPermissions(context.getAccessControlManager(), authorizable.getPrincipal(), true);
        }
        actionResult.logMessage("Added allow privilege for " + authorizable.getID() + " on " + path);
        if (permissions.contains("MODIFY")) {
            String preparedGlob = "";
            if (!StringUtils.isBlank(glob)) {
                preparedGlob = glob;
                if (StringUtils.endsWith(glob, "*")) {
                    preparedGlob = StringUtils.substring(glob, 0, StringUtils.lastIndexOf(glob, '*'));
                }
            }
            new Allow(path, preparedGlob + "*/jcr:content*", ignoreInexistingPaths, Collections.singletonList("MODIFY_PAGE")).process(context, simulate);
        }
    } catch (final PathNotFoundException e) {
        if (ignoreInexistingPaths) {
            actionResult.logWarning("Path " + path + " not found");
        } else {
            actionResult.logError("Path " + path + " not found");
            return actionResult;
        }
    } catch (RepositoryException | PermissionException | ActionExecutionException e) {
        actionResult.logError(MessagingUtils.createMessage(e));
    }
    return actionResult;
}
Also used : PermissionException(com.cognifide.cq.cqsm.foundation.permissions.exceptions.PermissionException) ActionResult(com.cognifide.cq.cqsm.api.actions.ActionResult) PermissionActionHelper(com.cognifide.cq.cqsm.foundation.permissions.PermissionActionHelper) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) ActionExecutionException(com.cognifide.cq.cqsm.api.exceptions.ActionExecutionException)

Example 4 with ActionResult

use of com.cognifide.cq.cqsm.api.actions.ActionResult in project APM by Cognifide.

the class CheckIncludes method process.

private ActionResult process(final Context context, boolean execute) {
    ActionResult actionResult = new ActionResult();
    Group authorizable = tryGetGroup(context, actionResult);
    if (authorizable == null) {
        return actionResult;
    }
    List<String> errors = new ArrayList<>();
    boolean checkFailed = checkMembers(context, actionResult, authorizable, errors);
    if (execute && checkFailed) {
        actionResult.logError(ActionUtils.ASSERTION_FAILED_MSG);
        return actionResult;
    }
    ActionUtils.logErrors(errors, actionResult);
    return actionResult;
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) ActionResult(com.cognifide.cq.cqsm.api.actions.ActionResult) ArrayList(java.util.ArrayList)

Example 5 with ActionResult

use of com.cognifide.cq.cqsm.api.actions.ActionResult in project APM by Cognifide.

the class CheckNotExists method process.

private ActionResult process(final Context context, boolean execute) {
    ActionResult actionResult = new ActionResult();
    List<String> errors = new ArrayList<>();
    boolean checkFailed = checkAuthorizables(context, actionResult, errors);
    if (execute && checkFailed) {
        actionResult.logError(ActionUtils.ASSERTION_FAILED_MSG);
        return actionResult;
    }
    logErrors(actionResult, errors);
    return actionResult;
}
Also used : ActionResult(com.cognifide.cq.cqsm.api.actions.ActionResult) ArrayList(java.util.ArrayList)

Aggregations

ActionResult (com.cognifide.cq.cqsm.api.actions.ActionResult)30 RepositoryException (javax.jcr.RepositoryException)22 ActionExecutionException (com.cognifide.cq.cqsm.api.exceptions.ActionExecutionException)21 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)16 ArrayList (java.util.ArrayList)10 Group (org.apache.jackrabbit.api.security.user.Group)10 User (org.apache.jackrabbit.api.security.user.User)5 PathNotFoundException (javax.jcr.PathNotFoundException)3 Action (com.cognifide.cq.cqsm.api.actions.Action)2 SessionSavingPolicy (com.cognifide.cq.cqsm.core.sessions.SessionSavingPolicy)2 CompositeActionResult (com.cognifide.cq.cqsm.foundation.actions.CompositeActionResult)2 RemoveFromGroup (com.cognifide.cq.cqsm.foundation.actions.removefromgroup.RemoveFromGroup)2 RemoveUser (com.cognifide.cq.cqsm.foundation.actions.removeuser.RemoveUser)2 PermissionActionHelper (com.cognifide.cq.cqsm.foundation.permissions.PermissionActionHelper)2 PermissionException (com.cognifide.cq.cqsm.foundation.permissions.exceptions.PermissionException)2 ActionDescriptor (com.cognifide.cq.cqsm.api.actions.ActionDescriptor)1 ExecutionException (com.cognifide.cq.cqsm.api.exceptions.ExecutionException)1 Context (com.cognifide.cq.cqsm.api.executors.Context)1 Progress (com.cognifide.cq.cqsm.api.logger.Progress)1 ActionExecutor (com.cognifide.cq.cqsm.core.actions.executor.ActionExecutor)1