diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index fd8d7301426..4ffc2520d22 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -61,6 +61,7 @@ org.apache.commons.logging, javax.naming, javax.xml.*, + javax.servlet.*, org.xml.sax, javax.sql.*, org.wso2.carbon.context, @@ -71,13 +72,19 @@ org.wso2.carbon.user.api, org.wso2.carbon.user.core.*, org.wso2.carbon.registry.core.service, + org.wso2.carbon.registry.core, + org.wso2.carbon.registry.core.exceptions, + org.wso2.carbon.registry.core.session, + org.wso2.carbon.registry.api, org.w3c.dom, org.wso2.carbon.identity.oauth.stub, org.wso2.carbon.identity.oauth.stub.dto, org.wso2.carbon.ndatasource.core, org.wso2.carbon.apimgt.impl, org.wso2.carbon.ndatasource.core, - org.apache.axis2.transport.mail + org.apache.axis2.transport.mail, + org.apache.catalina, + org.apache.catalina.core !org.wso2.carbon.device.mgt.core.internal, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/Permission.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/Permission.java new file mode 100644 index 00000000000..76810771b86 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/Permission.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2015, 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.core.config.permission; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "Permission") +public class Permission{ + + private String name; + private String path; + + public String getName() { + return name; + } + + @XmlElement(name = "name", required = true) + public void setName(String name) { + this.name = name; + } + + public String getPath() { + return path; + } + + @XmlElement(name = "path", required = true) + public void setPath(String path) { + this.path = path; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionConfiguration.java new file mode 100644 index 00000000000..c2c9d08e3b4 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionConfiguration.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2015, 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.core.config.permission; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.List; + +@XmlRootElement(name = "PermissionConfiguration") +public class PermissionConfiguration { + + private List permissions; + + public List getPermissions() { + return permissions; + } + + @XmlElement(name = "Permission", required = true) + public void setPermissions(List permissions) { + this.permissions = permissions; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionManager.java new file mode 100644 index 00000000000..2681ebc8e03 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionManager.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2015, 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.core.config.permission; + +import org.wso2.carbon.device.mgt.common.DeviceManagementException; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import java.io.InputStream; +import java.util.List; + +/** + * This class will add, update custom permissions defined in permission.xml in webapps. + */ +public class PermissionManager { + + private static PermissionManager permissionManager; + + public static PermissionManager getInstance() { + if (permissionManager == null) { + synchronized (PermissionManager.class) { + if (permissionManager == null) { + permissionManager = new PermissionManager(); + } + } + } + return permissionManager; + } + + public boolean addPermission(Permission permission) throws DeviceManagementException { + try { + return PermissionUtils.putPermission(permission); + } catch (DeviceManagementException e) { + throw new DeviceManagementException("Error occurred while adding the permission : " + + permission.getName(), e); + } + } + + public boolean addPermissions(List permissions) throws DeviceManagementException{ + for(Permission permission:permissions){ + this.addPermission(permission); + } + return true; + } + + public void initializePermissions(InputStream permissionStream) throws DeviceManagementException { + try { + if(permissionStream != null){ + /* Un-marshaling Device Management configuration */ + JAXBContext cdmContext = JAXBContext.newInstance(PermissionConfiguration.class); + Unmarshaller unmarshaller = cdmContext.createUnmarshaller(); + PermissionConfiguration permissionConfiguration = (PermissionConfiguration) unmarshaller.unmarshal(permissionStream); + this.addPermissions(permissionConfiguration.getPermissions()); + } + } catch (JAXBException e) { + throw new DeviceManagementException("Error occurred while initializing Data Source config", e); + } + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionUtils.java new file mode 100644 index 00000000000..7e1f45a833c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionUtils.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2015, 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.core.config.permission; + +import org.w3c.dom.Document; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.registry.api.RegistryException; +import org.wso2.carbon.registry.api.Resource; +import org.wso2.carbon.registry.core.Registry; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; + +/** + * Utility class which holds necessary utility methods required for persisting permissions in + * registry. + */ +public class PermissionUtils { + + public static String ADMIN_PERMISSION_REGISTRY_PATH = "/permission/admin"; + public static String PERMISSION_PROPERTY_NAME = "name"; + + public static Registry getGovernanceRegistry() throws DeviceManagementException { + try { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return DeviceManagementDataHolder.getInstance().getRegistryService() + .getGovernanceSystemRegistry( + tenantId); + } catch (RegistryException e) { + throw new DeviceManagementException( + "Error in retrieving governance registry instance: " + + e.getMessage(), e); + } + } + + public static Permission getPermission(String path) throws DeviceManagementException { + try { + Resource resource = PermissionUtils.getGovernanceRegistry().get(path); + Permission permission = new Permission(); + permission.setName(resource.getProperty(PERMISSION_PROPERTY_NAME)); + permission.setPath(resource.getPath()); + return permission; + } catch (RegistryException e) { + throw new DeviceManagementException("Error in retrieving registry resource : " + + e.getMessage(), e); + } + } + + public static boolean putPermission(Permission permission) + throws DeviceManagementException { + boolean status; + try { + Resource resource = PermissionUtils.getGovernanceRegistry().newCollection(); + resource.addProperty(PERMISSION_PROPERTY_NAME, permission.getName()); + PermissionUtils.getGovernanceRegistry().beginTransaction(); + PermissionUtils.getGovernanceRegistry().put(ADMIN_PERMISSION_REGISTRY_PATH + + permission.getPath(), resource); + PermissionUtils.getGovernanceRegistry().commitTransaction(); + status = true; + } catch (RegistryException e) { + throw new DeviceManagementException( + "Error occurred while persisting permission : " + + permission.getName(), e); + } + return status; + } + + public static boolean checkPermissionExistance(Permission permission) + throws DeviceManagementException, + org.wso2.carbon.registry.core.exceptions.RegistryException { + return PermissionUtils.getGovernanceRegistry().resourceExists(permission.getPath()); + } + + public static Document convertToDocument(File file) throws DeviceManagementException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + try { + DocumentBuilder docBuilder = factory.newDocumentBuilder(); + return docBuilder.parse(file); + } catch (Exception e) { + throw new DeviceManagementException("Error occurred while parsing file, while converting " + + "to a org.w3c.dom.Document", e); + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/lifecycle/WebAppDeploymentLifecycleListener.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/lifecycle/WebAppDeploymentLifecycleListener.java new file mode 100644 index 00000000000..eee570a8f00 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/lifecycle/WebAppDeploymentLifecycleListener.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2015, 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.core.config.permission.lifecycle; + +import org.apache.catalina.Lifecycle; +import org.apache.catalina.LifecycleEvent; +import org.apache.catalina.LifecycleListener; +import org.apache.catalina.core.StandardContext; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.config.permission.PermissionManager; + +import javax.servlet.ServletContext; +import java.io.File; + +@SuppressWarnings("unused") +public class WebAppDeploymentLifecycleListener implements LifecycleListener { + + private static final String PERMISSION_CONFIG_PATH = "META-INF" + File.separator + "permissions.xml"; + private static final Log log = LogFactory.getLog(WebAppDeploymentLifecycleListener.class); + + @Override + public void lifecycleEvent(LifecycleEvent lifecycleEvent) { + if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) { + StandardContext context = (StandardContext) lifecycleEvent.getLifecycle(); + ServletContext servletContext = context.getServletContext(); + try { + PermissionManager.getInstance().initializePermissions(servletContext.getResourceAsStream(PERMISSION_CONFIG_PATH)); + } catch (DeviceManagementException e) { + log.error("Exception occurred while adding the permissions from webapp : " + + servletContext.getContextPath(),e); + } + } + } + +} diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 44a178ef51f..4fcb779dcc4 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -1,20 +1,15 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( ID INT auto_increment NOT NULL, - NAME VARCHAR(300) NULL DEFAULT NULL, + NAME VARCHAR(300) DEFAULT NULL, PRIMARY KEY (ID) ); CREATE TABLE IF NOT EXISTS DM_DEVICE ( ID INTEGER auto_increment NOT NULL, - DESCRIPTION TEXT NULL DEFAULT NULL, - NAME VARCHAR(100) NULL DEFAULT NULL, - DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL, - DATE_OF_LAST_UPDATE BIGINT NULL DEFAULT NULL, - OWNERSHIP VARCHAR(45) NULL DEFAULT NULL, - STATUS VARCHAR(15) NULL DEFAULT NULL, - DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL, - DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL, - OWNER VARCHAR(45) NULL DEFAULT NULL, + DESCRIPTION TEXT DEFAULT NULL, + NAME VARCHAR(100) DEFAULT NULL, + DEVICE_TYPE_ID INT(11) DEFAULT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) @@ -68,10 +63,10 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, OWNER VARCHAR(50) NOT NULL, - OWNERSHIP VARCHAR(45) NULL DEFAULT NULL, + OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, - DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL, - DATE_OF_LAST_UPDATE TIMESTAMP NULL DEFAULT NULL, + DATE_OF_ENROLMENT TIMESTAMP DEFAULT NULL, + DATE_OF_LAST_UPDATE TIMESTAMP DEFAULT NULL, TENANT_ID INT NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES @@ -128,7 +123,7 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE ( CREATE TABLE IF NOT EXISTS DM_POLICY ( ID INT(11) NOT NULL AUTO_INCREMENT , - NAME VARCHAR(45) NULL DEFAULT NULL , + NAME VARCHAR(45) DEFAULT NULL , TENANT_ID INT(11) NOT NULL , PROFILE_ID INT(11) NOT NULL , OWNERSHIP_TYPE VARCHAR(45) NULL, @@ -335,10 +330,10 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, OWNER VARCHAR(50) NOT NULL, - OWNERSHIP VARCHAR(45) NULL DEFAULT NULL, + OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, - DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL, - DATE_OF_LAST_UPDATE TIMESTAMP NULL DEFAULT NULL, + DATE_OF_ENROLMENT TIMESTAMP DEFAULT NULL, + DATE_OF_LAST_UPDATE TIMESTAMP DEFAULT NULL, TENANT_ID INT NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES @@ -349,12 +344,12 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( ID INTEGER AUTO_INCREMENT NOT NULL, NAME VARCHAR(50) NOT NULL, APP_IDENTIFIER VARCHAR(50) NOT NULL, - PLATFORM VARCHAR(50) NULL DEFAULT NULL, + PLATFORM VARCHAR(50) DEFAULT NULL, CATEGORY VARCHAR(50) NULL, VERSION VARCHAR(50) NULL, TYPE VARCHAR(50) NULL, - LOCATION_URL VARCHAR(100) NULL DEFAULT NULL, - IMAGE_URL VARCHAR(100) NULL DEFAULT NULL, + LOCATION_URL VARCHAR(100) DEFAULT NULL, + IMAGE_URL VARCHAR(100) DEFAULT NULL, APP_PROPERTIES BLOB NULL, TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql index bc04de8732b..eb887f68c66 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -3,7 +3,7 @@ -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `DM_DEVICE_TYPE` ( `ID` INT(11) NOT NULL , - `NAME` VARCHAR(300) NULL DEFAULT NULL , + `NAME` VARCHAR(300) DEFAULT NULL , PRIMARY KEY (`ID`) ) ENGINE = InnoDB DEFAULT CHARACTER SET = latin1; @@ -14,16 +14,11 @@ DEFAULT CHARACTER SET = latin1; -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `DM_DEVICE` ( `ID` VARCHAR(20) NOT NULL , - `DESCRIPTION` TEXT NULL DEFAULT NULL , - `NAME` VARCHAR(100) NULL DEFAULT NULL , - `DATE_OF_ENROLLMENT` DATETIME NULL DEFAULT NULL , - `DATE_OF_LAST_UPDATE` DATETIME NULL DEFAULT NULL , - `OWNERSHIP` VARCHAR(45) NULL DEFAULT NULL , - `STATUS` VARCHAR(15) NULL DEFAULT NULL , - `DEVICE_TYPE_ID` INT(11) NULL DEFAULT NULL , - `DEVICE_IDENTIFICATION` VARCHAR(300) NULL DEFAULT NULL , - `OWNER` VARCHAR(45) NULL DEFAULT NULL , - TENANT_ID INTEGER DEFAULT 0, + `DESCRIPTION` TEXT DEFAULT NULL , + `NAME` VARCHAR(100) DEFAULT NULL , + `DEVICE_TYPE_ID` INT(11) DEFAULT NULL , + `DEVICE_IDENTIFICATION` VARCHAR(300) DEFAULT NULL , + `TENANT_ID` INTEGER DEFAULT 0, PRIMARY KEY (`ID`) , INDEX `fk_DM_DEVICE_DM_DEVICE_TYPE2_idx` (`DEVICE_TYPE_ID` ASC) , CONSTRAINT `fk_DM_DEVICE_DM_DEVICE_TYPE2`