Refactor code

pull/102/head
Rajitha Kumara 1 year ago
parent 9c9c391fd4
commit e383f3b456

@ -86,11 +86,9 @@ import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientServic
import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl;
import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.device.mgt.core.util.DeviceMgtTenantMgtListener;
import org.wso2.carbon.email.sender.core.service.EmailSenderService;
import org.wso2.carbon.ndatasource.core.DataSourceService;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.stratos.common.listeners.TenantMgtListener;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
import org.wso2.carbon.utils.ConfigurationContextService;
@ -271,9 +269,6 @@ public class DeviceManagementServiceComponent {
componentContext.getBundleContext().registerService(PrivacyComplianceProvider.class.getName(),
privacyComplianceProvider, null);
// componentContext.getBundleContext()
// .registerService(TenantMgtListener.class.getName(), new DeviceMgtTenantMgtListener(), null);
if (log.isDebugEnabled()) {
log.debug("Device management core bundle has been successfully initialized");
}

@ -1,155 +0,0 @@
/* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.core.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.exceptions.MetadataManagementException;
import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException;
import org.wso2.carbon.device.mgt.common.roles.config.Role;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.stratos.common.beans.TenantInfoBean;
import org.wso2.carbon.stratos.common.listeners.TenantMgtListener;
import org.wso2.carbon.user.api.Permission;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DeviceMgtTenantMgtListener implements TenantMgtListener {
private static final Log log = LogFactory.getLog(DeviceMgtTenantMgtListener.class);
private static final int EXEC_ORDER = 10;
private static final String PERMISSION_ACTION = "ui.execute";
@Override
public void onTenantCreate(TenantInfoBean tenantInfoBean) {
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
if (config.getDefaultRoles().isEnabled()) {
Map<String, List<Permission>> roleMap = getValidRoleMap(config);
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setTenantDomain(tenantInfoBean.getTenantDomain(), true);
UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService()
.getTenantUserRealm(tenantInfoBean.getTenantId()).getUserStoreManager();
roleMap.forEach((key, value) -> {
try {
userStoreManager.addRole(key, null, value.toArray(new Permission[0]));
} catch (UserStoreException e) {
log.error("Error occurred while adding default roles into user store.", e);
}
});
} catch (UserStoreException e) {
log.error("Error occurred while getting user store manager.", e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
try {
DeviceManagementDataHolder.getInstance().getWhiteLabelManagementService().
addDefaultWhiteLabelThemeIfNotExist(tenantInfoBean.getTenantId());
} catch (MetadataManagementException e) {
log.error("Error occurred while adding default white label theme to created tenant.", e);
}
}
@Override
public void onTenantUpdate(TenantInfoBean tenantInfoBean) {
// doing nothing
}
@Override
public void onTenantDelete(int i) {
// doing nothing
}
@Override
public void onTenantRename(int i, String s, String s1) {
// doing nothing
}
@Override
public void onTenantInitialActivation(int i) {
// doing nothing
}
@Override
public void onTenantActivation(int i) {
// doing nothing
}
@Override
public void onTenantDeactivation(int i) {
// doing nothing
}
@Override
public void onSubscriptionPlanChange(int i, String s, String s1) {
// doing nothing
}
@Override
public int getListenerOrder() {
return EXEC_ORDER;
}
@Override
public void onPreDelete(int i) {
// doing nothing
}
/**
* Use the default roles defined in the cdm-config and evaluate the defined permissions. If permissions does not
* exist then exclude them and return role map which contains defined roles in the cdm-config and existing
* permission list as a roleMap
* @param config cdm-config
* @return {@link Map} key is role name and value is list of permissions which needs to be assigned to the role
* defined in the key.
*/
private Map<String, List<Permission>> getValidRoleMap(DeviceManagementConfig config) {
Map<String, List<Permission>> roleMap = new HashMap<>();
try {
for (Role role : config.getDefaultRoles().getRoles()) {
List<Permission> permissionList = new ArrayList<>();
for (String permissionPath : role.getPermissions()) {
if (PermissionUtils.checkResourceExists(permissionPath)) {
Permission permission = new Permission(permissionPath, PERMISSION_ACTION);
permissionList.add(permission);
} else {
log.warn("Permission " + permissionPath + " does not exist. Hence it will not add to role "
+ role.getName());
}
}
roleMap.put(role.getName(), permissionList);
}
} catch (PermissionManagementException | RegistryException e) {
log.error("Error occurred while checking permission existence.", e);
}
return roleMap;
}
}

@ -21,7 +21,19 @@ import io.entgra.tenant.mgt.common.exception.TenantMgtException;
import org.wso2.carbon.stratos.common.beans.TenantInfoBean;
public interface TenantManager {
/**
* Add default roles to a tenant described by the tenant info bean
* @param tenantInfoBean The info bean that provides tenant info
* @throws TenantMgtException Throws when error occurred while adding
* a role into user store or adding default white label theme to created tenant
*/
void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException;
/**
* Add default application categories to a tenant described by the tenant info bean
* @param tenantInfoBean The info bean that provides tenant info
* @throws TenantMgtException Throws when error occurred while adding default application categories
*/
void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException;
}

@ -18,7 +18,6 @@
package io.entgra.tenant.mgt.core.impl;
import io.entgra.application.mgt.common.exception.ApplicationManagementException;
import io.entgra.application.mgt.common.exception.InvalidConfigurationException;
import io.entgra.application.mgt.core.config.ConfigurationManager;
import io.entgra.application.mgt.common.services.ApplicationManager;
import io.entgra.tenant.mgt.core.TenantManager;
@ -90,9 +89,6 @@ public class TenantManagerImpl implements TenantManager {
ApplicationManager applicationManager = TenantMgtDataHolder.getInstance().getApplicationManager();
applicationManager
.addApplicationCategories(ConfigurationManager.getInstance().getConfiguration().getAppCategories());
} catch (InvalidConfigurationException e) {
String msg = "Error occurred while getting application manager";
throw new TenantMgtException(msg, e);
} catch (ApplicationManagementException e) {
String msg = "Error occurred while getting default application categories";
log.error(msg, e);
@ -100,7 +96,6 @@ public class TenantManagerImpl implements TenantManager {
} finally {
endTenantFlow();
}
}
private void initTenantFlow(TenantInfoBean tenantInfoBean) {

@ -18,27 +18,19 @@
package io.entgra.tenant.mgt.core.impl;
import io.entgra.tenant.mgt.common.spi.TenantManagerService;
import io.entgra.tenant.mgt.core.TenantManager;
import io.entgra.tenant.mgt.common.exception.TenantMgtException;
import io.entgra.tenant.mgt.core.internal.TenantMgtDataHolder;
import org.wso2.carbon.stratos.common.beans.TenantInfoBean;
public class TenantManagerServiceImpl implements TenantManagerService {
private final TenantManager tenantManager;
public TenantManagerServiceImpl() {
tenantManager = new TenantManagerImpl();
TenantMgtDataHolder.getInstance().setTenantManager(tenantManager);
}
@Override
public void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException {
tenantManager.addDefaultRoles(tenantInfoBean);
TenantMgtDataHolder.getInstance().getTenantManager().addDefaultRoles(tenantInfoBean);
}
@Override
public void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException {
tenantManager.addDefaultAppCategories(tenantInfoBean);
TenantMgtDataHolder.getInstance().getTenantManager().addDefaultAppCategories(tenantInfoBean);
}
}

@ -19,6 +19,8 @@ package io.entgra.tenant.mgt.core.internal;
import io.entgra.application.mgt.common.services.ApplicationManager;
import io.entgra.tenant.mgt.common.spi.TenantManagerService;
import io.entgra.tenant.mgt.core.TenantManager;
import io.entgra.tenant.mgt.core.impl.TenantManagerImpl;
import io.entgra.tenant.mgt.core.impl.TenantManagerServiceImpl;
import io.entgra.tenant.mgt.core.listener.DeviceMgtTenantListener;
import org.apache.commons.logging.Log;
@ -56,6 +58,8 @@ public class TenantMgtServiceComponent {
TenantManagerService tenantManagerService = new TenantManagerServiceImpl();
componentContext.getBundleContext().
registerService(TenantManagerServiceImpl.class.getName(), tenantManagerService, null);
TenantManager tenantManager = new TenantManagerImpl();
TenantMgtDataHolder.getInstance().setTenantManager(tenantManager);
WhiteLabelManagementService whiteLabelManagementService = new WhiteLabelManagementServiceImpl();
componentContext.getBundleContext().registerService(WhiteLabelManagementServiceImpl.class.getName(),
whiteLabelManagementService, null);

@ -1,3 +1,20 @@
/*
* Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) 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.tenant.mgt.core.listener;
import io.entgra.tenant.mgt.core.TenantManager;
@ -12,7 +29,7 @@ import org.wso2.carbon.stratos.common.listeners.TenantMgtListener;
public class DeviceMgtTenantListener implements TenantMgtListener {
private static final Log log = LogFactory.getLog(DeviceMgtTenantListener.class);
public static final int LISTENER_EXECUTION_ORDER = 11;
public static final int LISTENER_EXECUTION_ORDER = 10;
@Override
public void onTenantCreate(TenantInfoBean tenantInfoBean) {

Loading…
Cancel
Save