This is an automated email from the ASF dual-hosted git repository.
isjarana pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git
The following commit(s) were added to refs/heads/develop by this push:
new fe7464f Add user management APIs
new 12af57c Merge pull request #18 from
isururanawaka/microservices_based_impl
fe7464f is described below
commit fe7464f73b4292cf0b8d002dbfd80e23a94c05cd
Author: Isuru Ranawaka <[email protected]>
AuthorDate: Mon Mar 2 23:10:07 2020 -0500
Add user management APIs
---
.../iam/admin/client/IamAdminServiceClient.java | 4 +
.../user/profile/client/UserProfileClient.java | 20 +-
.../apache/custos/iam/service/IamAdminService.java | 54 +-
.../org/apache/custos/iam/utils/IAMOperations.java | 3 +-
.../src/main/proto/IamAdminService.proto | 15 +-
.../mapper/AttributeUpdateMetadataMapper.java | 22 +-
.../profile/mapper/StatusUpdateMetadataMapper.java | 6 +-
.../user/profile/mapper/UserProfileMapper.java | 118 +++-
.../persistance/model/AttributeUpdateMetadata.java | 12 +-
.../persistance/model/StatusUpdateMetadata.java | 12 +-
.../profile/persistance/model/UserAttribute.java | 74 +++
.../{UserProfileEntity.java => UserProfile.java} | 30 +-
.../user/profile/persistance/model/UserRole.java | 74 +++
.../AttributeUpdateMetadataRepository.java | 2 +-
.../{UserRepository.java => RoleRepository.java} | 10 +-
.../repository/StatusUpdateMetadataRepository.java | 2 +-
...epository.java => UserAttributeRepository.java} | 10 +-
.../persistance/repository/UserRepository.java | 6 +-
.../user/profile/service/UserProfileService.java | 129 +++--
.../UserRepository.java => utils/Constants.java} | 15 +-
.../user/profile/validators/InputValidator.java | 45 +-
.../src/main/proto/UserProfileService.proto | 46 +-
.../src/main/resources/application.properties | 2 +-
.../services/clients/keycloak/KeycloakClient.java | 86 ++-
.../src/main/resources/user-management-service.pb | Bin 100895 -> 102049 bytes
.../interceptors/ClientAuthInterceptorImpl.java | 56 +-
.../interceptors/UserAuthInterceptorImpl.java | 81 +--
.../management/service/UserManagementService.java | 597 +++++++++++++++++----
.../src/main/proto/UserManagementService.proto | 27 +-
29 files changed, 1176 insertions(+), 382 deletions(-)
diff --git
a/custos-core-services-client-stubs/iam-admin-core-service-client-stub/src/main/java/org/apache/custos/iam/admin/client/IamAdminServiceClient.java
b/custos-core-services-client-stubs/iam-admin-core-service-client-stub/src/main/java/org/apache/custos/iam/admin/client/IamAdminServiceClient.java
index 0383b55..1cd15a0 100644
---
a/custos-core-services-client-stubs/iam-admin-core-service-client-stub/src/main/java/org/apache/custos/iam/admin/client/IamAdminServiceClient.java
+++
b/custos-core-services-client-stubs/iam-admin-core-service-client-stub/src/main/java/org/apache/custos/iam/admin/client/IamAdminServiceClient.java
@@ -239,6 +239,10 @@ public class IamAdminServiceClient {
return
iamAdminServiceBlockingStub.addUserAttributes(addUserAttributesRequest);
}
+ public OperationStatus deleteUserAttributes (DeleteUserAttributeRequest
deleteUserAttributeRequest) {
+ return
iamAdminServiceBlockingStub.deleteUserAttributes(deleteUserAttributeRequest);
+ }
+
public String getIamServerURL() {
return iamServerURL;
}
diff --git
a/custos-core-services-client-stubs/user-profile-core-service-client-stub/src/main/java/org/apache/custos/user/profile/client/UserProfileClient.java
b/custos-core-services-client-stubs/user-profile-core-service-client-stub/src/main/java/org/apache/custos/user/profile/client/UserProfileClient.java
index 73daefd..9a5366f 100644
---
a/custos-core-services-client-stubs/user-profile-core-service-client-stub/src/main/java/org/apache/custos/user/profile/client/UserProfileClient.java
+++
b/custos-core-services-client-stubs/user-profile-core-service-client-stub/src/main/java/org/apache/custos/user/profile/client/UserProfileClient.java
@@ -56,7 +56,7 @@ public class UserProfileClient {
}
- public void createUserProfileAsync(UserProfile profile, final
ServiceCallback callback) {
+ public void createUserProfileAsync(UserProfileRequest profile, final
ServiceCallback callback) {
StreamObserver observer = this.getObserver(callback, "Create user
profile task failed");
@@ -64,7 +64,7 @@ public class UserProfileClient {
}
- public void updateUserProfileAsync(UserProfile profile, final
ServiceCallback callback) {
+ public void updateUserProfileAsync(UserProfileRequest profile, final
ServiceCallback callback) {
StreamObserver observer = this.getObserver(callback, "Update user
profile task failed");
@@ -72,7 +72,7 @@ public class UserProfileClient {
}
- public void getUserAsync(GetUserProfileRequest request, final
ServiceCallback callback) {
+ public void getUserAsync(UserProfileRequest request, final ServiceCallback
callback) {
StreamObserver observer = this.getObserver(callback, "get user profile
task failed");
@@ -80,7 +80,7 @@ public class UserProfileClient {
}
- public void deleteUserAsync(DeleteUserProfileRequest request, final
ServiceCallback callback) {
+ public void deleteUserAsync(UserProfileRequest request, final
ServiceCallback callback) {
StreamObserver observer = this.getObserver(callback, "delete user
profile task failed");
@@ -97,7 +97,7 @@ public class UserProfileClient {
}
- public void
getAllUserProfilesInTenantAsync(org.apache.custos.user.profile.service.GetAllUserProfilesRequest
request,
+ public void getAllUserProfilesInTenantAsync(UserProfileRequest request,
final ServiceCallback
callback) {
StreamObserver observer = this.getObserver(callback, "get all user
profiles in tenant async");
@@ -105,24 +105,24 @@ public class UserProfileClient {
userProfileServiceStub.getAllUserProfilesInTenant(request, observer);
}
- public UserProfile createUserProfile(UserProfile profile) {
+ public UserProfile createUserProfile(UserProfileRequest profile) {
return userProfileServiceBlockingStub.createUserProfile(profile);
}
- public UserProfile updateUserProfile(UserProfile profile) {
+ public UserProfile updateUserProfile(UserProfileRequest profile) {
return userProfileServiceBlockingStub.updateUserProfile(profile);
}
- public UserProfile getUser(GetUserProfileRequest request) {
+ public UserProfile getUser(UserProfileRequest request) {
return userProfileServiceBlockingStub.getUserProfile(request);
}
- public UserProfile deleteUser(DeleteUserProfileRequest request) {
+ public UserProfile deleteUser(UserProfileRequest request) {
return userProfileServiceBlockingStub.deleteUserProfile(request);
}
@@ -135,7 +135,7 @@ public class UserProfileClient {
}
- public GetAllUserProfilesResponse
getAllUserProfilesInTenant(org.apache.custos.user.profile.service.GetAllUserProfilesRequest
request) {
+ public GetAllUserProfilesResponse
getAllUserProfilesInTenant(UserProfileRequest request) {
return
userProfileServiceBlockingStub.getAllUserProfilesInTenant(request);
}
diff --git
a/custos-core-services/iam-admin-core-service/src/main/java/org/apache/custos/iam/service/IamAdminService.java
b/custos-core-services/iam-admin-core-service/src/main/java/org/apache/custos/iam/service/IamAdminService.java
index 1494dd9..49bc386 100644
---
a/custos-core-services/iam-admin-core-service/src/main/java/org/apache/custos/iam/service/IamAdminService.java
+++
b/custos-core-services/iam-admin-core-service/src/main/java/org/apache/custos/iam/service/IamAdminService.java
@@ -168,7 +168,7 @@ public class IamAdminService extends
IamAdminServiceImplBase {
request.getUser().getFirstName(),
request.getUser().getLastName(),
request.getUser().getEmail(),
- false,
+ request.getUser().getTemporaryPassword(),
request.getAccessToken());
@@ -404,8 +404,6 @@ public class IamAdminService extends
IamAdminServiceImplBase {
@Override
public void deleteUser(UserSearchRequest request,
StreamObserver<CheckingResponse> responseObserver) {
- String userId = request.getUser().getUsername() + "@" +
request.getTenantId();
-
try {
LOGGER.debug("Request received to deleteUser for " +
request.getTenantId());
@@ -414,9 +412,9 @@ public class IamAdminService extends
IamAdminServiceImplBase {
CheckingResponse response =
CheckingResponse.newBuilder().setIsExist(isUpdated).build();
-
statusUpdater.updateStatus(IAMOperations.UPDATE_USER_PROFILE.name(),
+ statusUpdater.updateStatus(IAMOperations.DELETE_USER.name(),
OperationStatus.SUCCESS,
- request.getTenantId(), userId);
+ request.getTenantId(), request.getPerformedBy());
responseObserver.onNext(response);
responseObserver.onCompleted();
@@ -424,9 +422,9 @@ public class IamAdminService extends
IamAdminServiceImplBase {
String msg = "Error occurred during deleteUser" + ex;
LOGGER.error(msg, ex);
-
statusUpdater.updateStatus(IAMOperations.UPDATE_USER_PROFILE.name(),
+ statusUpdater.updateStatus(IAMOperations.DELETE_USER.name(),
OperationStatus.FAILED,
- request.getTenantId(), userId);
+ request.getTenantId(), request.getPerformedBy());
responseObserver.onError(io.grpc.Status.INTERNAL.withDescription(msg).asRuntimeException());
}
@@ -804,7 +802,7 @@ public class IamAdminService extends
IamAdminServiceImplBase {
OperationStatus.FAILED,
request.getTenantId(),
String.valueOf(request.getTenantId()));
- String msg = " Add roles failed for " + request.getTenantId() +
" " + ex.getMessage();
+ String msg = " Add protocol mapper failed for " +
request.getTenantId() + " " + ex.getMessage();
LOGGER.error(msg, ex);
responseObserver.onError(io.grpc.Status.INTERNAL.withDescription(msg).asRuntimeException());
}
@@ -849,6 +847,46 @@ public class IamAdminService extends
IamAdminServiceImplBase {
}
}
+ @Override
+ public void deleteUserAttributes(DeleteUserAttributeRequest request,
+
StreamObserver<org.apache.custos.iam.service.OperationStatus> responseObserver)
{
+ try {
+ LOGGER.debug("Request received to delete user attributes " +
request.getTenantId());
+
+ List<UserAttribute> attributes = request.getAttributesList();
+
+ Map<String, List<String>> attributeMap = new HashMap<>();
+ for (UserAttribute attribute : attributes) {
+ attributeMap.put(attribute.getKey(),
attribute.getValuesList());
+ }
+
+
keycloakClient.deleteUserAttributes(String.valueOf(request.getTenantId()),
request.getAccessToken(), attributeMap, request.getUsersList());
+
+
statusUpdater.updateStatus(IAMOperations.DELETE_USER_ATTRIBUTES.name(),
+ OperationStatus.SUCCESS,
+ request.getTenantId(),
+ request.getPerformedBy());
+
+ org.apache.custos.iam.service.OperationStatus status =
+
org.apache.custos.iam.service.OperationStatus.newBuilder().setStatus(true).build();
+ responseObserver.onNext(status);
+ responseObserver.onCompleted();
+
+ } catch (Exception ex) {
+
statusUpdater.updateStatus(IAMOperations.DELETE_USER_ATTRIBUTES.name(),
+ OperationStatus.FAILED,
+ request.getTenantId(),
+ request.getPerformedBy());
+ String msg = " Add attributes failed for " +
request.getTenantId() + " " + ex.getMessage();
+ LOGGER.error(msg, ex);
+ if (ex.getMessage().contains("HTTP 401 Unauthorized")) {
+
responseObserver.onError(io.grpc.Status.UNAUTHENTICATED.withDescription(msg).asRuntimeException());
+ } else {
+
responseObserver.onError(io.grpc.Status.INTERNAL.withDescription(msg).asRuntimeException());
+ }
+ }
+ }
+
private OperationMetadata convertFromEntity(StatusEntity entity) {
return OperationMetadata.newBuilder()
.setEvent(entity.getEvent())
diff --git
a/custos-core-services/iam-admin-core-service/src/main/java/org/apache/custos/iam/utils/IAMOperations.java
b/custos-core-services/iam-admin-core-service/src/main/java/org/apache/custos/iam/utils/IAMOperations.java
index bafb0c1..edff42a 100644
---
a/custos-core-services/iam-admin-core-service/src/main/java/org/apache/custos/iam/utils/IAMOperations.java
+++
b/custos-core-services/iam-admin-core-service/src/main/java/org/apache/custos/iam/utils/IAMOperations.java
@@ -37,5 +37,6 @@ public enum IAMOperations {
ADD_ROLES_TO_TENANT,
ADD_PROTOCOL_MAPPER,
ADD_USER_ATTRIBUTE,
- ADD_ROLES_TO_USERS
+ ADD_ROLES_TO_USERS,
+ DELETE_USER_ATTRIBUTES
}
diff --git
a/custos-core-services/iam-admin-core-service/src/main/proto/IamAdminService.proto
b/custos-core-services/iam-admin-core-service/src/main/proto/IamAdminService.proto
index fc0bebc..3e739c8 100644
---
a/custos-core-services/iam-admin-core-service/src/main/proto/IamAdminService.proto
+++
b/custos-core-services/iam-admin-core-service/src/main/proto/IamAdminService.proto
@@ -140,6 +140,7 @@ message FindUsersRequest {
int64 tenantId = 1;
string accessToken = 2;
string client_id = 6;
+ string client_sec = 7;
}
@@ -149,6 +150,7 @@ message UserSearchRequest {
string accessToken = 3;
string client_id = 4;
string client_sec = 5;
+ string performedBy = 6;
}
message FindUsersResponse {
@@ -273,7 +275,15 @@ message AddUserAttributesRequest {
string client_id = 4;
string access_token = 5;
string performedBy = 6;
+}
+message DeleteUserAttributeRequest {
+ repeated UserAttribute attributes = 1;
+ repeated string users = 2;
+ int64 tenant_id = 3;
+ string client_id = 4;
+ string access_token = 5;
+ string performedBy = 6;
}
message UserAttribute {
@@ -295,20 +305,19 @@ service IamAdminService {
rpc enableUser (UserSearchRequest) returns (UserRepresentation);
rpc isUserEnabled (UserSearchRequest) returns (CheckingResponse);
rpc isUserExist (UserSearchRequest) returns (CheckingResponse);
-
rpc getUser (UserSearchRequest) returns (UserRepresentation);
rpc findUsers (FindUsersRequest) returns (FindUsersResponse);
rpc resetPassword (ResetUserPassword) returns (CheckingResponse);
+
rpc registerAndEnableUsers (RegisterUsersRequest) returns
(RegisterUsersResponse);
rpc addUserAttributes (AddUserAttributesRequest) returns (OperationStatus);
+ rpc deleteUserAttributes (DeleteUserAttributeRequest) returns
(OperationStatus);
rpc addRolesToUsers (AddUserRolesRequest) returns (OperationStatus);
rpc deleteUser (UserSearchRequest) returns (CheckingResponse);
rpc deleteRolesFromUser (DeleteUserRolesRequest) returns
(CheckingResponse);
-
-
rpc updateUserProfile (UpdateUserProfileRequest) returns
(CheckingResponse);
rpc getOperationMetadata (GetOperationsMetadataRequest) returns
(GetOperationsMetadataResponse);
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/AttributeUpdateMetadataMapper.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/AttributeUpdateMetadataMapper.java
index 611f70a..f56f37c 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/AttributeUpdateMetadataMapper.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/AttributeUpdateMetadataMapper.java
@@ -21,8 +21,7 @@ package org.apache.custos.user.profile.mapper;
import
org.apache.custos.user.profile.persistance.model.AttributeUpdateMetadata;
-import org.apache.custos.user.profile.persistance.model.UserProfileEntity;
-import org.apache.custos.user.profile.service.UserProfile;
+import org.apache.custos.user.profile.persistance.model.UserProfile;
import
org.apache.custos.user.profile.service.UserProfileAttributeUpdateMetadata;
import java.util.HashSet;
@@ -36,15 +35,16 @@ public class AttributeUpdateMetadataMapper {
/**
* This creates Attribute update entity List from comparing oldTenant and
newTenant
+ *
* @param updatedBy
* @return
*/
- public static Set<AttributeUpdateMetadata>
createAttributeUpdateMetadataEntity(UserProfileEntity oldProf,
UserProfileEntity newProf, String updatedBy) {
+ public static Set<AttributeUpdateMetadata>
createAttributeUpdateMetadataEntity(UserProfile oldProf, UserProfile newProf,
String updatedBy) {
Set<AttributeUpdateMetadata> metadataSet = new HashSet<>();
if (!(oldProf.getTenantId() == newProf.getTenantId())) {
AttributeUpdateMetadata attributeUpdateMetadata = new
AttributeUpdateMetadata();
- attributeUpdateMetadata.setUserProfileEntity(newProf);
+ attributeUpdateMetadata.setUserProfile(newProf);
attributeUpdateMetadata.setUpdatedFieldKey("tenanId");
attributeUpdateMetadata.setUpdatedFieldValue(String.valueOf(newProf.getTenantId()));
attributeUpdateMetadata.setUpdatedBy(updatedBy);
@@ -53,7 +53,7 @@ public class AttributeUpdateMetadataMapper {
if (!oldProf.getUsername().equals(newProf.getUsername())) {
AttributeUpdateMetadata attributeUpdateMetadata = new
AttributeUpdateMetadata();
- attributeUpdateMetadata.setUserProfileEntity(newProf);
+ attributeUpdateMetadata.setUserProfile(newProf);
attributeUpdateMetadata.setUpdatedFieldKey("username");
attributeUpdateMetadata.setUpdatedFieldValue(newProf.getUsername());
attributeUpdateMetadata.setUpdatedBy(updatedBy);
@@ -62,7 +62,7 @@ public class AttributeUpdateMetadataMapper {
if (!oldProf.getEmailAddress().equals(newProf.getEmailAddress())) {
AttributeUpdateMetadata attributeUpdateMetadata = new
AttributeUpdateMetadata();
- attributeUpdateMetadata.setUserProfileEntity(newProf);
+ attributeUpdateMetadata.setUserProfile(newProf);
attributeUpdateMetadata.setUpdatedFieldKey("emailAddress");
attributeUpdateMetadata.setUpdatedFieldValue(newProf.getEmailAddress());
attributeUpdateMetadata.setUpdatedBy(updatedBy);
@@ -71,7 +71,7 @@ public class AttributeUpdateMetadataMapper {
if (!oldProf.getFirstName().equals(newProf.getFirstName())) {
AttributeUpdateMetadata attributeUpdateMetadata = new
AttributeUpdateMetadata();
- attributeUpdateMetadata.setUserProfileEntity(newProf);
+ attributeUpdateMetadata.setUserProfile(newProf);
attributeUpdateMetadata.setUpdatedFieldKey("firstName");
attributeUpdateMetadata.setUpdatedFieldValue(newProf.getFirstName());
attributeUpdateMetadata.setUpdatedBy(updatedBy);
@@ -80,7 +80,7 @@ public class AttributeUpdateMetadataMapper {
if (!oldProf.getLastName().equals(newProf.getLastName())) {
AttributeUpdateMetadata attributeUpdateMetadata = new
AttributeUpdateMetadata();
- attributeUpdateMetadata.setUserProfileEntity(newProf);
+ attributeUpdateMetadata.setUserProfile(newProf);
attributeUpdateMetadata.setUpdatedFieldKey("lastName");
attributeUpdateMetadata.setUpdatedFieldValue(newProf.getLastName());
attributeUpdateMetadata.setUpdatedBy(updatedBy);
@@ -93,13 +93,14 @@ public class AttributeUpdateMetadataMapper {
/**
* create attribute update metadata from db entity
+ *
* @param metadata
* @return
*/
- public static UserProfileAttributeUpdateMetadata
createAttributeUpdateMetadataFromEntity (AttributeUpdateMetadata metadata) {
+ public static UserProfileAttributeUpdateMetadata
createAttributeUpdateMetadataFromEntity(AttributeUpdateMetadata metadata) {
return UserProfileAttributeUpdateMetadata.newBuilder()
- .setUpdatedAt(metadata.getUpdatedAt().toString())
+ .setUpdatedAt(metadata.getUpdatedAt().toString())
.setUpdatedBy(metadata.getUpdatedBy())
.setUpdatedAttributeValue(metadata.getUpdatedFieldValue())
.setUpdatedAttribute(metadata.getUpdatedFieldKey())
@@ -107,5 +108,4 @@ public class AttributeUpdateMetadataMapper {
}
-
}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/StatusUpdateMetadataMapper.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/StatusUpdateMetadataMapper.java
index fd2c365..994b0b1 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/StatusUpdateMetadataMapper.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/StatusUpdateMetadataMapper.java
@@ -21,7 +21,7 @@ package org.apache.custos.user.profile.mapper;
import org.apache.custos.user.profile.persistance.model.StatusUpdateMetadata;
-import org.apache.custos.user.profile.persistance.model.UserProfileEntity;
+import org.apache.custos.user.profile.persistance.model.UserProfile;
import org.apache.custos.user.profile.service.UserProfileStatusUpdateMetadata;
import org.apache.custos.user.profile.service.UserStatus;
@@ -41,12 +41,12 @@ public class StatusUpdateMetadataMapper {
* @param updatedBy
* @return
*/
- public static Set<StatusUpdateMetadata>
createStatusUpdateMetadataEntity(UserProfileEntity tenant, String updatedBy) {
+ public static Set<StatusUpdateMetadata>
createStatusUpdateMetadataEntity(UserProfile tenant, String updatedBy) {
Set<StatusUpdateMetadata> metaDataSet = new HashSet<>();
StatusUpdateMetadata metadata = new StatusUpdateMetadata();
- metadata.setUserProfileEntity(tenant);
+ metadata.setUserProfile(tenant);
metadata.setUpdatedBy(updatedBy);
metadata.setUpdatedStatus(tenant.getStatus());
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/UserProfileMapper.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/UserProfileMapper.java
index d04954a..03abc79 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/UserProfileMapper.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/mapper/UserProfileMapper.java
@@ -20,7 +20,15 @@
package org.apache.custos.user.profile.mapper;
-import org.apache.custos.user.profile.persistance.model.UserProfileEntity;
+import org.apache.custos.user.profile.persistance.model.UserAttribute;
+import org.apache.custos.user.profile.persistance.model.UserProfile;
+import org.apache.custos.user.profile.persistance.model.UserRole;
+import org.apache.custos.user.profile.service.UserStatus;
+import org.apache.custos.user.profile.utils.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.*;
/**
@@ -28,6 +36,7 @@ import
org.apache.custos.user.profile.persistance.model.UserProfileEntity;
*/
public class UserProfileMapper {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(UserProfileMapper.class);
/**
* Maps gRPC UserProfile Model to DB Layer UserProfile Entity
@@ -35,20 +44,63 @@ public class UserProfileMapper {
* @param {@link org.apache.custos.user.profile.service.UserProfile} tenant
* @return Tenant
*/
- public static UserProfileEntity
createUserProfileEntityFromUserProfile(org.apache.custos.user.profile.service.UserProfile
userProfile) {
+ public static UserProfile
createUserProfileEntityFromUserProfile(org.apache.custos.user.profile.service.UserProfile
userProfile) {
- UserProfileEntity entity = new UserProfileEntity();
+ UserProfile entity = new UserProfile();
- entity.setId(userProfile.getUserId());
entity.setUsername(userProfile.getUsername());
entity.setEmailAddress(userProfile.getEmail());
entity.setFirstName(userProfile.getFirstName());
entity.setLastName(userProfile.getLastName());
- entity.setTenantId(userProfile.getTenantId());
entity.setStatus(userProfile.getStatus().name());
- return entity;
+ if (userProfile.getAttributesList() != null &&
!userProfile.getAttributesList().isEmpty()) {
+ Set<UserAttribute> attributeSet = new HashSet<>();
+
+ userProfile.getAttributesList().forEach(atr -> {
+ if (atr.getValueList() != null &&
!atr.getValueList().isEmpty()) {
+ for (String value : atr.getValueList()) {
+ UserAttribute userAttribute = new UserAttribute();
+ userAttribute.setKey(atr.getKey());
+ userAttribute.setValue(value);
+ userAttribute.setUserProfile(entity);
+ attributeSet.add(userAttribute);
+ }
+ }
+
+ });
+
+ entity.setUserAttribute(attributeSet);
+ }
+ Set<UserRole> userRoleSet = new HashSet<>();
+ if (userProfile.getRealmRolesList() != null &&
!userProfile.getRealmRolesList().isEmpty()) {
+
+
+ userProfile.getRealmRolesList().forEach(role -> {
+ UserRole userRole = new UserRole();
+ userRole.setValue(role);
+ userRole.setType(Constants.ROLE_TYPE_REALM);
+ userRole.setUserProfile(entity);
+ userRoleSet.add(userRole);
+ });
+
+ entity.setUserRole(userRoleSet);
+ }
+
+ if (userProfile.getClientRolesList() != null &&
!userProfile.getClientRolesList().isEmpty()) {
+ userProfile.getClientRolesList().forEach(role -> {
+ UserRole userRole = new UserRole();
+ userRole.setValue(role);
+ userRole.setType(Constants.ROLE_TYPE_CLIENT);
+ userRole.setUserProfile(entity);
+ userRoleSet.add(userRole);
+ });
+
+ entity.setUserRole(userRoleSet);
+ }
+
+ return entity;
}
@@ -58,15 +110,59 @@ public class UserProfileMapper {
* @param profileEntity
* @return tenant
*/
- public static org.apache.custos.user.profile.service.UserProfile
createUserProfileFromUserProfileEntity(UserProfileEntity profileEntity) {
+ public static org.apache.custos.user.profile.service.UserProfile
createUserProfileFromUserProfileEntity(UserProfile profileEntity) {
+
- return org.apache.custos.user.profile.service.UserProfile.newBuilder()
+ org.apache.custos.user.profile.service.UserProfile.Builder builder =
+
org.apache.custos.user.profile.service.UserProfile.newBuilder();
+
+
+ if (profileEntity.getUserRole() != null &&
!profileEntity.getUserRole().isEmpty()) {
+
+ profileEntity.getUserRole().forEach(role -> {
+ if (role.getType().equals(Constants.ROLE_TYPE_CLIENT)) {
+ builder.addClientRoles(role.getValue());
+ } else {
+ builder.addRealmRoles(role.getValue());
+ }
+ });
+ }
+
+ List<org.apache.custos.user.profile.service.UserAttribute>
attributeList = new ArrayList<>();
+ if (profileEntity.getUserAttribute() != null &&
!profileEntity.getUserAttribute().isEmpty()) {
+
+ Map<String, List<String>> atrMap = new HashMap<>();
+
+ profileEntity.getUserAttribute().forEach(atr -> {
+
+ if (atrMap.get(atr.getKey()) == null) {
+ atrMap.put(atr.getKey(), new ArrayList<String>());
+ }
+ atrMap.get(atr.getKey()).add(atr.getValue());
+
+ });
+
+
+ atrMap.keySet().forEach(key-> {
+ org.apache.custos.user.profile.service.UserAttribute
attribute = org.apache.custos.user.profile.service
+ .UserAttribute
+ .newBuilder()
+ .setKey(key)
+ .addAllValue(atrMap.get(key))
+ .build();
+ attributeList.add(attribute);
+ });
+ }
+
+
+ return builder
.setUsername(profileEntity.getUsername())
.setEmail(profileEntity.getEmailAddress())
.setFirstName(profileEntity.getFirstName())
.setLastName(profileEntity.getLastName())
- .setTenantId(profileEntity.getTenantId())
- .setUserId(profileEntity.getId())
+ .setCreatedAt(profileEntity.getCreatedAt().toString())
+ .setStatus(UserStatus.valueOf(profileEntity.getStatus()))
+ .addAllAttributes(attributeList)
.build();
@@ -77,8 +173,6 @@ public class UserProfileMapper {
StringBuffer buffer = new StringBuffer();
buffer.append("username : " + userProfile.getUsername());
buffer.append("\n");
- buffer.append("tenantId : " + userProfile.getTenantId());
- buffer.append("\n");
buffer.append("emailAddress : " + userProfile.getEmail());
buffer.append("\n");
buffer.append("firstName : " + userProfile.getFirstName());
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/AttributeUpdateMetadata.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/AttributeUpdateMetadata.java
index 5ca7b96..ed1a829 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/AttributeUpdateMetadata.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/AttributeUpdateMetadata.java
@@ -52,8 +52,8 @@ public class AttributeUpdateMetadata {
private String updatedBy;
@ManyToOne(fetch = FetchType.LAZY)
- @JoinColumn(name = "user_profile_entity_id")
- private UserProfileEntity userProfileEntity;
+ @JoinColumn(name = "user_profile_id")
+ private UserProfile userProfile;
public Long getId() {
@@ -96,11 +96,11 @@ public class AttributeUpdateMetadata {
this.updatedAt = updatedAt;
}
- public UserProfileEntity getUserProfileEntity() {
- return userProfileEntity;
+ public UserProfile getUserProfile() {
+ return userProfile;
}
- public void setUserProfileEntity(UserProfileEntity userProfileEntity) {
- this.userProfileEntity = userProfileEntity;
+ public void setUserProfile(UserProfile userProfile) {
+ this.userProfile = userProfile;
}
}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/StatusUpdateMetadata.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/StatusUpdateMetadata.java
index fe0629c..45dcc13 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/StatusUpdateMetadata.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/StatusUpdateMetadata.java
@@ -49,8 +49,8 @@ public class StatusUpdateMetadata {
private String updatedBy;
@ManyToOne(fetch = FetchType.LAZY)
- @JoinColumn(name = "user_profile_entity_id")
- private UserProfileEntity userProfileEntity;
+ @JoinColumn(name = "user_profile_id")
+ private UserProfile userProfile;
public Long getId() {
return id;
@@ -85,11 +85,11 @@ public class StatusUpdateMetadata {
this.updatedAt = updatedAt;
}
- public UserProfileEntity getUserProfileEntity() {
- return userProfileEntity;
+ public UserProfile getUserProfile() {
+ return userProfile;
}
- public void setUserProfileEntity(UserProfileEntity userProfileEntity) {
- this.userProfileEntity = userProfileEntity;
+ public void setUserProfile(UserProfile userProfile) {
+ this.userProfile = userProfile;
}
}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserAttribute.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserAttribute.java
new file mode 100644
index 0000000..4ea49b8
--- /dev/null
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserAttribute.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.custos.user.profile.persistance.model;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name = "user_attribute")
+public class UserAttribute {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private Long id;
+
+ @Column(nullable = false)
+ private String keyValue;
+
+ @Column(nullable = false)
+ private String value;
+
+ @ManyToOne
+ @JoinColumn(name = "user_profile_id")
+ private UserProfile userProfile;
+
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getKey() {
+ return keyValue;
+ }
+
+ public void setKey(String key) {
+ this.keyValue = key;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+ public UserProfile getUserProfile() {
+ return userProfile;
+ }
+
+ public void setUserProfile(UserProfile userProfile) {
+ this.userProfile = userProfile;
+ }
+}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserProfileEntity.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserProfile.java
similarity index 80%
rename from
custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserProfileEntity.java
rename to
custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserProfile.java
index 068e72d..b2ce0a9 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserProfileEntity.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserProfile.java
@@ -30,9 +30,9 @@ import java.util.Set;
* User profile entity model
*/
@Entity
-@Table(name = "user_profile_entity")
+@Table(name = "user_profile")
@EntityListeners(AuditingEntityListener.class)
-public class UserProfileEntity {
+public class UserProfile {
@Id
private String id;
@@ -61,10 +61,16 @@ public class UserProfileEntity {
private Date createdAt;
- @OneToMany(mappedBy = "userProfileEntity", cascade = CascadeType.ALL)
+ @OneToMany(fetch = FetchType.EAGER, mappedBy = "userProfile",
orphanRemoval=true, cascade = CascadeType.ALL)
+ private Set<UserRole> userRole;
+
+ @OneToMany(fetch = FetchType.EAGER, mappedBy =
"userProfile",orphanRemoval=true, cascade = CascadeType.ALL)
+ private Set<UserAttribute> userAttribute;
+
+ @OneToMany(mappedBy = "userProfile", cascade = CascadeType.ALL)
private Set<AttributeUpdateMetadata> attributeUpdateMetadata;
- @OneToMany(mappedBy = "userProfileEntity", cascade = CascadeType.ALL)
+ @OneToMany(mappedBy = "userProfile", cascade = CascadeType.ALL)
private Set<StatusUpdateMetadata> statusUpdateMetadata;
@@ -148,4 +154,20 @@ public class UserProfileEntity {
public void setStatusUpdateMetadata(Set<StatusUpdateMetadata>
statusUpdateMetadata) {
this.statusUpdateMetadata = statusUpdateMetadata;
}
+
+ public Set<UserAttribute> getUserAttribute() {
+ return userAttribute;
+ }
+
+ public void setUserAttribute(Set<UserAttribute> userAttribute) {
+ this.userAttribute = userAttribute;
+ }
+
+ public Set<UserRole> getUserRole() {
+ return userRole;
+ }
+
+ public void setUserRole(Set<UserRole> userRole) {
+ this.userRole = userRole;
+ }
}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserRole.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserRole.java
new file mode 100644
index 0000000..5daaf07
--- /dev/null
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/model/UserRole.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.custos.user.profile.persistance.model;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name = "user_role")
+public class UserRole {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private Long id;
+
+ @Column(nullable = false)
+ private String type;
+
+ @Column(nullable = false)
+ private String value;
+
+ @ManyToOne
+ @JoinColumn(name = "user_profile_id")
+ private UserProfile userProfile;
+
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+ public UserProfile getUserProfile() {
+ return userProfile;
+ }
+
+ public void setUserProfile(UserProfile userProfile) {
+ this.userProfile = userProfile;
+ }
+}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/AttributeUpdateMetadataRepository.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/AttributeUpdateMetadataRepository.java
index 2d077ba..c3365dd 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/AttributeUpdateMetadataRepository.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/AttributeUpdateMetadataRepository.java
@@ -26,5 +26,5 @@ import java.util.List;
public interface AttributeUpdateMetadataRepository extends
JpaRepository<AttributeUpdateMetadata, Long> {
- public List<AttributeUpdateMetadata> findAllByUserProfileEntityId(String
userId);
+ public List<AttributeUpdateMetadata> findAllByUserProfileId(String userId);
}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/RoleRepository.java
similarity index 76%
copy from
custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
copy to
custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/RoleRepository.java
index 4c9858a..fc5a465 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/RoleRepository.java
@@ -19,14 +19,14 @@
package org.apache.custos.user.profile.persistance.repository;
-import org.apache.custos.user.profile.persistance.model.UserProfileEntity;
+import org.apache.custos.user.profile.persistance.model.UserRole;
import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.transaction.annotation.Transactional;
-import java.util.List;
+public interface RoleRepository extends JpaRepository<UserRole,Long> {
-public interface UserRepository extends JpaRepository<UserProfileEntity,
String> {
-
- public List<UserProfileEntity> findByTenantId(long tenantId);
+ @Transactional
+ public void deleteAllByUserProfileId(String userProfileEntityId);
}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/StatusUpdateMetadataRepository.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/StatusUpdateMetadataRepository.java
index ae8611a..2bdebe2 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/StatusUpdateMetadataRepository.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/StatusUpdateMetadataRepository.java
@@ -27,5 +27,5 @@ import java.util.List;
public interface StatusUpdateMetadataRepository extends
JpaRepository<StatusUpdateMetadata, Long> {
- public List<StatusUpdateMetadata> findAllByUserProfileEntityId(String
userId);
+ public List<StatusUpdateMetadata> findAllByUserProfileId(String userId);
}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserAttributeRepository.java
similarity index 70%
copy from
custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
copy to
custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserAttributeRepository.java
index 4c9858a..0c300b5 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserAttributeRepository.java
@@ -19,14 +19,16 @@
package org.apache.custos.user.profile.persistance.repository;
-import org.apache.custos.user.profile.persistance.model.UserProfileEntity;
+import org.apache.custos.user.profile.persistance.model.UserAttribute;
import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.transaction.annotation.Transactional;
import java.util.List;
-public interface UserRepository extends JpaRepository<UserProfileEntity,
String> {
-
- public List<UserProfileEntity> findByTenantId(long tenantId);
+public interface UserAttributeRepository extends JpaRepository<UserAttribute,
Long> {
+ @Transactional
+ public void deleteAllByUserProfileId(String userProfileEntityId);
+ public List<UserAttribute> findAllByUserProfileId(String
userProfileEntityId);
}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
index 4c9858a..52b85a8 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
@@ -19,14 +19,14 @@
package org.apache.custos.user.profile.persistance.repository;
-import org.apache.custos.user.profile.persistance.model.UserProfileEntity;
+import org.apache.custos.user.profile.persistance.model.UserProfile;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
-public interface UserRepository extends JpaRepository<UserProfileEntity,
String> {
+public interface UserRepository extends JpaRepository<UserProfile, String> {
- public List<UserProfileEntity> findByTenantId(long tenantId);
+ public List<UserProfile> findByTenantId(long tenantId);
}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/service/UserProfileService.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/service/UserProfileService.java
index 5f9642b..a17da49 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/service/UserProfileService.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/service/UserProfileService.java
@@ -26,10 +26,8 @@ import
org.apache.custos.user.profile.mapper.StatusUpdateMetadataMapper;
import org.apache.custos.user.profile.mapper.UserProfileMapper;
import
org.apache.custos.user.profile.persistance.model.AttributeUpdateMetadata;
import org.apache.custos.user.profile.persistance.model.StatusUpdateMetadata;
-import org.apache.custos.user.profile.persistance.model.UserProfileEntity;
-import
org.apache.custos.user.profile.persistance.repository.AttributeUpdateMetadataRepository;
-import
org.apache.custos.user.profile.persistance.repository.StatusUpdateMetadataRepository;
-import org.apache.custos.user.profile.persistance.repository.UserRepository;
+import org.apache.custos.user.profile.persistance.model.UserProfile;
+import org.apache.custos.user.profile.persistance.repository.*;
import org.lognet.springboot.grpc.GRpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -54,30 +52,36 @@ public class UserProfileService extends
UserProfileServiceGrpc.UserProfileServic
@Autowired
private AttributeUpdateMetadataRepository
attributeUpdateMetadataRepository;
+ @Autowired
+ private UserAttributeRepository userAttributeRepository;
+
+ @Autowired
+ private RoleRepository roleRepository;
+
@Override
- public void createUserProfile(UserProfile request,
StreamObserver<UserProfile> responseObserver) {
+ public void createUserProfile(UserProfileRequest request,
StreamObserver<org.apache.custos.user.profile.service.UserProfile>
responseObserver) {
try {
- LOGGER.debug("Request received to createUserProfile for " +
request.getUsername() + "at " + request.getTenantId());
-
- String userId = request.getUsername() + "@" +
request.getTenantId();
+ LOGGER.debug("Request received to createUserProfile for " +
request.getProfile().getUsername() + "at " + request.getTenantId());
- UserProfile profile =
request.toBuilder().setUserId(userId).build();
+ String userId = request.getProfile().getUsername() + "@" +
request.getTenantId();
- Optional<UserProfileEntity> op = repository.findById(userId);
+ Optional<UserProfile> op = repository.findById(userId);
if (op.isEmpty()) {
- UserProfileEntity entity =
UserProfileMapper.createUserProfileEntityFromUserProfile(profile);
-
+ UserProfile entity =
UserProfileMapper.createUserProfileEntityFromUserProfile(request.getProfile());
+ entity.setId(userId);
+ entity.setTenantId(request.getTenantId());
repository.save(entity);
}
- responseObserver.onNext(profile);
+ responseObserver.onNext(request.getProfile());
responseObserver.onCompleted();
} catch (Exception ex) {
- String msg = "Error occurred while creating user profile for " +
request.getUsername() + "at " + request.getTenantId();
+ String msg = "Error occurred while creating user profile for " +
request.getProfile().getUsername() + "at "
+ + request.getTenantId() + " reason :" + ex.getMessage();
LOGGER.error(msg);
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
}
@@ -85,86 +89,109 @@ public class UserProfileService extends
UserProfileServiceGrpc.UserProfileServic
}
@Override
- public void updateUserProfile(UserProfile request,
StreamObserver<UserProfile> responseObserver) {
+ public void updateUserProfile(UserProfileRequest request,
StreamObserver<org.apache.custos.user.profile.service.UserProfile>
responseObserver) {
try {
- LOGGER.debug("Request received to updateUserProfile for " +
request.getUsername() + "at " + request.getTenantId());
+ LOGGER.debug("Request received to updateUserProfile for " +
request.getProfile().getUsername() + "at " + request.getTenantId());
+
+ String userId = request.getProfile().getUsername() + "@" +
request.getTenantId();
- Optional<UserProfileEntity> exEntity =
repository.findById(request.getUserId());
+ Optional<UserProfile> exEntity = repository.findById(userId);
- if (exEntity.isPresent()) {
+ if (exEntity.isPresent()) {
- UserProfileEntity entity =
UserProfileMapper.createUserProfileEntityFromUserProfile(request);
+ UserProfile entity =
UserProfileMapper.createUserProfileEntityFromUserProfile(request.getProfile());
- Set<AttributeUpdateMetadata> metadata =
AttributeUpdateMetadataMapper.
- createAttributeUpdateMetadataEntity(exEntity.get(),
entity, request.getUpdatedBy());
+ Set<AttributeUpdateMetadata> metadata =
AttributeUpdateMetadataMapper.
+ createAttributeUpdateMetadataEntity(exEntity.get(),
entity, request.getPerformedBy());
- entity.setAttributeUpdateMetadata(metadata);
- repository.save(entity);
+ entity.setAttributeUpdateMetadata(metadata);
+ entity.setId(userId);
+ entity.setTenantId(request.getTenantId());
+ entity.setCreatedAt(exEntity.get().getCreatedAt());
- responseObserver.onNext(request);
- responseObserver.onCompleted();
- } else {
+ UserProfile exProfile = exEntity.get();
+ if (exProfile.getUserAttribute() != null) {
+ exProfile.getUserAttribute().forEach(atr -> {
+ userAttributeRepository.delete(atr);
- }
+ });
+ }
+
+ if (exProfile.getUserRole() != null) {
+ exProfile.getUserRole().forEach(role -> {
+ roleRepository.delete(role);
+ });
+ }
+
+ repository.save(entity);
+
+ responseObserver.onNext(request.getProfile());
+ responseObserver.onCompleted();
+ } else {
+ String msg = "Cannot find a user profile for " + userId;
+ LOGGER.error(msg);
+
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
+ }
} catch (Exception ex) {
- String msg = "Error occurred while updating user profile for " +
request.getUsername() + "at " + request.getTenantId();
+ String msg = "Error occurred while updating user profile for " +
request.getProfile().getUsername() + "at "
+ + request.getTenantId() + " reason :" + ex.getMessage();
LOGGER.error(msg);
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
}
}
@Override
- public void getUserProfile(GetUserProfileRequest request,
StreamObserver<UserProfile> responseObserver) {
+ public void getUserProfile(UserProfileRequest request,
StreamObserver<org.apache.custos.user.profile.service.UserProfile>
responseObserver) {
try {
- LOGGER.debug("Request received to getUserProfile for " +
request.getUsername() + "at " + request.getTenantId());
+ LOGGER.debug("Request received to getUserProfile for " +
request.getProfile().getUsername() + "at " + request.getTenantId());
- String userId = request.getUsername() + "@" +
request.getTenantId();
+ String userId = request.getProfile().getUsername() + "@" +
request.getTenantId();
- Optional<UserProfileEntity> entity = repository.findById(userId);
+ Optional<UserProfile> entity = repository.findById(userId);
if (entity.isPresent()) {
- UserProfileEntity profileEntity = entity.get();
-
- UserProfile profile =
UserProfileMapper.createUserProfileFromUserProfileEntity(profileEntity);
+ UserProfile profileEntity = entity.get();
+ org.apache.custos.user.profile.service.UserProfile profile =
UserProfileMapper.createUserProfileFromUserProfileEntity(profileEntity);
responseObserver.onNext(profile);
responseObserver.onCompleted();
} else {
-
responseObserver.onError(Status.NOT_FOUND.withDescription("User not
found").asRuntimeException());
+ responseObserver.onNext(null);
+ responseObserver.onCompleted();
}
} catch (Exception ex) {
- String msg = "Error occurred while fetching user profile for " +
request.getUsername() + "at " + request.getTenantId();
+ String msg = "Error occurred while fetching user profile for " +
request.getProfile().getUsername() + "at " + request.getTenantId();
LOGGER.error(msg);
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
}
}
@Override
- public void deleteUserProfile(DeleteUserProfileRequest request,
StreamObserver<UserProfile> responseObserver) {
+ public void deleteUserProfile(UserProfileRequest request,
StreamObserver<org.apache.custos.user.profile.service.UserProfile>
responseObserver) {
try {
- LOGGER.debug("Request received to deleteUserProfile for " +
request.getUsername() + "at " + request.getTenantId());
+ LOGGER.debug("Request received to deleteUserProfile for " +
request.getProfile().getUsername() + "at " + request.getTenantId());
long tenantId = request.getTenantId();
- String username = request.getUsername();
+ String username = request.getProfile().getUsername();
String userId = username + "@" + tenantId;
- Optional<UserProfileEntity> profileEntity =
repository.findById(userId);
+ Optional<UserProfile> profileEntity = repository.findById(userId);
if (profileEntity.isPresent()) {
- UserProfileEntity entity = profileEntity.get();
+ UserProfile entity = profileEntity.get();
- UserProfile prof =
UserProfileMapper.createUserProfileFromUserProfileEntity(entity);
+ org.apache.custos.user.profile.service.UserProfile prof =
UserProfileMapper.createUserProfileFromUserProfileEntity(entity);
repository.delete(profileEntity.get());
responseObserver.onNext(prof);
@@ -175,26 +202,26 @@ public class UserProfileService extends
UserProfileServiceGrpc.UserProfileServic
}
} catch (Exception ex) {
- String msg = "Error occurred while deleting user profile for " +
request.getUsername() + "at " + request.getTenantId();
+ String msg = "Error occurred while deleting user profile for " +
request.getProfile().getUsername() + "at " + request.getTenantId();
LOGGER.error(msg);
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
}
}
@Override
- public void getAllUserProfilesInTenant(GetAllUserProfilesRequest request,
+ public void getAllUserProfilesInTenant(UserProfileRequest request,
StreamObserver<GetAllUserProfilesResponse> responseObserver) {
try {
LOGGER.debug("Request received to getAllUserProfilesInTenant for "
+ request.getTenantId());
long tenantId = request.getTenantId();
- List<UserProfileEntity> profileList =
repository.findByTenantId(tenantId);
+ List<UserProfile> profileList =
repository.findByTenantId(tenantId);
- List<UserProfile> userProfileList = new ArrayList<>();
+ List<org.apache.custos.user.profile.service.UserProfile>
userProfileList = new ArrayList<>();
if (profileList != null && profileList.size() > 0) {
- for (UserProfileEntity entity : profileList) {
- UserProfile prof =
UserProfileMapper.createUserProfileFromUserProfileEntity(entity);
+ for (UserProfile entity : profileList) {
+ org.apache.custos.user.profile.service.UserProfile prof =
UserProfileMapper.createUserProfileFromUserProfileEntity(entity);
userProfileList.add(prof);
}
}
@@ -226,9 +253,9 @@ public class UserProfileService extends
UserProfileServiceGrpc.UserProfileServic
String userId = username + "@" + tenantId;
- List<StatusUpdateMetadata> statusUpdateMetadata =
statusUpdaterRepository.findAllByUserProfileEntityId(userId);
+ List<StatusUpdateMetadata> statusUpdateMetadata =
statusUpdaterRepository.findAllByUserProfileId(userId);
- List<AttributeUpdateMetadata> attributeUpdateMetadata =
attributeUpdateMetadataRepository.findAllByUserProfileEntityId(userId);
+ List<AttributeUpdateMetadata> attributeUpdateMetadata =
attributeUpdateMetadataRepository.findAllByUserProfileId(userId);
List<UserProfileStatusUpdateMetadata>
userProfileStatusUpdateMetadata = new ArrayList<>();
List<UserProfileAttributeUpdateMetadata>
userProfileAttributeUpdateMetadata = new ArrayList<>();
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/utils/Constants.java
similarity index 68%
copy from
custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
copy to
custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/utils/Constants.java
index 4c9858a..7a7a877 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/persistance/repository/UserRepository.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/utils/Constants.java
@@ -17,16 +17,9 @@
* under the License.
*/
-package org.apache.custos.user.profile.persistance.repository;
-
-import org.apache.custos.user.profile.persistance.model.UserProfileEntity;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-import java.util.List;
-
-public interface UserRepository extends JpaRepository<UserProfileEntity,
String> {
-
- public List<UserProfileEntity> findByTenantId(long tenantId);
-
+package org.apache.custos.user.profile.utils;
+public final class Constants {
+ public static final String ROLE_TYPE_REALM = "realm";
+ public static final String ROLE_TYPE_CLIENT = "client";
}
diff --git
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/validators/InputValidator.java
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/validators/InputValidator.java
index d13ea59..79f64d8 100644
---
a/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/validators/InputValidator.java
+++
b/custos-core-services/user-profile-core-service/src/main/java/org/apache/custos/user/profile/validators/InputValidator.java
@@ -21,7 +21,9 @@ package org.apache.custos.user.profile.validators;
import org.apache.custos.core.services.commons.Validator;
import
org.apache.custos.core.services.commons.exceptions.MissingParameterException;
-import org.apache.custos.user.profile.service.*;
+import org.apache.custos.user.profile.service.GetUpdateAuditTrailRequest;
+import org.apache.custos.user.profile.service.UserProfile;
+import org.apache.custos.user.profile.service.UserProfileRequest;
/**
* Validate inputs
@@ -49,31 +51,30 @@ public class InputValidator implements Validator {
private boolean validateUserProfile(Object obj, String method) {
- if (obj instanceof UserProfile) {
- UserProfile profile = (UserProfile) obj;
- if (method.equals("updateUserProfile")) {
- if (profile.getUserId() == null ||
profile.getUserId().equals("")) {
- throw new MissingParameterException("userId should not be
null", null);
- }
- }
+ if (obj instanceof UserProfileRequest) {
+ UserProfileRequest profile = (UserProfileRequest) obj;
+
if (profile.getTenantId() == 0) {
throw new MissingParameterException("tenantId should be valid
", null);
}
- if (profile.getUsername() == null ||
profile.getUsername().equals("")) {
+ if (profile.getProfile() == null) {
+ throw new MissingParameterException("Profile should be valid
", null);
+ }
+ if (profile.getProfile().getUsername() == null ||
profile.getProfile().getUsername().equals("")) {
throw new MissingParameterException("username should not be
null", null);
}
- if (profile.getFirstName() == null ||
profile.getFirstName().equals("")) {
+ if (profile.getProfile().getFirstName() == null ||
profile.getProfile().getFirstName().equals("")) {
throw new MissingParameterException("firstName should not be
null", null);
}
- if (profile.getLastName() == null ||
profile.getLastName().equals("")) {
+ if (profile.getProfile().getLastName() == null ||
profile.getProfile().getLastName().equals("")) {
throw new MissingParameterException("lastName should not be
null", null);
}
- if (profile.getEmail() == null || profile.getEmail().equals("")) {
+ if (profile.getProfile().getEmail() == null ||
profile.getProfile().getEmail().equals("")) {
throw new MissingParameterException("emailAddress should not
be null", null);
}
} else {
- throw new RuntimeException("Unexpected input type for method" +
method);
+ throw new RuntimeException("Unexpected input type for method " +
method);
}
return true;
}
@@ -81,20 +82,26 @@ public class InputValidator implements Validator {
private boolean validateUsernameAndTenantId(Object obj, String method) {
if (method.equals("getUserProfile")) {
- GetUserProfileRequest profileRequest = (GetUserProfileRequest) obj;
+ UserProfileRequest profileRequest = (UserProfileRequest) obj;
if (profileRequest.getTenantId() == 0) {
throw new MissingParameterException("tenantId should be valid
", null);
}
- if (profileRequest.getUsername() == null ||
profileRequest.getUsername().equals("")) {
+ if (profileRequest.getProfile() == null) {
+ throw new MissingParameterException("Profile should be valid
", null);
+ }
+ if (profileRequest.getProfile().getUsername() == null ||
profileRequest.getProfile().getUsername().equals("")) {
throw new MissingParameterException("username should not be
null", null);
}
} else if (method.equals("deleteUserProfile")) {
- DeleteUserProfileRequest profileRequest =
(DeleteUserProfileRequest) obj;
+ UserProfileRequest profileRequest = (UserProfileRequest) obj;
if (profileRequest.getTenantId() == 0) {
throw new MissingParameterException("tenantId should be valid
", null);
}
- if (profileRequest.getUsername() == null ||
profileRequest.getUsername().equals("")) {
+ if (profileRequest.getProfile() == null) {
+ throw new MissingParameterException("Profile should be valid
", null);
+ }
+ if (profileRequest.getProfile().getUsername() == null ||
profileRequest.getProfile().getUsername().equals("")) {
throw new MissingParameterException("username should not be
null", null);
}
@@ -115,8 +122,8 @@ public class InputValidator implements Validator {
}
private boolean validateGetAllUserProfiles(Object obj, String method) {
- if (obj instanceof GetAllUserProfilesRequest) {
- GetAllUserProfilesRequest profileReq = (GetAllUserProfilesRequest)
obj;
+ if (obj instanceof UserProfileRequest) {
+ UserProfileRequest profileReq = (UserProfileRequest) obj;
if (profileReq.getTenantId() == 0) {
throw new MissingParameterException("tenantId should be valid
", null);
}
diff --git
a/custos-core-services/user-profile-core-service/src/main/proto/UserProfileService.proto
b/custos-core-services/user-profile-core-service/src/main/proto/UserProfileService.proto
index c626cbf..bf095b9 100644
---
a/custos-core-services/user-profile-core-service/src/main/proto/UserProfileService.proto
+++
b/custos-core-services/user-profile-core-service/src/main/proto/UserProfileService.proto
@@ -43,33 +43,31 @@ enum UserStatus {
message UserProfile {
- int64 tenantId = 1;
- string username = 2;
- string email = 3;
- string first_name = 4;
- string last_name = 5;
- string created_at = 6;
- string last_access_at = 7;
- int32 valid_till = 8;
- UserStatus status = 9;
- string user_id = 10;
- string updated_by = 11;
+ string username = 1;
+ string email = 2;
+ string first_name = 3;
+ string last_name = 4;
+ string created_at = 5;
+ UserStatus status = 6;
+ repeated UserAttribute attributes = 7;
+ repeated string client_roles = 8;
+ repeated string realm_roles = 9;
}
-message GetUserProfileRequest {
+message UserProfileRequest {
int64 tenantId = 1;
- string username = 2;
+ UserProfile profile = 2;
+ string performedBy = 3;
}
-message DeleteUserProfileRequest {
- int64 tenantId = 1;
- string username = 2;
-}
-message GetAllUserProfilesRequest {
- int64 tenantId = 1;
+message UserAttribute {
+ int64 id = 1;
+ string key = 2;
+ repeated string value = 3;
}
+
message GetAllUserProfilesResponse {
repeated UserProfile profiles = 1;
}
@@ -98,10 +96,10 @@ message GetUpdateAuditTrailResponse {
}
service UserProfileService {
- rpc createUserProfile (UserProfile) returns (UserProfile);
- rpc updateUserProfile (UserProfile) returns (UserProfile);
- rpc getUserProfile (GetUserProfileRequest) returns (UserProfile);
- rpc deleteUserProfile (DeleteUserProfileRequest) returns (UserProfile);
- rpc getAllUserProfilesInTenant (GetAllUserProfilesRequest) returns
(GetAllUserProfilesResponse);
+ rpc createUserProfile (UserProfileRequest) returns (UserProfile);
+ rpc updateUserProfile (UserProfileRequest) returns (UserProfile);
+ rpc getUserProfile (UserProfileRequest) returns (UserProfile);
+ rpc deleteUserProfile (UserProfileRequest) returns (UserProfile);
+ rpc getAllUserProfilesInTenant (UserProfileRequest) returns
(GetAllUserProfilesResponse);
rpc getUserProfileAuditTrails (GetUpdateAuditTrailRequest) returns
(GetUpdateAuditTrailResponse);
}
\ No newline at end of file
diff --git
a/custos-core-services/user-profile-core-service/src/main/resources/application.properties
b/custos-core-services/user-profile-core-service/src/main/resources/application.properties
index eb0b70d..c045435 100644
---
a/custos-core-services/user-profile-core-service/src/main/resources/application.properties
+++
b/custos-core-services/user-profile-core-service/src/main/resources/application.properties
@@ -35,6 +35,6 @@ spring.datasource.password = root
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.MySQL5InnoDBDialect
-
+spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
\ No newline at end of file
diff --git
a/custos-federated-services-clients/src/main/java/org/apache/custos/federated/services/clients/keycloak/KeycloakClient.java
b/custos-federated-services-clients/src/main/java/org/apache/custos/federated/services/clients/keycloak/KeycloakClient.java
index f07c088..8236d05 100644
---
a/custos-federated-services-clients/src/main/java/org/apache/custos/federated/services/clients/keycloak/KeycloakClient.java
+++
b/custos-federated-services-clients/src/main/java/org/apache/custos/federated/services/clients/keycloak/KeycloakClient.java
@@ -560,7 +560,7 @@ public class KeycloakClient {
Keycloak client = null;
try {
- client = getClient(iamServerURL, realmId, accessToken);
+ client = getClient(iamServerURL, realmId, accessToken);
UserRepresentation representation = getUserByUsername(client,
realmId, username.toLowerCase());
if (representation != null) {
@@ -576,7 +576,7 @@ public class KeycloakClient {
for (String roleName : roles) {
RoleResource roleResource = client.realm(realmId).
clients().get(clientRep.getId()).roles().get(roleName);
- LOGGER.info("Roles Representatioin "+ roleName+ "
roles resource " + roleResource);
+ LOGGER.info("Roles Representatioin " + roleName +
" roles resource " + roleResource);
if (roleResource != null) {
roleRepresentations.add(roleResource.toRepresentation());
}
@@ -703,7 +703,17 @@ public class KeycloakClient {
UserRepresentation userRepresentation =
getUserByUsername(client, realmId, user.toLowerCase());
UserResource resource =
realmResource.users().get(userRepresentation.getId());
- userRepresentation.setAttributes(attributeMap);
+ Map<String, List<String>> exAtrMap =
userRepresentation.getAttributes();
+
+ if (exAtrMap != null && !exAtrMap.isEmpty()) {
+ attributeMap.keySet().forEach(key -> {
+ exAtrMap.put(key, attributeMap.get(key));
+ });
+ userRepresentation.setAttributes(exAtrMap);
+ } else {
+ userRepresentation.setAttributes(attributeMap);
+ }
+
resource.update(userRepresentation);
}
@@ -724,6 +734,57 @@ public class KeycloakClient {
/**
+ * This deletes user attributes of users
+ *
+ * @param realmId
+ * @param attributeMap
+ * @param users
+ * @return
+ */
+ public boolean deleteUserAttributes(String realmId, String accessToken,
Map<String, List<String>> attributeMap, List<String> users) {
+ Keycloak client = null;
+ try {
+ client = getClient(iamServerURL, realmId, accessToken);
+
+ RealmResource realmResource = client.realm(realmId);
+
+ for (String user : users) {
+
+ UserRepresentation userRepresentation =
getUserByUsername(client, realmId, user.toLowerCase());
+ UserResource resource =
realmResource.users().get(userRepresentation.getId());
+
+ Map<String, List<String>> exAtrMap =
userRepresentation.getAttributes();
+
+ if (exAtrMap != null && !exAtrMap.isEmpty()) {
+ attributeMap.keySet().forEach(key -> {
+ List<String> stringList = exAtrMap.get(key);
+ if (stringList != null && !stringList.isEmpty()) {
+ stringList.removeAll(attributeMap.get(key));
+ exAtrMap.put(key,stringList);
+ }
+ });
+ userRepresentation.setAttributes(exAtrMap);
+ }
+
+ resource.update(userRepresentation);
+ }
+
+
+ } catch (Exception ex) {
+ String msg = "Error occurred while deleting user attributes in
Keycloak Server, reason: " + ex.getMessage();
+ LOGGER.error(msg, ex);
+ throw new RuntimeException(msg, ex);
+
+ } finally {
+ if (client != null) {
+ client.close();
+ }
+ }
+ return true;
+
+ }
+
+ /**
* Create protocol mapper representation in given client
*
* @param protocolMapperRepresentations
@@ -745,25 +806,6 @@ public class KeycloakClient {
ProtocolMappersResource resource =
realmResource.clients().get(representation.getId()).getProtocolMappers();
resource.createMapper(protocolMapperRepresentations);
- ProtocolMappersResource resource2 =
realmResource.clients().get(representation.getId()).getProtocolMappers();
-
- List<ProtocolMapperRepresentation> mapperRepresentations =
resource2.getMappers();
-
- for (ProtocolMapperRepresentation protocolMapperRepresentation :
mapperRepresentations) {
-
- LOGGER.info("Id " + protocolMapperRepresentation.getId());
- LOGGER.info("Name " + protocolMapperRepresentation.getName());
- LOGGER.info("Protocol " +
protocolMapperRepresentation.getProtocol());
- LOGGER.info("Protocol Mapper " +
protocolMapperRepresentation.getProtocolMapper());
- Map<String, String> config =
protocolMapperRepresentation.getConfig();
-
- for (String key : config.keySet()) {
- LOGGER.info("Key " + key + " value" + config.get(key));
-
- }
-
- }
-
} catch (Exception ex) {
String msg = "Error occurred while adding protocol mappers in
Keycloak Server, reason: " + ex.getMessage();
diff --git
a/custos-integration-services/user-management-service-parent/user-management-service-sidecar/src/main/resources/user-management-service.pb
b/custos-integration-services/user-management-service-parent/user-management-service-sidecar/src/main/resources/user-management-service.pb
index 946e614..7cb68d5 100644
Binary files
a/custos-integration-services/user-management-service-parent/user-management-service-sidecar/src/main/resources/user-management-service.pb
and
b/custos-integration-services/user-management-service-parent/user-management-service-sidecar/src/main/resources/user-management-service.pb
differ
diff --git
a/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/interceptors/ClientAuthInterceptorImpl.java
b/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/interceptors/ClientAuthInterceptorImpl.java
index 5e347d0..0fde480 100644
---
a/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/interceptors/ClientAuthInterceptorImpl.java
+++
b/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/interceptors/ClientAuthInterceptorImpl.java
@@ -21,6 +21,7 @@ package org.apache.custos.user.management.interceptors;
import io.grpc.Metadata;
import org.apache.custos.credential.store.client.CredentialStoreServiceClient;
+import org.apache.custos.iam.service.FindUsersRequest;
import org.apache.custos.iam.service.RegisterUserRequest;
import org.apache.custos.iam.service.ResetUserPassword;
import org.apache.custos.iam.service.UserSearchRequest;
@@ -28,9 +29,8 @@ import
org.apache.custos.integration.core.exceptions.NotAuthorizedException;
import
org.apache.custos.integration.services.commons.interceptors.AuthInterceptor;
import org.apache.custos.integration.services.commons.model.AuthClaim;
import org.apache.custos.tenant.profile.client.async.TenantProfileClient;
-import org.apache.custos.user.management.service.DeleteProfileRequest;
import org.apache.custos.user.management.service.UserProfileRequest;
-import org.apache.custos.user.profile.service.*;
+import org.apache.custos.user.profile.service.GetUpdateAuditTrailRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -53,7 +53,7 @@ public class ClientAuthInterceptorImpl extends
AuthInterceptor {
public <ReqT> ReqT intercept(String method, Metadata headers, ReqT reqT) {
- if (method.equals("deleteUserProfile")) {
+ if (method.equals("deleteUserProfile")) {
AuthClaim claim = authorize(headers);
if (claim == null) {
@@ -64,9 +64,11 @@ public class ClientAuthInterceptorImpl extends
AuthInterceptor {
String oauthSec = claim.getIamAuthSecret();
long tenantId = claim.getTenantId();
- DeleteUserProfileRequest pr = ((DeleteProfileRequest)
reqT).getDeleteRequest().toBuilder().setTenantId(tenantId).build();
- return (ReqT) ((DeleteProfileRequest) reqT).toBuilder()
-
.setIamClientId(oauthId).setDeleteRequest(pr).setIamClientSecret(oauthSec).build();
+ return (ReqT) ((UserProfileRequest) reqT).toBuilder()
+ .setClientId(oauthId)
+ .setClientSecret(oauthSec)
+ .setTenantId(tenantId)
+ .build();
} else if (method.equals("registerUser")) {
AuthClaim claim = authorize(headers);
@@ -118,7 +120,7 @@ public class ClientAuthInterceptorImpl extends
AuthInterceptor {
String oauthSec = claim.getIamAuthSecret();
long tenantId = claim.getTenantId();
- GetUserProfileRequest request = ((GetUserProfileRequest) reqT)
+ UserProfileRequest request = ((UserProfileRequest) reqT)
.toBuilder()
.setTenantId(tenantId).build();
@@ -134,7 +136,7 @@ public class ClientAuthInterceptorImpl extends
AuthInterceptor {
String oauthSec = claim.getIamAuthSecret();
long tenantId = claim.getTenantId();
- GetAllUserProfilesRequest request = ((GetAllUserProfilesRequest)
reqT)
+ UserProfileRequest request = ((UserProfileRequest) reqT)
.toBuilder().setTenantId(tenantId).build();
return (ReqT) request;
@@ -175,6 +177,44 @@ public class ClientAuthInterceptorImpl extends
AuthInterceptor {
.build();
return (ReqT) request;
+ } else if (method.equals("getUser")) {
+ AuthClaim claim = authorize(headers);
+
+ if (claim == null) {
+ throw new NotAuthorizedException("Request is not authorized",
null);
+ }
+
+ String oauthId = claim.getIamAuthId();
+ String oauthSec = claim.getIamAuthSecret();
+
+ long tenantId = claim.getTenantId();
+ UserSearchRequest request = ((UserSearchRequest) reqT)
+ .toBuilder()
+ .setClientId(oauthId)
+ .setTenantId(tenantId)
+ .setClientSec(oauthSec)
+ .build();
+ return (ReqT) request;
+
+ } else if (method.equals("findUsers")) {
+ AuthClaim claim = authorize(headers);
+
+ if (claim == null) {
+ throw new NotAuthorizedException("Request is not authorized",
null);
+ }
+
+ String oauthId = claim.getIamAuthId();
+ String oauthSec = claim.getIamAuthSecret();
+
+ long tenantId = claim.getTenantId();
+ FindUsersRequest request = ((FindUsersRequest) reqT)
+ .toBuilder()
+ .setClientId(oauthId)
+ .setClientSec(oauthSec)
+ .setTenantId(tenantId).build();
+
+
+ return (ReqT) request;
}
return reqT;
}
diff --git
a/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/interceptors/UserAuthInterceptorImpl.java
b/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/interceptors/UserAuthInterceptorImpl.java
index 3bdb87e..fee0c0a 100644
---
a/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/interceptors/UserAuthInterceptorImpl.java
+++
b/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/interceptors/UserAuthInterceptorImpl.java
@@ -27,7 +27,6 @@ import
org.apache.custos.integration.services.commons.interceptors.AuthIntercept
import org.apache.custos.integration.services.commons.model.AuthClaim;
import org.apache.custos.tenant.profile.client.async.TenantProfileClient;
import org.apache.custos.user.management.service.UserProfileRequest;
-import org.apache.custos.user.profile.service.UserProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -71,6 +70,27 @@ public class UserAuthInterceptorImpl extends AuthInterceptor
{
.setPerformedBy(claim.getPerformedBy())
.build();
+ } else if (method.equals("deleteUserAttributes")) {
+ String token = getToken(headers);
+ AuthClaim claim = authorizeUsingUserToken(headers);
+
+
+ if (claim == null) {
+ throw new NotAuthorizedException("Request is not authorized",
null);
+ }
+
+ String oauthId = claim.getIamAuthId();
+
+ long tenantId = claim.getTenantId();
+
+
+ return (ReqT) ((DeleteUserAttributeRequest) msg).toBuilder()
+ .setClientId(oauthId)
+ .setTenantId(tenantId)
+ .setAccessToken(token)
+ .setPerformedBy(claim.getPerformedBy())
+ .build();
+
} else if (method.equals("addRolesToUsers")) {
String token = getToken(headers);
AuthClaim claim = authorizeUsingUserToken(headers);
@@ -112,7 +132,7 @@ public class UserAuthInterceptorImpl extends
AuthInterceptor {
.setPerformedBy(claim.getPerformedBy())
.build();
return (ReqT) registerUserRequest;
- } else if (method.equals("getUser")) {
+ } else if (method.equals("deleteUserRoles")) {
String token = getToken(headers);
AuthClaim claim = authorizeUsingUserToken(headers);
@@ -121,34 +141,21 @@ public class UserAuthInterceptorImpl extends
AuthInterceptor {
}
String oauthId = claim.getIamAuthId();
- long tenantId = claim.getTenantId();
- UserSearchRequest request = ((UserSearchRequest) msg)
- .toBuilder()
- .setAccessToken(token)
- .setClientId(oauthId)
- .setTenantId(tenantId)
- .build();
- return (ReqT) request;
-
- } else if (method.equals("findUsers")) {
- String token = getToken(headers);
- AuthClaim claim = authorizeUsingUserToken(headers);
+ String oauthSec = claim.getIamAuthSecret();
- if (claim == null) {
- throw new NotAuthorizedException("Request is not authorized",
null);
- }
- String oauthId = claim.getIamAuthId();
long tenantId = claim.getTenantId();
-
- FindUsersRequest request = ((FindUsersRequest) msg)
+ DeleteUserRolesRequest operationRequest =
((DeleteUserRolesRequest) msg)
.toBuilder()
.setClientId(oauthId)
.setAccessToken(token)
- .setTenantId(tenantId).build();
+ .setTenantId(tenantId)
+ .setPerformedBy(claim.getPerformedBy())
+ .build();
+ return (ReqT) operationRequest;
- return (ReqT) request;
- } else if (method.equals("deleteUserRoles")) {
+ }
+ else if (method.equals("updateUserProfile")) {
String token = getToken(headers);
AuthClaim claim = authorizeUsingUserToken(headers);
@@ -160,18 +167,16 @@ public class UserAuthInterceptorImpl extends
AuthInterceptor {
String oauthSec = claim.getIamAuthSecret();
long tenantId = claim.getTenantId();
- DeleteUserRolesRequest operationRequest =
((DeleteUserRolesRequest) msg)
- .toBuilder()
- .setClientId(oauthId)
+
+ return (ReqT) ((UserProfileRequest) msg).toBuilder()
.setAccessToken(token)
.setTenantId(tenantId)
+ .setClientId(oauthId)
+ .setClientSecret(oauthSec)
.setPerformedBy(claim.getPerformedBy())
.build();
- return (ReqT) operationRequest;
-
- }
- if (method.equals("updateUserProfile")) {
+ } else if (method.equals("deleteUser")) {
String token = getToken(headers);
AuthClaim claim = authorizeUsingUserToken(headers);
@@ -183,16 +188,18 @@ public class UserAuthInterceptorImpl extends
AuthInterceptor {
String oauthSec = claim.getIamAuthSecret();
long tenantId = claim.getTenantId();
- UserProfile profile = ((UserProfileRequest)
msg).getUserProfile().toBuilder().setUpdatedBy(claim.getPerformedBy()).build();
-
- return (ReqT) ((UserProfileRequest) msg).toBuilder()
- .setAccessToken(token)
- .setTenantId(tenantId)
+ UserSearchRequest operationRequest = ((UserSearchRequest) msg)
+ .toBuilder()
.setClientId(oauthId)
- .setUserProfile(profile)
+ .setClientSec(oauthId)
+ .setTenantId(tenantId)
+ .setAccessToken(token)
+ .setPerformedBy(claim.getPerformedBy())
.build();
- } else
+ return (ReqT) operationRequest;
+
+ }
return msg;
}
diff --git
a/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/service/UserManagementService.java
b/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/service/UserManagementService.java
index fb20eab..ea45ee3 100644
---
a/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/service/UserManagementService.java
+++
b/custos-integration-services/user-management-service-parent/user-management-service/src/main/java/org/apache/custos/user/management/service/UserManagementService.java
@@ -22,6 +22,7 @@ package org.apache.custos.user.management.service;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import org.apache.custos.iam.admin.client.IamAdminServiceClient;
+import org.apache.custos.iam.service.UserAttribute;
import org.apache.custos.iam.service.*;
import org.apache.custos.identity.client.IdentityClient;
import org.apache.custos.identity.service.AuthToken;
@@ -33,6 +34,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* Service class of User Management Service
*/
@@ -85,7 +89,11 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
} catch (Exception ex) {
String msg = "Error occurred at registerUser " + ex.getMessage();
LOGGER.error(msg);
-
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
+ if (ex.getMessage().contains("CredentialGenerationException")) {
+
responseObserver.onError(Status.UNAUTHENTICATED.withDescription(msg).asRuntimeException());
+ } else {
+
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
+ }
}
@@ -100,10 +108,74 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
registerAndEnableUsers(request);
+ if (request.getUsersList() != null &&
!request.getUsersList().isEmpty() &&
+ registerUsersResponse.getAllUseresRegistered()) {
+ try {
+
+
+ request.getUsersList().forEach(user -> {
+
List<org.apache.custos.user.profile.service.UserAttribute> userAtrList = new
ArrayList<>();
+ if (user.getAttributesList() != null &&
!user.getAttributesList().isEmpty()) {
+
+ user.getAttributesList().forEach(atr -> {
+
org.apache.custos.user.profile.service.UserAttribute userAttribute =
+
org.apache.custos.user.profile.service.UserAttribute
+ .newBuilder()
+ .setKey(atr.getKey())
+
.addAllValue(atr.getValuesList())
+ .build();
+
+ userAtrList.add(userAttribute);
+ });
+ }
+
+
+ UserProfile profile = UserProfile.newBuilder()
+ .setFirstName(user.getFirstName())
+ .setLastName(user.getLastName())
+ .setEmail(user.getEmail())
+ .setStatus(UserStatus.valueOf("ACTIVE"))
+ .addAllAttributes(userAtrList)
+ .addAllRealmRoles(user.getRealmRolesList())
+ .addAllClientRoles(user.getClientRolesList())
+ .setUsername(user.getUsername().toLowerCase())
+ .build();
+
org.apache.custos.user.profile.service.UserProfileRequest profileRequest =
+
org.apache.custos.user.profile.service.UserProfileRequest.newBuilder()
+ .setProfile(profile)
+ .setTenantId(request.getTenantId())
+ .build();
+
+ userProfileClient.createUserProfile(profileRequest);
+
+
+ });
+ } catch (Exception ex) {
+
+ request.getUsersList().forEach(user -> {
+
+ UserSearchMetadata metadata = UserSearchMetadata
+ .newBuilder()
+ .setUsername(user.getUsername())
+ .build();
+ UserSearchRequest searchRequest = UserSearchRequest
+ .newBuilder()
+ .setTenantId(request.getTenantId())
+ .setClientId(request.getClientId())
+ .setAccessToken(request.getAccessToken())
+ .setUser(metadata)
+ .build();
+ iamAdminServiceClient.deleteUser(searchRequest);
+
+ });
+
+ }
+
+ }
+
responseObserver.onNext(registerUsersResponse);
responseObserver.onCompleted();
-
} catch (Exception ex) {
String msg = "Error occurred at registerAndEnableUsers " +
ex.getMessage();
LOGGER.error(msg);
@@ -121,13 +193,47 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
public void addUserAttributes(AddUserAttributesRequest request,
StreamObserver<OperationStatus> responseObserver) {
try {
-
OperationStatus status =
iamAdminServiceClient.addUserAttributes(request);
+
+ for (String user : request.getUsersList()) {
+
+ UserSearchMetadata metadata = UserSearchMetadata
+ .newBuilder()
+ .setUsername(user).build();
+
+ UserSearchRequest searchRequest = UserSearchRequest
+ .newBuilder()
+ .setClientId(request.getClientId())
+ .setTenantId(request.getTenantId())
+ .setAccessToken(request.getAccessToken())
+ .setUser(metadata)
+ .build();
+
+ UserRepresentation representation =
iamAdminServiceClient.getUser(searchRequest);
+
+
+ if (representation != null) {
+
+ UserProfile profile =
this.convertToProfile(representation);
+
+ org.apache.custos.user.profile.service.UserProfileRequest
req =
+
org.apache.custos.user.profile.service.UserProfileRequest
+ .newBuilder()
+ .setTenantId(request.getTenantId())
+ .setProfile(profile)
+ .build();
+
+
+ userProfileClient.updateUserProfile(req);
+
+
+ }
+
+ }
responseObserver.onNext(status);
responseObserver.onCompleted();
-
} catch (Exception ex) {
String msg = "Error occurred at addUserAttributes " +
ex.getMessage();
LOGGER.error(msg);
@@ -139,6 +245,66 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
}
}
+
+ @Override
+ public void deleteUserAttributes(DeleteUserAttributeRequest request,
StreamObserver<OperationStatus> responseObserver) {
+ try {
+
+ OperationStatus status =
iamAdminServiceClient.deleteUserAttributes(request);
+
+
+ for (String user : request.getUsersList()) {
+
+ UserSearchMetadata metadata = UserSearchMetadata
+ .newBuilder()
+ .setUsername(user).build();
+
+ UserSearchRequest searchRequest = UserSearchRequest
+ .newBuilder()
+ .setClientId(request.getClientId())
+ .setTenantId(request.getTenantId())
+ .setAccessToken(request.getAccessToken())
+ .setUser(metadata)
+ .build();
+
+ UserRepresentation representation =
iamAdminServiceClient.getUser(searchRequest);
+
+
+ if (representation != null) {
+
+ UserProfile profile =
this.convertToProfile(representation);
+
+ org.apache.custos.user.profile.service.UserProfileRequest
req =
+
org.apache.custos.user.profile.service.UserProfileRequest
+ .newBuilder()
+ .setTenantId(request.getTenantId())
+ .setProfile(profile)
+ .build();
+
+
+ userProfileClient.updateUserProfile(req);
+
+
+ }
+
+ }
+ responseObserver.onNext(status);
+ responseObserver.onCompleted();
+
+ } catch (Exception ex) {
+ String msg = "Error occurred at deleteUserAttributes " +
ex.getMessage();
+ LOGGER.error(msg);
+ if (ex.getMessage().contains("UNAUTHENTICATED")) {
+
responseObserver.onError(Status.UNAUTHENTICATED.withDescription(msg).asRuntimeException());
+ } else {
+
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
+ }
+ }
+ }
+
+
+
+
@Override
public void enableUser(UserSearchRequest request,
StreamObserver<UserRepresentation> responseObserver) {
try {
@@ -159,28 +325,27 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
if (user != null) {
- UserProfile profile = UserProfile.newBuilder()
- .setFirstName(user.getFirstName())
- .setLastName(user.getLastName())
- .setEmail(user.getEmail())
- .setStatus(UserStatus.valueOf(user.getState()))
- .setTenantId(request.getTenantId())
- .setUsername(user.getUsername())
- .build();
+ UserProfile profile = this.convertToProfile(user);
- UserProfile userProfile =
userProfileClient.createUserProfile(profile);
+ org.apache.custos.user.profile.service.UserProfileRequest
profileRequest =
+
org.apache.custos.user.profile.service.UserProfileRequest.newBuilder()
+ .setProfile(profile)
+ .setTenantId(request.getTenantId())
+ .build();
- if (userProfile != null && userProfile.getUserId() !=
null) {
- responseObserver.onNext(user);
- responseObserver.onCompleted();
+ UserProfile exProfile =
userProfileClient.getUser(profileRequest);
+ if (exProfile.getUsername().equals("")) {
+ userProfileClient.createUserProfile(profileRequest);
} else {
- String msg = "User enabling failed at user profile
creation";
- LOGGER.error(msg);
-
responseObserver.onError(Status.CANCELLED.withDescription(msg).asRuntimeException());
-
+ userProfileClient.updateUserProfile(profileRequest);
}
+
+ responseObserver.onNext(user);
+ responseObserver.onCompleted();
+
+
} else {
String msg = "User enabling failed at IDP server";
LOGGER.error(msg);
@@ -204,24 +369,21 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
@Override
public void deleteUser(UserSearchRequest request,
StreamObserver<CheckingResponse> responseObserver) {
try {
+ UserProfile profileReq =
UserProfile.newBuilder().setUsername(request.getUser().getUsername().toLowerCase()).build();
- GetUserProfileRequest req = GetUserProfileRequest
- .newBuilder()
- .setTenantId(request.getTenantId())
- .setUsername(request.getUser().getUsername())
- .build();
+ org.apache.custos.user.profile.service.UserProfileRequest req =
+ org.apache.custos.user.profile.service.UserProfileRequest
+ .newBuilder()
+ .setTenantId(request.getTenantId())
+ .setProfile(profileReq)
+ .build();
UserProfile profile = userProfileClient.getUser(req);
if (profile != null) {
- DeleteUserProfileRequest deleteUserProfileRequest =
DeleteUserProfileRequest
- .newBuilder()
- .setTenantId(request.getTenantId())
- .setUsername(request.getUser().getUsername())
- .build();
- UserProfile deletedProfile =
userProfileClient.deleteUser(deleteUserProfileRequest);
+ UserProfile deletedProfile = userProfileClient.deleteUser(req);
if (deletedProfile != null) {
@@ -255,10 +417,27 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
public void getUser(UserSearchRequest request,
StreamObserver<UserRepresentation> responseObserver) {
try {
- UserRepresentation user = iamAdminServiceClient.getUser(request);
+ GetUserManagementSATokenRequest userManagementSATokenRequest =
GetUserManagementSATokenRequest
+ .newBuilder()
+ .setClientId(request.getClientId())
+ .setClientSecret(request.getClientSec())
+ .setTenantId(request.getTenantId())
+ .build();
+ AuthToken token =
identityClient.getUserManagementSATokenRequest(userManagementSATokenRequest);
+
+ if (token != null && token.getAccessToken() != null) {
- responseObserver.onNext(user);
- responseObserver.onCompleted();
+ request =
request.toBuilder().setAccessToken(token.getAccessToken()).build();
+
+ UserRepresentation user =
iamAdminServiceClient.getUser(request);
+
+ responseObserver.onNext(user);
+ responseObserver.onCompleted();
+ } else {
+ LOGGER.error("Cannot find service token");
+ responseObserver.onError(Status.CANCELLED.
+ withDescription("Cannot find service
token").asRuntimeException());
+ }
} catch (Exception ex) {
String msg = "Error occurred at getUser " + ex.getMessage();
@@ -274,11 +453,25 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
@Override
public void findUsers(FindUsersRequest request,
StreamObserver<FindUsersResponse> responseObserver) {
try {
+ GetUserManagementSATokenRequest userManagementSATokenRequest =
GetUserManagementSATokenRequest
+ .newBuilder()
+ .setClientId(request.getClientId())
+ .setClientSecret(request.getClientSec())
+ .setTenantId(request.getTenantId())
+ .build();
+ AuthToken token =
identityClient.getUserManagementSATokenRequest(userManagementSATokenRequest);
+ if (token != null && token.getAccessToken() != null) {
- FindUsersResponse user = iamAdminServiceClient.getUsers(request);
- responseObserver.onNext(user);
- responseObserver.onCompleted();
+ request =
request.toBuilder().setAccessToken(token.getAccessToken()).build();
+ FindUsersResponse user =
iamAdminServiceClient.getUsers(request);
+ responseObserver.onNext(user);
+ responseObserver.onCompleted();
+ } else {
+ LOGGER.error("Cannot find service token");
+ responseObserver.onError(Status.CANCELLED.
+ withDescription("Cannot find service
token").asRuntimeException());
+ }
} catch (Exception ex) {
String msg = "Error occurred at getUsers " + ex.getMessage();
@@ -328,6 +521,43 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
try {
OperationStatus response =
iamAdminServiceClient.addRolesToUsers(request);
+
+ for (String user : request.getUsernamesList()) {
+
+ UserSearchMetadata metadata = UserSearchMetadata
+ .newBuilder()
+ .setUsername(user).build();
+
+ UserSearchRequest searchRequest = UserSearchRequest
+ .newBuilder()
+ .setClientId(request.getClientId())
+ .setTenantId(request.getTenantId())
+ .setAccessToken(request.getAccessToken())
+ .setUser(metadata)
+ .build();
+
+ UserRepresentation representation =
iamAdminServiceClient.getUser(searchRequest);
+
+
+ if (representation != null) {
+
+ UserProfile profile =
this.convertToProfile(representation);
+
+ org.apache.custos.user.profile.service.UserProfileRequest
req =
+
org.apache.custos.user.profile.service.UserProfileRequest
+ .newBuilder()
+ .setTenantId(request.getTenantId())
+ .setProfile(profile)
+ .build();
+
+
+ userProfileClient.updateUserProfile(req);
+
+
+ }
+
+ }
+
responseObserver.onNext(response);
responseObserver.onCompleted();
@@ -345,13 +575,49 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
@Override
public void deleteUserRoles(DeleteUserRolesRequest
- request,
StreamObserver<CheckingResponse> responseObserver) {
+ request,
StreamObserver<CheckingResponse> responseObserver) {
try {
CheckingResponse response =
iamAdminServiceClient.deleteUserRoles(request);
+
+
+ UserSearchMetadata metadata = UserSearchMetadata
+ .newBuilder()
+ .setUsername(request.getUsername()).build();
+
+ UserSearchRequest searchRequest = UserSearchRequest
+ .newBuilder()
+ .setClientId(request.getClientId())
+ .setTenantId(request.getTenantId())
+ .setAccessToken(request.getAccessToken())
+ .setUser(metadata)
+ .build();
+
+ UserRepresentation representation =
iamAdminServiceClient.getUser(searchRequest);
+
+
+ if (representation != null) {
+
+ UserProfile profile = this.convertToProfile(representation);
+
+ org.apache.custos.user.profile.service.UserProfileRequest req =
+
org.apache.custos.user.profile.service.UserProfileRequest
+ .newBuilder()
+ .setTenantId(request.getTenantId())
+ .setProfile(profile)
+ .build();
+
+
+ userProfileClient.updateUserProfile(req);
+
+
+ }
+
+
responseObserver.onNext(response);
responseObserver.onCompleted();
- } catch (Exception ex) {
+ } catch (
+ Exception ex) {
String msg = "Error occurred at deleteRoleFromUser " +
ex.getMessage();
LOGGER.error(msg);
if (ex.getMessage().contains("UNAUTHENTICATED")) {
@@ -360,6 +626,7 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
}
}
+
}
@Override
@@ -427,141 +694,180 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
}
}
- //TODO: this is not updated
+
@Override
public void updateUserProfile(UserProfileRequest request,
StreamObserver<UserProfile> responseObserver) {
try {
LOGGER.debug("Request received to updateUserProfile " +
request.getUserProfile().getUsername() +
- " at" + request.getUserProfile().getTenantId());
+ " at" + request.getTenantId());
- UserRepresentation.Builder builder =
UserRepresentation.newBuilder()
- .setFirstName(request.getUserProfile().getFirstName())
- .setLastName(request.getUserProfile().getLastName())
- .setEmail(request.getUserProfile().getEmail())
- .setUsername(request.getUserProfile().getUsername());
+ UserRepresentation.Builder builder =
UserRepresentation.newBuilder()
+ .setFirstName(request.getUserProfile().getFirstName())
+ .setLastName(request.getUserProfile().getLastName())
+ .setEmail(request.getUserProfile().getEmail())
+ .setUsername(request.getUserProfile().getUsername());
- if (request.getUserProfile().getStatus() != null) {
-
builder.setState(request.getUserProfile().getStatus().name());
- }
+ if (request.getUserProfile().getStatus() != null) {
+ builder.setState(request.getUserProfile().getStatus().name());
+ }
- UpdateUserProfileRequest updateUserProfileRequest =
UpdateUserProfileRequest
- .newBuilder()
- .setUser(builder.build())
- .setAccessToken(request.getAccessToken())
- .setTenantId(request.getTenantId())
- .build();
+ UpdateUserProfileRequest updateUserProfileRequest =
UpdateUserProfileRequest
+ .newBuilder()
+ .setUser(builder.build())
+ .setAccessToken(request.getAccessToken())
+ .setTenantId(request.getTenantId())
+ .build();
+ UserSearchMetadata metadata = UserSearchMetadata
+ .newBuilder()
+
.setUsername(request.getUserProfile().getUsername()).build();
- UserSearchRequest info = UserSearchRequest
- .newBuilder()
- .setAccessToken(request.getAccessToken())
- .setTenantId(request.getTenantId())
- .build();
+ UserSearchRequest info = UserSearchRequest
+ .newBuilder()
+ .setAccessToken(request.getAccessToken())
+ .setTenantId(request.getTenantId())
+ .setUser(metadata)
+ .build();
- UserRepresentation exUser =
iamAdminServiceClient.getUser(info);
+ UserRepresentation exUser = iamAdminServiceClient.getUser(info);
- CheckingResponse response =
iamAdminServiceClient.updateUserProfile(updateUserProfileRequest);
+ CheckingResponse response =
iamAdminServiceClient.updateUserProfile(updateUserProfileRequest);
- if (response != null && response.getIsExist()) {
- try {
- UserProfile userProfile =
userProfileClient.updateUserProfile(request.getUserProfile());
- responseObserver.onNext(userProfile);
+ if (response != null && response.getIsExist()) {
+ try {
+ org.apache.custos.user.profile.service.UserProfileRequest
userProfileRequest =
+
org.apache.custos.user.profile.service.UserProfileRequest.
+ newBuilder()
+ .setProfile(request.getUserProfile())
+ .setTenantId(request.getTenantId())
+ .build();
+
+ UserProfile profile =
userProfileClient.getUser(userProfileRequest);
+
+ if (profile != null && !profile.getUsername().equals("")) {
+ profile = profile.toBuilder()
+ .setEmail(request.getUserProfile().getEmail())
+
.setFirstName(request.getUserProfile().getFirstName())
+
.setLastName(request.getUserProfile().getLastName())
+
.setUsername(request.getUserProfile().getUsername())
+ .build();
+
+ userProfileClient.
+
updateUserProfile(userProfileRequest.toBuilder().setProfile(profile).build());
+
+ responseObserver.onNext(profile);
responseObserver.onCompleted();
- } catch (Exception ex) {
- LOGGER.error("Error occurred while saving user profile
in local DB, rolling back IAM service");
+
+ } else {
+ String msg = "Error occurred while saving user profile
in local DB, " +
+ "rolling back IAM service" + "User profile not
found";
+ LOGGER.error(msg);
UpdateUserProfileRequest rollingRequest =
UpdateUserProfileRequest
.newBuilder()
.setUser(exUser)
.setAccessToken(request.getAccessToken())
+ .setTenantId(request.getTenantId())
.build();
iamAdminServiceClient.updateUserProfile(rollingRequest);
+ responseObserver.onError(Status.CANCELLED.
+ withDescription(msg).asRuntimeException());
}
- } else {
- LOGGER.error("User profile not found in IDP server");
+
+
+ } catch (Exception ex) {
+ String msg = "Error occurred while saving user profile in
local DB, " +
+ "rolling back IAM service" + ex.getMessage();
+ LOGGER.error(msg);
+ UpdateUserProfileRequest rollingRequest =
UpdateUserProfileRequest
+ .newBuilder()
+ .setUser(exUser)
+ .setAccessToken(request.getAccessToken())
+ .setTenantId(request.getTenantId())
+ .build();
+ iamAdminServiceClient.updateUserProfile(rollingRequest);
responseObserver.onError(Status.CANCELLED.
- withDescription("IAM server failed to update user
profile").asRuntimeException());
+ withDescription(msg).asRuntimeException());
}
+ } else {
+ LOGGER.error("User profile not found in IDP server");
+ responseObserver.onError(Status.CANCELLED.
+ withDescription("IAM server failed to update user
profile").asRuntimeException());
+ }
} catch (Exception ex) {
String msg = "Error occurred while updating user profile " +
ex.getMessage();
LOGGER.error(msg);
-
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
+ if (ex.getMessage().contains("UNAUTHENTICATED")) {
+
responseObserver.onError(io.grpc.Status.UNAUTHENTICATED.withDescription(msg).asRuntimeException());
+ } else {
+
responseObserver.onError(io.grpc.Status.INTERNAL.withDescription(msg).asRuntimeException());
+ }
}
}
- //TODO: this is not updated
-
@Override
- public void deleteUserProfile(DeleteProfileRequest request,
StreamObserver<UserProfile> responseObserver) {
+ public void deleteUserProfile(UserProfileRequest request,
StreamObserver<UserProfile> responseObserver) {
try {
- LOGGER.debug("Request received to deleteUserProfile " +
request.getDeleteRequest().getUsername() +
- " at" + request.getDeleteRequest().getTenantId());
+ LOGGER.debug("Request received to deleteUserProfile " +
request.getUserProfile().getUsername() +
+ " at" + request.getTenantId());
+ UserSearchMetadata metadata = UserSearchMetadata
+ .newBuilder()
+
.setUsername(request.getUserProfile().getUsername()).build();
- GetUserManagementSATokenRequest userManagementSATokenRequest =
GetUserManagementSATokenRequest
+ UserSearchRequest info = UserSearchRequest
.newBuilder()
- .setClientId(request.getIamClientId())
- .setClientSecret(request.getIamClientSecret())
- .setTenantId(request.getDeleteRequest().getTenantId())
+ .setAccessToken(request.getAccessToken())
+ .setTenantId(request.getTenantId())
+ .setUser(metadata)
.build();
- AuthToken token =
identityClient.getUserManagementSATokenRequest(userManagementSATokenRequest);
- if (token != null && token.getAccessToken() != null) {
+ org.apache.custos.user.profile.service.UserProfileRequest
userProfileRequest =
+ org.apache.custos.user.profile.service.UserProfileRequest
+ .newBuilder()
+ .setProfile(request.getUserProfile())
+ .setTenantId(request.getTenantId())
+ .build();
+ UserProfile userProfile =
userProfileClient.deleteUser(userProfileRequest);
- UserSearchRequest info = UserSearchRequest
- .newBuilder()
- .setAccessToken(token.getAccessToken())
- .setTenantId(request.getDeleteRequest().getTenantId())
- //
.setUsername(request.getDeleteRequest().getUsername())
- .build();
-
-
- UserProfile userProfile =
userProfileClient.deleteUser(request.getDeleteRequest());
-
- responseObserver.onNext(userProfile);
- responseObserver.onCompleted();
-
-
- try {
- iamAdminServiceClient.deleteUser(info);
-
- } catch (Exception ex) {
- String msg = "Error occurred while deleting user profile
in IDP , rolling back local DB";
- LOGGER.error(msg);
- userProfileClient.createUserProfile(userProfile);
- responseObserver.onError(Status.CANCELLED.
- withDescription("IAM server failed to update user
profile").asRuntimeException());
-
- }
+ responseObserver.onNext(userProfile);
+ responseObserver.onCompleted();
- } else {
- LOGGER.error("Error occurred retreving service account");
- responseObserver.onError(Status.CANCELLED.
- withDescription("Service account not
found").asRuntimeException());
- }
+ iamAdminServiceClient.deleteUser(info);
} catch (Exception ex) {
String msg = "Error occurred while delete user profile " +
ex.getMessage();
LOGGER.error(msg);
-
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
+ if (ex.getMessage().contains("UNAUTHENTICATED")) {
+
responseObserver.onError(io.grpc.Status.UNAUTHENTICATED.withDescription(msg).asRuntimeException());
+ } else {
+
responseObserver.onError(io.grpc.Status.INTERNAL.withDescription(msg).asRuntimeException());
+ }
}
}
@Override
- public void getUserProfile(GetUserProfileRequest request,
StreamObserver<UserProfile> responseObserver) {
+ public void getUserProfile(UserProfileRequest request,
StreamObserver<UserProfile> responseObserver) {
try {
- LOGGER.debug("Request received to getUserProfile " +
request.getUsername() +
+ LOGGER.debug("Request received to getUserProfile " +
request.getUserProfile().getUsername() +
" at" + request.getTenantId());
+ org.apache.custos.user.profile.service.UserProfileRequest
userProfileRequest =
+
+ org.apache.custos.user.profile.service.UserProfileRequest
+ .newBuilder()
+ .setProfile(request.getUserProfile())
+ .setTenantId(request.getTenantId())
+ .build();
- UserProfile userProfile = userProfileClient.getUser(request);
+ UserProfile userProfile =
userProfileClient.getUser(userProfileRequest);
responseObserver.onNext(userProfile);
responseObserver.onCompleted();
@@ -575,13 +881,22 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
@Override
- public void getAllUserProfilesInTenant(GetAllUserProfilesRequest
+ public void getAllUserProfilesInTenant(UserProfileRequest
request,
StreamObserver<GetAllUserProfilesResponse> responseObserver) {
try {
LOGGER.debug("Request received to getAllUserProfilesInTenant " +
request.getTenantId() +
" at" + request.getTenantId());
- GetAllUserProfilesResponse response =
userProfileClient.getAllUserProfilesInTenant(request);
+ org.apache.custos.user.profile.service.UserProfileRequest
userProfileRequest =
+
+ org.apache.custos.user.profile.service.UserProfileRequest
+ .newBuilder()
+ .setProfile(request.getUserProfile())
+ .setTenantId(request.getTenantId())
+ .build();
+
+
+ GetAllUserProfilesResponse response =
userProfileClient.getAllUserProfilesInTenant(userProfileRequest);
responseObserver.onNext(response);
responseObserver.onCompleted();
@@ -612,4 +927,48 @@ public class UserManagementService extends
UserManagementServiceGrpc.UserManagem
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
}
}
+
+ private UserProfile convertToProfile(UserRepresentation representation) {
+ UserProfile.Builder profileBuilder = UserProfile.newBuilder();
+
+
+ if (representation.getRealmRolesCount() > 0) {
+
profileBuilder.addAllRealmRoles(representation.getRealmRolesList());
+
+ }
+
+ if (representation.getClientRolesCount() > 0) {
+
profileBuilder.addAllClientRoles(representation.getClientRolesList());
+
+ }
+
+ if (representation.getAttributesCount() > 0) {
+ List<UserAttribute> attributeList =
representation.getAttributesList();
+
+ List<org.apache.custos.user.profile.service.UserAttribute>
userAtrList = new ArrayList<>();
+ attributeList.forEach(atr -> {
+ org.apache.custos.user.profile.service.UserAttribute
userAttribute =
+ org.apache.custos.user.profile.service.UserAttribute
+ .newBuilder()
+ .setKey(atr.getKey())
+ .addAllValue(atr.getValuesList())
+ .build();
+
+ userAtrList.add(userAttribute);
+ });
+ profileBuilder.addAllAttributes(userAtrList);
+
+
+ }
+
+ profileBuilder.setUsername(representation.getUsername().toLowerCase());
+ profileBuilder.setFirstName(representation.getFirstName());
+ profileBuilder.setLastName(representation.getLastName());
+ profileBuilder.setEmail(representation.getEmail());
+
+ return profileBuilder.build();
+
+ }
+
+
}
diff --git
a/custos-integration-services/user-management-service-parent/user-management-service/src/main/proto/UserManagementService.proto
b/custos-integration-services/user-management-service-parent/user-management-service/src/main/proto/UserManagementService.proto
index 77077a9..0d9500f 100644
---
a/custos-integration-services/user-management-service-parent/user-management-service/src/main/proto/UserManagementService.proto
+++
b/custos-integration-services/user-management-service-parent/user-management-service/src/main/proto/UserManagementService.proto
@@ -29,16 +29,12 @@ import "IamAdminService.proto";
message UserProfileRequest {
- org.apache.custos.user.profile.service.UserProfile userProfile = 1;
+ org.apache.custos.user.profile.service.UserProfile user_profile = 1;
string clientId = 2;
int64 tenantId = 3;
string accessToken = 4;
-}
-
-message DeleteProfileRequest {
- org.apache.custos.user.profile.service.DeleteUserProfileRequest
deleteRequest = 1;
- string iamClientId = 2;
- string iamClientSecret = 3;
+ string clientSecret = 5;
+ string performedBy = 6;
}
message GetUserRequest {
@@ -95,6 +91,12 @@ service UserManagementService {
};
}
+ rpc deleteUserAttributes
(org.apache.custos.iam.service.DeleteUserAttributeRequest) returns
(org.apache.custos.iam.service.OperationStatus) {
+ option (google.api.http) = {
+ delete: "/user-management/v1.0.0/attributes"
+ };
+ }
+
rpc enableUser (org.apache.custos.iam.service.UserSearchRequest) returns
(org.apache.custos.iam.service.UserRepresentation) {
option (google.api.http) = {
post: "/user-management/v1.0.0/user/activation"
@@ -148,7 +150,7 @@ service UserManagementService {
rpc deleteUser (org.apache.custos.iam.service.UserSearchRequest) returns
(org.apache.custos.iam.service.CheckingResponse) {
option (google.api.http) = {
delete: "/user-management/v1.0.0/user"
- body: "info"
+ body: "user"
};
}
@@ -165,23 +167,24 @@ service UserManagementService {
option (google.api.http) = {
put: "/user-management/v1.0.0/user/profile"
- body: "userProfile"
+ body: "user_profile"
};
}
- rpc getUserProfile
(org.apache.custos.user.profile.service.GetUserProfileRequest) returns
(org.apache.custos.user.profile.service.UserProfile) {
+
+ rpc getUserProfile (UserProfileRequest) returns
(org.apache.custos.user.profile.service.UserProfile) {
option (google.api.http) = {
get: "/user-management/v1.0.0/user/profile"
};
}
- rpc deleteUserProfile (DeleteProfileRequest) returns
(org.apache.custos.user.profile.service.UserProfile) {
+ rpc deleteUserProfile (UserProfileRequest) returns
(org.apache.custos.user.profile.service.UserProfile) {
option (google.api.http) = {
delete: "/user-management/v1.0.0/user/profile"
};
}
- rpc getAllUserProfilesInTenant
(org.apache.custos.user.profile.service.GetAllUserProfilesRequest) returns
(org.apache.custos.user.profile.service.GetAllUserProfilesResponse) {
+ rpc getAllUserProfilesInTenant (UserProfileRequest) returns
(org.apache.custos.user.profile.service.GetAllUserProfilesResponse) {
option (google.api.http) = {
get: "/user-management/v1.0.0/users/profile"