SysRoleController.java 6.6 KB
Newer Older
RuoYi's avatar
RuoYi committed
1
package com.ruoyi.web.controller.system;
RuoYi's avatar
RuoYi committed
2 3 4 5

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
RuoYi's avatar
RuoYi committed
6
import org.springframework.validation.annotation.Validated;
RuoYi's avatar
RuoYi committed
7 8 9 10 11 12 13 14
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
RuoYi's avatar
RuoYi committed
15
import com.ruoyi.common.annotation.Log;
RuoYi's avatar
RuoYi committed
16
import com.ruoyi.common.constant.UserConstants;
RuoYi's avatar
RuoYi committed
17 18 19
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
20
import com.ruoyi.common.core.domain.model.LoginUser;
RuoYi's avatar
RuoYi committed
21 22
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
RuoYi's avatar
RuoYi committed
23
import com.ruoyi.common.utils.SecurityUtils;
24 25
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
RuoYi's avatar
RuoYi committed
26
import com.ruoyi.common.utils.poi.ExcelUtil;
27 28
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;
RuoYi's avatar
RuoYi committed
29
import com.ruoyi.system.service.ISysRoleService;
30
import com.ruoyi.system.service.ISysUserService;
RuoYi's avatar
RuoYi committed
31 32 33 34 35 36 37 38 39 40 41 42 43

/**
 * 角色信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController
{
    @Autowired
    private ISysRoleService roleService;

44 45 46 47 48 49 50 51 52
    @Autowired
    private TokenService tokenService;
    
    @Autowired
    private SysPermissionService permissionService;
    
    @Autowired
    private ISysUserService userService;

RuoYi's avatar
RuoYi committed
53 54 55 56 57 58 59 60 61
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRole role)
    {
        startPage();
        List<SysRole> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }

RuoYi's avatar
RuoYi committed
62 63 64 65 66 67 68 69 70 71
    @Log(title = "角色管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:role:export')")
    @GetMapping("/export")
    public AjaxResult export(SysRole role)
    {
        List<SysRole> list = roleService.selectRoleList(role);
        ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
        return util.exportExcel(list, "角色数据");
    }

RuoYi's avatar
RuoYi committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    /**
     * 根据角色编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping(value = "/{roleId}")
    public AjaxResult getInfo(@PathVariable Long roleId)
    {
        return AjaxResult.success(roleService.selectRoleById(roleId));
    }

    /**
     * 新增角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
RuoYi's avatar
RuoYi committed
88
    public AjaxResult add(@Validated @RequestBody SysRole role)
RuoYi's avatar
RuoYi committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    {
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
        {
            return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
        {
            return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
        }
        role.setCreateBy(SecurityUtils.getUsername());
        return toAjax(roleService.insertRole(role));

    }

    /**
     * 修改保存角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
RuoYi's avatar
RuoYi committed
109
    public AjaxResult edit(@Validated @RequestBody SysRole role)
RuoYi's avatar
RuoYi committed
110 111 112 113 114 115 116 117 118 119 120
    {
        roleService.checkRoleAllowed(role);
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
        {
            return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
        {
            return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
        }
        role.setUpdateBy(SecurityUtils.getUsername());
121 122 123 124 125 126 127 128 129 130 131 132 133 134
        
        if (roleService.updateRole(role) > 0)
        {
            // 更新缓存用户权限
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            if (StringUtils.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin())
            {
                loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getUser()));
                loginUser.setUser(userService.selectUserByUserName(loginUser.getUser().getUserName()));
                tokenService.setLoginUser(loginUser);
            }
            return AjaxResult.success();
        }
        return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,请联系管理员");
RuoYi's avatar
RuoYi committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    }

    /**
     * 修改保存数据权限
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/dataScope")
    public AjaxResult dataScope(@RequestBody SysRole role)
    {
        roleService.checkRoleAllowed(role);
        return toAjax(roleService.authDataScope(role));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysRole role)
    {
        roleService.checkRoleAllowed(role);
        role.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(roleService.updateRoleStatus(role));
    }

    /**
RuoYi's avatar
RuoYi committed
163
     * 删除角色
RuoYi's avatar
RuoYi committed
164 165 166
     */
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
RuoYi's avatar
RuoYi committed
167 168
    @DeleteMapping("/{roleIds}")
    public AjaxResult remove(@PathVariable Long[] roleIds)
RuoYi's avatar
RuoYi committed
169
    {
RuoYi's avatar
RuoYi committed
170
        return toAjax(roleService.deleteRoleByIds(roleIds));
RuoYi's avatar
RuoYi committed
171 172 173 174 175 176 177 178 179 180 181
    }

    /**
     * 获取角色选择框列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        return AjaxResult.success(roleService.selectRoleAll());
    }
RuoYi's avatar
RuoYi committed
182
}