try {
CommonUtil.validateEmail(tenantInfoBean.getEmail());
} catch (Exception e) {
String msg = "Invalid email is provided.";
log.error(msg, e);
throw new RestAPIException(msg);
}
String tenantDomain = tenantInfoBean.getTenantDomain();
try {
TenantMgtUtil.validateDomain(tenantDomain);
} catch (Exception e) {
String msg = "Tenant domain validation error for tenant " + tenantDomain;
log.error(msg, e);
throw new RestAPIException(msg);
}
UserRegistry userRegistry = (UserRegistry) PrivilegedCarbonContext.getThreadLocalCarbonContext().
getRegistry(RegistryType.USER_GOVERNANCE);
if (userRegistry == null) {
log.error("Security Alert! User registry is null. A user is trying create a tenant "
+ " without an authenticated session.");
throw new RestAPIException("Invalid data."); // obscure error message.
}
if (userRegistry.getTenantId() != MultitenantConstants.SUPER_TENANT_ID) {
log.error("Security Alert! Non super tenant trying to create a tenant.");
throw new RestAPIException("Invalid data."); // obscure error message.
}
Tenant tenant = TenantMgtUtil.initializeTenant(tenantInfoBean);
TenantPersistor persistor = ServiceHolder.getTenantPersistor();
// not validating the domain ownership, since created by super tenant
int tenantId = 0; //TODO verify whether this is the correct approach (isSkeleton)
try {
tenantId = persistor.persistTenant(tenant, false, tenantInfoBean.getSuccessKey(),
tenantInfoBean.getOriginatedService(),false);
} catch (Exception e) {
String msg = "Error in persisting tenant " + tenantDomain;
log.error(msg, e);
throw new RestAPIException(msg);
}
tenantInfoBean.setTenantId(tenantId);
try {
TenantMgtUtil.addClaimsToUserStoreManager(tenant);
} catch (Exception e) {
String msg = "Error in granting permissions for tenant " + tenantDomain;
log.error(msg, e);
throw new RestAPIException(msg);
}
//Notify tenant addition
try {
TenantMgtUtil.triggerAddTenant(tenantInfoBean);
} catch (StratosException e) {
String msg = "Error in notifying tenant addition.";
log.error(msg, e);
throw new RestAPIException(msg);
}
// For the super tenant tenant creation, tenants are always activated as they are created.
try {
TenantMgtUtil.activateTenantInitially(tenantInfoBean, tenantId);
} catch (Exception e) {
String msg = "Error in initial activation of tenant " + tenantDomain;
log.error(msg, e);
throw new RestAPIException(msg);
}
try {
TenantMgtUtil.prepareStringToShowThemeMgtPage(tenant.getId());
} catch (RegistryException e) {
String msg = "Error in preparing theme mgt page for tenant " + tenantDomain;
log.error(msg, e);
throw new RestAPIException(msg);
}
StratosAdminResponse stratosAdminResponse = new StratosAdminResponse();
stratosAdminResponse.setMessage("Successfully added new tenant with domain " + tenantInfoBean.getTenantDomain());
return stratosAdminResponse;