From b62b4aebc3483111bf2aee61ba5e11815f76cf89 Mon Sep 17 00:00:00 2001 From: navodzoysa Date: Fri, 22 Apr 2022 21:06:38 +0530 Subject: [PATCH] Add new API to fetch userstores --- .../device/mgt/jaxrs/beans/UserStoreList.java | 53 +++++++++++++++++++ .../service/api/UserManagementService.java | 51 ++++++++++++++++++ .../impl/UserManagementServiceImpl.java | 36 +++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserStoreList.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserStoreList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserStoreList.java new file mode 100644 index 0000000000..b33feb8b97 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserStoreList.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.jaxrs.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.List; + +@ApiModel(value = "User Store List") +public class UserStoreList extends BasePaginatedResult { + + private List userStores; + + @ApiModelProperty(value = "Returns the list of user stores available for a tenant.") + @JsonProperty("userStores") + public List getList() { + return userStores; + } + + public void setList(List userStores) { + this.userStores = userStores; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + sb.append(" count: ").append(getCount()).append(",\n"); + sb.append(" userStores: [").append(userStores).append("\n"); + sb.append("]}\n"); + return sb.toString(); + } + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java index f27d740099..99fbf8eee6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java @@ -60,6 +60,7 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.PermissionList; import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList; import org.wso2.carbon.device.mgt.jaxrs.beans.UserInfo; +import org.wso2.carbon.device.mgt.jaxrs.beans.UserStoreList; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import javax.validation.Valid; @@ -1234,4 +1235,54 @@ public interface UserManagementService { response = ErrorResponse.class) }) Response getPermissionsOfUser(); + + @GET + @Path("/user-stores") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Getting list of userstores", + notes = "You are able to manage users in WSO2 IoTS by adding, updating and removing users. If you wish to" + + " get the list of users registered with WSO2 IoTS, you can do so " + + "using this REST API", + tags = "User Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:users:user-details") + }) + } + ) + @ApiResponses(value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the list of user stores registered with WSO2 IoTS.", + response = UserStoreList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client already has the latest version of " + + "the requested resource.\n"), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the list of WSO2 IoTS " + + "user stores.", + response = ErrorResponse.class) + }) + Response getUserStores(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java index 6f240093ec..f39c4cd1f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java @@ -64,6 +64,7 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.PermissionList; import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList; import org.wso2.carbon.device.mgt.jaxrs.beans.UserInfo; +import org.wso2.carbon.device.mgt.jaxrs.beans.UserStoreList; import org.wso2.carbon.device.mgt.jaxrs.exception.BadRequestException; import org.wso2.carbon.device.mgt.jaxrs.service.api.UserManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; @@ -1270,4 +1271,39 @@ public class UserManagementServiceImpl implements UserManagementService { return permissionsList; } + /** + * Returns a Response with the list of user stores available for a tenant + * @return list of user stores + * @throws UserStoreException If unable to search for user stores + */ + @GET + @Path("/user-stores") + @Override + public Response getUserStores() { + String domain; + List userStores = new ArrayList<>(); + UserStoreList userStoreList = new UserStoreList(); + try { + RealmConfiguration realmConfiguration = DeviceMgtAPIUtils.getUserRealm().getRealmConfiguration(); + userStores.add(realmConfiguration + .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME)); + + while (realmConfiguration != null) { + realmConfiguration = realmConfiguration.getSecondaryRealmConfig(); + if (realmConfiguration != null) { + domain = realmConfiguration + .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME); + userStores.add(domain); + } else { + break; + } + } + } catch (UserStoreException e) { + String msg = "Error occurred while retrieving user stores."; + log.error(msg, e); + } + userStoreList.setList(userStores); + userStoreList.setCount(userStores.size()); + return Response.status(Response.Status.OK).entity(userStoreList).build(); + } }