org.xinta.eazycode.components.shiro.web.controller.ManageUsersController.java Source code

Java tutorial

Introduction

Here is the source code for org.xinta.eazycode.components.shiro.web.controller.ManageUsersController.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.xinta.eazycode.components.shiro.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.xinta.eazycode.components.shiro.model.User;
import org.xinta.eazycode.components.shiro.service.UserService;
import org.xinta.eazycode.components.shiro.web.validator.EditUserValidator;
import org.xinta.eazycode.components.shiro.web.vo.EditUserVO;

/**
 * Web MVC controller that handles operations related to managing users, such as editing them and deleting them. 
 */
@Controller
public class ManageUsersController {

    private EditUserValidator editUserValidator = new EditUserValidator();

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/manageUsers")
    //@RequiresPermissions("user:manage")
    public void manageUsers(Model model, HttpServletRequest request) {

        model.addAttribute("users", userService.getAllUsers());
    }

    @RequestMapping(value = "/editUser", method = RequestMethod.GET)
    //@RequiresPermissions("user:edit")
    public String showEditUserForm(Model model, @RequestParam Long userId, @ModelAttribute EditUserVO command) {

        User user = userService.getUser(userId);
        command.setUserId(userId);
        command.setLoginName(user.getLoginName());
        command.setEmail(user.getEmail());
        return "editUser";
    }

    @RequestMapping(value = "/editUser", method = RequestMethod.POST)
    // @RequiresPermissions("user:edit")
    public String editUser(Model model, @RequestParam Long userId, @ModelAttribute EditUserVO command,
            BindingResult errors) {
        editUserValidator.validate(command, errors);

        if (errors.hasErrors()) {
            return "editUser";
        }

        User user = userService.getUser(userId);
        command.updateUser(user);

        userService.updateUser(user);

        return "redirect:/manageUsers.do";
    }

    @RequestMapping("/deleteUser")
    // @RequiresPermissions("user:delete")
    public String deleteUser(@RequestParam Long userId) {
        Assert.isTrue(userId != 1, "Cannot delete admin user");
        userService.deleteUser(userId);
        return "redirect:/manageUsers.do";
    }

}