Skip to content

Commit 5b02e76

Browse files
committed
修正移除授权的逻辑
1 parent 7ef32cb commit 5b02e76

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

hypervisor/src/main/java/io/leafage/basic/hypervisor/controller/GroupController.java

+18
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,22 @@ public ResponseEntity<Mono<GroupPrivileges>> relation(@PathVariable Long id, @Pa
212212
}
213213
return ResponseEntity.ok(mono);
214214
}
215+
216+
/**
217+
* 关联权限
218+
*
219+
* @param id 组id
220+
* @return 查询到的数据集,异常时返回204状态码
221+
*/
222+
@DeleteMapping("/{id}/privileges/{privilegeId}")
223+
public ResponseEntity<Mono<Void>> removeRelation(@PathVariable Long id, @PathVariable Long privilegeId, Set<String> actions) {
224+
Mono<Void> voidMono;
225+
try {
226+
voidMono = groupPrivilegesService.removeRelation(id, privilegeId, actions);
227+
} catch (Exception e) {
228+
logger.error("Remove group privileges occurred an error: ", e);
229+
return ResponseEntity.noContent().build();
230+
}
231+
return ResponseEntity.ok(voidMono);
232+
}
215233
}

hypervisor/src/main/java/io/leafage/basic/hypervisor/repository/GroupAuthoritiesRepository.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import io.leafage.basic.hypervisor.domain.GroupAuthorities;
2121
import org.springframework.data.r2dbc.repository.R2dbcRepository;
2222
import org.springframework.stereotype.Repository;
23-
import reactor.core.publisher.Flux;
23+
import reactor.core.publisher.Mono;
2424

2525
/**
2626
* group authorities repository
@@ -31,11 +31,11 @@
3131
public interface GroupAuthoritiesRepository extends R2dbcRepository<GroupAuthorities, Long> {
3232

3333
/**
34-
* 根据group查member
34+
* 根据group删除
3535
*
36-
* @param groupId group主键
37-
* @return 关联数据集
36+
* @param groupId group 主键
37+
* @param authority 权限
3838
*/
39-
Flux<GroupAuthorities> findByGroupId(Long groupId);
39+
Mono<Void> deleteByGroupIdAndAuthority(Long groupId, String authority);
4040

4141
}

hypervisor/src/main/java/io/leafage/basic/hypervisor/repository/GroupPrivilegesRepository.java

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.data.r2dbc.repository.R2dbcRepository;
2222
import org.springframework.stereotype.Repository;
2323
import reactor.core.publisher.Flux;
24+
import reactor.core.publisher.Mono;
2425

2526
/**
2627
* group privileges repository
@@ -38,6 +39,15 @@ public interface GroupPrivilegesRepository extends R2dbcRepository<GroupPrivileg
3839
*/
3940
Flux<GroupPrivileges> findByGroupId(Long groupId);
4041

42+
/**
43+
* 根据group和privilege查
44+
*
45+
* @param groupId group主键
46+
* @param privilegeId privilege主键
47+
* @return 关联数据集
48+
*/
49+
Mono<GroupPrivileges> findByGroupIdAndPrivilegeId(Long groupId, Long privilegeId);
50+
4151
/**
4252
* 根据privilege查
4353
*

hypervisor/src/main/java/io/leafage/basic/hypervisor/service/GroupPrivilegesService.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,22 @@ public interface GroupPrivilegesService {
4747
Mono<List<GroupPrivileges>> groups(Long privilegeId);
4848

4949
/**
50-
* group-privileges关系
50+
* group-privileges关联
5151
*
5252
* @param groupId group 主键
5353
* @param privilegeId privilege 主键
5454
* @param actions 按钮
5555
* @return 数据集
5656
*/
5757
Mono<GroupPrivileges> relation(Long groupId, Long privilegeId, Set<String> actions);
58+
59+
/**
60+
* 移除group-privileges关系
61+
*
62+
* @param groupId group 主键
63+
* @param privilegeId privilege 主键
64+
* @param actions 按钮
65+
* @return 数据集
66+
*/
67+
Mono<Void> removeRelation(Long groupId, Long privilegeId, Set<String> actions);
5868
}

hypervisor/src/main/java/io/leafage/basic/hypervisor/service/impl/GroupPrivilegesServiceImpl.java

+19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.leafage.basic.hypervisor.service.GroupPrivilegesService;
2626
import org.springframework.stereotype.Service;
2727
import org.springframework.util.Assert;
28+
import org.springframework.util.CollectionUtils;
2829
import reactor.core.publisher.Mono;
2930

3031
import java.util.List;
@@ -104,4 +105,22 @@ public Mono<GroupPrivileges> relation(Long groupId, Long privilegeId, Set<String
104105
.then(groupPrivilegesRepository.save(groupPrivileges));
105106
});
106107
}
108+
109+
@Override
110+
public Mono<Void> removeRelation(Long groupId, Long privilegeId, Set<String> actions) {
111+
Assert.notNull(groupId, "groupId must not be null.");
112+
Assert.notNull(privilegeId, "privilegeId must not be null.");
113+
114+
return groupPrivilegesRepository.findByGroupIdAndPrivilegeId(groupId, privilegeId)
115+
.flatMap(groupPrivileges -> {
116+
// 删除 GroupAuthorities
117+
if (CollectionUtils.isEmpty(actions) || groupPrivileges.getActions().containsAll(actions)) {
118+
return groupPrivilegesRepository.deleteById(groupPrivileges.getId());
119+
}
120+
return privilegeRepository.findById(privilegeId).map(privilege -> actions.stream().map(action ->
121+
groupAuthoritiesRepository.deleteByGroupIdAndAuthority(groupId, privilege.getName() + ":" + action)))
122+
.then(groupPrivilegesRepository.delete(groupPrivileges));
123+
124+
});
125+
}
107126
}

0 commit comments

Comments
 (0)