com.github.rexsheng.springboot.faster.system.role.adapter.RoleController Maven / Gradle / Ivy
The newest version!
package com.github.rexsheng.springboot.faster.system.role.adapter;
import com.github.rexsheng.springboot.faster.common.constant.CommonConstant;
import com.github.rexsheng.springboot.faster.common.domain.Result;
import com.github.rexsheng.springboot.faster.logging.RequestLog;
import com.github.rexsheng.springboot.faster.request.repeat.annotation.RequestRepeat;
import com.github.rexsheng.springboot.faster.system.post.application.dto.ValidateRoleCodeRequest;
import com.github.rexsheng.springboot.faster.system.role.application.RoleService;
import com.github.rexsheng.springboot.faster.system.role.application.dto.AddRoleRequest;
import com.github.rexsheng.springboot.faster.system.role.application.dto.ChangeRoleAuthRequest;
import com.github.rexsheng.springboot.faster.system.role.application.dto.QueryRoleRequest;
import com.github.rexsheng.springboot.faster.system.role.application.dto.UpdateRoleRequest;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
@RestController
@RequestMapping("/sys/role")
@Tag(name = "RoleController",description = "角色管理")
@ConditionalOnProperty(prefix = "app.module.management",name = "controller",havingValue = "true",matchIfMissing = true)
@ConditionalOnClass(SqlSessionFactoryBean.class)
public class RoleController {
private static Logger logger= LoggerFactory.getLogger(RoleController.class);
@Resource
private RoleService roleService;
@PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).ROLE_LIST)")
@GetMapping
public Result queryRolePagedList(@RequestParam(required = false) String keyword,
@RequestParam Long pageIndex,
@RequestParam Long pageSize){
QueryRoleRequest request=new QueryRoleRequest();
request.setKeyword(keyword);
request.setPageIndex(pageIndex);
request.setPageSize(pageSize);
return Result.success(roleService.pagedList(request));
}
@PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).USER_ASSIGNROLE)")
@GetMapping("/all")
public Result queryRoleList(@RequestParam(required = false) String keyword){
QueryRoleRequest request=new QueryRoleRequest();
request.setKeyword(keyword);
return Result.success(roleService.queryList(request));
}
@PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).ROLE_ADD)")
@PostMapping
@RequestLog("新增角色")
public Result addRole(@RequestBody @Validated AddRoleRequest request){
roleService.add(request);
return Result.success();
}
@PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).ROLE_EDIT)")
@GetMapping("/{id}")
public Result getRole(@PathVariable Integer id){
return Result.success(roleService.get(id));
}
@PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).ROLE_EDIT)")
@PutMapping("/{id}")
@RequestLog("修改角色")
public Result updateRole(@PathVariable Integer id, @RequestBody @Validated UpdateRoleRequest request){
request.setRoleId(id);
roleService.update(request);
return Result.success();
}
@PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).ROLE_EDIT)")
@PutMapping("/{ids}/status/{status}")
@RequestLog("修改角色状态")
public Result changeRoleValid(@PathVariable Integer[] ids,@PathVariable Boolean status){
roleService.updateStatus(UpdateRoleRequest.of(ids,status?CommonConstant.STATUS_RUNNING:CommonConstant.STATUS_STOP));
return Result.success();
}
@PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).ROLE_DEL)")
@DeleteMapping()
@RequestLog("删除角色")
public Result deleteRole(@RequestBody Integer[] ids){
roleService.delete(Arrays.asList(ids));
return Result.success();
}
@PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).ROLE_ADD)" +
" || hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).ROLE_EDIT)")
@PutMapping("/code/valid")
@RequestRepeat(enabled = false)
public Result validRoleCode(@RequestBody @Validated ValidateRoleCodeRequest request){
return Result.success(roleService.validCode(request));
}
@PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).ROLE_ASSIGNAUTH)")
@PutMapping("/{id}/auth")
@RequestLog("修改角色权限")
public Result assignRoleAuth(@PathVariable Integer id,@RequestBody List authIds){
ChangeRoleAuthRequest request=new ChangeRoleAuthRequest(id,authIds);
roleService.changeAuth(request);
return Result.success();
}
@PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).ROLE_ASSIGNAUTH)")
@GetMapping("/{id}/auth")
public Result getRoleAuth(@PathVariable Integer id){
return Result.success(roleService.getAuths(id));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy