Coverage Report - org.apache.shiro.samples.sprhib.web.ManageUsersController
 
Classes in this File Line Coverage Branch Coverage Complexity
ManageUsersController
0%
0/21
0%
0/4
1.4
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.shiro.samples.sprhib.web;
 20  
 
 21  
 import org.apache.shiro.authz.annotation.RequiresPermissions;
 22  
 import org.apache.shiro.samples.sprhib.model.User;
 23  
 import org.apache.shiro.samples.sprhib.service.UserService;
 24  
 import org.springframework.beans.factory.annotation.Autowired;
 25  
 import org.springframework.stereotype.Controller;
 26  
 import org.springframework.ui.Model;
 27  
 import org.springframework.util.Assert;
 28  
 import org.springframework.validation.BindingResult;
 29  
 import org.springframework.web.bind.annotation.ModelAttribute;
 30  
 import org.springframework.web.bind.annotation.RequestMapping;
 31  
 import org.springframework.web.bind.annotation.RequestMethod;
 32  
 import org.springframework.web.bind.annotation.RequestParam;
 33  
 
 34  
 /**
 35  
  * Web MVC controller that handles operations related to managing users, such as editing them and deleting them. 
 36  
  */
 37  
 @Controller
 38  0
 public class ManageUsersController {
 39  
 
 40  0
     private EditUserValidator editUserValidator = new EditUserValidator();
 41  
 
 42  
     private UserService userService;
 43  
 
 44  
     @Autowired
 45  
     public void setUserService(UserService userService) {
 46  0
         this.userService = userService;
 47  0
     }
 48  
 
 49  
     @RequestMapping("/manageUsers")
 50  
     @RequiresPermissions("user:manage")
 51  
     public void manageUsers(Model model) {
 52  0
         model.addAttribute("users", userService.getAllUsers());
 53  0
     }
 54  
 
 55  
     @RequestMapping(value="/editUser",method= RequestMethod.GET)
 56  
     @RequiresPermissions("user:edit")
 57  
     public String showEditUserForm(Model model, @RequestParam Long userId, @ModelAttribute EditUserCommand command) {
 58  
 
 59  0
         User user = userService.getUser( userId );
 60  0
         command.setUserId(userId);
 61  0
         command.setUsername(user.getUsername());
 62  0
         command.setEmail(user.getEmail());
 63  0
         return "editUser";
 64  
     }
 65  
 
 66  
     @RequestMapping(value="/editUser",method= RequestMethod.POST)
 67  
     @RequiresPermissions("user:edit")
 68  
     public String editUser(Model model, @RequestParam Long userId, @ModelAttribute EditUserCommand command, BindingResult errors) {
 69  0
         editUserValidator.validate( command, errors );
 70  
 
 71  0
         if( errors.hasErrors() ) {
 72  0
             return "editUser";
 73  
         }
 74  
 
 75  0
         User user = userService.getUser( userId );
 76  0
         command.updateUser( user );
 77  
 
 78  0
         userService.updateUser( user );
 79  
 
 80  0
         return "redirect:/s/manageUsers";
 81  
     }
 82  
 
 83  
     @RequestMapping("/deleteUser")
 84  
     @RequiresPermissions("user:delete")
 85  
     public String deleteUser(@RequestParam Long userId) {
 86  0
         Assert.isTrue( userId != 1, "Cannot delete admin user" );
 87  0
         userService.deleteUser( userId );
 88  0
         return "redirect:/s/manageUsers";
 89  
     }
 90  
 
 91  
 }