From 4326ea01aeb74bc179f9ed64d1f56ef658c1a1fb Mon Sep 17 00:00:00 2001 From: zhou-hao Date: Tue, 25 May 2021 11:06:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=BA=E6=9E=84=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=EF=BC=8C=E8=A7=A3=E7=BB=91=E7=94=A8=E6=88=B7=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/web/OrganizationController.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/web/OrganizationController.java b/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/web/OrganizationController.java index a3885061..ec9f63da 100644 --- a/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/web/OrganizationController.java +++ b/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/web/OrganizationController.java @@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import org.apache.commons.collections4.CollectionUtils; +import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult; import org.hswebframework.web.api.crud.entity.PagerResult; import org.hswebframework.web.api.crud.entity.QueryOperation; import org.hswebframework.web.api.crud.entity.QueryParamEntity; @@ -12,13 +13,17 @@ import org.hswebframework.web.authorization.Authentication; import org.hswebframework.web.authorization.Dimension; import org.hswebframework.web.authorization.annotation.*; import org.hswebframework.web.system.authorization.api.entity.DimensionEntity; +import org.hswebframework.web.system.authorization.api.entity.DimensionUserEntity; import org.hswebframework.web.system.authorization.defaults.service.DefaultDimensionService; +import org.hswebframework.web.system.authorization.defaults.service.DefaultDimensionUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import java.util.Collections; import java.util.List; +import java.util.function.Function; import java.util.stream.Collectors; @RequestMapping("/organization") @@ -30,6 +35,10 @@ public class OrganizationController { @Autowired private DefaultDimensionService dimensionService; + @Autowired + private DefaultDimensionUserService dimensionUserService; + + public OrganizationController(DefaultDimensionService dimensionService) { this.dimensionService = dimensionService; } @@ -134,5 +143,43 @@ public class OrganizationController { .then(); } + @PostMapping("/{id}/users/_bind") + @ResourceAction(id = "bind-user", name = "绑定用户") + @Operation(summary = "绑定用户到机构") + public Mono bindUser(@Parameter(description = "机构ID") @PathVariable String id, + @Parameter(description = "用户ID") + @RequestBody Mono> userId) { + + return userId + .flatMapIterable(Function.identity()) + .map(uId -> { + DimensionUserEntity userEntity = new DimensionUserEntity(); + userEntity.setUserId(uId); + userEntity.setUserName(uId); + userEntity.setDimensionId(id); + userEntity.setDimensionTypeId(orgDimensionTypeId); + userEntity.setDimensionName(orgDimensionTypeId); + return userEntity; + }) + .as(dimensionUserService::save) + .map(SaveResult::getTotal); + + } + + @PostMapping("/{id}/users/_unbind") + @ResourceAction(id = "unbind-user", name = "解绑用户") + @Operation(summary = "从机构解绑用户") + public Mono unbindUser(@Parameter(description = "机构ID") @PathVariable String id, + @Parameter(description = "用户ID") + @RequestBody Mono> userId) { + return userId + .flatMap(newUserIdList -> dimensionUserService + .createDelete() + .where(DimensionUserEntity::getDimensionTypeId, orgDimensionTypeId) + .in(DimensionUserEntity::getUserId, newUserIdList) + .and(DimensionUserEntity::getDimensionId, id) + .execute()); + } + }