|
@@ -0,0 +1,118 @@
|
|
|
+package com.fjhx.tenant.controller.tenant;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
+import com.ruoyi.common.annotation.Log;
|
|
|
+import com.ruoyi.common.annotation.TenantIgnore;
|
|
|
+import com.ruoyi.common.constant.DatasourceConstant;
|
|
|
+import com.ruoyi.common.core.controller.BaseController;
|
|
|
+import com.ruoyi.common.core.domain.entity.SysRole;
|
|
|
+import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
+import com.ruoyi.common.core.domain.model.LoginUser;
|
|
|
+import com.ruoyi.common.core.page.TableDataInfo;
|
|
|
+import com.ruoyi.common.enums.BusinessType;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
+import com.ruoyi.framework.web.service.SysPermissionService;
|
|
|
+import com.ruoyi.framework.web.service.TokenService;
|
|
|
+import com.ruoyi.system.service.ISysRoleService;
|
|
|
+import com.ruoyi.system.service.ISysUserService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@TenantIgnore
|
|
|
+@DS(DatasourceConstant.SLAVE_NAME)
|
|
|
+@RestController
|
|
|
+@RequestMapping("/tenantRole")
|
|
|
+public class RoleTenantController extends BaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysRoleService roleService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysPermissionService permissionService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysUserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TokenService tokenService;
|
|
|
+
|
|
|
+
|
|
|
+ * 角色分页
|
|
|
+ */
|
|
|
+ @GetMapping("/list")
|
|
|
+ public TableDataInfo list(SysRole role) {
|
|
|
+ startPage();
|
|
|
+ List<SysRole> list = roleService.selectRoleList(role);
|
|
|
+ return getDataTable(list);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 新增角色
|
|
|
+ */
|
|
|
+ @Log(title = "角色管理", businessType = BusinessType.INSERT)
|
|
|
+ @PostMapping
|
|
|
+ public void add(@Validated @RequestBody SysRole role) {
|
|
|
+ String tenantId = role.getTenantId();
|
|
|
+ if (ObjectUtil.isEmpty(tenantId)) {
|
|
|
+ throw new ServiceException("租户id不能为空");
|
|
|
+ }
|
|
|
+ if (!roleService.checkRoleNameUnique(role)) {
|
|
|
+ throw new ServiceException("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
|
|
|
+ } else if (!roleService.checkRoleKeyUnique(role)) {
|
|
|
+ throw new ServiceException("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
|
|
|
+ }
|
|
|
+ role.setCreateBy(getUsername());
|
|
|
+ roleService.insertRole(role);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 修改保存角色
|
|
|
+ */
|
|
|
+ @Log(title = "角色管理", businessType = BusinessType.UPDATE)
|
|
|
+ @PutMapping
|
|
|
+ public void edit(@Validated @RequestBody SysRole role) {
|
|
|
+ String tenantId = role.getTenantId();
|
|
|
+ if (ObjectUtil.isEmpty(tenantId)) {
|
|
|
+ throw new ServiceException("租户id不能为空");
|
|
|
+ }
|
|
|
+ roleService.checkRoleAllowed(role);
|
|
|
+ roleService.checkRoleDataScope(role.getRoleId());
|
|
|
+
|
|
|
+ if (!roleService.checkRoleNameUnique(role)) {
|
|
|
+ throw new ServiceException("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
|
|
|
+ } else if (!roleService.checkRoleKeyUnique(role)) {
|
|
|
+ throw new ServiceException("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
|
|
|
+ }
|
|
|
+ role.setUpdateBy(getUsername());
|
|
|
+
|
|
|
+ int num = roleService.updateRole(role);
|
|
|
+ if (num == 0) {
|
|
|
+ throw new ServiceException("修改角色'" + role.getRoleName() + "'失败,请联系管理员");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ LoginUser loginUser = getLoginUser();
|
|
|
+ SysUser user = loginUser.getUser();
|
|
|
+ if (StringUtils.isNotNull(user) && !user.isAdmin()) {
|
|
|
+ loginUser.setPermissions(permissionService.getMenuPermission(user));
|
|
|
+ loginUser.setUser(userService.selectUserByUserName(user.getTenantId(), user.getUserName()));
|
|
|
+ tokenService.setLoginUser(loginUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 删除角色
|
|
|
+ */
|
|
|
+ @Log(title = "角色管理", businessType = BusinessType.DELETE)
|
|
|
+ @DeleteMapping("/{roleIds}")
|
|
|
+ public void remove(@PathVariable Long[] roleIds) {
|
|
|
+ roleService.deleteRoleByIds(roleIds);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|