Skip to content

Commit

Permalink
增加基础数据接口 权限,更细分了.
Browse files Browse the repository at this point in the history
登录时候优化获取角色权限SQL,增加用户租户判断.
  • Loading branch information
vnobo committed Jan 22, 2024
1 parent 41c2ae8 commit 65fd380
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.platform.boot.security.core.group;

import com.platform.boot.commons.query.ParamSql;
import com.platform.boot.commons.utils.BeanUtils;
import com.platform.boot.commons.utils.CriteriaUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.data.relational.core.query.Criteria;

import java.util.Set;
import java.util.List;
import java.util.Map;

/**
* @author <a href="https://github.com/vnobo">Alex bob</a>
Expand All @@ -16,6 +18,8 @@
@ToString(callSuper = true)
public class GroupRequest extends Group {

private Map<String, Object> query;

private String securityCode;

public GroupRequest securityCode(String securityCode) {
Expand All @@ -27,7 +31,7 @@ public Group toGroup() {
return BeanUtils.copyProperties(this, Group.class);
}

public Criteria toCriteria() {
return criteria(Set.of("securityCode"));
public ParamSql bindParamSql() {
return CriteriaUtils.buildParamSql(this, List.of(), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,21 @@ public Mono<Page<Group>> page(GroupRequest request, Pageable pageable) {
@PostMapping("add")
@PreAuthorize("hasRole(@contextUtils.RULE_ADMINISTRATORS)")
public Mono<Group> add(@Valid @RequestBody GroupRequest request) {
// Check that the Group ID is null (i.e. this is a new Group)
Assert.isTrue(request.isNew(), "When adding a new Group, the ID must be null");
// Call the Groups service to add the Group and return the result as a Mono
Assert.isNull(request.getId(), "When adding a new Group, the ID must be null");
return this.groupsService.operate(request);
}

@PutMapping("modify")
@PreAuthorize("hasRole(@contextUtils.RULE_ADMINISTRATORS)")
public Mono<Group> modify(@Valid @RequestBody GroupRequest request) {
// Check that the Group ID is not null (i.e. this is an existing Group)
Assert.isTrue(!request.isNew(), "When modifying an existing Group, the ID must not be null");
// Call the Groups service to modify the Group and return the result as a Mono
Assert.notNull(request.getId(), "When modifying an existing Group, the ID must not be null");
return this.groupsService.operate(request);
}

@DeleteMapping("delete")
@PreAuthorize("hasRole(@contextUtils.RULE_ADMINISTRATORS)")
public Mono<Void> delete(@Valid @RequestBody GroupRequest request) {
// Check that the Group ID is not null (i.e. this is an existing Group)
Assert.isTrue(!request.isNew(), "When deleting a Group, the ID must not be null");
// Call the Groups service to delete the Group and return the result as a Mono
Assert.notNull(request.getId(), "When deleting a Group, the ID must not be null");
return this.groupsService.delete(request);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.platform.boot.security.core.group;

import com.platform.boot.commons.base.AbstractDatabase;
import com.platform.boot.commons.query.ParamSql;
import com.platform.boot.commons.utils.BeanUtils;
import com.platform.boot.commons.utils.ContextUtils;
import com.platform.boot.commons.utils.CriteriaUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.relational.core.query.Query;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand All @@ -23,16 +24,20 @@ public class GroupsService extends AbstractDatabase {

public Flux<Group> search(GroupRequest request, Pageable pageable) {
var cacheKey = ContextUtils.cacheKey(request, pageable);
Query query = Query.query(request.toCriteria()).with(pageable);
return super.queryWithCache(cacheKey, query, Group.class)
ParamSql paramSql = request.bindParamSql();
String query = "select * from se_groups" + paramSql.whereSql() + CriteriaUtils.applyPage(pageable);
return super.queryWithCache(cacheKey, query, paramSql.params(), Group.class)
.flatMap(ContextUtils::serializeUserAuditor);
}

public Mono<Page<Group>> page(GroupRequest request, Pageable pageable) {
var cacheKey = ContextUtils.cacheKey(request);
Query query = Query.query(request.toCriteria());
var searchMono = this.search(request, pageable).collectList();
var countMono = this.countWithCache(cacheKey, query, Group.class);

var cacheKey = ContextUtils.cacheKey(request);
ParamSql paramSql = request.bindParamSql();
String query = "select count(*) from se_groups" + paramSql.whereSql() + CriteriaUtils.applyPage(pageable);
var countMono = this.countWithCache(cacheKey, query, paramSql.params());

return Mono.zip(searchMono, countMono)
.map(tuple2 -> new PageImpl<>(tuple2.getT1(), pageable, tuple2.getT2()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
Expand All @@ -26,11 +25,6 @@ public Flux<GroupAuthority> search(GroupAuthorityRequest request, Pageable pagea
return this.authoritiesService.search(request, pageable);
}

@GetMapping("page")
public Mono<Page<GroupAuthority>> page(GroupAuthorityRequest request, Pageable pageable) {
return this.authoritiesService.page(request, pageable);
}

@PostMapping("save")
public Mono<GroupAuthority> save(@Valid @RequestBody GroupAuthorityRequest request) {
return this.authoritiesService.operate(request);
Expand All @@ -45,9 +39,7 @@ public Mono<Object> batch(@RequestBody GroupAuthorityRequest request) {

@DeleteMapping("delete")
public Mono<Void> delete(@Valid @RequestBody GroupAuthorityRequest request) {
// Check that the Group ID is not null (i.e. this is an existing Group)
Assert.isTrue(!request.isNew(), "When deleting a Group, the ID must not be null");
// Call the Groups service to delete the Group and return the result as a Mono
Assert.notNull(request.getId(), "When deleting a Group, the ID must not be null");
return this.authoritiesService.delete(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.platform.boot.commons.base.AbstractDatabase;
import com.platform.boot.commons.utils.ContextUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.relational.core.query.Query;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -38,18 +36,6 @@ public Flux<GroupAuthority> search(GroupAuthorityRequest request, Pageable pagea
.flatMap(ContextUtils::serializeUserAuditor);
}

public Mono<Page<GroupAuthority>> page(GroupAuthorityRequest request, Pageable pageable) {

var searchMono = this.search(request, pageable).collectList();

var cacheKey = ContextUtils.cacheKey(request);
Query query = Query.query(request.toCriteria());
var countMono = super.countWithCache(cacheKey, query, GroupAuthority.class);

return Mono.zip(searchMono, countMono)
.map(tuple2 -> new PageImpl<>(tuple2.getT1(), pageable, tuple2.getT2()));
}

@Transactional(rollbackFor = Exception.class)
public Mono<Integer> batch(GroupAuthorityRequest request) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public Mono<GroupMember> save(@Valid @RequestBody GroupMemberRequest request) {

@DeleteMapping("delete")
public Mono<Void> delete(@Valid @RequestBody GroupMemberRequest request) {
// Check that the Tenant ID is not null (i.e. this is an existing Tenant)
Assert.isTrue(!request.isNew(), "When deleting a Tenant, the ID must not be null");
// Call the Tenants service to delete the Tenant and return the result as a Mono
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 @@ -36,27 +36,21 @@ public Mono<Page<Tenant>> page(TenantRequest request, Pageable pageable) {
@PostMapping("add")
@PreAuthorize("hasRole(@contextUtils.RULE_ADMINISTRATORS)")
public Mono<Tenant> add(@Valid @RequestBody TenantRequest request) {
// Check that the Tenant ID is null (i.e. this is a new Tenant)
Assert.isTrue(request.isNew(), "When adding a new Tenant, the ID must be null");
// Call the Tenants service to add the Tenant and return the result as a Mono
Assert.isNull(request.getId(), "When adding a new Tenant, the ID must be null");
return this.tenantsService.operate(request);
}

@PutMapping("modify")
@PreAuthorize("hasRole(@contextUtils.RULE_ADMINISTRATORS)")
public Mono<Tenant> modify(@Valid @RequestBody TenantRequest request) {
// Check that the Tenant ID is not null (i.e. this is an existing Tenant)
Assert.isTrue(!request.isNew(), "When modifying an existing Tenant, the ID must not be null");
// Call the Tenants service to modify the Tenant and return the result as a Mono
Assert.notNull(request.getId(), "When modifying an existing Tenant, the ID must not be null");
return this.tenantsService.operate(request);
}

@DeleteMapping("delete")
@PreAuthorize("hasRole(@contextUtils.RULE_ADMINISTRATORS)")
public Mono<Void> delete(@Valid @RequestBody TenantRequest request) {
// Check that the Tenant ID is not null (i.e. this is an existing Tenant)
Assert.isTrue(!request.isNew(), "When deleting a Tenant, the ID must not be null");
// Call the Tenants service to delete the Tenant and return the result as a Mono
Assert.notNull(request.getId(), "When deleting a Tenant, the ID must not be null");
return this.tenantsService.delete(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,14 @@ public Mono<Page<TenantMemberResponse>> page(TenantMemberRequest request, Pageab
.securityCode(securityDetails.getTenantCode()), pageable));
}

@PostMapping("add")
public Mono<TenantMember> add(@Valid @RequestBody TenantMemberRequest request) {
// Check that the Tenant ID is null (i.e. this is a new Tenant)
Assert.isTrue(request.isNew(), "When adding a new Tenant, the ID must be null");
// Call the Tenants service to add the Tenant and return the result as a Mono
return this.tenantMembersService.operate(request);
}

@PutMapping("modify")
public Mono<TenantMember> modify(@Valid @RequestBody TenantMemberRequest request) {
// Check that the Tenant ID is not null (i.e. this is an existing Tenant)
Assert.isTrue(!request.isNew(), "When modifying an existing Tenant, the ID must not be null");
// Call the Tenants service to modify the Tenant and return the result as a Mono
@PostMapping("save")
public Mono<TenantMember> save(@Valid @RequestBody TenantMemberRequest request) {
return this.tenantMembersService.operate(request);
}

@DeleteMapping("delete")
public Mono<Void> delete(@Valid @RequestBody TenantMemberRequest request) {
// Check that the Tenant ID is not null (i.e. this is an existing Tenant)
Assert.isTrue(!request.isNew(), "When deleting a Tenant, the ID must not be null");
// Call the Tenants service to delete the Tenant and return the result as a Mono
Assert.notNull(request.getId(), "When deleting a Tenant, the ID must not be null");
return this.tenantMembersService.delete(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,21 @@ public Mono<Page<UserResponse>> page(UserRequest request, Pageable pageable) {
@PostMapping("add")
@PreAuthorize("hasAuthority('users:write')")
public Mono<User> add(@Valid @RequestBody UserRequest request) {
// Check that the user ID is null (i.e. this is a new user)
Assert.isTrue(request.isNew(), "When adding a new user, the ID must be null");
// Call the users service to add the user and return the result as a Mono
Assert.isNull(request.getId(), "When adding a new user, the ID must be null");
return this.usersService.add(request);
}

@PutMapping("modify")
@PreAuthorize("hasAuthority('users:write')")
public Mono<User> modify(@Validated(Update.class) @RequestBody UserRequest request) {
// Check that the user ID is not null (i.e. this is an existing user)
Assert.isTrue(!request.isNew(), "When modifying an existing user, the ID must not be null");
// Call the users service to modify the user and return the result as a Mono
Assert.notNull(request.getId(), "When modifying an existing user, the ID must not be null");
return this.usersService.operate(request);
}

@DeleteMapping("delete")
@PreAuthorize("hasAuthority('users:delete')")
public Mono<Void> delete(@Valid @RequestBody UserRequest request) {
// Check that the user ID is not null (i.e. this is an existing user)
Assert.isTrue(!request.isNew(), "When deleting a user, the ID must not be null");
// Call the users service to delete the user and return the result as a Mono
Assert.notNull(request.getId(), "When deleting a user, the ID must not be null");
return this.usersService.delete(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ public Flux<UserAuthority> search(UserAuthorityRequest request) {
return this.authoritiesService.search(request);
}

@PostMapping("add")
public Mono<UserAuthority> add(@Valid @RequestBody UserAuthorityRequest request) {
Assert.isTrue(request.isNew(), "新增用户时,[ID]必须为空");
@PostMapping("save")
public Mono<UserAuthority> save(@Valid @RequestBody UserAuthorityRequest request) {
return this.authoritiesService.operate(request);
}

@DeleteMapping("delete")
public Mono<Void> delete(@Valid @RequestBody UserAuthorityRequest request) {
Assert.isTrue(!request.isNew(), "删除用户时,[ID]不能为空");
Assert.notNull(request.getId(), "删除用户时,[ID]不能为空");
return this.authoritiesService.delete(request);
}

Expand Down

0 comments on commit 65fd380

Please sign in to comment.