com.ssrs.platform.controller.RoleController Maven / Gradle / Ivy
package com.ssrs.platform.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ssrs.framework.Current;
import com.ssrs.framework.PrivilegeModel;
import com.ssrs.framework.cache.FrameworkCacheManager;
import com.ssrs.framework.extend.ExtendManager;
import com.ssrs.framework.security.annotation.Priv;
import com.ssrs.framework.web.ApiResponses;
import com.ssrs.framework.web.BaseController;
import com.ssrs.platform.bl.PrivBL;
import com.ssrs.platform.model.entity.Privilege;
import com.ssrs.platform.model.entity.Role;
import com.ssrs.platform.model.entity.UserRole;
import com.ssrs.platform.model.parm.RoleParm;
import com.ssrs.platform.model.query.RoleQuery;
import com.ssrs.platform.point.AfterRoleAddPoint;
import com.ssrs.platform.point.AfterRoleDeletePoint;
import com.ssrs.platform.point.AfterRoleModifyPoint;
import com.ssrs.platform.priv.RoleManagerPriv;
import com.ssrs.platform.service.IPrivilegeService;
import com.ssrs.platform.service.IRoleService;
import com.ssrs.platform.service.IUserRoleService;
import com.ssrs.platform.util.Page;
import com.ssrs.platform.util.PlatformCache;
import com.ssrs.platform.util.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
* 角色定义表 前端控制器
*
*
* @author ssrs
* @since 2020-04-04
*/
@RestController
@RequestMapping("/api/role")
public class RoleController extends BaseController {
@Autowired
private IRoleService roleService;
@Autowired
private IPrivilegeService privilegeService;
@Autowired
private IUserRoleService userRoleService;
@Priv
@GetMapping
public ApiResponses list(@RequestParam Map params) {
String branchInnercode = (String) params.get("branchInnercode");
String roleName = (String) params.get("roleName");
LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery();
if (!Current.getUser().isBranchAdministrator()) {
if (StrUtil.isEmpty(branchInnercode)) {
lambdaQueryWrapper.likeRight(Role::getBranchInnercode, Current.getUser().getBranchInnerCode());
} else {
lambdaQueryWrapper.likeRight(Role::getBranchInnercode, branchInnercode);
}
} else {
if (StrUtil.isNotEmpty(branchInnercode)) {
lambdaQueryWrapper.likeRight(Role::getBranchInnercode, branchInnercode);
}
}
lambdaQueryWrapper.like(StrUtil.isNotEmpty(roleName), Role::getRoleName, roleName);
lambdaQueryWrapper.orderByAsc(Role::getBranchInnercode, Role::getCreateTime);
IPage ipage = roleService.page(new Query().getPage(params), lambdaQueryWrapper);
Page page = new Page(ipage);
List data = (List) page.getData();
if (CollUtil.isEmpty(data)) {
return success(page);
}
List tmpData = new ArrayList<>(data.size());
for (Role role : data) {
RoleQuery roleQuery = BeanUtil.toBean(role, RoleQuery.class);
roleQuery.setBranchName(PlatformCache.getBranch(roleQuery.getBranchInnercode()).getName());
tmpData.add(roleQuery);
}
page.setData(tmpData);
return success(page);
}
@Priv(RoleManagerPriv.Add)
@PostMapping
@Transactional(rollbackFor = Exception.class)
public ApiResponses create(@Validated(RoleParm.Create.class) RoleParm roleParm) {
Role role = roleParm.convert(Role.class);
Role exitRole = roleService.getById(role.getRoleCode());
if (exitRole != null) {
return failure("该角色代码已经存在");
}
roleService.save(role);
Privilege priv = new Privilege();
priv.setOwnerType(PrivilegeModel.OwnerType_Role);
priv.setOwner(role.getRoleCode());
privilegeService.save(priv);
// 缓存角色信息
FrameworkCacheManager.set(PlatformCache.ProviderID, PlatformCache.Type_Role, role.getRoleCode(), role);
// 角色添加后的扩展点
ExtendManager.invoke(AfterRoleAddPoint.ID, new Object[] { role, priv });
return success("添加成功");
}
@Priv(RoleManagerPriv.Edit)
@PutMapping("/{roleCode}")
public ApiResponses update(@PathVariable String roleCode, @Validated(RoleParm.Update.class) RoleParm roleParm) {
Role role = roleParm.convert(Role.class);
Role exitRole = roleService.getById(roleCode);
if (exitRole == null) {
return failure("没有找到角色");
}
roleService.updateById(role);
FrameworkCacheManager.set(PlatformCache.ProviderID, PlatformCache.Type_Role, role.getRoleCode(), role);
// 角色修改后的扩展点
ExtendManager.invoke(AfterRoleModifyPoint.ID, new Object[] { role , PrivBL.getRolePriv(role.getRoleCode())});
return success("修改成功");
}
@Priv(RoleManagerPriv.Delete)
@DeleteMapping("/{id}")
@Transactional(rollbackFor = Exception.class)
public ApiResponses delete(@PathVariable String id) {
Role role = roleService.getById(id);
PrivBL.assertBranch(role.getBranchInnercode());
// 删除角色
roleService.removeById(id);
// 删除角色与用户的关系
userRoleService.remove(Wrappers.lambdaQuery().eq(UserRole::getRoleCode, id));
//删除角色的权限
privilegeService.remove(Wrappers.lambdaQuery().eq(Privilege::getOwnerType, PrivilegeModel.OwnerType_Role).eq(Privilege::getOwner, id));
// 删除缓存
PlatformCache.removeRole(role.getRoleCode());
// 删除角色后的扩展点
ExtendManager.invoke(AfterRoleDeletePoint.ID, new Object[] { role });
return success("删除成功");
}
}