Enabled adding custom permissions to permission-tree through webapps

revert-70aa11f8
harshanl 9 years ago
parent 1850a76462
commit aeed8b92a1

@ -61,6 +61,7 @@
org.apache.commons.logging, org.apache.commons.logging,
javax.naming, javax.naming,
javax.xml.*, javax.xml.*,
javax.servlet.*,
org.xml.sax, org.xml.sax,
javax.sql.*, javax.sql.*,
org.wso2.carbon.context, org.wso2.carbon.context,
@ -71,13 +72,19 @@
org.wso2.carbon.user.api, org.wso2.carbon.user.api,
org.wso2.carbon.user.core.*, org.wso2.carbon.user.core.*,
org.wso2.carbon.registry.core.service, 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.w3c.dom,
org.wso2.carbon.identity.oauth.stub, org.wso2.carbon.identity.oauth.stub,
org.wso2.carbon.identity.oauth.stub.dto, org.wso2.carbon.identity.oauth.stub.dto,
org.wso2.carbon.ndatasource.core, org.wso2.carbon.ndatasource.core,
org.wso2.carbon.apimgt.impl, org.wso2.carbon.apimgt.impl,
org.wso2.carbon.ndatasource.core, org.wso2.carbon.ndatasource.core,
org.apache.axis2.transport.mail org.apache.axis2.transport.mail,
org.apache.catalina,
org.apache.catalina.core
</Import-Package> </Import-Package>
<Export-Package> <Export-Package>
!org.wso2.carbon.device.mgt.core.internal, !org.wso2.carbon.device.mgt.core.internal,

@ -1,7 +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; package org.wso2.carbon.device.mgt.core.config.permission;
/** import javax.xml.bind.annotation.XmlElement;
* Created by harshan on 8/14/15. import javax.xml.bind.annotation.XmlRootElement;
*/
@XmlRootElement(name = "Permission")
public class 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;
}
} }

@ -1,7 +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; package org.wso2.carbon.device.mgt.core.config.permission;
/** import javax.xml.bind.annotation.XmlElement;
* Created by harshan on 8/14/15. import javax.xml.bind.annotation.XmlRootElement;
*/ import java.util.List;
@XmlRootElement(name = "PermissionConfiguration")
public class PermissionConfiguration { public class PermissionConfiguration {
private List<Permission> permissions;
public List<Permission> getPermissions() {
return permissions;
}
@XmlElement(name = "Permission", required = true)
public void setPermissions(List<Permission> permissions) {
this.permissions = permissions;
}
} }

@ -1,7 +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; 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;
/** /**
* Created by harshan on 8/14/15. * This class will add, update custom permissions defined in permission.xml in webapps.
*/ */
public class PermissionManager { 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<Permission> 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);
}
}
} }

@ -1,7 +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; 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;
/** /**
* Created by harshan on 8/14/15. * Utility class which holds necessary utility methods required for persisting permissions in
* registry.
*/ */
public class PermissionUtils { 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);
}
}
} }

@ -22,14 +22,31 @@ import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent; import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener; import org.apache.catalina.LifecycleListener;
import org.apache.catalina.core.StandardContext; 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") @SuppressWarnings("unused")
public class WebAppDeploymentLifecycleListener implements LifecycleListener { 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 @Override
public void lifecycleEvent(LifecycleEvent lifecycleEvent) { public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) { if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) {
StandardContext context = (StandardContext) lifecycleEvent.getLifecycle(); 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);
}
} }
} }

Loading…
Cancel
Save