Skip to content

Commit

Permalink
Improve tenanat endpoint to support b2b association creation
Browse files Browse the repository at this point in the history
  • Loading branch information
bhagyasakalanka committed Mar 8, 2024
1 parent c95d394 commit c4b9f34
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,30 @@ public Response organizationsGet( @Valid@ApiParam(value = "Condition to filte
return delegate.organizationsGet(filter, limit, after, before, recursive );
}

@Valid
@GET
@Path("/name/{organization-name}")

@Produces({ "application/json" })
@ApiOperation(value = "Get an existing organization, identified by the organization name.", notes = "This API is used to get an existing organization identified by the organization name.", response = GetOrganizationResponse.class, authorizations = {
@Authorization(value = "BasicAuth"),
@Authorization(value = "OAuth2", scopes = {

})
}, tags={ "Organization", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successful response", response = GetOrganizationResponse.class),
@ApiResponse(code = 400, message = "Invalid input in the request.", response = Error.class),
@ApiResponse(code = 401, message = "Authentication information is missing or invalid.", response = Void.class),
@ApiResponse(code = 403, message = "Access forbidden.", response = Void.class),
@ApiResponse(code = 404, message = "Requested resource is not found.", response = Error.class),
@ApiResponse(code = 500, message = "Internal server error.", response = Error.class)
})
public Response organizationsNameOrganizationNameGet(@ApiParam(value = "Name of the organization.",required=true) @PathParam("organization-name") String organizationName, @Valid@ApiParam(value = "Returns the organization details along with permissions assigned for the requested user in this organization.", defaultValue="false") @DefaultValue("false") @QueryParam("includePermissions") Boolean includePermissions) {

return delegate.organizationsNameOrganizationNameGet(organizationName, includePermissions );
}

@Valid
@DELETE
@Path("/{organization-id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public interface OrganizationsApiService {

public Response organizationsGet(String filter, Integer limit, String after, String before, Boolean recursive);

public Response organizationsNameOrganizationNameGet(String organizationName, Boolean includePermissions);

public Response organizationsOrganizationIdDelete(String organizationId);

public Response organizationsOrganizationIdDiscoveryDelete(String organizationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,10 @@ public Response organizationsDiscoveryGet(String filter, Integer offset, Integer

return organizationManagementService.getOrganizationsDiscoveryAttributes(filter, offset, limit);
}

@Override
public Response organizationsNameOrganizationNameGet(String organizationName, Boolean includePermissions) {

return organizationManagementService.getOrganizationByName(organizationName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ public Response getOrganizations(String filter, Integer limit, String after, Str
}
}

/**
* Retrieve organization Id by organization name.
*
* @param organizationName Organization Name
* @return The list of organization IDs.
*/
public Response getOrganizationByName(String organizationName) {

try {

String organizationId = getOrganizationManager().getOrganizationIdByName(organizationName);
return Response.ok().entity(organizationId).build();
} catch (OrganizationManagementClientException e) {
return OrganizationManagementEndpointUtil.handleClientErrorResponse(e, LOG);
} catch (OrganizationManagementException e) {
return OrganizationManagementEndpointUtil.handleServerErrorResponse(e, LOG);
}
}

/**
* Check if organization exist for given name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,40 @@ paths:
$ref: '#/components/responses/ServerError'
tags:
- Organization

/organizations/name/{organization-name}:
get:
description:
This API is used to get an existing organization identified by the organization name.
summary:
Get an existing organization, identified by the organization name.
parameters:
- name: organization-name
in: path
description: Name of the organization.
required: true
schema:
type: string
example: org1
- $ref: '#/components/parameters/includePermissionsQueryParam'
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/components/schemas/GetOrganizationResponse'
'400':
$ref: '#/components/responses/BadRequest'
'401':
$ref: '#/components/responses/Unauthorized'
'403':
$ref: '#/components/responses/Forbidden'
'404':
$ref: '#/components/responses/NotFound'
'500':
$ref: '#/components/responses/ServerError'
tags:
- Organization
/organizations/{organization-id}:
get:
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,9 @@
<groupId>org.wso2.carbon.multitenancy</groupId>
<artifactId>org.wso2.carbon.tenant.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.organization.management</groupId>
<artifactId>org.wso2.carbon.identity.organization.management.organization.user.sharing</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
*/
package org.wso2.carbon.identity.api.server.tenant.management.common;

import org.wso2.carbon.identity.organization.management.organization.user.sharing.OrganizationUserSharingService;
import org.wso2.carbon.tenant.mgt.services.TenantMgtService;
import org.wso2.carbon.user.core.service.RealmService;

/**
* Service holder class for tenant management.
*/
public class TenantManagementServiceHolder {

private static TenantMgtService tenantMgtService;
private static OrganizationUserSharingService organizationUserSharingService;
private static RealmService realmService;

/**
* Get TenantMgtService osgi service.
Expand All @@ -43,4 +47,35 @@ public static void setTenantMgtService(TenantMgtService tenantMgtService) {

TenantManagementServiceHolder.tenantMgtService = tenantMgtService;
}

/**
* Get OrganizationUserSharingService osgi service.
*
* @return OrganizationUserSharingService
*/
public static OrganizationUserSharingService getOrganizationUserSharingService() {

return organizationUserSharingService;
}

/**
* Set OrganizationUserSharingService osgi service.
*
* @param organizationUserSharingService OrganizationUserSharingService.
*/
public static void setOrganizationUserSharingService(
OrganizationUserSharingService organizationUserSharingService) {

TenantManagementServiceHolder.organizationUserSharingService = organizationUserSharingService;
}

public static RealmService getRealmService() {

return realmService;
}

public static void setRealmService(RealmService realmService) {

TenantManagementServiceHolder.realmService = realmService;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.wso2.carbon.identity.api.server.tenant.management.common.factory;

import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.organization.management.organization.user.sharing.OrganizationUserSharingService;

/**
*
*/
public class OrganizationSharingOSGIServiceFactory extends AbstractFactoryBean<OrganizationUserSharingService> {

private OrganizationUserSharingService organizationUserSharingService;

@Override
public Class<?> getObjectType() {

return Object.class;
}

@Override
protected OrganizationUserSharingService createInstance() throws Exception {

OrganizationUserSharingService organizationUserSharingService = null;
if (this.organizationUserSharingService == null) {
organizationUserSharingService = (OrganizationUserSharingService)
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getOSGiService(OrganizationUserSharingService.class,
null);
if (organizationUserSharingService != null) {
this.organizationUserSharingService = organizationUserSharingService;
} else {
throw new Exception("Unable to get the OrganizationUserSharingService");
}
}
return this.organizationUserSharingService;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.identity.api.server.tenant.management.common.factory;

import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.user.core.service.RealmService;

/**
* Factory Beans serves as a factory for creating other beans within the IOC container. This factory bean is used to
* instantiate the RealmService type of object inside the container.
*/
public class RealmServiceFactory extends AbstractFactoryBean<RealmService> {

private RealmService realmService;

@Override
public Class<RealmService> getObjectType() {

return RealmService.class;
}

@Override
protected RealmService createInstance() throws Exception {

if (this.realmService == null) {
RealmService realmService = (RealmService)
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getOSGiService(RealmService.class, null);
if (realmService != null) {
this.realmService = realmService;
} else {
throw new Exception("Unable to retrieve UserStoreConfig Service.");
}
}
return this.realmService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@
<artifactId>org.wso2.carbon.identity.api.server.common</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.organization.management</groupId>
<artifactId>org.wso2.carbon.identity.organization.management.organization.user.sharing</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class Owner {
private String provisioningMethod;
private List<AdditionalClaims> additionalClaims = null;

private String userId;

/**
* Username for the tenant owner.
Expand Down Expand Up @@ -188,7 +189,26 @@ public Owner addAdditionalClaimsItem(AdditionalClaims additionalClaimsItem) {
return this;
}

/**
* id of the tenant owner.
**/
public Owner userId(String userId) {

this.userId = userId;
return this;
}

@ApiModelProperty(example = "4875-jhgr-454hb", value = "id of the tenant owner.")
@JsonProperty("userId")
@Valid
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}



@Override
public boolean equals(java.lang.Object o) {
Expand All @@ -206,12 +226,13 @@ public boolean equals(java.lang.Object o) {
Objects.equals(this.firstname, owner.firstname) &&
Objects.equals(this.lastname, owner.lastname) &&
Objects.equals(this.provisioningMethod, owner.provisioningMethod) &&
Objects.equals(this.additionalClaims, owner.additionalClaims);
Objects.equals(this.additionalClaims, owner.additionalClaims) &&
Objects.equals(this.userId, owner.userId);
}

@Override
public int hashCode() {
return Objects.hash(username, password, email, firstname, lastname, provisioningMethod, additionalClaims);
return Objects.hash(username, password, email, firstname, lastname, provisioningMethod, additionalClaims, userId);
}

@Override
Expand All @@ -227,6 +248,7 @@ public String toString() {
sb.append(" lastname: ").append(toIndentedString(lastname)).append("\n");
sb.append(" provisioningMethod: ").append(toIndentedString(provisioningMethod)).append("\n");
sb.append(" additionalClaims: ").append(toIndentedString(additionalClaims)).append("\n");
sb.append(" userId: ").append(toIndentedString(userId)).append("\n");
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.wso2.carbon.stratos.common.exception.TenantManagementServerException;
import org.wso2.carbon.stratos.common.exception.TenantMgtException;
import org.wso2.carbon.tenant.mgt.services.TenantMgtService;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.core.common.User;
import org.wso2.carbon.user.core.tenant.Tenant;
import org.wso2.carbon.user.core.tenant.TenantSearchResult;
Expand Down Expand Up @@ -92,10 +94,28 @@ public String addTenant(TenantModel tenantModel) {
TenantMgtService tenantMgtService = TenantManagementServiceHolder.getTenantMgtService();
try {
Tenant tenant = createTenantInfoBean(tenantModel);
tenant.setAdminUserId(tenantModel.getOwners().get(0).getUserId());
resourceId = tenantMgtService.addTenant(tenant);
tenant = tenantMgtService.getTenantByDomain(tenantModel.getDomain());
String userId =
((AbstractUserStoreManager) (TenantManagementServiceHolder.getRealmService()).getTenantUserRealm(
tenant.getId()).getUserStoreManager()).getUserIDFromUserName(tenant.getAdminName());
try {
TenantManagementServiceHolder.getOrganizationUserSharingService()
.createOrganizationUserAssociation(userId, tenant.getAssociatedOrganizationUUID(),
tenantModel.getOwners().get(0).getUserId(), "10084a8d-113f-4211-a0d5-efe36b082211");
// TenantManagementServiceHolder.getOrganizationUserSharingService()
// .shareOrganizationUser(tenant.getAssociatedOrganizationUUID(),
// tenantModel.getOwners().get(0).getUserId(), "10084a8d-113f-4211-a0d5-efe36b082211",
// tenant.getAdminName());
} catch (Exception e) {
log.error("Error while sharing organization user.", e);
}
} catch (TenantMgtException e) {
throw handleTenantManagementException(e, TenantManagementConstants.ErrorMessage
.ERROR_CODE_ERROR_ADDING_TENANT, null);
} catch (UserStoreException e) {
throw new APIError(Response.Status.INTERNAL_SERVER_ERROR, new ErrorResponse());
}
return resourceId;
}
Expand Down
Loading

0 comments on commit c4b9f34

Please sign in to comment.