Skip to content

Commit 7260aa7

Browse files
authored
Merge pull request #1464 from lowcoder-org/user_manangement_api
User manangement api
2 parents c38fb82 + 4c9c247 commit 7260aa7

File tree

4 files changed

+111
-24
lines changed

4 files changed

+111
-24
lines changed

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/usermanagement/OrgApiService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.lowcoder.api.usermanagement.view.OrgView;
77
import org.lowcoder.api.usermanagement.view.UpdateOrgRequest;
88
import org.lowcoder.api.usermanagement.view.UpdateRoleRequest;
9+
import org.lowcoder.domain.organization.model.OrgMember;
910
import org.lowcoder.domain.organization.model.Organization;
1011
import org.lowcoder.domain.organization.model.Organization.OrganizationCommonSettings;
1112
import org.lowcoder.infra.annotation.NonEmptyMono;
@@ -23,8 +24,12 @@ public interface OrgApiService {
2324

2425
Mono<Boolean> updateRoleForMember(String orgId, UpdateRoleRequest updateRoleRequest);
2526

27+
Mono<OrgMember> checkVisitorAdminRole(String orgId);
28+
2629
Mono<Boolean> switchCurrentOrganizationTo(String orgId);
2730

31+
Mono<Boolean> switchCurrentOrganizationTo(String userId, String orgId);
32+
2833
Mono<Boolean> deleteLogo(String orgId);
2934

3035
Mono<Boolean> uploadLogo(String orgId, Mono<Part> fileMono);

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/usermanagement/OrgApiServiceImpl.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ public Mono<Boolean> updateRoleForMember(String orgId, UpdateRoleRequest updateR
158158
MemberRole.fromValue(updateRoleRequest.getRole())));
159159
}
160160

161-
private Mono<OrgMember> checkVisitorAdminRole(String orgId) {
161+
@Override
162+
public Mono<OrgMember> checkVisitorAdminRole(String orgId) {
162163
return sessionUserService.getVisitorId()
163164
.flatMap(visitor -> orgMemberService.getOrgMember(orgId, visitor))
164165
.filter(it -> it.getRole() == MemberRole.ADMIN || it.getRole() == MemberRole.SUPER_ADMIN)
@@ -177,15 +178,18 @@ private Mono<Void> checkDeveloperCount(String orgId, String role, String userId)
177178
@Override
178179
public Mono<Boolean> switchCurrentOrganizationTo(String nextCurrentOrgId) {
179180
return sessionUserService.getVisitorId()
180-
.flatMap(it -> orgMemberService.getAllActiveOrgs(it).collectList())
181+
.flatMap(it -> switchCurrentOrganizationTo(it, nextCurrentOrgId));
182+
}
183+
184+
@Override
185+
public Mono<Boolean> switchCurrentOrganizationTo(String userId, String nextCurrentOrgId) {
186+
return orgMemberService.getAllActiveOrgs(userId).collectList()
181187
.defaultIfEmpty(Collections.emptyList())
182188
.flatMap(orgMembers -> {
183189
if (!collectSet(orgMembers, OrgMember::getOrgId).contains(nextCurrentOrgId)) {
184190
return Mono.error(new BizException(BizError.INVALID_ORG_ID, "INVALID_ORG_ID"));
185191
}
186192

187-
String userId = orgMembers.get(0).getUserId();
188-
189193
Optional<OrgMember> previousCurrentOrgMember = orgMembers.stream()
190194
.filter(OrgMember::isCurrentOrg)
191195
.findFirst();

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/usermanagement/UserController.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
import lombok.RequiredArgsConstructor;
44
import org.apache.commons.lang3.StringUtils;
55
import org.lowcoder.api.authentication.dto.OrganizationDomainCheckResult;
6+
import org.lowcoder.api.authentication.service.AuthenticationApiService;
67
import org.lowcoder.api.framework.view.ResponseView;
78
import org.lowcoder.api.home.SessionUserService;
89
import org.lowcoder.api.home.UserHomeApiService;
910
import org.lowcoder.api.usermanagement.view.UpdateUserRequest;
1011
import org.lowcoder.api.usermanagement.view.UserProfileView;
12+
import org.lowcoder.domain.organization.model.MemberRole;
13+
import org.lowcoder.domain.organization.service.OrgMemberService;
1114
import org.lowcoder.domain.user.constant.UserStatusType;
1215
import org.lowcoder.domain.user.model.User;
1316
import org.lowcoder.domain.user.model.UserDetail;
1417
import org.lowcoder.domain.user.service.UserService;
1518
import org.lowcoder.domain.user.service.UserStatusService;
1619
import org.lowcoder.sdk.config.CommonConfig;
20+
import org.lowcoder.sdk.constants.AuthSourceConstants;
1721
import org.lowcoder.sdk.exception.BizError;
1822
import org.springframework.http.HttpStatus;
1923
import org.springframework.http.codec.multipart.Part;
@@ -35,6 +39,19 @@ public class UserController implements UserEndpoints
3539
private final UserStatusService userStatusService;
3640
private final UserApiService userApiService;
3741
private final CommonConfig commonConfig;
42+
private final AuthenticationApiService authenticationApiService;
43+
private final OrgMemberService orgMemberService;
44+
45+
@Override
46+
public Mono<ResponseView<?>> createUserAndAddToOrg(@PathVariable String orgId, CreateUserRequest request) {
47+
return orgApiService.checkVisitorAdminRole(orgId).flatMap(__ ->
48+
authenticationApiService.authenticateByForm(request.email(), request.password(),
49+
AuthSourceConstants.EMAIL, true, null, orgId))
50+
.flatMap(authUser -> userService.createNewUserByAuthUser(authUser, false))
51+
.delayUntil(user -> orgMemberService.tryAddOrgMember(orgId, user.getId(), MemberRole.MEMBER))
52+
.delayUntil(user -> orgApiService.switchCurrentOrganizationTo(user.getId(), orgId))
53+
.map(ResponseView::success);
54+
}
3855

3956
@Override
4057
public Mono<ResponseView<?>> getUserProfile(ServerWebExchange exchange) {
@@ -67,19 +84,27 @@ public Mono<ResponseView<Boolean>> markStatus(@RequestBody MarkUserStatusRequest
6784
@Override
6885
public Mono<ResponseView<UserProfileView>> update(@RequestBody UpdateUserRequest updateUserRequest, ServerWebExchange exchange) {
6986
return sessionUserService.getVisitorId()
70-
.flatMap(uid -> {
71-
User updateUser = new User();
72-
if (StringUtils.isNotBlank(updateUserRequest.getName())) {
73-
updateUser.setName(updateUserRequest.getName());
74-
updateUser.setHasSetNickname(true);
75-
}
76-
if (StringUtils.isNotBlank(updateUserRequest.getUiLanguage())) {
77-
updateUser.setUiLanguage(updateUserRequest.getUiLanguage());
78-
}
79-
return userService.update(uid, updateUser);
80-
})
81-
.flatMap(user -> userHomeApiService.buildUserProfileView(user, exchange))
82-
.map(ResponseView::success);
87+
.flatMap(uid -> updateUser(uid, updateUserRequest, exchange));
88+
}
89+
90+
@Override
91+
public Mono<ResponseView<UserProfileView>> update(@PathVariable String orgId, @PathVariable String userId, @RequestBody UpdateUserRequest updateUserRequest, ServerWebExchange exchange) {
92+
return orgApiService.checkVisitorAdminRole(orgId)
93+
.flatMap(__ -> updateUser(userId, updateUserRequest, exchange));
94+
}
95+
96+
public Mono<ResponseView<UserProfileView>> updateUser(String userId, @RequestBody UpdateUserRequest updateUserRequest, ServerWebExchange exchange) {
97+
User updateUser = new User();
98+
if (StringUtils.isNotBlank(updateUserRequest.getName())) {
99+
updateUser.setName(updateUserRequest.getName());
100+
updateUser.setHasSetNickname(true);
101+
}
102+
if (StringUtils.isNotBlank(updateUserRequest.getUiLanguage())) {
103+
updateUser.setUiLanguage(updateUserRequest.getUiLanguage());
104+
}
105+
return userService.update(userId, updateUser)
106+
.flatMap(user -> userHomeApiService.buildUserProfileView(user, exchange))
107+
.map(ResponseView::success);
83108
}
84109

85110
@Override
@@ -89,13 +114,28 @@ public Mono<ResponseView<Boolean>> uploadProfilePhoto(@RequestPart("file") Mono<
89114
.map(ResponseView::success);
90115
}
91116

117+
@Override
118+
public Mono<ResponseView<Boolean>> uploadProfilePhotoById(@PathVariable String orgId, @PathVariable String userId, @RequestPart("file") Mono<Part> fileMono) {
119+
return orgApiService.checkVisitorAdminRole(orgId).flatMap(__ -> userService.findById(userId))
120+
.zipWith(fileMono)
121+
.flatMap(tuple -> userService.saveProfilePhoto(tuple.getT2(), tuple.getT1()))
122+
.map(ResponseView::success);
123+
}
124+
92125
@Override
93126
public Mono<ResponseView<Void>> deleteProfilePhoto() {
94127
return sessionUserService.getVisitor()
95128
.flatMap(visitor -> userService.deleteProfilePhoto(visitor)
96129
.map(ResponseView::success));
97130
}
98131

132+
@Override
133+
public Mono<ResponseView<Void>> deleteProfilePhotoById(@PathVariable String orgId, @PathVariable String userId) {
134+
return orgApiService.checkVisitorAdminRole(orgId).flatMap(__ -> userService.findById(userId))
135+
.flatMap(user -> userService.deleteProfilePhoto(user)
136+
.map(ResponseView::success));
137+
}
138+
99139
@Override
100140
public Mono<Void> getProfilePhoto(ServerWebExchange exchange) {
101141
return sessionUserService.getVisitorId()

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/usermanagement/UserEndpoints.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,20 @@
2525

2626
@RestController
2727
@RequestMapping(value = {Url.USER_URL, NewUrl.USER_URL})
28-
public interface UserEndpoints
28+
public interface UserEndpoints
2929
{
3030
public static final String TAG_USER_MANAGEMENT = "User APIs";
3131
public static final String TAG_USER_PASSWORD_MANAGEMENT = "User Password APIs";
3232
public static final String TAG_USER_PROFILE_PHOTO_MANAGEMENT = "User Profile Photo APIs";
33-
33+
@Operation(
34+
tags = TAG_USER_MANAGEMENT,
35+
operationId = "createUserAndAddToOrg",
36+
summary = "Create user and add to the org",
37+
description = "Create a new user and add to specified organization."
38+
)
39+
@PostMapping("/new/{orgId}")
40+
public Mono<ResponseView<?>> createUserAndAddToOrg(@PathVariable String orgId, @RequestBody CreateUserRequest request);
41+
3442
@Operation(
3543
tags = TAG_USER_MANAGEMENT,
3644
operationId = "getUserProfile",
@@ -67,6 +75,15 @@ public interface UserEndpoints
6775
@PutMapping
6876
public Mono<ResponseView<UserProfileView>> update(@RequestBody UpdateUserRequest updateUserRequest, ServerWebExchange exchange);
6977

78+
@Operation(
79+
tags = TAG_USER_MANAGEMENT,
80+
operationId = "updateUser",
81+
summary = "Update selected User",
82+
description = "Update specified user profile information within Lowcoder, ensuring accuracy and relevance."
83+
)
84+
@PutMapping("/{orgId}/{userId}")
85+
public Mono<ResponseView<UserProfileView>> update(@PathVariable String orgId, @PathVariable String userId, @RequestBody UpdateUserRequest updateUserRequest, ServerWebExchange exchange);
86+
7087
@Operation(
7188
tags = TAG_USER_PROFILE_PHOTO_MANAGEMENT,
7289
operationId = "uploadUserProfilePhoto",
@@ -78,12 +95,30 @@ public interface UserEndpoints
7895

7996
@Operation(
8097
tags = TAG_USER_PROFILE_PHOTO_MANAGEMENT,
81-
operationId = "deleteUserProfilePhoto",
82-
summary = "Delete current users profile photo",
83-
description = "Remove the profile Photo associated with the current User within Lowcoder."
98+
operationId = "uploadUserProfilePhotoById",
99+
summary = "Upload specific Users profile photo",
100+
description = "Upload or change specific profile photo within Lowcoder for personalization."
101+
)
102+
@PostMapping(value = "/photo/{orgId}/{userId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
103+
public Mono<ResponseView<Boolean>> uploadProfilePhotoById(@PathVariable String orgId, @PathVariable String userId, @RequestPart("file") Mono<Part> fileMono);
104+
105+
@Operation(
106+
tags = TAG_USER_PROFILE_PHOTO_MANAGEMENT,
107+
operationId = "deleteUserProfilePhotoById",
108+
summary = "Delete specific users profile photo",
109+
description = "Remove the profile Photo associated with the specific User within Lowcoder."
84110
)
85-
@DeleteMapping("/photo")
86-
public Mono<ResponseView<Void>> deleteProfilePhoto();
111+
112+
@DeleteMapping("/photo/{orgId}/{userId}")
113+
public Mono<ResponseView<Void>> deleteProfilePhotoById(@PathVariable String orgId, @PathVariable String userId);
114+
@Operation(
115+
tags = TAG_USER_PROFILE_PHOTO_MANAGEMENT,
116+
operationId = "deleteUserProfilePhoto",
117+
summary = "Delete current users profile photo",
118+
description = "Remove the profile Photo associated with the current User within Lowcoder."
119+
)
120+
@DeleteMapping("/photo")
121+
public Mono<ResponseView<Void>> deleteProfilePhoto();
87122

88123
@Operation(
89124
tags = TAG_USER_PROFILE_PHOTO_MANAGEMENT,
@@ -181,4 +216,7 @@ public record UpdatePasswordRequest(String oldPassword, String newPassword) {
181216
public record MarkUserStatusRequest(String type, Object value) {
182217
}
183218

219+
public record CreateUserRequest(String email, String password) {
220+
}
221+
184222
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy