mirror of
https://github.com/FatttSnake/Pinnacle-OA.git
synced 2026-04-04 22:41:24 +08:00
Refactored ResponseResult, added api documentation for PowerController, RoleController, GroupController and UserController
This commit is contained in:
@@ -83,7 +83,7 @@ public class SecurityConfig {
|
||||
|
||||
// Allow anonymous access
|
||||
.authorizeHttpRequests()
|
||||
.requestMatchers("/login", "/doc.html", "swagger-ui/**", "/webjars/**", "/v3/**")
|
||||
.requestMatchers("/login", "/doc.html", "/swagger-ui/**", "/webjars/**", "/v3/**", "/swagger-ui.html")
|
||||
.anonymous()
|
||||
|
||||
// Authentication required
|
||||
|
||||
@@ -5,7 +5,13 @@ import com.cfive.pinnacle.entity.Group;
|
||||
import com.cfive.pinnacle.entity.common.ResponseCode;
|
||||
import com.cfive.pinnacle.entity.common.ResponseResult;
|
||||
import com.cfive.pinnacle.service.IGroupService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Parameters;
|
||||
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -21,6 +27,7 @@ import java.util.List;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/group")
|
||||
@Tag(name = "用户组", description = "用户组相关接口")
|
||||
public class GroupController {
|
||||
private IGroupService groupService;
|
||||
|
||||
@@ -29,14 +36,18 @@ public class GroupController {
|
||||
this.groupService = groupService;
|
||||
}
|
||||
|
||||
@Operation(summary = "获取所有用户组")
|
||||
@GetMapping
|
||||
public ResponseResult getAllGroup() {
|
||||
@PreAuthorize("hasAnyAuthority('system:group:all', 'system:group:add', 'system:group:delete', 'system:group:modify', 'system:user:add', 'system:user:modify')")
|
||||
public ResponseResult<List<Group>> getAllGroup() {
|
||||
List<Group> groups = groupService.getAllGroup();
|
||||
return ResponseResult.databaseSelectSuccess(groups);
|
||||
}
|
||||
|
||||
@Operation(summary = "添加用户组")
|
||||
@PostMapping
|
||||
public ResponseResult addGroup(@RequestBody Group group) {
|
||||
@PreAuthorize("hasAuthority('system:group:add')")
|
||||
public ResponseResult<Group> addGroup(@RequestBody Group group) {
|
||||
if (!StringUtils.hasText(group.getName())) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_SAVE_ERROR, "Name cannot be empty", null);
|
||||
}
|
||||
@@ -47,8 +58,13 @@ public class GroupController {
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "删除用户组")
|
||||
@Parameters({
|
||||
@Parameter(name = "id", description = "用户组ID", in = ParameterIn.PATH)
|
||||
})
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseResult deleteGroup(@PathVariable Long id) {
|
||||
@PreAuthorize("hasAuthority('system:group:delete')")
|
||||
public ResponseResult<?> deleteGroup(@PathVariable Long id) {
|
||||
LambdaQueryWrapper<Group> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Group::getId, id);
|
||||
if (groupService.remove(wrapper)) {
|
||||
@@ -58,8 +74,10 @@ public class GroupController {
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "修改用户组")
|
||||
@PutMapping
|
||||
public ResponseResult modifyGroup(@RequestBody Group group) {
|
||||
@PreAuthorize("hasAuthority('system:group:modify')")
|
||||
public ResponseResult<Group> modifyGroup(@RequestBody Group group) {
|
||||
if (!StringUtils.hasText(group.getName())) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_UPDATE_ERROR, "Name cannot be empty", null);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,13 @@ import com.cfive.pinnacle.entity.Role;
|
||||
import com.cfive.pinnacle.entity.common.ResponseCode;
|
||||
import com.cfive.pinnacle.entity.common.ResponseResult;
|
||||
import com.cfive.pinnacle.service.IRoleService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Parameters;
|
||||
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -21,6 +27,7 @@ import java.util.List;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/role")
|
||||
@Tag(name = "角色", description = "角色相关接口")
|
||||
public class RoleController {
|
||||
|
||||
private IRoleService roleService;
|
||||
@@ -30,14 +37,18 @@ public class RoleController {
|
||||
this.roleService = roleService;
|
||||
}
|
||||
|
||||
@Operation(summary = "获取所有角色")
|
||||
@GetMapping
|
||||
public ResponseResult getAllRole() {
|
||||
@PreAuthorize("hasAnyAuthority('system:role:all', 'system:role:add', 'system:role:delete', 'system:role:modeify', 'system:group:add', 'system:group:modify', 'system:user:add', 'system:user:modify')")
|
||||
public ResponseResult<List<Role>> getAllRole() {
|
||||
List<Role> roles = roleService.getAllRole();
|
||||
return ResponseResult.databaseSelectSuccess(roles);
|
||||
}
|
||||
|
||||
@Operation(summary = "添加角色")
|
||||
@PostMapping
|
||||
public ResponseResult addRole(@RequestBody Role role) {
|
||||
@PreAuthorize("hasAuthority('system:role:add')")
|
||||
public ResponseResult<Role> addRole(@RequestBody Role role) {
|
||||
if (!StringUtils.hasText(role.getName())) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_SAVE_ERROR, "Name cannot be empty", null);
|
||||
}
|
||||
@@ -48,8 +59,13 @@ public class RoleController {
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "删除角色")
|
||||
@Parameters({
|
||||
@Parameter(name = "id", description = "角色ID", in = ParameterIn.PATH)
|
||||
})
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseResult deleteRole(@PathVariable Long id) {
|
||||
@PreAuthorize("hasAuthority('system:role:delete')")
|
||||
public ResponseResult<?> deleteRole(@PathVariable Long id) {
|
||||
LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Role::getId, id);
|
||||
if (roleService.remove(wrapper)) {
|
||||
@@ -59,8 +75,10 @@ public class RoleController {
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "修改角色")
|
||||
@PutMapping()
|
||||
public ResponseResult modifyRole(@RequestBody Role role) {
|
||||
@PreAuthorize("hasAuthority('system:role:modify')")
|
||||
public ResponseResult<Role> modifyRole(@RequestBody Role role) {
|
||||
if (!StringUtils.hasText(role.getName())) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_UPDATE_ERROR, "Name cannot be empty", null);
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.cfive.pinnacle.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 中间表-角色-用户组 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author FatttSnake
|
||||
* @since 2023-04-30
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/roleGroup")
|
||||
public class RoleGroupController {
|
||||
|
||||
}
|
||||
@@ -31,20 +31,22 @@ public class UserController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@PreAuthorize("hasAuthority('system:user:all')")
|
||||
public ResponseResult getAllUser() {
|
||||
@PreAuthorize("hasAnyAuthority('system:user:all', 'system:user:add', 'system:user:modify')")
|
||||
public ResponseResult<List<User>> getAllUser() {
|
||||
List<User> users = userService.getAllUser();
|
||||
return ResponseResult.databaseSelectSuccess(users);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseResult getUser(@PathVariable Long id) {
|
||||
@PreAuthorize("hasAuthority('system:user:one')")
|
||||
public ResponseResult<User> getUser(@PathVariable Long id) {
|
||||
User user = userService.getUser(id);
|
||||
return ResponseResult.databaseSelectSuccess(user);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseResult addUser(@RequestBody User user) {
|
||||
@PreAuthorize("hasAuthority('system:user:add')")
|
||||
public ResponseResult<User> addUser(@RequestBody User user) {
|
||||
if (!StringUtils.hasText(user.getUsername())) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_SAVE_ERROR, "Username cannot be empty", null);
|
||||
}
|
||||
@@ -59,7 +61,8 @@ public class UserController {
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseResult deleteUser(@PathVariable Long id) {
|
||||
@PreAuthorize("hasAuthority('system:user:delete')")
|
||||
public ResponseResult<?> deleteUser(@PathVariable Long id) {
|
||||
if (id == 1L) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_DELETE_ERROR, "Unable to remove super admin", null);
|
||||
}
|
||||
@@ -73,7 +76,8 @@ public class UserController {
|
||||
}
|
||||
|
||||
@PutMapping()
|
||||
public ResponseResult modifyUser(@RequestBody User user) {
|
||||
@PreAuthorize("hasAuthority('system:user:modify')")
|
||||
public ResponseResult<User> modifyUser(@RequestBody User user) {
|
||||
if (!StringUtils.hasText(user.getUsername())) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_UPDATE_ERROR, "Username cannot be empty", null);
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.cfive.pinnacle.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 中间表-用户-用户组 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author FatttSnake
|
||||
* @since 2023-04-30
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/userGroup")
|
||||
public class UserGroupController {
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.cfive.pinnacle.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 中间表-用户-角色 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author FatttSnake
|
||||
* @since 2023-04-30
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/userRole")
|
||||
public class UserRoleController {
|
||||
|
||||
}
|
||||
@@ -3,7 +3,10 @@ package com.cfive.pinnacle.controller.permission;
|
||||
import com.cfive.pinnacle.entity.common.ResponseResult;
|
||||
import com.cfive.pinnacle.entity.permission.PowerSet;
|
||||
import com.cfive.pinnacle.service.permission.IPowerService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -16,6 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
* @author FatttSnake
|
||||
* @since 2023-04-30
|
||||
*/
|
||||
@Tag(name = "权限", description = "权限相关接口")
|
||||
@RestController
|
||||
@RequestMapping("/power")
|
||||
public class PowerController {
|
||||
@@ -26,8 +30,10 @@ public class PowerController {
|
||||
this.powerService = powerService;
|
||||
}
|
||||
|
||||
@Operation(summary = "获取所有权限")
|
||||
@GetMapping
|
||||
public ResponseResult getAllPower() {
|
||||
@PreAuthorize("hasAnyAuthority('system:role:add', 'system:role:modify')")
|
||||
public ResponseResult<PowerSet> getAllPower() {
|
||||
PowerSet powerSet = powerService.getAllPower();
|
||||
|
||||
return ResponseResult.databaseSelectSuccess(powerSet);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.cfive.pinnacle.entity.common;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@@ -11,64 +12,68 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class ResponseResult implements Serializable {
|
||||
@Schema(title = "ResponseResult",description = "响应结果")
|
||||
public class ResponseResult<T> implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(description = "响应码")
|
||||
private int code;
|
||||
@Schema(description = "响应信息")
|
||||
private String msg;
|
||||
private Object data;
|
||||
@Schema(description = "响应数据")
|
||||
private T data;
|
||||
|
||||
public static ResponseResult build(int code, String msg, Object data) {
|
||||
return new ResponseResult(code, msg, data);
|
||||
public static <T> ResponseResult<T> build(int code, String msg, T data) {
|
||||
return new ResponseResult<>(code, msg, data);
|
||||
}
|
||||
|
||||
public static ResponseResult success() {
|
||||
public static ResponseResult<?> success() {
|
||||
return success(null);
|
||||
}
|
||||
|
||||
public static ResponseResult success(String msg) {
|
||||
public static ResponseResult<?> success(String msg) {
|
||||
return success(msg, null);
|
||||
}
|
||||
|
||||
public static ResponseResult success(Object data) {
|
||||
public static <T> ResponseResult<T> success(T data) {
|
||||
return success("success", data);
|
||||
}
|
||||
|
||||
public static ResponseResult success(String msg, Object data) {
|
||||
public static <T> ResponseResult<T> success(String msg, T data) {
|
||||
return build(ResponseCode.SYSTEM_OK, msg, data);
|
||||
}
|
||||
|
||||
public static ResponseResult fail() {
|
||||
public static ResponseResult<?> fail() {
|
||||
return fail(null);
|
||||
}
|
||||
|
||||
public static ResponseResult fail(String msg) {
|
||||
public static ResponseResult<?> fail(String msg) {
|
||||
return fail(msg, null);
|
||||
}
|
||||
|
||||
public static ResponseResult fail(Object data) {
|
||||
public static <T> ResponseResult<T> fail(T data) {
|
||||
return fail("error", data);
|
||||
}
|
||||
|
||||
public static ResponseResult fail(String msg, Object data) {
|
||||
public static <T> ResponseResult<T> fail(String msg, T data) {
|
||||
return build(ResponseCode.SYSTEM_ERROR, msg, data);
|
||||
}
|
||||
|
||||
public static ResponseResult databaseSelectSuccess(Object object) {
|
||||
public static <T> ResponseResult<T> databaseSelectSuccess(T object) {
|
||||
return build(ResponseCode.DATABASE_SELECT_OK, "success", object);
|
||||
}
|
||||
|
||||
public static ResponseResult databaseSaveSuccess(Object object) {
|
||||
public static <T> ResponseResult<T> databaseSaveSuccess(T object) {
|
||||
return build(ResponseCode.DATABASE_SAVE_OK, "success", object);
|
||||
}
|
||||
|
||||
public static ResponseResult databaseUpdateSuccess(Object object) {
|
||||
public static <T> ResponseResult<T> databaseUpdateSuccess(T object) {
|
||||
return build(ResponseCode.DATABASE_UPDATE_OK, "success", object);
|
||||
}
|
||||
|
||||
public static ResponseResult databaseDeleteSuccess() {
|
||||
public static ResponseResult<?> databaseDeleteSuccess() {
|
||||
return build(ResponseCode.DATABASE_DELETE_OK, "success", null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user