Search in sources :

Example 1 with UserRole

use of com.luoxiao.model.UserRole in project ssm_shiro_blog by Mandelo.

the class MainController method updateRoles.

/**
 * 修改角色roles,并保存
 *
 * @param userId
 * @return
 */
@ResponseBody
@RequestMapping(value = "/updateRoles", method = RequestMethod.GET)
public UserRoleTemp updateRoles(@RequestParam(value = "userId") Integer userId, @RequestParam(value = "roleStr") String roleStr) {
    UserRoleTemp u = new UserRoleTemp();
    /* System.out.println(roleStr); */
    // 根据用户userId删除所有RoleId
    userRoleService.deleteById(userId);
    // 遍历角色名
    String[] roleNames = roleStr.split(",");
    for (int i = 0; i < roleNames.length; i++) {
        Integer roleId = (roleService.getIdByRoleName(roleNames[i]));
        // 插入新的数据到UserRoleUserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        userRoleService.insert(userRole);
    }
    // 重新获取结果
    User user = userService.selectById(userId);
    List<String> roles = userExtendService.getRoles(user.getUsername());
    u.setId(userId);
    u.setRoles(roles);
    u.setUsername(user.getUsername());
    return u;
}
Also used : User(com.luoxiao.model.User) UserRoleTemp(com.luoxiao.model.UserRoleTemp) UserRole(com.luoxiao.model.UserRole) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UserRole

use of com.luoxiao.model.UserRole in project ssm_shiro_blog by Mandelo.

the class MainController method save.

/**
 * 注册
 *
 * @param user
 * @return
 */
@RequestMapping(value = "/user/regist", method = RequestMethod.POST)
public ModelAndView save(User user) {
    ModelAndView mv = new ModelAndView();
    try {
        user.setStatus("1");
        user.setRegTime(new Date());
        userService.save(user);
        Integer id = user.getId();
        System.out.println(id);
        /* UserRole userRole = userRoleService.selectByUserId(id); */
        UserRole userRole = new UserRole();
        userRole.setUserId(id);
        // 默认注册用户,RoleId即为3
        userRole.setRoleId(3);
        userRoleService.insert(userRole);
        mv.setViewName("success/registSuccess");
    } catch (Exception e) {
        mv.setViewName("error/registError");
        e.printStackTrace();
    }
    mv.addObject("user", userService.selectById(user.getId()));
    return mv;
}
Also used : UserRole(com.luoxiao.model.UserRole) ModelAndView(org.springframework.web.servlet.ModelAndView) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

UserRole (com.luoxiao.model.UserRole)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 User (com.luoxiao.model.User)1 UserRoleTemp (com.luoxiao.model.UserRoleTemp)1 Date (java.util.Date)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1