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

Java tutorial

Introduction

Here is the source code for ru.org.linux.user.UserModificationController.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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
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 org.springframework.web.servlet.view.RedirectView;
import ru.org.linux.auth.AccessViolationException;
import ru.org.linux.comment.CommentService;
import ru.org.linux.comment.DeleteCommentResult;
import ru.org.linux.search.SearchQueueSender;
import ru.org.linux.site.Template;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
public class UserModificationController {
    private static final Log logger = LogFactory.getLog(UserModificationController.class);

    private SearchQueueSender searchQueueSender;
    private UserDao userDao;
    @Autowired
    private CommentService commentService;

    @Autowired
    @Required
    public void setSearchQueueSender(SearchQueueSender searchQueueSender) {
        this.searchQueueSender = searchQueueSender;
    }

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     *   User , ? ? ????  ??,  ?
     * @param request  http ?
     * @return  
     * @throws Exception ?  
     */
    private static User getModerator(HttpServletRequest request) throws Exception {
        Template tmpl = Template.getTemplate(request);
        if (!tmpl.isModeratorSession()) {
            throw new AccessViolationException("Not moderator");
        }
        return tmpl.getCurrentUser();
    }

    /**
     *   ?
     * @param request http ?
     * @param user  
     * @param reason  
     * @return ??  
     * @throws Exception  ?       ?
     * ? 
     */
    @RequestMapping(value = "/usermod.jsp", method = RequestMethod.POST, params = "action=block")
    public ModelAndView blockUser(HttpServletRequest request, @RequestParam("id") User user,
            @RequestParam(value = "reason", required = false) String reason) throws Exception {
        User moderator = getModerator(request);
        if (!user.isBlockable() && !moderator.isAdministrator()) {
            throw new AccessViolationException(
                    "? " + user.getNick() + " ? ");
        }

        if (user.isBlocked()) {
            throw new UserErrorException("  ");
        }

        userDao.blockWithResetPassword(user, moderator, reason);
        logger.info("User " + user.getNick() + " blocked by " + moderator.getNick());
        return redirectToProfile(user);
    }

    /**
     *   ?
     * @param request http ?
     * @param user  
     * @return ??  
     * @throws Exception  ?      ? ? 
     */
    @RequestMapping(value = "/usermod.jsp", method = RequestMethod.POST, params = "action=unblock")
    public ModelAndView unblockUser(HttpServletRequest request, @RequestParam("id") User user) throws Exception {

        User moderator = getModerator(request);
        if (!user.isBlockable() && !moderator.isAdministrator()) {
            throw new AccessViolationException(
                    "? " + user.getNick() + " ? ");
        }
        userDao.unblock(user);
        logger.info("User " + user.getNick() + " unblocked by " + moderator.getNick());
        return redirectToProfile(user);
    }

    private static ModelAndView redirectToProfile(User user) throws UnsupportedEncodingException {
        return new ModelAndView(new RedirectView(getNoCacheLinkToProfile(user)));
    }

    private static String getNoCacheLinkToProfile(User user) throws UnsupportedEncodingException {
        Random random = new Random();
        return "/people/" + URLEncoder.encode(user.getNick(), "UTF-8") + "/profile?nocache=" + random.nextInt();
    }

    /**
     *  ?   ?    ?
     * @param request http ?
     * @param user  
     * @return ??  
     * @throws Exception  ?      ? ? 
     */
    @RequestMapping(value = "/usermod.jsp", method = RequestMethod.POST, params = "action=block-n-delete-comments")
    public ModelAndView blockAndMassiveDeleteCommentUser(HttpServletRequest request, @RequestParam("id") User user,
            @RequestParam(value = "reason", required = false) String reason) throws Exception {
        User moderator = getModerator(request);
        if (!user.isBlockable() && !moderator.isAdministrator()) {
            throw new AccessViolationException(
                    "? " + user.getNick() + " ? ");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("message", "");
        DeleteCommentResult deleteCommentResult = commentService.deleteAllCommentsAndBlock(user, moderator, reason);

        logger.info("User " + user.getNick() + " blocked by " + moderator.getNick());

        params.put("bigMessage", deleteCommentResult.getDeletedCommentIds()); // TODO

        for (int topicId : deleteCommentResult.getDeletedTopicIds()) {
            searchQueueSender.updateMessage(topicId, true);
        }
        searchQueueSender.updateComment(deleteCommentResult.getDeletedCommentIds());

        return new ModelAndView("action-done", params);
    }

    /**
     *  ?  
     * @param request http ?
     * @param user  
     * @return ??  
     * @throws Exception  ?      ? ? ? 
     */
    @RequestMapping(value = "/usermod.jsp", method = RequestMethod.POST, params = "action=toggle_corrector")
    public ModelAndView toggleUserCorrector(HttpServletRequest request, @RequestParam("id") User user)
            throws Exception {
        User moderator = getModerator(request);
        if (user.getScore() < User.CORRECTOR_SCORE) {
            throw new AccessViolationException("? " + user.getNick()
                    + " ? ? ");
        }
        userDao.toggleCorrector(user);
        logger.info("Toggle corrector " + user.getNick() + " by " + moderator.getNick());

        return redirectToProfile(user);
    }

    /**
     * ? ? 
     * @param request http ?
     * @param user   ?? 
     * @return ?  ?? ??
     * @throws Exception    ?? 
     */
    @RequestMapping(value = "/usermod.jsp", method = RequestMethod.POST, params = "action=reset-password")
    public ModelAndView resetPassword(HttpServletRequest request, @RequestParam("id") User user) throws Exception {
        User moderator = getModerator(request);

        if (user.isModerator()) {
            throw new AccessViolationException(
                    " " + user.getNick() + " ? ?? ");
        }

        userDao.resetPassword(user);

        logger.info(
                " " + user.getNick() + " ?  " + moderator.getNick());

        ModelAndView mv = new ModelAndView("action-done");
        mv.getModel().put("link", getNoCacheLinkToProfile(user));
        mv.getModel().put("message", " ?");
        return mv;
    }

    /**
     *  ?    
     * @param request http ?
     * @param user  
     * @return ??  
     * @throws Exception  ?      ?   ??
     */
    @RequestMapping(value = "/usermod.jsp", method = RequestMethod.POST, params = "action=remove_userinfo")
    public ModelAndView removeUserInfo(HttpServletRequest request, @RequestParam("id") User user) throws Exception {
        User moderator = getModerator(request);
        if (user.isModerator()) {
            throw new AccessViolationException(
                    " " + user.getNick() + " ?  ??");
        }
        userDao.removeUserInfo(user);
        logger.info("Clearing " + user.getNick() + " userinfo by " + moderator.getNick());

        return redirectToProfile(user);
    }

    @RequestMapping(value = "/remove-userpic.jsp", method = RequestMethod.POST)
    public ModelAndView removeUserpic(ServletRequest request, @RequestParam("id") User user) throws Exception {
        Template tmpl = Template.getTemplate(request);

        if (!tmpl.isSessionAuthorized()) {
            throw new AccessViolationException("Not autorized");
        }

        User currentUser = tmpl.getCurrentUser();

        if (!currentUser.isModerator() && currentUser.getId() != user.getId()) {
            throw new AccessViolationException("Not permitted");
        }

        if (user.isModerator()) {
            throw new AccessViolationException(
                    " " + user.getNick() + " ?  ");
        }

        if (user.getPhoto() == null) {
            throw new AccessViolationException(
                    " " + user.getNick() + "   ");
        }

        if (userDao.removePhoto(user, currentUser)) {
            logger.info("Clearing " + user.getNick() + " userpic by " + currentUser.getNick());
        } else {
            logger.debug("SKIP Clearing " + user.getNick() + " userpic by " + currentUser.getNick());
        }

        return redirectToProfile(user);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(User.class, new UserIdPropertyEditor(userDao));
    }
}