Search in sources :

Example 1 with RequestContext

use of org.craftercms.studio.api.v1.service.workflow.context.RequestContext in project studio by craftercms.

the class UserServiceImpl method sendForgotPasswordEmail.

private void sendForgotPasswordEmail(String emailAddress, String token) {
    try {
        Template emailTemplate = freeMarkerConfig.getObject().getConfiguration().getTemplate(studioConfiguration.getProperty(SECURITY_FORGOT_PASSWORD_EMAIL_TEMPLATE));
        Writer out = new StringWriter();
        Map<String, Object> model = new HashMap<String, Object>();
        RequestContext context = RequestContext.getCurrent();
        HttpServletRequest request = context.getRequest();
        String authoringUrl = request.getRequestURL().toString().replace(request.getPathInfo(), "");
        String serviceUrl = studioConfiguration.getProperty(SECURITY_RESET_PASSWORD_SERVICE_URL);
        model.put("authoringUrl", authoringUrl);
        model.put("serviceUrl", serviceUrl);
        model.put("token", token);
        if (emailTemplate != null) {
            emailTemplate.process(model, out);
        }
        MimeMessage mimeMessage = emailService.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        messageHelper.setFrom(studioConfiguration.getProperty(MAIL_FROM_DEFAULT));
        messageHelper.setTo(emailAddress);
        messageHelper.setSubject(studioConfiguration.getProperty(SECURITY_FORGOT_PASSWORD_MESSAGE_SUBJECT));
        messageHelper.setText(out.toString(), true);
        logger.info("Sending password recovery message to " + emailAddress);
        if (isAuthenticatedSMTP()) {
            emailService.send(mimeMessage);
        } else {
            emailServiceNoAuth.send(mimeMessage);
        }
        logger.info("Password recovery message successfully sent to " + emailAddress);
    } catch (Exception e) {
        logger.error("Failed to send password recovery message to " + emailAddress, e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) RequestContext(org.craftercms.commons.http.RequestContext) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Writer(java.io.Writer) StringWriter(java.io.StringWriter) MessagingException(javax.mail.MessagingException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PasswordDoesNotMatchException(org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException) UserAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException) TemplateException(freemarker.template.TemplateException) EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) GroupNotFoundException(org.craftercms.studio.api.v1.exception.security.GroupNotFoundException) UserExternallyManagedException(org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException) IOException(java.io.IOException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) CryptoException(org.craftercms.commons.crypto.CryptoException) PermissionException(org.craftercms.commons.security.exception.PermissionException) AuthenticationException(org.craftercms.studio.api.v1.exception.security.AuthenticationException) Template(freemarker.template.Template)

Example 2 with RequestContext

use of org.craftercms.studio.api.v1.service.workflow.context.RequestContext in project studio by craftercms.

the class DebugUtils method addDebugStack.

public static void addDebugStack(Logger logger) {
    if (logger.getLevel().equals(Logger.LEVEL_DEBUG)) {
        Thread thread = Thread.currentThread();
        String threadName = thread.getName();
        logger.debug("Thread: " + threadName);
        StackTraceElement[] stackTraceElements = thread.getStackTrace();
        StringBuilder sbStack = new StringBuilder();
        int stackSize = (20 < stackTraceElements.length - 2) ? 20 : stackTraceElements.length;
        for (int i = 2; i < stackSize + 2; i++) {
            sbStack.append("\n\t").append(stackTraceElements[i].toString());
        }
        RequestContext context = RequestContext.getCurrent();
        CronJobContext cronJobContext = CronJobContext.getCurrent();
        if (context != null) {
            HttpServletRequest request = context.getRequest();
            String url = request.getRequestURI() + "?" + request.getQueryString();
            logger.debug("Http request: " + url);
        } else if (cronJobContext != null) {
            logger.debug("Cron Job");
        }
        logger.debug("Stack trace (depth 20): " + sbStack.toString());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CronJobContext(org.craftercms.studio.api.v1.job.CronJobContext) RequestContext(org.craftercms.commons.http.RequestContext)

Example 3 with RequestContext

use of org.craftercms.studio.api.v1.service.workflow.context.RequestContext in project studio by craftercms.

the class WorkflowServiceImpl method submitForApproval.

@SuppressWarnings("unchecked")
protected ResultTO submitForApproval(final String site, String submittedBy, final String request, final boolean delete) throws ServiceLayerException {
    RequestContext requestContext = RequestContextBuilder.buildSubmitContext(site, submittedBy);
    ResultTO result = new ResultTO();
    try {
        SimpleDateFormat format = new SimpleDateFormat(StudioConstants.DATE_PATTERN_WORKFLOW_WITH_TZ);
        JSONObject requestObject = JSONObject.fromObject(request);
        JSONArray items = requestObject.getJSONArray(JSON_KEY_ITEMS);
        int length = items.size();
        if (length > 0) {
            for (int index = 0; index < length; index++) {
                objectStateService.setSystemProcessing(site, items.optString(index), true);
            }
        }
        boolean isNow = (requestObject.containsKey(JSON_KEY_SCHEDULE)) ? StringUtils.equalsIgnoreCase(requestObject.getString(JSON_KEY_SCHEDULE), JSON_KEY_IS_NOW) : false;
        ZonedDateTime scheduledDate = null;
        if (!isNow) {
            scheduledDate = (requestObject.containsKey(JSON_KEY_SCHEDULED_DATE)) ? getScheduledDate(site, format, requestObject.getString(JSON_KEY_SCHEDULED_DATE)) : null;
        }
        boolean sendEmail = (requestObject.containsKey(JSON_KEY_SEND_EMAIL)) ? requestObject.getBoolean(JSON_KEY_SEND_EMAIL) : false;
        String environment = (requestObject != null && requestObject.containsKey(JSON_KEY_ENVIRONMENT)) ? requestObject.getString(JSON_KEY_ENVIRONMENT) : null;
        String submissionComment = (requestObject != null && requestObject.containsKey(JSON_KEY_SUBMISSION_COMMENT)) ? requestObject.getString(JSON_KEY_SUBMISSION_COMMENT) : null;
        // TODO: check scheduled date to make sure it is not null when isNow
        // = true and also it is not past
        String schDate = null;
        if (requestObject.containsKey(JSON_KEY_SCHEDULED_DATE)) {
            schDate = requestObject.getString(JSON_KEY_SCHEDULED_DATE);
        }
        if (length > 0) {
            List<DmDependencyTO> submittedItems = new ArrayList<DmDependencyTO>();
            for (int index = 0; index < length; index++) {
                String stringItem = items.optString(index);
                DmDependencyTO submittedItem = getSubmittedItem(site, stringItem, format, schDate, null);
                String user = submittedBy;
                submittedItems.add(submittedItem);
                if (delete) {
                    submittedItem.setSubmittedForDeletion(true);
                }
            }
            submittedItems.addAll(addDependenciesForSubmitForApproval(site, submittedItems, format, schDate));
            List<String> submittedPaths = new ArrayList<String>();
            for (DmDependencyTO goLiveItem : submittedItems) {
                submittedPaths.add(goLiveItem.getUri());
                objectStateService.setSystemProcessing(site, goLiveItem.getUri(), true);
                DependencyRules rule = new DependencyRules(site);
                rule.setObjectStateService(objectStateService);
                rule.setContentService(contentService);
                Set<DmDependencyTO> depSet = rule.applySubmitRule(goLiveItem);
                for (DmDependencyTO dep : depSet) {
                    submittedPaths.add(dep.getUri());
                    objectStateService.setSystemProcessing(site, dep.getUri(), true);
                }
            }
            List<DmError> errors = submitToGoLive(submittedItems, scheduledDate, sendEmail, delete, requestContext, submissionComment, environment);
            SiteFeed siteFeed = siteService.getSite(site);
            AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
            auditLog.setOperation(OPERATION_REQUEST_PUBLISH);
            auditLog.setActorId(submittedBy);
            auditLog.setSiteId(siteFeed.getId());
            auditLog.setPrimaryTargetId(site);
            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
            auditLog.setPrimaryTargetValue(site);
            auditServiceInternal.insertAuditLog(auditLog);
            result.setSuccess(true);
            result.setStatus(200);
            result.setMessage(notificationService.getNotificationMessage(site, NotificationMessageType.CompleteMessages, COMPLETE_SUBMIT_TO_GO_LIVE_MSG, Locale.ENGLISH));
            for (String relativePath : submittedPaths) {
                objectStateService.setSystemProcessing(site, relativePath, false);
            }
        }
    } catch (Exception e) {
        result.setSuccess(false);
        result.setMessage(e.getMessage());
        logger.error("Error while submitting content for approval.", e);
    }
    return result;
}
Also used : JSONArray(net.sf.json.JSONArray) ArrayList(java.util.ArrayList) DependencyRules(org.craftercms.studio.api.v1.service.dependency.DependencyRules) DmDependencyTO(org.craftercms.studio.api.v1.to.DmDependencyTO) ResultTO(org.craftercms.studio.api.v1.to.ResultTO) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) JSONException(net.sf.json.JSONException) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) JSONObject(net.sf.json.JSONObject) ZonedDateTime(java.time.ZonedDateTime) DmError(org.craftercms.studio.api.v1.to.DmError) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) RequestContext(org.craftercms.studio.api.v1.service.workflow.context.RequestContext) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with RequestContext

use of org.craftercms.studio.api.v1.service.workflow.context.RequestContext in project studio by craftercms.

the class WorkflowServiceImpl method submitToGoLive.

protected List<DmError> submitToGoLive(List<DmDependencyTO> submittedItems, ZonedDateTime scheduledDate, boolean sendEmail, boolean submitForDeletion, RequestContext requestContext, String submissionComment, String environment) throws ServiceLayerException {
    List<DmError> errors = new ArrayList<DmError>();
    String site = requestContext.getSite();
    String submittedBy = requestContext.getUser();
    for (DmDependencyTO submittedItem : submittedItems) {
        try {
            DependencyRules rule = new DependencyRules(site);
            rule.setContentService(contentService);
            rule.setObjectStateService(objectStateService);
            submitThisAndReferredComponents(submittedItem, site, scheduledDate, sendEmail, submitForDeletion, submittedBy, rule, submissionComment, environment);
            List<DmDependencyTO> children = submittedItem.getChildren();
            if (children != null && !submitForDeletion) {
                for (DmDependencyTO child : children) {
                    if (!child.isReference()) {
                        submitThisAndReferredComponents(child, site, scheduledDate, sendEmail, submitForDeletion, submittedBy, rule, submissionComment, environment);
                    }
                }
            }
        } catch (ContentNotFoundException e) {
            errors.add(new DmError(site, submittedItem.getUri(), e));
        }
    }
    notificationService.notifyApprovesContentSubmission(site, null, getDeploymentPaths(submittedItems), submittedBy, scheduledDate, submitForDeletion, submissionComment, Locale.ENGLISH);
    return errors;
}
Also used : ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) DmError(org.craftercms.studio.api.v1.to.DmError) ArrayList(java.util.ArrayList) DependencyRules(org.craftercms.studio.api.v1.service.dependency.DependencyRules) DmDependencyTO(org.craftercms.studio.api.v1.to.DmDependencyTO)

Example 5 with RequestContext

use of org.craftercms.studio.api.v1.service.workflow.context.RequestContext in project studio by craftercms.

the class SecurityServiceImpl method logout.

@Override
public boolean logout() throws SiteNotFoundException {
    String username = getCurrentUser();
    RequestContext context = RequestContext.getCurrent();
    if (username != null && context != null) {
        HttpServletRequest httpServletRequest = context.getRequest();
        String ipAddress = httpServletRequest.getRemoteAddr();
        SiteFeed siteFeed = siteService.getSite(studioConfiguration.getProperty(CONFIGURATION_GLOBAL_SYSTEM_SITE));
        AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
        auditLog.setOperation(OPERATION_LOGOUT);
        auditLog.setActorId(username);
        auditLog.setSiteId(siteFeed.getId());
        auditLog.setPrimaryTargetId(username);
        auditLog.setPrimaryTargetType(TARGET_TYPE_USER);
        auditLog.setPrimaryTargetValue(username);
        auditServiceInternal.insertAuditLog(auditLog);
        logger.info("User " + username + " logged out from IP: " + ipAddress);
    }
    return true;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) RequestContext(org.craftercms.commons.http.RequestContext) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog)

Aggregations

RequestContext (org.craftercms.commons.http.RequestContext)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)3 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)3 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)3 ArrayList (java.util.ArrayList)2 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)2 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)2 UserAlreadyExistsException (org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException)2 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)2 CronJobContext (org.craftercms.studio.api.v1.job.CronJobContext)2 DependencyRules (org.craftercms.studio.api.v1.service.dependency.DependencyRules)2 DmDependencyTO (org.craftercms.studio.api.v1.to.DmDependencyTO)2 DmError (org.craftercms.studio.api.v1.to.DmError)2 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 SimpleDateFormat (java.text.SimpleDateFormat)1