<commit_before>package no.kantega.publishing.security.action;

import no.kantega.commons.exception.ConfigurationException;
import no.kantega.publishing.common.Aksess;
import no.kantega.publishing.modules.mailsender.MailSender;
import no.kantega.security.api.common.SystemException;
import no.kantega.security.api.identity.Identity;
import no.kantega.security.api.password.ResetPasswordToken;
import no.kantega.security.api.password.ResetPasswordTokenManager;
import no.kantega.security.api.profile.Profile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class RequestPasswordResetAction extends AbstractLoginAction  {
    private String requestResetPasswordView = null;
    private String mailSubject;
    private String mailFrom;
    private String mailTemplate;

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Identity identity = getIdentityFromRequest(request);
        if (identity != null) {
            return sendPasswordRequest(request, response);
        } else {
            Map<String, Object> model = new HashMap<String, Object>();
            model.put("loginLayout", getLoginLayout());
            return new ModelAndView(requestResetPasswordView, model);
        }
    }

    private ModelAndView sendPasswordRequest(HttpServletRequest request, HttpServletResponse response) throws SystemException, ConfigurationException {
        Identity identity = getIdentityFromRequest(request);

        Profile userProfile = getProfileManager().getProfileForUser(identity);

        Map<String, Object> model = new HashMap<String, Object>();
        model.put("loginLayout", getLoginLayout());
        
        if (userProfile == null) {
            model.put("error", "aksess.resetpassword.no-profile");
            return new ModelAndView(requestResetPasswordView, model);
        } else if (userProfile.getEmail() == null || userProfile.getEmail().indexOf("@") == -1) {
            model.put("error", "aksess.resetpassword.no-email");
            return new ModelAndView(requestResetPasswordView, model);
        }

        Date expireDate = getExpireDate();

        ResetPasswordTokenManager tokenManager = getResetPasswordTokenManager();
        ResetPasswordToken token = tokenManager.generateResetPasswordToken(identity, expireDate);

        Map<String, Object> mailParam = new HashMap<String, Object>();
        String url = Aksess.getApplicationUrl();
        url += "/ResetPassword.action";<commit_msg>AP-982<commit_after>package no.kantega.publishing.security.action;

import no.kantega.commons.exception.ConfigurationException;
import no.kantega.commons.util.URLHelper;
import no.kantega.publishing.common.Aksess;
import no.kantega.publishing.modules.mailsender.MailSender;
import no.kantega.security.api.common.SystemException;
import no.kantega.security.api.identity.Identity;
import no.kantega.security.api.password.ResetPasswordToken;
import no.kantega.security.api.password.ResetPasswordTokenManager;
import no.kantega.security.api.profile.Profile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class RequestPasswordResetAction extends AbstractLoginAction  {
    private String requestResetPasswordView = null;
    private String mailSubject;
    private String mailFrom;
    private String mailTemplate;

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Identity identity = getIdentityFromRequest(request);
        if (identity != null) {
            return sendPasswordRequest(request, response);
        } else {
            Map<String, Object> model = new HashMap<String, Object>();
            model.put("loginLayout", getLoginLayout());
            return new ModelAndView(requestResetPasswordView, model);
        }
    }

    private ModelAndView sendPasswordRequest(HttpServletRequest request, HttpServletResponse response) throws SystemException, ConfigurationException {
        Identity identity = getIdentityFromRequest(request);

        Profile userProfile = getProfileManager().getProfileForUser(identity);

        Map<String, Object> model = new HashMap<String, Object>();
        model.put("loginLayout", getLoginLayout());
        
        if (userProfile == null) {
            model.put("error", "aksess.resetpassword.no-profile");
            return new ModelAndView(requestResetPasswordView, model);
        } else if (userProfile.getEmail() == null || userProfile.getEmail().indexOf("@") == -1) {
            model.put("error", "aksess.resetpassword.no-email");
            return new ModelAndView(requestResetPasswordView, model);
        }

        Date expireDate = getExpireDate();

        ResetPasswordTokenManager tokenManager = getResetPasswordTokenManager();
        ResetPasswordToken token = tokenManager.generateResetPasswordToken(identity, expireDate);

        Map<String, Object> mailParam = new HashMap<String, Object>();
        String url = URLHelper.getServerURL(request) + request.getContextPath();
        url += "/ResetPassword.action";
{'fraction_non_alphanumeric': 0.09706587931352648, 'fraction_numerical': 0.0009226794611551947, 'mean_word_length': 4.070159027128157, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6821104', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1358, 'n_words': 288}