From 99ac00719a5c19aaae87402e8a4c2a398729280e Mon Sep 17 00:00:00 2001 From: osh Date: Wed, 25 Oct 2023 17:17:45 +0530 Subject: [PATCH 1/2] Move user api implementation to service layer --- .../mgt/api/jaxrs/beans/ActivityList.java | 1 + .../mgt/api/jaxrs/beans/ApplicationList.java | 3 +- .../api/jaxrs/beans/ComplianceDeviceList.java | 3 +- .../api/jaxrs/beans/DeviceActivityList.java | 1 + .../mgt/api/jaxrs/beans/DeviceGroupList.java | 1 + .../mgt/api/jaxrs/beans/DeviceList.java | 1 + .../jaxrs/beans/DeviceToGroupsAssignment.java | 2 +- .../mgt/api/jaxrs/beans/GeofenceList.java | 1 + .../mgt/api/jaxrs/beans/MetadataList.java | 1 + .../mgt/api/jaxrs/beans/NotificationList.java | 1 + .../mgt/api/jaxrs/beans/OperationList.java | 1 + .../mgt/api/jaxrs/beans/PolicyList.java | 1 + .../device/mgt/api/jaxrs/beans/RoleList.java | 1 + .../mgt/api/jaxrs/beans/UserInfoList.java | 2 + .../mgt/api/jaxrs/beans/UserStoreList.java | 1 + .../beans/analytics/EventAttributeList.java | 2 - .../jaxrs/beans/analytics/EventRecords.java | 3 +- .../service/api/UserManagementService.java | 9 +- .../impl/UserManagementServiceImpl.java | 460 ++----------- .../mgt/api/jaxrs/util/DeviceMgtAPIUtils.java | 13 + .../impl/UserManagementServiceImplTest.java | 12 +- .../mgt/common}/BasePaginatedResult.java | 4 +- .../device/mgt/common}/BasicUserInfo.java | 2 +- .../device/mgt/common}/BasicUserInfoList.java | 4 +- .../mgt/common/BasicUserInfoMetadata.java | 49 ++ .../mgt/common}/BasicUserInfoWrapper.java | 3 +- .../mgt/common}/EnrollmentInvitation.java | 0 .../mgt/core/device/mgt/common}/UserInfo.java | 5 +- .../exceptions/UserManagementException.java | 44 ++ .../mgt/core/DeviceManagementConstants.java | 9 + .../internal/DeviceManagementDataHolder.java | 11 + .../DeviceManagementServiceComponent.java | 10 +- .../UserManagementProviderService.java | 136 ++++ .../UserManagementProviderServiceImpl.java | 609 ++++++++++++++++++ .../UserManagementProviderServiceTest.java | 36 ++ 35 files changed, 1019 insertions(+), 423 deletions(-) rename components/device-mgt/{io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans => io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common}/BasePaginatedResult.java (94%) rename components/device-mgt/{io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans => io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common}/BasicUserInfo.java (97%) rename components/device-mgt/{io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans => io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common}/BasicUserInfoList.java (90%) create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfoMetadata.java rename components/device-mgt/{io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans => io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common}/BasicUserInfoWrapper.java (93%) rename components/device-mgt/{io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans => io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common}/EnrollmentInvitation.java (100%) rename components/device-mgt/{io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans => io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common}/UserInfo.java (92%) create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/UserManagementException.java create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderService.java create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceImpl.java create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceTest.java diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ActivityList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ActivityList.java index e4a9fce702..ad3f0a7a3e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ActivityList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ActivityList.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.gson.Gson; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Activity; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ApplicationList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ApplicationList.java index b57f0ba437..0b049101e1 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ApplicationList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ApplicationList.java @@ -19,13 +19,14 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModelProperty; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.Application; import java.util.ArrayList; import java.util.List; -public class ApplicationList extends BasePaginatedResult{ +public class ApplicationList extends BasePaginatedResult { private List applicationList = new ArrayList<>(); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ComplianceDeviceList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ComplianceDeviceList.java index 65b823ea61..7af9955392 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ComplianceDeviceList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ComplianceDeviceList.java @@ -19,13 +19,14 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModelProperty; import io.entgra.device.mgt.core.device.mgt.common.policy.mgt.monitor.ComplianceData; import java.util.ArrayList; import java.util.List; -public class ComplianceDeviceList extends BasePaginatedResult{ +public class ComplianceDeviceList extends BasePaginatedResult { private List complianceData = new ArrayList<>(); @ApiModelProperty(value = "List of devices returned") diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceActivityList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceActivityList.java index a538ea4d33..7805ca0701 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceActivityList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceActivityList.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.gson.Gson; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.DeviceActivity; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceGroupList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceGroupList.java index d03f32913d..325415e191 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceGroupList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceGroupList.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModelProperty; import java.util.ArrayList; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceList.java index 7b3e084276..8410800a5b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceList.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModelProperty; import io.entgra.device.mgt.core.device.mgt.common.Device; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceToGroupsAssignment.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceToGroupsAssignment.java index ef70a0e371..43330fb1ae 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceToGroupsAssignment.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/DeviceToGroupsAssignment.java @@ -20,7 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.annotations.ApiModelProperty; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.BasePaginatedResult; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import java.util.ArrayList; import java.util.List; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/GeofenceList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/GeofenceList.java index b076739775..480bfab1f4 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/GeofenceList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/GeofenceList.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModelProperty; import java.util.ArrayList; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/MetadataList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/MetadataList.java index f69fcc259e..0e4eb42932 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/MetadataList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/MetadataList.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/NotificationList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/NotificationList.java index 366733b8b0..6da61cdf45 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/NotificationList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/NotificationList.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import io.entgra.device.mgt.core.device.mgt.common.notification.mgt.Notification; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/OperationList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/OperationList.java index 54f41972e8..34dea0d982 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/OperationList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/OperationList.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModelProperty; import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Operation; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/PolicyList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/PolicyList.java index 0d98688257..25993e8d2b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/PolicyList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/PolicyList.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import io.entgra.device.mgt.core.device.mgt.common.policy.mgt.Policy; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/RoleList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/RoleList.java index b91a46a99d..481d76d269 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/RoleList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/RoleList.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/UserInfoList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/UserInfoList.java index a8a8f63e8b..5d8c22f580 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/UserInfoList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/UserInfoList.java @@ -18,6 +18,8 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; +import io.entgra.device.mgt.core.device.mgt.common.UserInfo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/UserStoreList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/UserStoreList.java index 4536752a7b..51f6bd878a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/UserStoreList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/UserStoreList.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/analytics/EventAttributeList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/analytics/EventAttributeList.java index 4fb2e3921d..26847a2153 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/analytics/EventAttributeList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/analytics/EventAttributeList.java @@ -19,8 +19,6 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.analytics; import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.annotations.ApiModelProperty; -import io.entgra.device.mgt.core.device.mgt.common.Device; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.BasePaginatedResult; import java.util.ArrayList; import java.util.List; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/analytics/EventRecords.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/analytics/EventRecords.java index 792e8de652..7a46b43344 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/analytics/EventRecords.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/analytics/EventRecords.java @@ -19,12 +19,11 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.analytics; import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.annotations.ApiModelProperty; -import io.entgra.device.mgt.core.device.mgt.common.Device; import java.util.ArrayList; import java.util.List; import org.wso2.carbon.analytics.datasource.commons.Record; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.BasePaginatedResult; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; /** * This hold stats data record diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/UserManagementService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/UserManagementService.java index afa5fdfc08..98812806ba 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/UserManagementService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/UserManagementService.java @@ -34,15 +34,15 @@ import io.entgra.device.mgt.core.apimgt.annotations.Scopes; import io.entgra.device.mgt.core.apimgt.annotations.Scope; import io.entgra.device.mgt.core.device.mgt.common.invitation.mgt.DeviceEnrollmentInvitation; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.ActivityList; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.BasicUserInfo; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.BasicUserInfoList; +import io.entgra.device.mgt.core.device.mgt.common.BasicUserInfo; +import io.entgra.device.mgt.core.device.mgt.common.BasicUserInfoList; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.Credential; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.EnrollmentInvitation; +import io.entgra.device.mgt.core.device.mgt.common.EnrollmentInvitation; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.ErrorResponse; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.OldPasswordResetWrapper; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.PermissionList; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.RoleList; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.UserInfo; +import io.entgra.device.mgt.core.device.mgt.common.UserInfo; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.UserStoreList; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.Constants; @@ -54,7 +54,6 @@ import javax.ws.rs.HeaderParam; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java index cdf3fb8342..b1e10668ad 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java @@ -19,7 +19,9 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import io.entgra.device.mgt.core.device.mgt.common.Device; +import io.entgra.device.mgt.core.device.mgt.common.BasicUserInfoMetadata; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.UserManagementException; +import io.entgra.device.mgt.core.device.mgt.core.service.UserManagementProviderService; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -28,7 +30,6 @@ import org.eclipse.wst.common.uriresolver.internal.util.URIEncoder; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; -import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.ConfigurationManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.OTPManagementException; import io.entgra.device.mgt.core.device.mgt.common.invitation.mgt.DeviceEnrollmentInvitation; @@ -39,16 +40,16 @@ import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import io.entgra.device.mgt.core.device.mgt.core.service.EmailMetaInfo; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.ActivityList; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.BasicUserInfo; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.BasicUserInfoList; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.BasicUserInfoWrapper; +import io.entgra.device.mgt.core.device.mgt.common.BasicUserInfo; +import io.entgra.device.mgt.core.device.mgt.common.BasicUserInfoList; +import io.entgra.device.mgt.core.device.mgt.common.BasicUserInfoWrapper; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.Credential; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.EnrollmentInvitation; +import io.entgra.device.mgt.core.device.mgt.common.EnrollmentInvitation; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.ErrorResponse; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.OldPasswordResetWrapper; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.PermissionList; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.RoleList; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.UserInfo; +import io.entgra.device.mgt.core.device.mgt.common.UserInfo; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.UserStoreList; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.exception.BadRequestException; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.UserManagementService; @@ -70,8 +71,6 @@ import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreManager; import org.wso2.carbon.user.core.UserCoreConstants; import org.wso2.carbon.user.core.service.RealmService; -import org.wso2.carbon.user.mgt.UserRealmProxy; -import org.wso2.carbon.user.mgt.common.UIPermissionNode; import org.wso2.carbon.user.mgt.common.UserAdminException; import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; @@ -83,7 +82,6 @@ import javax.ws.rs.HeaderParam; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; @@ -92,10 +90,8 @@ import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; -import java.security.SecureRandom; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -129,6 +125,7 @@ public class UserManagementServiceImpl implements UserManagementService { @Override public Response addUser(UserInfo userInfo) { try { + UserManagementProviderService ums = DeviceMgtAPIUtils.getUserManagementService(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (userStoreManager.isExistingUser(userInfo.getUsername())) { // if user already exists @@ -140,51 +137,8 @@ public class UserManagementServiceImpl implements UserManagementService { String msg = "User by username: " + userInfo.getUsername() + " already exists. Try with another username." ; return Response.status(Response.Status.CONFLICT).entity(msg).build(); } - - String initialUserPassword; - if (userInfo.getPassword() != null) { - initialUserPassword = userInfo.getPassword(); - } else { - initialUserPassword = this.generateInitialUserPassword(); - } - - Map defaultUserClaims = - this.buildDefaultUserClaims(userInfo.getFirstname(), userInfo.getLastname(), - userInfo.getEmailAddress(), true); - - userStoreManager.addUser(userInfo.getUsername(), initialUserPassword, - userInfo.getRoles(), defaultUserClaims, null); - // Outputting debug message upon successful addition of user - if (log.isDebugEnabled()) { - log.debug("User '" + userInfo.getUsername() + "' has successfully been added."); - } - - BasicUserInfo createdUserInfo = this.getBasicUserInfo(userInfo.getUsername()); - // Outputting debug message upon successful retrieval of user - if (log.isDebugEnabled()) { - log.debug("User by username: " + userInfo.getUsername() + " was found."); - } - DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); - String[] bits = userInfo.getUsername().split("/"); - String username = bits[bits.length - 1]; - String recipient = userInfo.getEmailAddress(); - Properties props = new Properties(); - props.setProperty("first-name", userInfo.getFirstname()); - props.setProperty("last-name", userInfo.getLastname()); - props.setProperty("username", username); - props.setProperty("password", initialUserPassword); - - EmailMetaInfo metaInfo = new EmailMetaInfo(recipient, props); BasicUserInfoWrapper userInfoWrapper = new BasicUserInfoWrapper(); - String message; - try { - dms.sendRegistrationEmail(metaInfo); - message = "An invitation mail will be sent to this user to initiate device enrollment."; - } catch (ConfigurationManagementException e) { - message = "Mail Server is not configured. Email invitation will not be sent."; - } - userInfoWrapper.setBasicUserInfo(createdUserInfo); - userInfoWrapper.setMessage(message); + userInfoWrapper = ums.addUser(userInfo); return Response.created(new URI(API_BASE_PATH + "/" + URIEncoder.encode(userInfo.getUsername(), "UTF-8"))).entity(userInfoWrapper).build(); } catch (UserStoreException e) { @@ -205,8 +159,8 @@ public class UserManagementServiceImpl implements UserManagementService { log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); - } catch (DeviceManagementException e) { - String msg = "Error occurred while sending registration email to the user " + userInfo.getUsername(); + } catch (UserManagementException e) { + String msg = "Error occurred while trying to add the user '" + userInfo.getUsername() + "'"; log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); @@ -221,6 +175,7 @@ public class UserManagementServiceImpl implements UserManagementService { username = domain + '/' + username; } try { + UserManagementProviderService ums = DeviceMgtAPIUtils.getUserManagementService(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (!userStoreManager.isExistingUser(username)) { if (log.isDebugEnabled()) { @@ -229,14 +184,19 @@ public class UserManagementServiceImpl implements UserManagementService { String msg = "User by username: " + username + " does not exist."; return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } - - BasicUserInfo user = this.getBasicUserInfo(username); + BasicUserInfo user = ums.getUser(username); return Response.status(Response.Status.OK).entity(user).build(); } catch (UserStoreException e) { String msg = "Error occurred while retrieving information of the user '" + username + "'"; log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } catch (UserManagementException e) { + String message = "Error occurred while trying to get the user '" + username + "'"; + log.error(message, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(message).build()) + .build(); } } @@ -247,6 +207,7 @@ public class UserManagementServiceImpl implements UserManagementService { username = domain + '/' + username; } try { + UserManagementProviderService ums = DeviceMgtAPIUtils.getUserManagementService(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (!userStoreManager.isExistingUser(username)) { if (log.isDebugEnabled()) { @@ -256,67 +217,21 @@ public class UserManagementServiceImpl implements UserManagementService { String msg = "User by username: " + username + " does not exist."; return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } - - Map defaultUserClaims = - this.buildDefaultUserClaims(userInfo.getFirstname(), userInfo.getLastname(), - userInfo.getEmailAddress(), false); - if (StringUtils.isNotEmpty(userInfo.getPassword())) { - // Decoding Base64 encoded password - userStoreManager.updateCredentialByAdmin(username, - userInfo.getPassword()); - log.debug("User credential of username: " + username + " has been changed"); - } - List currentRoles = this.getFilteredRoles(userStoreManager, username); - - List newRoles = new ArrayList<>(); - if (userInfo.getRoles() != null) { - newRoles = Arrays.asList(userInfo.getRoles()); - } - - List rolesToAdd = new ArrayList<>(newRoles); - List rolesToDelete = new ArrayList<>(); - - for (String role : currentRoles) { - if (newRoles.contains(role)) { - rolesToAdd.remove(role); - } else { - rolesToDelete.add(role); - } - } - rolesToDelete.remove(ROLE_EVERYONE); - rolesToAdd.remove(ROLE_EVERYONE); - userStoreManager.updateRoleListOfUser(username, - rolesToDelete.toArray(new String[rolesToDelete.size()]), - rolesToAdd.toArray(new String[rolesToAdd.size()])); - userStoreManager.setUserClaimValues(username, defaultUserClaims, null); - // Outputting debug message upon successful addition of user - if (log.isDebugEnabled()) { - log.debug("User by username: " + username + " was successfully updated."); - } - - BasicUserInfo updatedUserInfo = this.getBasicUserInfo(username); - return Response.ok().entity(updatedUserInfo).build(); + return Response.ok().entity(ums.updateUser(username, userInfo)).build(); } catch (UserStoreException e) { String msg = "Error occurred while trying to update user '" + username + "'"; log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } catch (UserManagementException e) { + String message = "Error occurred while trying to update the user '" + username + "'"; + log.error(message, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(message).build()) + .build(); } } - private List getFilteredRoles(UserStoreManager userStoreManager, String username) - throws UserStoreException { - String[] roleListOfUser; - roleListOfUser = userStoreManager.getRoleListOfUser(username); - List filteredRoles = new ArrayList<>(); - for (String role : roleListOfUser) { - if (!(role.startsWith("Internal/") || role.startsWith("Authentication/"))) { - filteredRoles.add(role); - } - } - return filteredRoles; - } - @DELETE @Consumes(MediaType.WILDCARD) @Override @@ -368,6 +283,7 @@ public class UserManagementServiceImpl implements UserManagementService { username = domain + '/' + username; } try { + UserManagementProviderService ums = DeviceMgtAPIUtils.getUserManagementService(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (!userStoreManager.isExistingUser(username)) { if (log.isDebugEnabled()) { @@ -378,13 +294,19 @@ public class UserManagementServiceImpl implements UserManagementService { } RoleList result = new RoleList(); - result.setList(getFilteredRoles(userStoreManager, username)); + result.setList(ums.getRoles(username)); return Response.status(Response.Status.OK).entity(result).build(); } catch (UserStoreException e) { String msg = "Error occurred while trying to retrieve roles of the user '" + username + "'"; log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } catch (UserManagementException e) { + String message = "Error occurred while trying to retrieve roles of the user '" + username + "'"; + log.error(message, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(message).build()) + .build(); } } @@ -409,46 +331,15 @@ public class UserManagementServiceImpl implements UserManagementService { int appliedLimit = -1; try { - UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); - - //As the listUsers function accepts limit only to accommodate offset we are passing offset + limit - List users = Arrays.asList(userStoreManager.listUsers(appliedFilter, appliedLimit)); - if (domain != null && !domain.isEmpty()) { - users = getUsersFromDomain(domain, users); - } - userList = new ArrayList<>(users.size()); - BasicUserInfo user; - for (String username : users) { - if (Constants.APIM_RESERVED_USER.equals(username) || Constants.RESERVED_USER.equals(username)) { - continue; - } - user = getBasicUserInfo(username); - userList.add(user); - } - - int toIndex = offset + limit; - int listSize = userList.size(); - int lastIndex = listSize - 1; - - if (offset <= lastIndex) { - if (toIndex <= listSize) { - offsetList = userList.subList(offset, toIndex); - } else { - offsetList = userList.subList(offset, listSize); - } - } else { - offsetList = new ArrayList<>(); - } - BasicUserInfoList result = new BasicUserInfoList(); - result.setList(offsetList); - result.setCount(userList.size()); - - return Response.status(Response.Status.OK).entity(result).build(); - } catch (UserStoreException e) { - String msg = "Error occurred while retrieving the list of users."; - log.error(msg, e); - return Response.serverError().entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + UserManagementProviderService ums = DeviceMgtAPIUtils.getUserManagementService(); + BasicUserInfoList basicUserInfoList = ums.getUsers(appliedFilter,appliedLimit, domain, limit, offset); + return Response.status(Response.Status.OK).entity(basicUserInfoList).build(); + } catch (UserManagementException e) { + String message = "Error occurred while trying to retrieve all users of domain '" + domain + "'"; + log.error(message, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(message).build()) + .build(); } } @@ -475,78 +366,17 @@ public class UserManagementServiceImpl implements UserManagementService { limit = Constants.DEFAULT_PAGE_LIMIT; } - List filteredUserList = new ArrayList<>(); - List commonUsers = null, tempList; + BasicUserInfo basicUserInfo = new BasicUserInfo(); + basicUserInfo.setUsername(username); + basicUserInfo.setFirstname(firstName); + basicUserInfo.setLastname(lastName); + basicUserInfo.setEmailAddress(emailAddress); try { - if (StringUtils.isNotEmpty(username)) { - commonUsers = getUserList(null, username); - } - if (commonUsers != null) { - commonUsers.remove(Constants.APIM_RESERVED_USER); - commonUsers.remove(Constants.RESERVED_USER); - } - - if (!skipSearch(commonUsers) && StringUtils.isNotEmpty(firstName)) { - tempList = getUserList(Constants.USER_CLAIM_FIRST_NAME, firstName); - if (commonUsers == null) { - commonUsers = tempList; - } else { - commonUsers.retainAll(tempList); - } - } - - if (!skipSearch(commonUsers) && StringUtils.isNotEmpty(lastName)) { - tempList = getUserList(Constants.USER_CLAIM_LAST_NAME, lastName); - if (commonUsers == null || commonUsers.size() == 0) { - commonUsers = tempList; - } else { - commonUsers.retainAll(tempList); - } - } - - if (!skipSearch(commonUsers) && StringUtils.isNotEmpty(emailAddress)) { - tempList = getUserList(Constants.USER_CLAIM_EMAIL_ADDRESS, emailAddress); - if (commonUsers == null || commonUsers.size() == 0) { - commonUsers = tempList; - } else { - commonUsers.retainAll(tempList); - } - } - - BasicUserInfo basicUserInfo; - if (commonUsers != null) { - for (String user : commonUsers) { - basicUserInfo = new BasicUserInfo(); - basicUserInfo.setUsername(user); - basicUserInfo.setEmailAddress(getClaimValue(user, Constants.USER_CLAIM_EMAIL_ADDRESS)); - basicUserInfo.setFirstname(getClaimValue(user, Constants.USER_CLAIM_FIRST_NAME)); - basicUserInfo.setLastname(getClaimValue(user, Constants.USER_CLAIM_LAST_NAME)); - filteredUserList.add(basicUserInfo); - } - } - - int toIndex = offset + limit; - int listSize = filteredUserList.size(); - int lastIndex = listSize - 1; - - List offsetList; - if (offset <= lastIndex) { - if (toIndex <= listSize) { - offsetList = filteredUserList.subList(offset, toIndex); - } else { - offsetList = filteredUserList.subList(offset, listSize); - } - } else { - offsetList = new ArrayList<>(); - } - - BasicUserInfoList result = new BasicUserInfoList(); - result.setList(offsetList); - result.setCount(commonUsers != null ? commonUsers.size() : 0); - + UserManagementProviderService ums = DeviceMgtAPIUtils.getUserManagementService(); + BasicUserInfoList result = ums.getUsersSearch(basicUserInfo, offset, limit); return Response.status(Response.Status.OK).entity(result).build(); - } catch (UserStoreException e) { + } catch (UserManagementException e) { String msg = "Error occurred while retrieving the list of users."; log.error(msg, e); return Response.serverError().entity( @@ -650,28 +480,10 @@ public class UserManagementServiceImpl implements UserManagementService { } List userList; try { - UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); - String[] users; - if (userStoreDomain.equals("all")) { - users = userStoreManager.listUsers(filter + "*", limit); - } else { - users = userStoreManager.listUsers(userStoreDomain + "/" + filter + "*", limit); - } - userList = new ArrayList<>(); - UserInfo user; - for (String username : users) { - if (Constants.APIM_RESERVED_USER.equals(username) || Constants.RESERVED_USER.equals(username)) { - continue; - } - user = new UserInfo(); - user.setUsername(username); - user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); - user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); - user.setLastname(getClaimValue(username, Constants.USER_CLAIM_LAST_NAME)); - userList.add(user); - } + UserManagementProviderService ums = DeviceMgtAPIUtils.getUserManagementService(); + userList = ums.getUserNames(filter, userStoreDomain, offset, limit); return Response.status(Response.Status.OK).entity(userList).build(); - } catch (UserStoreException e) { + } catch (UserManagementException e) { String msg = "Error occurred while retrieving the list of users using the filter : " + filter; log.error(msg, e); return Response.serverError().entity( @@ -1043,28 +855,13 @@ public class UserManagementServiceImpl implements UserManagementService { String username = CarbonContext.getThreadLocalCarbonContext().getUsername(); try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); + UserManagementProviderService ums = DeviceMgtAPIUtils.getUserManagementService(); if (!userStoreManager.isExistingUser(username)) { String message = "User by username: " + username + " does not exist for permission retrieval."; log.error(message); return Response.status(Response.Status.NOT_FOUND).entity(message).build(); } - // Get a list of roles which the user assigned to - List roles = getFilteredRoles(userStoreManager, username); - List permissions = new ArrayList<>(); - UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - // Get permissions for each role - for (String roleName : roles) { - try { - permissions.addAll(getPermissionsListFromRole(roleName, userRealm, tenantId)); - } catch (UserAdminException e) { - String message = "Error occurred while retrieving the permissions of role '" + roleName + "'"; - log.error(message, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(message).build()) - .build(); - } - } + List permissions = ums.getPermissions(username); PermissionList permissionList = new PermissionList(); permissionList.setList(permissions); return Response.status(Response.Status.OK).entity(permissionList).build(); @@ -1074,26 +871,15 @@ public class UserManagementServiceImpl implements UserManagementService { return Response.status(Response.Status.INTERNAL_SERVER_ERROR) .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(message).build()) .build(); + } catch (UserManagementException e) { + String message = "Error occurred while trying to retrieve permissions of the user '" + username + "'"; + log.error(message, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(message).build()) + .build(); } } - private Map buildDefaultUserClaims(String firstName, String lastName, String emailAddress, - boolean isFresh) { - Map defaultUserClaims = new HashMap<>(); - defaultUserClaims.put(Constants.USER_CLAIM_FIRST_NAME, firstName); - defaultUserClaims.put(Constants.USER_CLAIM_LAST_NAME, lastName); - defaultUserClaims.put(Constants.USER_CLAIM_EMAIL_ADDRESS, emailAddress); - if (isFresh) { - defaultUserClaims.put(Constants.USER_CLAIM_CREATED, String.valueOf(Instant.now().getEpochSecond())); - } else { - defaultUserClaims.put(Constants.USER_CLAIM_MODIFIED, String.valueOf(Instant.now().getEpochSecond())); - } - if (log.isDebugEnabled()) { - log.debug("Default claim map is created for new user: " + defaultUserClaims.toString()); - } - return defaultUserClaims; - } - /** * This method is used to build String map for user claims with updated external device details * @@ -1128,37 +914,6 @@ public class UserManagementServiceImpl implements UserManagementService { return userClaims; } - private String generateInitialUserPassword() { - int passwordLength = 6; - //defining the pool of characters to be used for initial password generation - String lowerCaseCharset = "abcdefghijklmnopqrstuvwxyz"; - String upperCaseCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - String numericCharset = "0123456789"; - SecureRandom randomGenerator = new SecureRandom(); - String totalCharset = lowerCaseCharset + upperCaseCharset + numericCharset; - int totalCharsetLength = totalCharset.length(); - StringBuilder initialUserPassword = new StringBuilder(); - for (int i = 0; i < passwordLength; i++) { - initialUserPassword.append( - totalCharset.charAt(randomGenerator.nextInt(totalCharsetLength))); - } - if (log.isDebugEnabled()) { - log.debug("Initial user password is created for new user: " + initialUserPassword); - } - return initialUserPassword.toString(); - } - - private BasicUserInfo getBasicUserInfo(String username) throws UserStoreException { - BasicUserInfo userInfo = new BasicUserInfo(); - userInfo.setUsername(username); - userInfo.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); - userInfo.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); - userInfo.setLastname(getClaimValue(username, Constants.USER_CLAIM_LAST_NAME)); - userInfo.setCreatedDate(getClaimValue(username, Constants.USER_CLAIM_CREATED)); - userInfo.setModifiedDate(getClaimValue(username, Constants.USER_CLAIM_MODIFIED)); - return userInfo; - } - private String getClaimValue(String username, String claimUri) throws UserStoreException { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); return userStoreManager.getUserClaimValue(username, claimUri, null); @@ -1179,74 +934,6 @@ public class UserManagementServiceImpl implements UserManagementService { return DeviceManagementConstants.EmailAttributes.DEFAULT_ENROLLMENT_TEMPLATE; } - /** - * Searches users which matches a given filter based on a claim - * - * @param claim the claim value to apply the filter. If null users will be filtered by username. - * @param filter the search query. - * @return List of users which matches. - * @throws UserStoreException If unable to search users. - */ - private ArrayList getUserList(String claim, String filter) throws UserStoreException { - String defaultFilter = "*"; - - org.wso2.carbon.user.core.UserStoreManager userStoreManager = - (org.wso2.carbon.user.core.UserStoreManager) DeviceMgtAPIUtils.getUserStoreManager(); - - String appliedFilter = filter + defaultFilter; - - String[] users; - if (log.isDebugEnabled()) { - log.debug("Searching Users - claim: " + claim + " filter: " + appliedFilter); - } - if (StringUtils.isEmpty(claim)) { - users = userStoreManager.listUsers(appliedFilter, -1); - } else { - users = userStoreManager.getUserList(claim, appliedFilter, null); - } - - if (log.isDebugEnabled()) { - log.debug("Returned user count: " + users.length); - } - - return new ArrayList<>(Arrays.asList(users)); - } - - /** - * User search provides an AND search result and if either of the filter returns an empty set of users, there is no - * need to carry on the search further. This method decides whether to carry on the search or not. - * - * @param commonUsers current filtered user list. - * @return true if further search is needed. - */ - private boolean skipSearch(List commonUsers) { - return commonUsers != null && commonUsers.size() == 0; - } - - /** - * Returns a list of permissions of a given role - * @param roleName name of the role - * @param tenantId the user's tenetId - * @param userRealm user realm of the tenant - * @return list of permissions - * @throws UserAdminException If unable to get the permissions - */ - private static List getPermissionsListFromRole(String roleName, UserRealm userRealm, int tenantId) - throws UserAdminException { - org.wso2.carbon.user.core.UserRealm userRealmCore; - try { - userRealmCore = (org.wso2.carbon.user.core.UserRealm) userRealm; - } catch (ClassCastException e) { - String message = "Provided UserRealm object is not an instance of org.wso2.carbon.user.core.UserRealm"; - log.error(message, e); - throw new UserAdminException(message, e); - } - UserRealmProxy userRealmProxy = new UserRealmProxy(userRealmCore); - List permissionsList = new ArrayList<>(); - final UIPermissionNode rolePermissions = userRealmProxy.getRolePermissions(roleName, tenantId); - DeviceMgtAPIUtils.iteratePermissions(rolePermissions, permissionsList); - return permissionsList; - } /** * Returns a Response with the list of user stores available for a tenant @@ -1283,23 +970,4 @@ public class UserManagementServiceImpl implements UserManagementService { userStoreList.setCount(userStores.size()); return Response.status(Response.Status.OK).entity(userStoreList).build(); } - - /** - * Iterates through the list of all users and returns a list of users from the specified user store domain - * @param domain user store domain name - * @param users list of all users from UserStoreManager - * @return list of users from specified user store domain - */ - public List getUsersFromDomain(String domain, List users) { - List userList = new ArrayList<>(); - for(String username : users) { - String[] domainName = username.split("/"); - if(domain.equals(Constants.PRIMARY_USER_STORE) && domainName.length == 1) { - userList.add(username); - } else if (domainName[0].equals(domain) && domainName.length > 1) { - userList.add(username); - } - } - return userList; - } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java index 1c3be6a797..dc3a845ab0 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java @@ -21,6 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.util; import io.entgra.device.mgt.core.apimgt.webapp.publisher.APIPublisherService; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager; +import io.entgra.device.mgt.core.device.mgt.core.service.UserManagementProviderService; import org.apache.axis2.AxisFault; import org.apache.axis2.client.Options; import org.apache.axis2.java.security.SSLProtocolSocketFactory; @@ -282,6 +283,18 @@ public class DeviceMgtAPIUtils { return deviceManagementProviderService; } + public static UserManagementProviderService getUserManagementService() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + UserManagementProviderService userManagementProviderService = + (UserManagementProviderService) ctx.getOSGiService(UserManagementProviderService.class, null); + if (userManagementProviderService == null) { + String msg = "UserImpl Management provider service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return userManagementProviderService; + } + public static DeviceTypeGeneratorService getDeviceTypeGeneratorService() { PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); DeviceTypeGeneratorService deviceTypeGeneratorService = diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java index d032194a1f..8aed173803 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java @@ -18,7 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; -import io.entgra.device.mgt.core.device.mgt.common.Device; +import io.entgra.device.mgt.core.device.mgt.core.service.UserManagementProviderService; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -40,9 +40,9 @@ import io.entgra.device.mgt.core.device.mgt.common.spi.OTPManagementService; import io.entgra.device.mgt.core.device.mgt.core.otp.mgt.service.OTPManagementServiceImpl; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderServiceImpl; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.BasicUserInfo; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.EnrollmentInvitation; -import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.UserInfo; +import io.entgra.device.mgt.core.device.mgt.common.BasicUserInfo; +import io.entgra.device.mgt.core.device.mgt.common.EnrollmentInvitation; +import io.entgra.device.mgt.core.device.mgt.common.UserInfo; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.UserManagementService; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.Constants; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; @@ -70,6 +70,8 @@ public class UserManagementServiceImplTest { private UserStoreManager userStoreManager; private UserManagementService userManagementService; private DeviceManagementProviderService deviceManagementProviderService; + + private UserManagementProviderService userManagementProviderService; private OTPManagementService otpManagementService; private static final String DEFAULT_DEVICE_USER = "Internal/devicemgt-user"; private UserRealm userRealm; @@ -168,6 +170,8 @@ public class UserManagementServiceImplTest { @Test(description = "This method tests the updateUser method of UserManagementService", dependsOnMethods = {"testGetUser"}) public void testUpdateUser() throws UserStoreException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserManagementService")) + .toReturn(this.userManagementProviderService); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); Response response = userManagementService.updateUser(TEST2_USERNAME, null, null); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasePaginatedResult.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasePaginatedResult.java similarity index 94% rename from components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasePaginatedResult.java rename to components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasePaginatedResult.java index 5b9cf6e77a..01f41d8802 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasePaginatedResult.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasePaginatedResult.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations * under the License. */ -package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; +package io.entgra.device.mgt.core.device.mgt.common; import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.annotations.ApiModelProperty; @@ -38,4 +38,4 @@ public class BasePaginatedResult { public void setCount(long count) { this.count = count; } -} \ No newline at end of file +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfo.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfo.java similarity index 97% rename from components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfo.java rename to components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfo.java index fa1e5612ce..5e44c5931f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfo.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfo.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations * under the License. */ -package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; +package io.entgra.device.mgt.core.device.mgt.common; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfoList.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfoList.java similarity index 90% rename from components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfoList.java rename to components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfoList.java index 31fe5a333e..247790a1b7 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfoList.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfoList.java @@ -15,9 +15,11 @@ * specific language governing permissions and limitations * under the License. */ -package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; +package io.entgra.device.mgt.core.device.mgt.common; import com.fasterxml.jackson.annotation.JsonProperty; +import io.entgra.device.mgt.core.device.mgt.common.BasePaginatedResult; +import io.entgra.device.mgt.core.device.mgt.common.BasicUserInfo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfoMetadata.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfoMetadata.java new file mode 100644 index 0000000000..6929b3d921 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfoMetadata.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.device.mgt.common; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModelProperty; + +import java.util.ArrayList; +import java.util.List; + +public class BasicUserInfoMetadata { + + private List users = new ArrayList<>(); + + private int totalUserCount; + + public int getTotalUserCount() { + return totalUserCount; + } + + public void setTotalUserCount(int totalUserCount) { + this.totalUserCount = totalUserCount; + } + + public List getList() { + return users; + } + + public void setList(List users) { + this.users = users; + } + +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfoWrapper.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfoWrapper.java similarity index 93% rename from components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfoWrapper.java rename to components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfoWrapper.java index 8530327f4a..4072a3123b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/BasicUserInfoWrapper.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/BasicUserInfoWrapper.java @@ -16,8 +16,9 @@ * under the License. */ -package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; +package io.entgra.device.mgt.core.device.mgt.common; +import io.entgra.device.mgt.core.device.mgt.common.BasicUserInfo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/EnrollmentInvitation.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/EnrollmentInvitation.java similarity index 100% rename from components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/EnrollmentInvitation.java rename to components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/EnrollmentInvitation.java diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/UserInfo.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/UserInfo.java similarity index 92% rename from components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/UserInfo.java rename to components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/UserInfo.java index dd2d8a6567..702b960f80 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/UserInfo.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/UserInfo.java @@ -16,8 +16,9 @@ * under the License. */ -package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; +package io.entgra.device.mgt.core.device.mgt.common; +import io.entgra.device.mgt.core.device.mgt.common.BasicUserInfo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -50,4 +51,4 @@ public class UserInfo extends BasicUserInfo { this.roles = roles; } -} \ No newline at end of file +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/UserManagementException.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/UserManagementException.java new file mode 100644 index 0000000000..613a7e88b5 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/UserManagementException.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.device.mgt.common.exceptions; + +public class UserManagementException extends Exception { + + private static final long serialVersionUID = -3151279311929070297L; + + public UserManagementException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public UserManagementException(String message, Throwable cause) { + super(message, cause); + } + + public UserManagementException(String msg) { + super(msg); + } + + public UserManagementException() { + super(); + } + + public UserManagementException(Throwable cause) { + super(cause); + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementConstants.java index 8fc2e2804a..d0123227a3 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementConstants.java @@ -148,6 +148,15 @@ public final class DeviceManagementConstants { public static final String CLAIM_EMAIL_ADDRESS = "http://wso2.org/claims/emailaddress"; public static final String CLAIM_FIRST_NAME = "http://wso2.org/claims/givenname"; + public static final String CLAIM_LAST_NAME = "http://wso2.org/claims/lastname"; + public static final String CLAIM_CREATED = "http://wso2.org/claims/created"; + public static final String CLAIM_MODIFIED = "http://wso2.org/claims/modified"; + public static final String CLAIM_DEVICES = "http://wso2.org/claims/devices"; + public static final String RESERVED_USER = "reserved_user"; + + public static final String APIM_RESERVED_USER = "apim_reserved_user"; + + public static final String PRIMARY_USER_STORE = "PRIMARY"; // Permissions that are given for a normal device user. public static final Permission[] PERMISSIONS_FOR_DEVICE_USER = { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java index a1dbe98c2a..f2697278f5 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.internal; +import io.entgra.device.mgt.core.device.mgt.core.service.UserManagementProviderService; import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService; import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.common.DeviceStatusTaskPluginConfig; @@ -93,6 +94,8 @@ public class DeviceManagementDataHolder { private WhiteLabelManagementService whiteLabelManagementService; private TraccarManagementService traccarManagementService; + private UserManagementProviderService userManagementProviderService; + private final Map deviceStatusTaskPluginConfigs = Collections.synchronizedMap( new HashMap<>()); @@ -384,6 +387,14 @@ public class DeviceManagementDataHolder { this.whiteLabelManagementService = whiteLabelManagementService; } + public UserManagementProviderService getUserManagementProviderService() { + return userManagementProviderService; + } + + public void setUserManagementProviderService(UserManagementProviderService userManagementProviderService) { + this.userManagementProviderService = userManagementProviderService; + } + public TraccarManagementService getTraccarManagementService() { TraccarManagementService traccarManagementService; PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java index d7b459d4b7..26cc8212ee 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.device.mgt.core.internal; +import io.entgra.device.mgt.core.device.mgt.core.service.*; import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -77,10 +78,6 @@ import io.entgra.device.mgt.core.device.mgt.core.push.notification.mgt.task.Push import io.entgra.device.mgt.core.device.mgt.core.report.mgt.ReportManagementServiceImpl; import io.entgra.device.mgt.core.device.mgt.core.search.mgt.SearchManagerService; import io.entgra.device.mgt.core.device.mgt.core.search.mgt.impl.SearchManagerServiceImpl; -import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; -import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderServiceImpl; -import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService; -import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderServiceImpl; import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService; import io.entgra.device.mgt.core.device.mgt.core.traccar.api.service.DeviceAPIClientService; import io.entgra.device.mgt.core.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl; @@ -306,6 +303,11 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProvider); bundleContext.registerService(DeviceManagementProviderService.class.getName(), deviceManagementProvider, null); + /* Registering User Management Service */ + UserManagementProviderService userManagementProviderService = new UserManagementProviderServiceImpl(); + DeviceManagementDataHolder.getInstance().setUserManagementProviderService(userManagementProviderService); + bundleContext.registerService(UserManagementProviderService.class.getName(), userManagementProviderService, null); + /* Registering Device API Client Service */ DeviceAPIClientService deviceAPIClientService = new DeviceAPIClientServiceImpl(); DeviceManagementDataHolder.getInstance().setDeviceAPIClientService(deviceAPIClientService); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderService.java new file mode 100644 index 0000000000..65fbdc3cc5 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderService.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.device.mgt.core.service; + +import com.google.gson.JsonArray; +import io.entgra.device.mgt.core.device.mgt.common.*; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.UserManagementException; + +import java.util.List; + +/** + * Proxy class for all Device Management related operations that take the corresponding plugin type in + * and resolve the appropriate plugin implementation + */ +public interface UserManagementProviderService { + + /** + * Method to retrieve the filtered roles of the user. + * + * @param userInfo new user data + * @return BasicUserInfoWrapper which has the information about the added user. + * @throws UserManagementException If some unusual behaviour is observed while adding the user. + */ + BasicUserInfoWrapper addUser(UserInfo userInfo) throws UserManagementException; + + /** + * Method to retrieve the filtered roles of the user. + * + * @param username name of the user + * @return BasicUserInfo object which consists of the user data. + * @throws UserManagementException If some unusual behaviour is observed while getting the user data. + */ + BasicUserInfo getUser(String username) throws UserManagementException; + + /** + * Method to retrieve the filtered roles of the user. + * + * @param username name of the user the permissions are retrieved from + * @return List of permissions of the given user. + * @throws UserManagementException If some unusual behaviour is observed while fetching the permissions of user. + */ + List getPermissions(String username) throws UserManagementException; + + /** + * Method to retrieve the filtered roles of the user. + * + * @param username name of the user the roles are retrieved from + * @return List of roles of the given user. + * @throws UserManagementException If some unusual behaviour is observed while fetching the roles of user. + */ + List getRoles(String username) throws UserManagementException; + + /** + * Method to retrieve the filtered roles of the user. + * + * @param username name of the user the roles are retrieved from + * @return Object with the updated user data. + * @throws UserManagementException If some unusual behaviour is observed while updating user. + */ + BasicUserInfo updateUser(String username, UserInfo userInfo) throws UserManagementException; + + + /** + * Method to retrieve the list of users. + * + * @param appliedFilter filter to be applied when retrieving the user list + * @param appliedLimit this value is set to 1-1 to get the whole set of users + * @param domain domain of the users + * @param limit the maximum number of the users to be retrieved + * @param offset the starting index of data retrieval + * @throws UserManagementException If some unusual behaviour is observed while fetching the users. + */ + BasicUserInfoList getUsers(String appliedFilter, int appliedLimit, String domain, int limit, int offset) throws UserManagementException; + + /** + * Method to retrieve the list of users. + * + * @param basicUserInfo data needed for the user search + * @param limit the maximum number of the users to be retrieved + * @param offset the starting index of data retrieval + * @throws UserManagementException If some unusual behaviour is observed while fetching the users. + */ + BasicUserInfoList getUsersSearch(BasicUserInfo basicUserInfo, int offset, int limit) throws UserManagementException; + + /** + * Method to retrieve the list of users based on filter. + * + * @param filter data needed for the user search + * @param userStoreDomain domain of the user + * @param limit the maximum number of the users to be retrieved + * @param offset the starting index of data retrieval + * @throws UserManagementException If some unusual behaviour is observed while fetching the users. + */ + List getUserNames(String filter, String userStoreDomain, int offset, int limit) throws UserManagementException; + + /** + * Method to retrieve the list of users based on filter. + * + * @param username data needed for the user search + * @param deviceList domain of the user + * @param domain the maximum number of the users to be retrieved + * @throws UserManagementException If some unusual behaviour is observed while fetching the users. + */ + List updateUserClaimsForDevices(String username, JsonArray deviceList, String domain) throws UserManagementException; + + /** + * Method to send enrollment invitation mail to existing user. + * + * @param enrollmentInvitation data related to the mail + * @throws UserManagementException If some unusual behaviour is observed while fetching the users. + */ +// EmailMetaInfo inviteToEnrollDevice(EnrollmentInvitation enrollmentInvitation) throws UserManagementException; + + /** + * Method to retrieve the count of users. + * + * @throws UserManagementException If some unusual behaviour is observed while fetching the count of users. + */ + int getCount() throws UserManagementException; +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceImpl.java new file mode 100644 index 0000000000..0fb7bd4944 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceImpl.java @@ -0,0 +1,609 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.device.mgt.core.service; + +import com.google.gson.JsonArray; +import io.entgra.device.mgt.core.device.mgt.common.*; +import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.ConfigurationManagementException; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.UserManagementException; +import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants; +import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraDeviceEnrolmentLoggerImpl; +import org.apache.commons.lang.StringUtils; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.user.api.UserRealm; +import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.user.api.UserStoreManager; +import org.wso2.carbon.user.mgt.UserRealmProxy; +import org.wso2.carbon.user.mgt.common.UIPermissionNode; +import org.wso2.carbon.user.mgt.common.UserAdminException; + +import java.security.SecureRandom; +import java.time.Instant; +import java.util.*; + +public class UserManagementProviderServiceImpl implements UserManagementProviderService { + + private static final EntgraLogger log = new EntgraDeviceEnrolmentLoggerImpl(UserManagementProviderServiceImpl.class); + + private static final String ROLE_EVERYONE = "Internal/everyone"; + + @Override + public BasicUserInfoWrapper addUser(UserInfo userInfo) throws UserManagementException { + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance(). + getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); + String initialUserPassword; + if (userInfo.getPassword() != null) { + initialUserPassword = userInfo.getPassword(); + } else { + initialUserPassword = this.generateInitialUserPassword(); + } + + Map defaultUserClaims = + this.buildDefaultUserClaims(userInfo.getFirstname(), userInfo.getLastname(), + userInfo.getEmailAddress(), true); + + userStoreManager.addUser(userInfo.getUsername(), initialUserPassword, + userInfo.getRoles(), defaultUserClaims, null); + // Outputting debug message upon successful addition of user + if (log.isDebugEnabled()) { + log.debug("User '" + userInfo.getUsername() + "' has successfully been added."); + } + + BasicUserInfo createdUserInfo = this.getBasicUserInfo(userInfo.getUsername()); + // Outputting debug message upon successful retrieval of user + if (log.isDebugEnabled()) { + log.debug("User by username: " + userInfo.getUsername() + " was found."); + } + DeviceManagementProviderService managementProviderService = DeviceManagementDataHolder + .getInstance().getDeviceManagementProvider(); + String[] bits = userInfo.getUsername().split("/"); + String username = bits[bits.length - 1]; + String recipient = userInfo.getEmailAddress(); + Properties props = new Properties(); + props.setProperty("first-name", userInfo.getFirstname()); + props.setProperty("last-name", userInfo.getLastname()); + props.setProperty("username", username); + props.setProperty("password", initialUserPassword); + + EmailMetaInfo metaInfo = new EmailMetaInfo(recipient, props); + BasicUserInfoWrapper userInfoWrapper = new BasicUserInfoWrapper(); + String message; + try { + managementProviderService.sendRegistrationEmail(metaInfo); + message = "An invitation mail will be sent to this user to initiate device enrollment."; + } catch (ConfigurationManagementException e) { + message = "Mail Server is not configured. Email invitation will not be sent."; + } catch (DeviceManagementException e) { + throw new RuntimeException(e); + } + userInfoWrapper.setBasicUserInfo(createdUserInfo); + userInfoWrapper.setMessage(message); + return userInfoWrapper; + } catch (UserStoreException e) { + String msg = "Error occurred while trying to add user '" + userInfo.getUsername() + "' to the " + + "underlying user management system"; + log.error(msg, e); + throw new UserManagementException(msg, e); + } + } + + @Override + public BasicUserInfo getUser(String username) throws UserManagementException { + try { + return this.getBasicUserInfo(username); + } catch (UserStoreException e) { + String message = "Error occurred while getting data of user '" + username + "'"; + log.error(message, e); + throw new UserManagementException(message, e); + } + } + + @Override + public List getPermissions(String username) throws UserManagementException { + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance(). + getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); + + List roles = getFilteredRoles(userStoreManager, username); + List permissions = new ArrayList<>(); + UserRealm userRealm = DeviceManagementDataHolder.getInstance(). + getRealmService().getTenantUserRealm(tenantId); + // Get permissions for each role + for (String roleName : roles) { + try { + permissions.addAll(getPermissionsListFromRole(roleName, userRealm, tenantId)); + } catch (UserAdminException e) { + String message = "Error occurred while retrieving the permissions of role '" + roleName + "'"; + log.error(message, e); + throw new UserManagementException(message, e); + } + } + return permissions; + } catch (UserStoreException e) { + String message = "Error occurred while trying to retrieve roles of the user '" + username + "'"; + log.error(message, e); + throw new UserManagementException(message, e); + } + } + + @Override + public List getRoles(String username) throws UserManagementException { + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance(). + getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); + + List roles = getFilteredRoles(userStoreManager, username); + return roles; + } catch (UserStoreException e) { + String message = "Error occurred while trying to retrieve roles of the user '" + username + "'"; + log.error(message, e); + throw new UserManagementException(message, e); + } + } + + @Override + public BasicUserInfo updateUser(String username, UserInfo userInfo) throws UserManagementException { + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance(). + getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); + + Map defaultUserClaims = + this.buildDefaultUserClaims(userInfo.getFirstname(), userInfo.getLastname(), + userInfo.getEmailAddress(), false); + if (StringUtils.isNotEmpty(userInfo.getPassword())) { + // Decoding Base64 encoded password + userStoreManager.updateCredentialByAdmin(username, + userInfo.getPassword()); + log.debug("User credential of username: " + username + " has been changed"); + } + List currentRoles = this.getFilteredRoles(userStoreManager, username); + + List newRoles = new ArrayList<>(); + if (userInfo.getRoles() != null) { + newRoles = Arrays.asList(userInfo.getRoles()); + } + + List rolesToAdd = new ArrayList<>(newRoles); + List rolesToDelete = new ArrayList<>(); + + for (String role : currentRoles) { + if (newRoles.contains(role)) { + rolesToAdd.remove(role); + } else { + rolesToDelete.add(role); + } + } + rolesToDelete.remove(ROLE_EVERYONE); + rolesToAdd.remove(ROLE_EVERYONE); + userStoreManager.updateRoleListOfUser(username, + rolesToDelete.toArray(new String[rolesToDelete.size()]), + rolesToAdd.toArray(new String[rolesToAdd.size()])); + userStoreManager.setUserClaimValues(username, defaultUserClaims, null); + // Outputting debug message upon successful addition of user + if (log.isDebugEnabled()) { + log.debug("User by username: " + username + " was successfully updated."); + } + + return this.getBasicUserInfo(username); + } catch (UserStoreException e) { + String message = "Error occurred while trying to retrieve roles of the user '" + username + "'"; + log.error(message, e); + throw new UserManagementException(message, e); + } + } + + @Override + public BasicUserInfoList getUsers(String appliedFilter, int appliedLimit, String domain, int limit, int offset) + throws UserManagementException { + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance(). + getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); + List userList, offsetList; + + //As the listUsers function accepts limit only to accommodate offset we are passing offset + limit + List users = Arrays.asList(userStoreManager.listUsers(appliedFilter, appliedLimit)); + if (domain != null && !domain.isEmpty()) { + users = getUsersFromDomain(domain, users); + } + userList = new ArrayList<>(users.size()); + BasicUserInfo user; + for (String username : users) { + if (DeviceManagementConstants.User.APIM_RESERVED_USER.equals(username) || DeviceManagementConstants.User.RESERVED_USER.equals(username)) { + continue; + } + user = getBasicUserInfo(username); + userList.add(user); + } + + int toIndex = offset + limit; + int listSize = userList.size(); + int lastIndex = listSize - 1; + + if (offset <= lastIndex) { + if (toIndex <= listSize) { + offsetList = userList.subList(offset, toIndex); + } else { + offsetList = userList.subList(offset, listSize); + } + } else { + offsetList = new ArrayList<>(); + } + + BasicUserInfoList result = new BasicUserInfoList(); + result.setList(offsetList); + result.setCount(userList.size()); + + return result; + } catch (UserStoreException e) { + String msg = "Error occurred while retrieving the list of users."; + log.error(msg, e); + throw new UserManagementException(msg, e); + } + } + + @Override + public BasicUserInfoList getUsersSearch(BasicUserInfo basicUserInfo, int offset, int limit) throws UserManagementException { + List filteredUserList = new ArrayList<>(); + List commonUsers = null, tempList; + try { + if (basicUserInfo.getUsername() != null && StringUtils.isNotEmpty(basicUserInfo.getUsername())) { + commonUsers = getUserList(null, basicUserInfo.getUsername()); + } + if (commonUsers != null) { + commonUsers.remove(DeviceManagementConstants.User.APIM_RESERVED_USER); + commonUsers.remove(DeviceManagementConstants.User.RESERVED_USER); + } + + if (!skipSearch(commonUsers) && basicUserInfo.getFirstname() != null && StringUtils.isNotEmpty(basicUserInfo.getFirstname())) { + tempList = getUserList(DeviceManagementConstants.User.CLAIM_FIRST_NAME, basicUserInfo.getFirstname()); + if (commonUsers == null) { + commonUsers = tempList; + } else { + commonUsers.retainAll(tempList); + } + } + + if (!skipSearch(commonUsers) && basicUserInfo.getLastname() != null && StringUtils.isNotEmpty(basicUserInfo.getLastname())) { + tempList = getUserList(DeviceManagementConstants.User.CLAIM_LAST_NAME, basicUserInfo.getLastname()); + if (commonUsers == null || commonUsers.size() == 0) { + commonUsers = tempList; + } else { + commonUsers.retainAll(tempList); + } + } + + if (!skipSearch(commonUsers) && basicUserInfo.getEmailAddress() != null && StringUtils.isNotEmpty(basicUserInfo.getEmailAddress())) { + tempList = getUserList(DeviceManagementConstants.User.CLAIM_EMAIL_ADDRESS, basicUserInfo.getEmailAddress()); + if (commonUsers == null || commonUsers.size() == 0) { + commonUsers = tempList; + } else { + commonUsers.retainAll(tempList); + } + } + + BasicUserInfo newBasicUserInfo; + if (commonUsers != null) { + for (String user : commonUsers) { + newBasicUserInfo = new BasicUserInfo(); + newBasicUserInfo.setUsername(user); + newBasicUserInfo.setEmailAddress(getClaimValue(user, DeviceManagementConstants.User.CLAIM_EMAIL_ADDRESS)); + newBasicUserInfo.setFirstname(getClaimValue(user, DeviceManagementConstants.User.CLAIM_FIRST_NAME)); + newBasicUserInfo.setLastname(getClaimValue(user, DeviceManagementConstants.User.CLAIM_LAST_NAME)); + filteredUserList.add(newBasicUserInfo); + } + } + + int toIndex = offset + limit; + int listSize = filteredUserList.size(); + int lastIndex = listSize - 1; + + List offsetList; + if (offset <= lastIndex) { + if (toIndex <= listSize) { + offsetList = filteredUserList.subList(offset, toIndex); + } else { + offsetList = filteredUserList.subList(offset, listSize); + } + } else { + offsetList = new ArrayList<>(); + } + + BasicUserInfoList basicUserInfoList = new BasicUserInfoList(); + basicUserInfoList.setList(offsetList); + basicUserInfoList.setCount(commonUsers != null ? commonUsers.size() : 0); + return basicUserInfoList; + } catch (UserStoreException e) { + String msg = "Error occurred while retrieving the list of users."; + log.error(msg, e); + throw new UserManagementException(msg, e); + } + } + + @Override + public List getUserNames(String filter, String userStoreDomain, int offset, int limit) throws UserManagementException { + List userList; + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance(). + getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); + String[] users; + if (userStoreDomain.equals("all")) { + users = userStoreManager.listUsers(filter + "*", limit); + } else { + users = userStoreManager.listUsers(userStoreDomain + "/" + filter + "*", limit); + } + userList = new ArrayList<>(); + UserInfo user; + for (String username : users) { + if (DeviceManagementConstants.User.APIM_RESERVED_USER.equals(username) || DeviceManagementConstants.User.RESERVED_USER.equals(username)) { + continue; + } + user = new UserInfo(); + user.setUsername(username); + user.setEmailAddress(getClaimValue(username, DeviceManagementConstants.User.CLAIM_EMAIL_ADDRESS)); + user.setFirstname(getClaimValue(username, DeviceManagementConstants.User.CLAIM_FIRST_NAME)); + user.setLastname(getClaimValue(username, DeviceManagementConstants.User.CLAIM_LAST_NAME)); + userList.add(user); + } + return userList; + } catch (UserStoreException e) { + String msg = "Error occurred while retrieving the list of users using the filter : " + filter; + log.error(msg, e); + throw new UserManagementException(msg, e); + } + } + + @Override + public List updateUserClaimsForDevices(String username, JsonArray deviceList, String domain) throws UserManagementException { + return null; + } + +// @Override +// public EmailMetaInfo inviteToEnrollDevice(EnrollmentInvitation enrollmentInvitation) throws UserManagementException { +// try { +// Set recipients = new HashSet<>(); +// recipients.addAll(enrollmentInvitation.getRecipients()); +// Properties props = new Properties(); +// String username = DeviceMgtAPIUtils.getAuthenticatedUser(); +// String firstName = getClaimValue(username, DeviceManagementConstants.User.CLAIM_FIRST_NAME); +// String lastName = getClaimValue(username, DeviceManagementConstants.User.CLAIM_LAST_NAME); +// if (firstName == null) { +// firstName = username; +// } +// if (lastName == null) { +// lastName = ""; +// } +// props.setProperty("first-name", firstName); +// props.setProperty("last-name", lastName); +// props.setProperty("device-type", enrollmentInvitation.getDeviceType()); +// EmailMetaInfo metaInfo = new EmailMetaInfo(recipients, props); +// return metaInfo; +// } catch (DeviceManagementException e) { +// String msg = "Error occurred while inviting user to enrol their device"; +// log.error(msg, e); +// throw new UserManagementException(msg, e); +// } catch (UserStoreException e) { +// String msg = "Error occurred while getting claim values to invite user"; +// log.error(msg, e); +// throw new UserManagementException(msg, e); +// } catch (ConfigurationManagementException e) { +// String msg = "Error occurred while sending the email invitations. Mail server not configured."; +// throw new UserManagementException(msg, e); +// } +// } + + + @Override + public int getCount() throws UserManagementException { + return 0; + } + + + /** + * User search provides an AND search result and if either of the filter returns an empty set of users, there is no + * need to carry on the search further. This method decides whether to carry on the search or not. + * + * @param commonUsers current filtered user list. + * @return true if further search is needed. + */ + private boolean skipSearch(List commonUsers) { + return commonUsers != null && commonUsers.size() == 0; + } + + + /** + * Searches users which matches a given filter based on a claim + * + * @param claim the claim value to apply the filter. If null users will be filtered by username. + * @param filter the search query. + * @return List of users which matches. + * @throws UserStoreException If unable to search users. + */ + private ArrayList getUserList(String claim, String filter) throws UserStoreException { + String defaultFilter = "*"; + + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + org.wso2.carbon.user.core.UserStoreManager userStoreManager = (org.wso2.carbon.user.core.UserStoreManager) DeviceManagementDataHolder.getInstance(). + getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); + + String appliedFilter = filter + defaultFilter; + + String[] users; + if (log.isDebugEnabled()) { + log.debug("Searching Users - claim: " + claim + " filter: " + appliedFilter); + } + if (StringUtils.isEmpty(claim)) { + users = userStoreManager.listUsers(appliedFilter, -1); + } else { + users = userStoreManager.getUserList(claim, appliedFilter, null); + } + + if (log.isDebugEnabled()) { + log.debug("Returned user count: " + users.length); + } + + return new ArrayList<>(Arrays.asList(users)); + } + + + /** + * Iterates through the list of all users and returns a list of users from the specified user store domain + * @param domain user store domain name + * @param users list of all users from UserStoreManager + * @return list of users from specified user store domain + */ + public List getUsersFromDomain(String domain, List users) { + List userList = new ArrayList<>(); + for(String username : users) { + String[] domainName = username.split("/"); + if(domain.equals(DeviceManagementConstants.User.PRIMARY_USER_STORE) && domainName.length == 1) { + userList.add(username); + } else if (domainName[0].equals(domain) && domainName.length > 1) { + userList.add(username); + } + } + return userList; + } + + private String generateInitialUserPassword() { + int passwordLength = 6; + //defining the pool of characters to be used for initial password generation + String lowerCaseCharset = "abcdefghijklmnopqrstuvwxyz"; + String upperCaseCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + String numericCharset = "0123456789"; + SecureRandom randomGenerator = new SecureRandom(); + String totalCharset = lowerCaseCharset + upperCaseCharset + numericCharset; + int totalCharsetLength = totalCharset.length(); + StringBuilder initialUserPassword = new StringBuilder(); + for (int i = 0; i < passwordLength; i++) { + initialUserPassword.append( + totalCharset.charAt(randomGenerator.nextInt(totalCharsetLength))); + } + if (log.isDebugEnabled()) { + log.debug("Initial user password is created for new user: " + initialUserPassword); + } + return initialUserPassword.toString(); + } + + private BasicUserInfo getBasicUserInfo(String username) throws UserStoreException { + BasicUserInfo userInfo = new BasicUserInfo(); + userInfo.setUsername(username); + userInfo.setEmailAddress(getClaimValue(username, DeviceManagementConstants.User.CLAIM_EMAIL_ADDRESS)); + userInfo.setFirstname(getClaimValue(username, DeviceManagementConstants.User.CLAIM_FIRST_NAME)); + userInfo.setLastname(getClaimValue(username, DeviceManagementConstants.User.CLAIM_LAST_NAME)); + userInfo.setCreatedDate(getClaimValue(username, DeviceManagementConstants.User.CLAIM_CREATED)); + userInfo.setModifiedDate(getClaimValue(username, DeviceManagementConstants.User.CLAIM_MODIFIED)); + return userInfo; + } + + private String getClaimValue(String username, String claimUri) throws UserStoreException { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance(). + getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); + return userStoreManager.getUserClaimValue(username, claimUri, null); + } + + private Map buildDefaultUserClaims(String firstName, String lastName, String emailAddress, + boolean isFresh) { + Map defaultUserClaims = new HashMap<>(); + defaultUserClaims.put(DeviceManagementConstants.User.CLAIM_FIRST_NAME, firstName); + defaultUserClaims.put(DeviceManagementConstants.User.CLAIM_LAST_NAME, lastName); + defaultUserClaims.put(DeviceManagementConstants.User.CLAIM_EMAIL_ADDRESS, emailAddress); + if (isFresh) { + defaultUserClaims.put(DeviceManagementConstants.User.CLAIM_CREATED, String.valueOf(Instant.now().getEpochSecond())); + } else { + defaultUserClaims.put(DeviceManagementConstants.User.CLAIM_MODIFIED, String.valueOf(Instant.now().getEpochSecond())); + } + if (log.isDebugEnabled()) { + log.debug("Default claim map is created for new user: " + defaultUserClaims.toString()); + } + return defaultUserClaims; + } + + private List getFilteredRoles(UserStoreManager userStoreManager, String username) + throws UserStoreException { + String[] roleListOfUser; + roleListOfUser = userStoreManager.getRoleListOfUser(username); + List filteredRoles = new ArrayList<>(); + for (String role : roleListOfUser) { + if (!(role.startsWith("Internal/") || role.startsWith("Authentication/"))) { + filteredRoles.add(role); + } + } + return filteredRoles; + } + + /** + * Returns a list of permissions of a given role + * @param roleName name of the role + * @param tenantId the user's tenetId + * @param userRealm user realm of the tenant + * @return list of permissions + * @throws UserAdminException If unable to get the permissions + */ + private static List getPermissionsListFromRole(String roleName, UserRealm userRealm, int tenantId) + throws UserAdminException { + org.wso2.carbon.user.core.UserRealm userRealmCore; + try { + userRealmCore = (org.wso2.carbon.user.core.UserRealm) userRealm; + } catch (ClassCastException e) { + String message = "Provided UserRealm object is not an instance of org.wso2.carbon.user.core.UserRealm"; + log.error(message, e); + throw new UserAdminException(message, e); + } + UserRealmProxy userRealmProxy = new UserRealmProxy(userRealmCore); + List permissionsList = new ArrayList<>(); + final UIPermissionNode rolePermissions = userRealmProxy.getRolePermissions(roleName, tenantId); + iteratePermissions(rolePermissions, permissionsList); + return permissionsList; + } + + /** + * Extract permissions from a UiPermissionNode using recursions + * @param uiPermissionNode an UiPermissionNode Object to extract permissions + * @param list provided list to add permissions + */ + public static void iteratePermissions(UIPermissionNode uiPermissionNode, List list) { + // To prevent NullPointer exceptions + if (uiPermissionNode == null) { + return; + } + for (UIPermissionNode permissionNode : uiPermissionNode.getNodeList()) { + if (permissionNode != null) { + if(permissionNode.isSelected()){ + list.add(permissionNode.getResourcePath()); + } + if (permissionNode.getNodeList() != null + && permissionNode.getNodeList().length > 0) { + iteratePermissions(permissionNode, list); + } + } + } + } + +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceTest.java new file mode 100644 index 0000000000..52af1682f0 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceTest.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.device.mgt.core.service; + +import io.entgra.device.mgt.core.device.mgt.core.common.BaseDeviceManagementTest; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class UserManagementProviderServiceTest extends BaseDeviceManagementTest { + + @BeforeClass + @Override + public void init() throws Exception { + + } + + @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) + public void updateUser() { + } +} -- 2.36.3 From 9df0939ca0e358b7b20a5745ba76a359f7d043dd Mon Sep 17 00:00:00 2001 From: osh Date: Wed, 15 Nov 2023 11:54:45 +0530 Subject: [PATCH 2/2] Add device filter changes --- .../service/api/DeviceManagementService.java | 10 ++- .../impl/DeviceManagementServiceImpl.java | 54 +++++++++++- .../impl/UserManagementServiceImplTest.java | 77 +++++++++++++++-- .../core/device/mgt/common/DeviceFilters.java | 24 ++++++ .../mgt/common/EnrollmentInvitation.java | 2 +- .../mgt/common/group/mgt/GroupFilter.java | 48 +++++++++++ .../core/device/mgt/core/dao/GroupDAO.java | 23 ++++- .../core/dao/impl/AbstractGroupDAOImpl.java | 84 +++++++++++++++++++ .../GroupManagementProviderService.java | 12 +++ .../GroupManagementProviderServiceImpl.java | 39 +++++++++ .../UserManagementProviderService.java | 25 ------ .../UserManagementProviderServiceImpl.java | 46 ---------- .../UserManagementProviderServiceTest.java | 36 -------- 13 files changed, 358 insertions(+), 122 deletions(-) create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/GroupFilter.java delete mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceTest.java diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/DeviceManagementService.java index eeb7caeeb3..464a2bb096 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/DeviceManagementService.java @@ -2749,7 +2749,15 @@ public interface DeviceManagementService { message = "Error occurred while getting the version data.", response = ErrorResponse.class) }) - Response getDeviceFilters(); + Response getDeviceFilters( + @ApiParam( + name = "isGroups", + value = "Check if group data is needed", required = false) + @QueryParam("isGroups") boolean isGroups, + @ApiParam( + name = "isConfig", + value = "Check if config data is needed", required = false) + @QueryParam("isConfig") boolean isConfig); @GET @Produces(MediaType.APPLICATION_JSON) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java index 1960d0e41d..a7a0fd2542 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -26,6 +26,10 @@ import io.entgra.device.mgt.core.application.mgt.common.exception.SubscriptionMa import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager; import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; +import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.ConfigurationEntry; +import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.ConfigurationManagementException; +import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.PlatformConfiguration; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupFilter; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; @@ -101,13 +105,14 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; -import java.util.concurrent.ExecutionException; import java.util.Map; +import java.util.Arrays; @Path("/devices") public class DeviceManagementServiceImpl implements DeviceManagementService { public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; + private static final String DEFAULT_ADMIN_ROLE = "admin"; private static final Log log = LogFactory.getLog(DeviceManagementServiceImpl.class); @GET @@ -1612,8 +1617,39 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @GET @Path("/filters") @Override - public Response getDeviceFilters() { + public Response getDeviceFilters( @QueryParam("isGroups") boolean isGroups, @QueryParam("isConfig") boolean isConfig) { try { + String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + List permissions = DeviceMgtAPIUtils + .getUserManagementService().getPermissions(currentUser); + List groupFilters = new ArrayList<>(); + List configList = new ArrayList<>(); + if (isGroups) { + List roles = DeviceMgtAPIUtils + .getUserManagementService().getRoles(currentUser); + boolean isAdmin = DEFAULT_ADMIN_ROLE.equals(currentUser); + boolean hasAdminRole = Arrays.asList(roles).contains(DEFAULT_ADMIN_ROLE); + if (permissions.contains("/permission/admin/device-mgt/admin/groups/view")) { + if (StringUtils.isBlank(currentUser) || isAdmin || hasAdminRole) { + groupFilters = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupFilterValues(null, null); + } else { + groupFilters = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupFilterValues(currentUser, null); + } + } else { + if (hasAdminRole) { + groupFilters = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupFilterValues(null, null); + } else { + groupFilters = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupFilterValues(currentUser, null); + } + } + } + if (isConfig) { + if (permissions.contains("/permission/admin/device-mgt/platform-configurations/view")) { + PlatformConfiguration config = DeviceMgtAPIUtils.getPlatformConfigurationManagementService(). + getConfiguration(MDMAppConstants.RegistryConstants.GENERAL_CONFIG_RESOURCE_PATH); + configList = config.getConfiguration(); + } + } List deviceTypeNames = new ArrayList<>(); List ownershipNames = new ArrayList<>(); List statusNames = new ArrayList<>(); @@ -1633,11 +1669,25 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { deviceFilters.setDeviceTypes(deviceTypeNames); deviceFilters.setOwnerships(ownershipNames); deviceFilters.setStatuses(statusNames); + deviceFilters.setConfigs(configList); + deviceFilters.setGroups(groupFilters); return Response.status(Response.Status.OK).entity(deviceFilters).build(); } catch (DeviceManagementException e) { String msg = "Error occurred white retrieving device types to be used in device filters."; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (UserManagementException e) { + String msg = "Error occurred while retrieving permission details to be used in device filters."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while retrieving group data for device filters values."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (ConfigurationManagementException e) { + String msg = "Error occurred while retrieving config data for device filter values."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java index 8aed173803..398aa55e10 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java @@ -18,6 +18,8 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; +import io.entgra.device.mgt.core.device.mgt.common.*; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.UserManagementException; import io.entgra.device.mgt.core.device.mgt.core.service.UserManagementProviderService; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; @@ -40,12 +42,10 @@ import io.entgra.device.mgt.core.device.mgt.common.spi.OTPManagementService; import io.entgra.device.mgt.core.device.mgt.core.otp.mgt.service.OTPManagementServiceImpl; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderServiceImpl; -import io.entgra.device.mgt.core.device.mgt.common.BasicUserInfo; -import io.entgra.device.mgt.core.device.mgt.common.EnrollmentInvitation; -import io.entgra.device.mgt.core.device.mgt.common.UserInfo; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.UserManagementService; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.Constants; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.user.api.RealmConfiguration; import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; @@ -88,9 +88,10 @@ public class UserManagementServiceImplTest { } @BeforeClass - public void setup() throws UserStoreException { + public void init() throws UserStoreException { initMocks(this); userManagementService = new UserManagementServiceImpl(); + userManagementProviderService = Mockito.mock(UserManagementProviderService.class); userStoreManager = Mockito.mock(UserStoreManager.class, Mockito.RETURNS_MOCKS); deviceManagementProviderService = Mockito .mock(DeviceManagementProviderServiceImpl.class, Mockito.CALLS_REAL_METHODS); @@ -114,6 +115,8 @@ public class UserManagementServiceImplTest { public void testAddUser() throws UserStoreException, ConfigurationManagementException, DeviceManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserManagementService")) + .toReturn(userManagementProviderService); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(this.deviceManagementProviderService); Mockito.doReturn(true).when(userStoreManager).isExistingUser("admin"); @@ -152,11 +155,27 @@ public class UserManagementServiceImplTest { @Test(description = "This method tests the getUser method of UserManagementService", dependsOnMethods = "testAddUser") - public void testGetUser() throws UserStoreException { + public void testGetUser() throws UserStoreException, UserManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserManagementService")) + .toReturn(userManagementProviderService); + BasicUserInfo basicUserInfo = new BasicUserInfo(); + Mockito.doReturn(basicUserInfo).when(userManagementProviderService) + .getUser(Mockito.anyString()); Response response = userManagementService.getUser(TEST_USERNAME, null, null); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "User retrieval failed"); + Mockito.reset(userManagementProviderService); + basicUserInfo.setUsername(TEST_USERNAME); + basicUserInfo.setFirstname(TEST_USERNAME); + basicUserInfo.setLastname(TEST_USERNAME); + basicUserInfo.setEmailAddress("test@gmail.com"); + Mockito.doReturn(basicUserInfo).when(userManagementProviderService) + .getUser(Mockito.anyString()); + response = userManagementService.getUser("test", null, null); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), + "GetUser request failed with valid parameters"); + Mockito.reset(this.userManagementProviderService); BasicUserInfo userInfo = (BasicUserInfo) response.getEntity(); Assert.assertEquals(userInfo.getFirstname(), TEST_USERNAME, "Retrieved user object is different from the original one " + "saved"); @@ -188,15 +207,24 @@ public class UserManagementServiceImplTest { @Test(description = "This method tests the getRolesOfUser method of UserManagementService", dependsOnMethods = {"testUpdateUser"}) - public void testGetRolesOfUser() { + public void testGetRolesOfUser() throws UserManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserManagementService")) + .toReturn(userManagementProviderService); + List roles= new ArrayList<>(); + Mockito.doReturn(roles).when(userManagementProviderService) + .getRoles(Mockito.anyString()); Response response = userManagementService.getRolesOfUser(TEST2_USERNAME, null); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "Roles of a non-existing user was successfully retrieved"); + Mockito.reset(userManagementProviderService); + Mockito.doReturn(roles).when(userManagementProviderService) + .getRoles(Mockito.anyString()); response = userManagementService.getRolesOfUser(TEST_USERNAME, null); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "Retrieval of roles of a existing user failed."); + Mockito.reset(this.userManagementProviderService); } @Test(description = "This method tests the IsUserExists method of UserManagementService", dependsOnMethods = @@ -232,12 +260,13 @@ public class UserManagementServiceImplTest { public void testGetUserNames() throws UserStoreException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserManagementService")) + .toReturn(userManagementProviderService); Mockito.doReturn(new String[] { TEST_USERNAME }).when(userStoreManager) .listUsers(Mockito.anyString(), Mockito.anyInt()); Response response = userManagementService.getUserNames(TEST_USERNAME, null, "00", 0, 0); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "Getting user names is failed for a valid request"); - } @Test(description = "This method tests the getUsers method of UserManagementService", @@ -245,6 +274,8 @@ public class UserManagementServiceImplTest { public void testGetUsers() { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(userStoreManager); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserManagementService")) + .toReturn(userManagementProviderService); Response response = userManagementService.getUsers(null, "00", 0, 10, null); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "GetUsers request failed"); } @@ -303,6 +334,8 @@ public class UserManagementServiceImplTest { + "DeviceManagementProviderService", dependsOnMethods = {"testGetUserCount"}) public void testNegativeScenarios1() throws ConfigurationManagementException, DeviceManagementException, OTPManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserManagementService")) + .toReturn(userManagementProviderService); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) @@ -322,41 +355,61 @@ public class UserManagementServiceImplTest { response = userManagementService.inviteToEnrollDevice(enrollmentInvitation); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Invite existing users to enroll device succeeded under erroneous conditions"); + Mockito.reset(this.userManagementProviderService); } @Test(description = "This method tests the behaviour of the different methods when there is an issue is " + "userStoreManager", dependsOnMethods = {"testNegativeScenarios1"}) - public void testNegativeScenarios2() throws UserStoreException { + public void testNegativeScenarios2() throws UserStoreException, UserManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserManagementService")) + .toReturn(userManagementProviderService); Mockito.doThrow(new UserStoreException()).when(userStoreManager).isExistingUser(TEST3_USERNAME); Response response = userManagementService.getUser(TEST3_USERNAME, null, null); + Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), + "Response returned successful for a user retrieval with problematic inputs"); + Mockito.when(this.userManagementProviderService.getUser(Mockito.anyString())) + .thenThrow(new UserManagementException()); + response = userManagementService.getUser(TEST3_USERNAME, null, null); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Response returned successful for a user retrieval with problematic inputs"); UserInfo userInfo = new UserInfo(); userInfo.setUsername(TEST3_USERNAME); + Mockito.reset(this.userManagementProviderService); + Mockito.when(this.userManagementProviderService.getUser(Mockito.anyString())) + .thenThrow(new UserManagementException()); response = userManagementService.addUser(userInfo); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Response returned successful for a user addition with problematic inputs"); + Mockito.reset(this.userManagementProviderService); + Mockito.when(this.userManagementProviderService.getUser(Mockito.anyString())) + .thenThrow(new UserManagementException()); response = userManagementService.updateUser(TEST3_USERNAME, null, userInfo); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Response returned successful for a user updating request with problematic inputs"); response = userManagementService.removeUser(TEST3_USERNAME, null); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), "Response returned successful for a user removal request with problematic inputs"); + Mockito.reset(this.userManagementProviderService); + Mockito.when(this.userManagementProviderService.getUser(Mockito.anyString())) + .thenThrow(new UserManagementException()); response = userManagementService.getRolesOfUser(TEST3_USERNAME, null); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Response returned successful for a user role retrieval request with problematic inputs"); response = userManagementService.isUserExists(TEST3_USERNAME); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Response returned successful for checking existence of user under problematic conditions"); + Mockito.reset(this.userManagementProviderService); } @Test(description = "This method tests the behaviour of various methods when there is an issue with UserStore " + "Manager", dependsOnMethods = {"testNegativeScenarios2"}) - public void testNegativeScenarios3() throws UserStoreException { + public void testNegativeScenarios3() throws UserStoreException, UserManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserManagementService")) + .toReturn(userManagementProviderService); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserRealm")).toReturn(userRealm); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreCountRetrieverService")) .toReturn(null); @@ -370,12 +423,17 @@ public class UserManagementServiceImplTest { .getUserClaimValue(Mockito.any(), Mockito.any(), Mockito.any()); Mockito.doThrow(new UserStoreException()).when(userStoreManager) .listUsers(Mockito.anyString(), Mockito.anyInt()); + Mockito.when(this.userManagementProviderService.getUsers(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt())) + .thenThrow(new UserManagementException()); Response response = userManagementService.getUsers(TEST_USERNAME, "00", 0, 10, null); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Response returned successful for a users retrieval request."); response = userManagementService.getUserCount(); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Response returned successful for a user count retrieval request."); + Mockito.reset(this.userManagementProviderService); + Mockito.when(this.userManagementProviderService.getUserNames(Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt())) + .thenThrow(new UserManagementException()); response = userManagementService.getUserNames(TEST_USERNAME, null, "00", 0, 10); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Response returned successful for a user count retrieval request."); @@ -385,6 +443,7 @@ public class UserManagementServiceImplTest { response = userManagementService.inviteExistingUsersToEnrollDevice(deviceEnrollmentInvitation); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Invite existing users to enroll device succeeded under erroneous conditions"); + Mockito.reset(this.userManagementProviderService); } /** diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceFilters.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceFilters.java index 04a57fdf9d..ff2cc89525 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceFilters.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceFilters.java @@ -17,6 +17,10 @@ */ package io.entgra.device.mgt.core.device.mgt.common; +import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.ConfigurationEntry; +import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.PlatformConfiguration; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupFilter; + import java.io.Serializable; import java.util.List; @@ -31,6 +35,10 @@ public class DeviceFilters implements Serializable { private List ownerships; private List statuses; + private List configs; + + private List groups; + public List getDeviceTypes() { return deviceTypes; } @@ -54,4 +62,20 @@ public class DeviceFilters implements Serializable { public void setStatuses(List statuses) { this.statuses = statuses; } + + public List getGroups() { + return groups; + } + + public void setGroups(List groups) { + this.groups = groups; + } + + public List getConfigs() { + return configs; + } + + public void setConfigs(List configs) { + this.configs = configs; + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/EnrollmentInvitation.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/EnrollmentInvitation.java index 76e9411da2..9af77625fc 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/EnrollmentInvitation.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/EnrollmentInvitation.java @@ -16,7 +16,7 @@ * under the License. */ -package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; +package io.entgra.device.mgt.core.device.mgt.common; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/GroupFilter.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/GroupFilter.java new file mode 100644 index 0000000000..9c882e3d3c --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/GroupFilter.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.device.mgt.common.group.mgt; + +import io.swagger.annotations.ApiModelProperty; + +public class GroupFilter { + private static final long serialVersionUID = 1998131711L; + + @ApiModelProperty(name = "id", value = "ID of the device group in the device group information database.") + private int id; + + @ApiModelProperty(name = "name", value = "The device group name that can be set on the device group by the user.", + required = true) + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java index b3c4321df3..f3b9812fb3 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java @@ -21,9 +21,9 @@ package io.entgra.device.mgt.core.device.mgt.core.dao; import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.GroupPaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; -import io.entgra.device.mgt.core.device.mgt.common.exceptions.ReportManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupFilter; import java.util.List; import java.util.Map; @@ -221,6 +221,16 @@ public interface GroupDAO { */ List getGroups(GroupPaginationRequest paginationRequest, int tenantId) throws GroupManagementDAOException; + /** + * Get paginated list of Device Groups Names in tenant for filtering. + * + * @param paginationRequest to filter results. + * @param tenantId of user's tenant. + * @return List of all Groups in the provided filter. + * @throws GroupManagementDAOException + */ + List getGroupFilterDetails(GroupPaginationRequest paginationRequest, int tenantId) throws GroupManagementDAOException; + /** * Get paginated list of Device Groups in tenant with specified device group ids. * @@ -255,6 +265,15 @@ public interface GroupDAO { */ List getGroups(List deviceGroupIds, int tenantId) throws GroupManagementDAOException; + /** + * Get the list of Device Groups in tenant for filtering. + * + * @param tenantId of user's tenant. + * @return List of all Device Groups in the provided filter. + * @throws GroupManagementDAOException + */ + List getGroupFilterDetails(List deviceGroupIds, int tenantId) throws GroupManagementDAOException; + /** * Get the list of Device Groups in tenant. * @@ -469,4 +488,4 @@ public interface GroupDAO { List groupNames) throws GroupManagementDAOException; -} \ No newline at end of file +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index b9c655c4ba..5e44b8e982 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupFilter; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -106,6 +107,48 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } } + @Override + public List getGroupFilterDetails(GroupPaginationRequest request, int tenantId) + throws GroupManagementDAOException { + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, GROUP_NAME FROM DM_GROUP " + + "WHERE TENANT_ID = ?"; + if (request != null && StringUtils.isNotBlank(request.getOwner())) { + sql += " AND OWNER LIKE ?"; + } + if (request != null && request.getRowCount() != 0) { + sql += " LIMIT ? OFFSET ?"; + } + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + int paramIndex = 1; + stmt.setInt(paramIndex++, tenantId); + if (request != null && StringUtils.isNotBlank(request.getOwner())) { + stmt.setString(paramIndex++, request.getOwner() + "%"); + } + if (request != null && request.getRowCount() != 0) { + stmt.setInt(paramIndex++, request.getRowCount()); + stmt.setInt(paramIndex, request.getStartIndex()); + } + List groupFilterDetails = new ArrayList<>(); + try (ResultSet resultSet = stmt.executeQuery()) { + while (resultSet.next()) { + GroupFilter group = new GroupFilter(); + group.setId(resultSet.getInt("ID")); + group.setName(resultSet.getString("GROUP_NAME")); + groupFilterDetails.add(group); + } + } + return groupFilterDetails; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving groups in tenant: " + tenantId; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } + @Override public List getGroups(GroupPaginationRequest request, List deviceGroupIds, int tenantId) throws GroupManagementDAOException { @@ -208,6 +251,47 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } } + @Override + public List getGroupFilterDetails(List deviceGroupIds, int tenantId) throws GroupManagementDAOException { + int deviceGroupIdsCount = deviceGroupIds.size(); + if (deviceGroupIdsCount == 0) { + return new ArrayList<>(); + } + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, GROUP_NAME FROM DM_GROUP WHERE TENANT_ID = ?"; + + sql += " AND ID IN ("; + for (int i = 0; i < deviceGroupIdsCount; i++) { + sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; + } + sql += ")"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + int paramIndex = 1; + stmt.setInt(paramIndex++, tenantId); + + for (Integer deviceGroupId : deviceGroupIds) { + stmt.setInt(paramIndex++, deviceGroupId); + } + List groupFilters = new ArrayList<>(); + try (ResultSet resultSet = stmt.executeQuery()) { + while (resultSet.next()) { + GroupFilter groupFilter = new GroupFilter(); + groupFilter.setId(resultSet.getInt("ID")); + groupFilter.setName(resultSet.getString("GROUP_NAME")); + groupFilters.add(groupFilter); + } + } + return groupFilters; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving groups of groups IDs " + deviceGroupIds + + " in tenant: " + tenantId; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } + @Override public List getGroups(GroupPaginationRequest request, List deviceGroupIds, diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java index 2c741730bd..ac74a8514c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java @@ -30,6 +30,7 @@ import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistEx import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupFilter; import org.wso2.carbon.user.api.AuthorizationManager; import org.wso2.carbon.user.api.UserStoreManager; @@ -189,6 +190,17 @@ public interface GroupManagementProviderService { PaginationResult getGroupsWithHierarchy(String username, GroupPaginationRequest request, boolean requireGroupProps) throws GroupManagementException; + + /** + * Get device groups of the provided filter. + * + * @param username of the user. + * @param request to filter results + * @return {@link PaginationResult} paginated groups. + * @throws GroupManagementException on error during retrieval of groups for provided filter + */ + List getGroupFilterValues(String username, GroupPaginationRequest request) throws GroupManagementException; + /** * Get all hierarchical device groups count in tenant * diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 0941d3d3a1..04d98c2632 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -26,6 +26,7 @@ import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistEx import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupFilter; import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceDAO; import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; @@ -615,6 +616,44 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return groupResult; } + + @Override + public List getGroupFilterValues(String username, GroupPaginationRequest request) throws GroupManagementException { + + if (request != null) { + DeviceManagerUtil.validateGroupListPageSize(request); + } + + if (log.isDebugEnabled()) { + log.debug("Get groups filters " + username); + } + + List groupFilters; + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + + if (StringUtils.isBlank(username)) { + GroupManagementDAOFactory.openConnection(); + groupFilters = groupDAO.getGroupFilterDetails(request, tenantId); + } else { + List allDeviceGroupIdsOfUser = getGroupIds(username); + GroupManagementDAOFactory.openConnection(); + groupFilters = groupDAO.getGroupFilterDetails(allDeviceGroupIdsOfUser, tenantId); + } + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source to retrieve all groups of filter."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } catch (GroupManagementDAOException e) { + String msg = "Error occurred while retrieving all groups of filter"; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + return groupFilters; + } + private List getGroups(List groupIds, int tenantId) throws GroupManagementException { try { GroupManagementDAOFactory.openConnection(); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderService.java index 65fbdc3cc5..f9208ae84c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderService.java @@ -18,7 +18,6 @@ package io.entgra.device.mgt.core.device.mgt.core.service; -import com.google.gson.JsonArray; import io.entgra.device.mgt.core.device.mgt.common.*; import io.entgra.device.mgt.core.device.mgt.common.exceptions.UserManagementException; @@ -109,28 +108,4 @@ public interface UserManagementProviderService { */ List getUserNames(String filter, String userStoreDomain, int offset, int limit) throws UserManagementException; - /** - * Method to retrieve the list of users based on filter. - * - * @param username data needed for the user search - * @param deviceList domain of the user - * @param domain the maximum number of the users to be retrieved - * @throws UserManagementException If some unusual behaviour is observed while fetching the users. - */ - List updateUserClaimsForDevices(String username, JsonArray deviceList, String domain) throws UserManagementException; - - /** - * Method to send enrollment invitation mail to existing user. - * - * @param enrollmentInvitation data related to the mail - * @throws UserManagementException If some unusual behaviour is observed while fetching the users. - */ -// EmailMetaInfo inviteToEnrollDevice(EnrollmentInvitation enrollmentInvitation) throws UserManagementException; - - /** - * Method to retrieve the count of users. - * - * @throws UserManagementException If some unusual behaviour is observed while fetching the count of users. - */ - int getCount() throws UserManagementException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceImpl.java index 0fb7bd4944..c6b024b396 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceImpl.java @@ -378,52 +378,6 @@ public class UserManagementProviderServiceImpl implements UserManagementProvider } } - @Override - public List updateUserClaimsForDevices(String username, JsonArray deviceList, String domain) throws UserManagementException { - return null; - } - -// @Override -// public EmailMetaInfo inviteToEnrollDevice(EnrollmentInvitation enrollmentInvitation) throws UserManagementException { -// try { -// Set recipients = new HashSet<>(); -// recipients.addAll(enrollmentInvitation.getRecipients()); -// Properties props = new Properties(); -// String username = DeviceMgtAPIUtils.getAuthenticatedUser(); -// String firstName = getClaimValue(username, DeviceManagementConstants.User.CLAIM_FIRST_NAME); -// String lastName = getClaimValue(username, DeviceManagementConstants.User.CLAIM_LAST_NAME); -// if (firstName == null) { -// firstName = username; -// } -// if (lastName == null) { -// lastName = ""; -// } -// props.setProperty("first-name", firstName); -// props.setProperty("last-name", lastName); -// props.setProperty("device-type", enrollmentInvitation.getDeviceType()); -// EmailMetaInfo metaInfo = new EmailMetaInfo(recipients, props); -// return metaInfo; -// } catch (DeviceManagementException e) { -// String msg = "Error occurred while inviting user to enrol their device"; -// log.error(msg, e); -// throw new UserManagementException(msg, e); -// } catch (UserStoreException e) { -// String msg = "Error occurred while getting claim values to invite user"; -// log.error(msg, e); -// throw new UserManagementException(msg, e); -// } catch (ConfigurationManagementException e) { -// String msg = "Error occurred while sending the email invitations. Mail server not configured."; -// throw new UserManagementException(msg, e); -// } -// } - - - @Override - public int getCount() throws UserManagementException { - return 0; - } - - /** * User search provides an AND search result and if either of the filter returns an empty set of users, there is no * need to carry on the search further. This method decides whether to carry on the search or not. diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceTest.java deleted file mode 100644 index 52af1682f0..0000000000 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/UserManagementProviderServiceTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. - * - * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.device.mgt.core.service; - -import io.entgra.device.mgt.core.device.mgt.core.common.BaseDeviceManagementTest; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -public class UserManagementProviderServiceTest extends BaseDeviceManagementTest { - - @BeforeClass - @Override - public void init() throws Exception { - - } - - @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) - public void updateUser() { - } -} -- 2.36.3