Skip to content

Commit

Permalink
This code is a refactoring of the security group member management mo…
Browse files Browse the repository at this point in the history
…dule in a Spring Boot application. The main changes include renaming classes and methods to better reflect their functionality, replacing direct SQL queries with QueryFragment objects for building dynamic SQL queries, and updating controller and service layer methods to use the new request and response classes.

Changes made:

1. Renamed `GroupMemberResponse` to `GroupMemberResp`.
2. Renamed `GroupMemberRequest` to `GroupMemberReq`.
3. Updated import statements in relevant files.
4. Modified method signatures in controllers (`GroupMembersController`) to use `GroupMemberReq` instead of `GroupMemberResponse`.
5. Updated service methods (`GroupMembersService`) to use `GroupMemberReq` and return `GroupMemberResp` as necessary.

These changes improve code readability, maintainability, and consistency with naming conventions used throughout the application.
  • Loading branch information
vnobo committed Dec 16, 2024
1 parent b88bca9 commit a8494d0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ public QueryFragment(int size, long offset, Map<String, Object> params) {
}

public static QueryFragment withNew() {
return withMap(25, 0, Map.of());
return withMap(Map.of());
}

public static QueryFragment withMap(Map<String, Object> params) {
return new QueryFragment(25, 0, params);
return new QueryFragment(Integer.MAX_VALUE, 0, params);
}

public static QueryFragment withMap(int size, long offset, Map<String, Object> params) {
return new QueryFragment(size, offset, params);
}

public static QueryFragment of(QueryFragment params) {
return of(25, 0, params);
return of(Integer.MAX_VALUE, 0, params);
}

public static QueryFragment of(int size, long offset, QueryFragment params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.plate.boot.security.core.UserAuditor;
import com.plate.boot.security.core.group.member.GroupMemberResponse;
import com.plate.boot.security.core.group.member.GroupMemberResp;
import com.plate.boot.security.core.tenant.member.TenantMemberResponse;
import com.plate.boot.security.core.user.User;
import lombok.Getter;
Expand Down Expand Up @@ -53,11 +53,11 @@ public final class SecurityDetails extends DefaultOAuth2User implements UserDeta
private Set<TenantMemberResponse> tenants;

/**
* Represents a collection of {@link GroupMemberResponse} instances associated with a security details object.
* Represents a collection of {@link GroupMemberResp} instances associated with a security details object.
* This set encapsulates the group membership responses for a user, providing information about the groups
* the user is a part of, including each group's name and additional metadata in the form of a JSON node.
*/
private Set<GroupMemberResponse> groups;
private Set<GroupMemberResp> groups;

/**
* The password field securely stores the authentication credential for a user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.plate.boot.commons.base.AbstractDatabase;
import com.plate.boot.commons.utils.BeanUtils;
import com.plate.boot.commons.utils.query.QueryFragment;
import com.plate.boot.security.core.group.authority.GroupAuthority;
import com.plate.boot.security.core.group.member.GroupMemberResponse;
import com.plate.boot.security.core.group.member.GroupMemberResp;
import com.plate.boot.security.core.tenant.member.TenantMemberResponse;
import com.plate.boot.security.core.user.User;
import com.plate.boot.security.core.user.UserRequest;
Expand Down Expand Up @@ -31,7 +32,6 @@
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -63,26 +63,25 @@
public class SecurityManager extends AbstractDatabase
implements ReactiveUserDetailsService, ReactiveUserDetailsPasswordService {

private final static String QUERY_GROUP_MEMBERS_SQL = """
select a.*,b.name,b.extend
from se_group_members a join se_groups b on a.group_code=b.code
where a.user_code like :userCode
""";
private final static String QUERY_TENANT_MEMBERS_SQL = """
select a.*,b.name ,b.extend
from se_tenant_members a join se_tenants b on a.tenant_code=b.code
where a.user_code like :userCode
""";
private final static String QUERY_USER_AUTHORITY_SQL = """
select * from se_authorities where user_code = :userCode
""";
private final static String QUERY_GROUP_AUTHORITY_SQL = """
select ga.* from se_group_authorities ga
join se_group_members gm on ga.group_code = gm.group_code
join se_users su on gm.user_code = su.code
join se_groups sg on gm.group_code = sg.code and sg.tenant_code = su.tenant_code
where gm.user_code = :userCode
""";
private final static QueryFragment QUERY_GROUP_MEMBERS_FRAGMENT = QueryFragment.withNew()
.addColumn("a.*", "b.name", "b.extend")
.addQuery("se_group_members a", "join se_groups b on a.group_code=b.code")
.addWhere("a.user_code like :userCode");
private final static QueryFragment QUERY_TENANT_MEMBERS_FRAGMENT = QueryFragment.withNew()
.addColumn("a.*", "b.name", "b.extend")
.addQuery("se_tenant_members a", "join se_tenants b on a.tenant_code=b.code")
.addWhere("a.user_code like :userCode");
private final static QueryFragment QUERY_USER_AUTHORITY_FRAGMENT = QueryFragment.withNew()
.addColumn("*")
.addQuery("se_authorities")
.addWhere("user_code = :userCode");
private final static QueryFragment QUERY_GROUP_AUTHORITY_FRAGMENT = QueryFragment.withNew()
.addColumn("ga.*")
.addQuery("se_group_authorities ga",
"join se_group_members gm on ga.group_code = gm.group_code",
"join se_users su on gm.user_code = su.code",
"join se_groups sg on gm.group_code = sg.code and sg.tenant_code = su.tenant_code")
.addWhere("gm.user_code = :userCode");

/**
* Represents the service layer for handling user-related operations.
Expand Down Expand Up @@ -134,9 +133,11 @@ public Mono<User> registerOrModifyUser(UserRequest request) {
* @return A Mono emitting the User if found, or an empty Mono if no user matches the given OAuth2 binding data.
*/
public Mono<User> loadByOauth2(String bindType, String openid) {
String query = "select * from se_users where extend->'oauth2'->:bindType->>'openid'::varchar = :openid";
var userMono = this.databaseClient.sql(query)
.bind("bindType", bindType).bind("openid", openid)
QueryFragment queryFragment = QueryFragment.withNew().addColumn("*").addQuery("se_users")
.addWhere("extend->'oauth2'->:bindType->>'openid'::varchar = :openid");
queryFragment.put("bindType", bindType);
queryFragment.put("openid", openid);
var userMono = this.databaseClient.sql(queryFragment::querySql).bindValues(queryFragment)
.map((row, metadata) -> this.r2dbcConverter.read(User.class, row, metadata))
.all();
return this.queryWithCache(bindType + openid, userMono).singleOrEmpty();
Expand Down Expand Up @@ -192,7 +193,7 @@ public Mono<UserDetails> findByUsername(String username) {
private Mono<SecurityDetails> buildUserDetails(User user, Set<GrantedAuthority> authorities) {
SecurityDetails userDetails = SecurityDetails.of(user.getCode(), authorities,
BeanUtils.beanToMap(UserResponse.withUser(user)), "username").buildUser(user);
Mono<Tuple2<List<GroupMemberResponse>, List<TenantMemberResponse>>> groupsAndTenantsMono =
Mono<Tuple2<List<GroupMemberResp>, List<TenantMemberResponse>>> groupsAndTenantsMono =
Mono.zipDelayError(this.loadGroups(user.getCode()), this.loadTenants(user.getCode()));
return groupsAndTenantsMono.doOnNext(tuple2 -> {
userDetails.setGroups(new HashSet<>(tuple2.getT1()));
Expand All @@ -207,13 +208,13 @@ private Mono<SecurityDetails> buildUserDetails(User user, Set<GrantedAuthority>
* with user auditor context before being collected and sorted into a list.
*
* @param userCode The unique code identifying the user whose groups are to be loaded.
* @return A Mono emitting a sorted list of {@link GroupMemberResponse} representing the user's group memberships,
* @return A Mono emitting a sorted list of {@link GroupMemberResp} representing the user's group memberships,
* once the asynchronous operation completes successfully.
*/
private Mono<List<GroupMemberResponse>> loadGroups(String userCode) {
return this.queryWithCache("USER_GROUPS-" + userCode,
QUERY_GROUP_MEMBERS_SQL, Map.of("userCode", userCode), GroupMemberResponse.class)
.collectSortedList();
private Mono<List<GroupMemberResp>> loadGroups(String userCode) {
QUERY_GROUP_MEMBERS_FRAGMENT.put("userCode", userCode);
return this.queryWithCache("USER_GROUPS-" + userCode, QUERY_GROUP_MEMBERS_FRAGMENT.querySql(),
QUERY_GROUP_MEMBERS_FRAGMENT, GroupMemberResp.class).collectSortedList();
}

/**
Expand All @@ -225,9 +226,9 @@ private Mono<List<GroupMemberResponse>> loadGroups(String userCode) {
* @return A Mono that, when subscribed to, emits a sorted list of {@link TenantMemberResponse} objects representing the tenant members.
*/
private Mono<List<TenantMemberResponse>> loadTenants(String userCode) {
return this.queryWithCache("USER_TENANTS-" + userCode,
QUERY_TENANT_MEMBERS_SQL, Map.of("userCode", userCode), TenantMemberResponse.class)
.collectSortedList();
QUERY_TENANT_MEMBERS_FRAGMENT.put("userCode", userCode);
return this.queryWithCache("USER_TENANTS-" + userCode, QUERY_TENANT_MEMBERS_FRAGMENT.querySql(),
QUERY_TENANT_MEMBERS_FRAGMENT, TenantMemberResponse.class).collectSortedList();
}

/**
Expand All @@ -251,9 +252,9 @@ private Mono<List<GrantedAuthority>> authorities(String userCode) {
* @return A Flux emitting GrantedAuthority instances representing the authorities assigned to the user.
*/
private Flux<GrantedAuthority> getAuthorities(String userCode) {
return this.queryWithCache("USER_AUTHORITIES-" + userCode,
QUERY_USER_AUTHORITY_SQL, Map.of("userCode", userCode), UserAuthority.class)
.cast(GrantedAuthority.class);
QUERY_USER_AUTHORITY_FRAGMENT.put("userCode", userCode);
return this.queryWithCache("USER_AUTHORITIES-" + userCode, QUERY_USER_AUTHORITY_FRAGMENT.querySql(),
QUERY_USER_AUTHORITY_FRAGMENT, UserAuthority.class).cast(GrantedAuthority.class);
}

/**
Expand All @@ -263,9 +264,9 @@ private Flux<GrantedAuthority> getAuthorities(String userCode) {
* @return A {@link Flux} emitting {@link GrantedAuthority} instances representing the authorities granted to the group.
*/
private Flux<GrantedAuthority> getGroupAuthorities(String userCode) {
return this.queryWithCache("GROUP_AUTHORITIES-" + userCode,
QUERY_GROUP_AUTHORITY_SQL, Map.of("userCode", userCode), GroupAuthority.class)
.cast(GrantedAuthority.class);
QUERY_GROUP_AUTHORITY_FRAGMENT.put("userCode", userCode);
return this.queryWithCache("GROUP_AUTHORITIES-" + userCode, QUERY_GROUP_AUTHORITY_FRAGMENT.querySql(),
QUERY_GROUP_AUTHORITY_FRAGMENT, GroupAuthority.class).cast(GrantedAuthority.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class GroupMemberRequest extends GroupMember {
public class GroupMemberReq extends GroupMember {

private Set<String> users;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class GroupMemberResponse extends GroupMember {
public class GroupMemberResp extends GroupMember {

private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ public class GroupMembersController {
private final GroupMembersService groupMembersService;

@GetMapping("search")
public Flux<GroupMemberResponse> search(GroupMemberRequest request, Pageable pageable) {
public Flux<GroupMemberResp> search(GroupMemberReq request, Pageable pageable) {
return this.groupMembersService.search(request, pageable);
}

@GetMapping("page")
public Mono<Page<GroupMemberResponse>> page(GroupMemberRequest request, Pageable pageable) {
public Mono<Page<GroupMemberResp>> page(GroupMemberReq request, Pageable pageable) {
return this.groupMembersService.page(request, pageable);
}

@PostMapping("save")
public Mono<GroupMember> save(@Valid @RequestBody GroupMemberRequest request) {
public Mono<GroupMember> save(@Valid @RequestBody GroupMemberReq request) {
return this.groupMembersService.operate(request);
}

@DeleteMapping("delete")
public Mono<Void> delete(@Valid @RequestBody GroupMemberRequest request) {
public Mono<Void> delete(@Valid @RequestBody GroupMemberReq request) {
Assert.notNull(request.getId(), "When deleting a Tenant, the ID must not be null");
return this.groupMembersService.delete(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ select count(*) from se_group_members a

private final GroupMembersRepository memberRepository;

public Flux<GroupMemberResponse> search(GroupMemberRequest request, Pageable pageable) {
public Flux<GroupMemberResp> search(GroupMemberReq request, Pageable pageable) {
QueryFragment queryFragment = request.toParamSql();
return super.queryWithCache(BeanUtils.cacheKey(request, pageable), queryFragment.querySql(),
queryFragment, GroupMemberResponse.class);
queryFragment, GroupMemberResp.class);
}

public Mono<Page<GroupMemberResponse>> page(GroupMemberRequest request, Pageable pageable) {
public Mono<Page<GroupMemberResp>> page(GroupMemberReq request, Pageable pageable) {
var searchMono = this.search(request, pageable).collectList();
QueryFragment QueryFragment = request.toParamSql();
String query = COUNT_SQL + QueryFragment.whereSql();
Expand All @@ -47,7 +47,7 @@ public Mono<Page<GroupMemberResponse>> page(GroupMemberRequest request, Pageable
.map(tuple2 -> new PageImpl<>(tuple2.getT1(), pageable, tuple2.getT2()));
}

public Mono<GroupMember> operate(GroupMemberRequest request) {
public Mono<GroupMember> operate(GroupMemberReq request) {
var dataMono = this.entityTemplate.selectOne(Query.query(request.toCriteria()), GroupMember.class)
.defaultIfEmpty(request.toGroupMember());
return dataMono.flatMap(this::save).doAfterTerminate(() -> this.cache.clear());
Expand All @@ -63,7 +63,7 @@ public Mono<GroupMember> save(GroupMember groupMember) {
}
}

public Mono<Void> delete(GroupMemberRequest request) {
public Mono<Void> delete(GroupMemberReq request) {
return this.memberRepository.delete(request.toGroupMember()).doAfterTerminate(() -> this.cache.clear());
}
}

0 comments on commit a8494d0

Please sign in to comment.