forked from community/device-mgt-core
parent
4c50d4842d
commit
ffc7542d4d
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, 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.apimgt.annotations.api;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is the representation of custom developed Permission annotation.
|
||||||
|
*/
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface Permission {
|
||||||
|
|
||||||
|
String scope();
|
||||||
|
String[] permissions();
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, 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.apimgt.webapp.publisher.config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents the information related to permissions.
|
||||||
|
*/
|
||||||
|
public class PermissionConfiguration {
|
||||||
|
|
||||||
|
private String scopeName;
|
||||||
|
private String[] permissions;
|
||||||
|
|
||||||
|
public String getScopeName() {
|
||||||
|
return scopeName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScopeName(String scope) {
|
||||||
|
this.scopeName = scope;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getPermissions() {
|
||||||
|
return permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPermissions(String[] permissions) {
|
||||||
|
this.permissions = permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.webapp.publisher.config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom exception class of Permission related operations.
|
||||||
|
*/
|
||||||
|
public class PermissionManagementException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -3151279311929070298L;
|
||||||
|
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PermissionManagementException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PermissionManagementException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PermissionManagementException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PermissionManagementException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public PermissionManagementException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.webapp.publisher.lifecycle.util;
|
||||||
|
|
||||||
|
import org.wso2.carbon.apimgt.webapp.publisher.config.PermissionManagementException;
|
||||||
|
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
||||||
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||||
|
import org.wso2.carbon.registry.api.RegistryException;
|
||||||
|
import org.wso2.carbon.registry.api.Resource;
|
||||||
|
import org.wso2.carbon.registry.core.Registry;
|
||||||
|
|
||||||
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility class which holds necessary utility methods required for persisting permissions in
|
||||||
|
* registry.
|
||||||
|
*/
|
||||||
|
public class PermissionUtils {
|
||||||
|
|
||||||
|
public static final String ADMIN_PERMISSION_REGISTRY_PATH = "/permission/admin";
|
||||||
|
public static final String PERMISSION_PROPERTY_NAME = "name";
|
||||||
|
|
||||||
|
public static Registry getGovernanceRegistry() throws PermissionManagementException {
|
||||||
|
try {
|
||||||
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
return APIPublisherDataHolder.getInstance().getRegistryService()
|
||||||
|
.getGovernanceSystemRegistry(
|
||||||
|
tenantId);
|
||||||
|
} catch (RegistryException e) {
|
||||||
|
throw new PermissionManagementException(
|
||||||
|
"Error in retrieving governance registry instance: " +
|
||||||
|
e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addPermission(String permission) throws PermissionManagementException {
|
||||||
|
String resourcePermission = getAbsolutePermissionPath(permission);
|
||||||
|
try {
|
||||||
|
StringTokenizer tokenizer = new StringTokenizer(resourcePermission, "/");
|
||||||
|
String lastToken = "", currentToken, tempPath;
|
||||||
|
while (tokenizer.hasMoreTokens()) {
|
||||||
|
currentToken = tokenizer.nextToken();
|
||||||
|
tempPath = lastToken + "/" + currentToken;
|
||||||
|
if (!checkResourceExists(tempPath)) {
|
||||||
|
createRegistryCollection(tempPath, currentToken);
|
||||||
|
}
|
||||||
|
lastToken = tempPath;
|
||||||
|
}
|
||||||
|
} catch (RegistryException e) {
|
||||||
|
throw new PermissionManagementException("Error occurred while persisting permission : " +
|
||||||
|
resourcePermission, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void createRegistryCollection(String path, String resourceName)
|
||||||
|
throws PermissionManagementException,
|
||||||
|
RegistryException {
|
||||||
|
Resource resource = PermissionUtils.getGovernanceRegistry().newCollection();
|
||||||
|
resource.addProperty(PERMISSION_PROPERTY_NAME, resourceName);
|
||||||
|
PermissionUtils.getGovernanceRegistry().beginTransaction();
|
||||||
|
PermissionUtils.getGovernanceRegistry().put(path, resource);
|
||||||
|
PermissionUtils.getGovernanceRegistry().commitTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean checkResourceExists(String path)
|
||||||
|
throws PermissionManagementException,
|
||||||
|
org.wso2.carbon.registry.core.exceptions.RegistryException {
|
||||||
|
return PermissionUtils.getGovernanceRegistry().resourceExists(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getAbsolutePermissionPath(String permissionPath) {
|
||||||
|
return PermissionUtils.ADMIN_PERMISSION_REGISTRY_PATH + permissionPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue