ru.org.linux.user.LostPasswordController.java Source code

Java tutorial

Introduction

Here is the source code for ru.org.linux.user.LostPasswordController.java

Source

/*
 * Copyright 1998-2012 Linux.org.ru
 *    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.
 */

package ru.org.linux.user;

import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import ru.org.linux.auth.AccessViolationException;
import ru.org.linux.site.BadInputException;
import ru.org.linux.site.Template;
import ru.org.linux.spring.Configuration;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Properties;

@Controller
@RequestMapping(value = "/lostpwd.jsp")
public class LostPasswordController {
    @Autowired
    private UserDao userDao;

    @Autowired
    private Configuration configuration;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView showForm() {
        return new ModelAndView("lostpwd-form");
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView sendPassword(@RequestParam("email") String email, HttpServletRequest request)
            throws Exception {
        Template tmpl = Template.getTemplate(request);

        if (Strings.isNullOrEmpty(email)) {
            throw new BadInputException("email  ");
        }

        User user = userDao.getByEmail(email, true);
        if (user == null) {
            throw new BadInputException(" email  ?!");
        }

        user.checkBlocked();
        user.checkAnonymous();

        if (user.isModerator() && !tmpl.isModeratorSession()) {
            throw new AccessViolationException(
                    "?   ??  ");
        }

        if (!tmpl.isModeratorSession() && !userDao.canResetPassword(user)) {
            throw new BadInputException(
                    "??       !");
        }

        Timestamp now = new Timestamp(System.currentTimeMillis());

        try {
            sendEmail(user, email, now);
            userDao.updateResetDate(user, now);

            return new ModelAndView("action-done", "message",
                    "??  ?? ?     email");
        } catch (AddressException ex) {
            throw new UserErrorException("Incorrect email address");
        }
    }

    private void sendEmail(User user, String email, Timestamp resetDate) throws MessagingException {
        Properties props = new Properties();
        props.put("mail.smtp.host", "localhost");
        Session mailSession = Session.getDefaultInstance(props, null);

        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress("no-reply@linux.org.ru"));

        String resetCode = UserService.getResetCode(configuration.getSecret(), user.getNick(), email, resetDate);

        msg.addRecipient(RecipientType.TO, new InternetAddress(email));
        msg.setSubject("Your password @linux.org.ru");
        msg.setSentDate(new Date());
        msg.setText("?!\n\n"
                + "? ??  ?   ?? http://www.linux.org.ru/reset-password\n\n"
                + "  " + user.getNick() + ",  ?: " + resetCode + "\n\n"
                + "!");

        Transport.send(msg);
    }

    @ExceptionHandler(UserErrorException.class)
    public ModelAndView handleUserError(UserErrorException ex) {
        return new ModelAndView("lostpwd-form", "error", ex.getMessage());
    }
}