Github user imesh commented on a diff in the pull request:
https://github.com/apache/stratos/pull/287#discussion_r28853408
--- Diff:
components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java
---
@@ -2239,4 +2258,501 @@ public static ClusterBean getClusterInfo(String
clusterId) throws RestAPIExcepti
return ObjectConverter.convertClusterToClusterBean(cluster,
clusterId);
}
+
+ //util methods for Tenants
+
+ /**
+ * Add Tenant
+ *
+ * @param tenantInfoBean
+ * @throws RestAPIException
+ */
+ public static void
addTenant(org.apache.stratos.common.beans.TenantInfoBean tenantInfoBean) throws
RestAPIException {
+
+ 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("Security alert! User registry is
null. A user is trying create a tenant "
+ + " without an authenticated session.");
+ }
+
+ if (userRegistry.getTenantId() !=
MultitenantConstants.SUPER_TENANT_ID) {
+ log.error("Security alert! None super tenant trying to create
a tenant.");
+ throw new RestAPIException("Security alert! None super tenant
trying to create a tenant.");
+ }
+
+ Tenant tenant = TenantMgtUtil
+
.initializeTenant(ObjectConverter.convertTenantInfoBeanToCarbonTenantInfoBean(tenantInfoBean));
+ TenantPersistor persistor = ServiceHolder.getTenantPersistor();
+ // not validating the domain ownership, since created by super
tenant
+ int tenantId; //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 = "Could not add tenant: " + e.getMessage();
+ 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(ObjectConverter.convertTenantInfoBeanToCarbonTenantInfoBean(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(
+
ObjectConverter.convertTenantInfoBeanToCarbonTenantInfoBean(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);
+ }
+ }
+
+ /**
+ * Update Existing Tenant
+ *
+ * @param tenantInfoBean
+ * @throws Exception
+ */
+ public static void
updateExistingTenant(org.apache.stratos.common.beans.TenantInfoBean
tenantInfoBean) throws Exception {
+
+ TenantManager tenantManager = ServiceHolder.getTenantManager();
+ UserStoreManager userStoreManager;
+
+ // filling the non-set admin and admin password first
+ UserRegistry configSystemRegistry =
ServiceHolder.getRegistryService()
+ .getConfigSystemRegistry(tenantInfoBean.getTenantId());
+
+ String tenantDomain = tenantInfoBean.getTenantDomain();
+ int tenantId;
+ try {
+ tenantId = tenantManager.getTenantId(tenantDomain);
+ } catch (UserStoreException e) {
+ String msg = "Error in retrieving the tenant id for the tenant
domain: " + tenantDomain + ".";
+ log.error(msg, e);
+ throw new Exception(msg, e);
+ }
+
+ Tenant tenant;
+ try {
+ tenant = (Tenant) tenantManager.getTenant(tenantId);
+ } catch (UserStoreException e) {
+ String msg = "Error in retrieving the tenant id for the tenant
domain: " +
+ tenantDomain + ".";
+ log.error(msg, e);
+ throw new TenantNotFoundException(msg, e);
+ }
+
+ // filling the first and last name values
+ if (tenantInfoBean.getFirstname() != null &&
!tenantInfoBean.getFirstname().trim().equals("")) {
+ try {
+ CommonUtil.validateName(tenantInfoBean.getFirstname(),
"First Name");
+ } catch (Exception e) {
+ String msg = "Invalid first name is provided.";
+ log.error(msg, e);
+ throw new Exception(msg, e);
+ }
+ }
+ if (tenantInfoBean.getLastname() != null &&
!tenantInfoBean.getLastname().trim().equals("")) {
+ try {
+ CommonUtil.validateName(tenantInfoBean.getLastname(),
"Last Name");
+ } catch (Exception e) {
+ String msg = "Invalid last name is provided.";
+ log.error(msg, e);
+ throw new Exception(msg, e);
--- End diff --
The same concern here, might be its better to throw a specific exception
rather than using Exception.
Thanks
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---