(improvement)(build) Add spotless during the build process. (#1639)

This commit is contained in:
lexluo09
2024-09-07 00:36:17 +08:00
committed by GitHub
parent ee15a88b06
commit 5f59e89eea
986 changed files with 15609 additions and 12706 deletions

View File

@@ -1,14 +1,14 @@
package com.tencent.supersonic.auth.authorization.rest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.auth.api.authentication.utils.UserHolder;
import com.tencent.supersonic.auth.api.authorization.pojo.AuthGroup;
import com.tencent.supersonic.auth.api.authorization.request.QueryAuthResReq;
import com.tencent.supersonic.auth.api.authorization.response.AuthorizedResourceResp;
import com.tencent.supersonic.auth.api.authorization.service.AuthService;
import com.tencent.supersonic.auth.api.authorization.pojo.AuthGroup;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@@ -17,6 +17,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/auth")
@Slf4j
@@ -29,14 +31,13 @@ public class AuthController {
}
@GetMapping("/queryGroup")
public List<AuthGroup> queryAuthGroup(@RequestParam("modelId") String modelId,
public List<AuthGroup> queryAuthGroup(
@RequestParam("modelId") String modelId,
@RequestParam(value = "groupId", required = false) Integer groupId) {
return authService.queryAuthGroups(modelId, groupId);
}
/**
* 新建权限组
*/
/** 新建权限组 */
@PostMapping("/createGroup")
public void newAuthGroup(@RequestBody AuthGroup group) {
group.setGroupId(null);
@@ -68,9 +69,10 @@ public class AuthController {
* @return
*/
@PostMapping("/queryAuthorizedRes")
public AuthorizedResourceResp queryAuthorizedResources(@RequestBody QueryAuthResReq req,
HttpServletRequest request,
HttpServletResponse response) {
public AuthorizedResourceResp queryAuthorizedResources(
@RequestBody QueryAuthResReq req,
HttpServletRequest request,
HttpServletResponse response) {
User user = UserHolder.findUser(request, response);
return authService.queryAuthorizedResources(req, user);
}

View File

@@ -30,23 +30,27 @@ public class AuthServiceImpl implements AuthService {
private UserService userService;
public AuthServiceImpl(JdbcTemplate jdbcTemplate,
UserService userService) {
public AuthServiceImpl(JdbcTemplate jdbcTemplate, UserService userService) {
this.jdbcTemplate = jdbcTemplate;
this.userService = userService;
}
private List<AuthGroup> load() {
List<String> rows = jdbcTemplate.queryForList("select config from s2_auth_groups", String.class);
List<String> rows =
jdbcTemplate.queryForList("select config from s2_auth_groups", String.class);
Gson g = new Gson();
return rows.stream().map(row -> g.fromJson(row, AuthGroup.class)).collect(Collectors.toList());
return rows.stream()
.map(row -> g.fromJson(row, AuthGroup.class))
.collect(Collectors.toList());
}
@Override
public List<AuthGroup> queryAuthGroups(String modelId, Integer groupId) {
return load().stream()
.filter(group -> (Objects.isNull(groupId) || groupId.equals(group.getGroupId()))
&& modelId.equals(group.getModelId().toString()))
.filter(
group ->
(Objects.isNull(groupId) || groupId.equals(group.getGroupId()))
&& modelId.equals(group.getModelId().toString()))
.collect(Collectors.toList());
}
@@ -61,10 +65,14 @@ public class AuthServiceImpl implements AuthService {
nextGroupId = obj + 1;
}
group.setGroupId(nextGroupId);
jdbcTemplate.update("insert into s2_auth_groups (group_id, config) values (?, ?);", nextGroupId,
jdbcTemplate.update(
"insert into s2_auth_groups (group_id, config) values (?, ?);",
nextGroupId,
g.toJson(group));
} else {
jdbcTemplate.update("update s2_auth_groups set config = ? where group_id = ?;", g.toJson(group),
jdbcTemplate.update(
"update s2_auth_groups set config = ? where group_id = ?;",
g.toJson(group),
group.getGroupId());
}
}
@@ -80,10 +88,11 @@ public class AuthServiceImpl implements AuthService {
return new AuthorizedResourceResp();
}
Set<String> userOrgIds = userService.getUserAllOrgId(user.getName());
List<AuthGroup> groups = getAuthGroups(req.getModelIds(), user.getName(), new ArrayList<>(userOrgIds));
List<AuthGroup> groups =
getAuthGroups(req.getModelIds(), user.getName(), new ArrayList<>(userOrgIds));
AuthorizedResourceResp resource = new AuthorizedResourceResp();
Map<Long, List<AuthGroup>> authGroupsByModelId = groups.stream()
.collect(Collectors.groupingBy(AuthGroup::getModelId));
Map<Long, List<AuthGroup>> authGroupsByModelId =
groups.stream().collect(Collectors.groupingBy(AuthGroup::getModelId));
for (Long modelId : req.getModelIds()) {
if (authGroupsByModelId.containsKey(modelId)) {
List<AuthGroup> authGroups = authGroupsByModelId.get(modelId);
@@ -110,26 +119,31 @@ public class AuthServiceImpl implements AuthService {
return resource;
}
private List<AuthGroup> getAuthGroups(List<Long> modelIds, String userName, List<String> departmentIds) {
List<AuthGroup> groups = load().stream()
.filter(group -> {
if (!modelIds.contains(group.getModelId())) {
return false;
}
if (!CollectionUtils.isEmpty(group.getAuthorizedUsers())
&& group.getAuthorizedUsers().contains(userName)) {
return true;
}
for (String departmentId : departmentIds) {
if (!CollectionUtils.isEmpty(group.getAuthorizedDepartmentIds())
&& group.getAuthorizedDepartmentIds().contains(departmentId)) {
return true;
}
}
return false;
}).collect(Collectors.toList());
private List<AuthGroup> getAuthGroups(
List<Long> modelIds, String userName, List<String> departmentIds) {
List<AuthGroup> groups =
load().stream()
.filter(
group -> {
if (!modelIds.contains(group.getModelId())) {
return false;
}
if (!CollectionUtils.isEmpty(group.getAuthorizedUsers())
&& group.getAuthorizedUsers().contains(userName)) {
return true;
}
for (String departmentId : departmentIds) {
if (!CollectionUtils.isEmpty(
group.getAuthorizedDepartmentIds())
&& group.getAuthorizedDepartmentIds()
.contains(departmentId)) {
return true;
}
}
return false;
})
.collect(Collectors.toList());
log.info("user:{} department:{} authGroups:{}", userName, departmentIds, groups);
return groups;
}
}