Search in sources :

Example 1 with PermissionException

use of logisticspipes.security.PermissionException in project LogisticsPipes by RS485.

the class CCCommandWrapper method callMethod.

@Override
public Object[] callMethod(ILuaContext context, int methodId, Object[] arguments) {
    if (methodId == 0) {
        return help(arguments);
    }
    methodId--;
    if (methodId == 0) {
        return helpCommand(arguments);
    }
    methodId--;
    if (methodId == 0) {
        return CCObjectWrapper.createArray(info.type);
    }
    methodId--;
    String name = info.commandMap.get(methodId);
    Method match = null;
    for (Method method : info.commands.values()) {
        if (!method.getName().equalsIgnoreCase(name)) {
            continue;
        }
        if (!argumentsMatch(method, arguments)) {
            continue;
        }
        match = method;
        break;
    }
    if (match == null) {
        StringBuilder error = new StringBuilder();
        error.append("No such method.");
        boolean handled = false;
        for (Method method : info.commands.values()) {
            if (!method.getName().equalsIgnoreCase(name)) {
                continue;
            }
            if (handled) {
                error.append("\n");
            }
            handled = true;
            error.append(method.getName());
            error.append("(");
            boolean a = false;
            for (Class<?> clazz : method.getParameterTypes()) {
                if (a) {
                    error.append(", ");
                }
                error.append(clazz.getName());
                a = true;
            }
            error.append(")");
        }
        if (!handled) {
            error = new StringBuilder();
            error.append("Internal Excption (Code: 1, ");
            error.append(name);
            error.append(")");
        }
        throw new UnsupportedOperationException(error.toString());
    }
    if (match.getAnnotation(CCDirectCall.class) != null) {
        if (!isDirectCall) {
            throw new PermissionException();
        }
    }
    if (match.getAnnotation(CCCommand.class).needPermission()) {
        if (info.securityMethod != null) {
            try {
                info.securityMethod.invoke(object);
            } catch (InvocationTargetException e) {
                if (e.getTargetException() instanceof Exception) {
                    throw new RuntimeException(e.getTargetException());
                }
                throw new RuntimeException(e);
            } catch (IllegalAccessException | IllegalArgumentException e) {
                throw new RuntimeException(e);
            }
        }
    }
    if (match.getAnnotation(CCQueued.class) != null) {
        final Method m = match;
        final Object[] a = arguments;
        final Object[] resultArray = new Object[1];
        final Boolean[] booleans = new Boolean[2];
        booleans[0] = false;
        booleans[1] = false;
        QueuedTasks.queueTask(() -> {
            try {
                Object result = m.invoke(object, a);
                if (result != null) {
                    resultArray[0] = result;
                }
            } catch (InvocationTargetException e) {
                if (e.getTargetException() instanceof PermissionException) {
                    booleans[1] = true;
                    resultArray[0] = e.getTargetException();
                } else {
                    booleans[0] = true;
                    throw e;
                }
            }
            booleans[0] = true;
            return null;
        });
        int count = 0;
        while (!booleans[0] && count < 200) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            count++;
        }
        if (count >= 199) {
            LogisticsPipes.log.warn("CC call " + m.getName() + " on " + object.getClass().getName() + ", (" + object.toString() + ") took too long.");
            throw new RuntimeException("Took too long");
        }
        if (m.getReturnType().equals(Void.class)) {
            return null;
        }
        if (booleans[1]) {
            //PermissionException
            throw ((RuntimeException) resultArray[0]);
        }
        return CCObjectWrapper.createArray(CCObjectWrapper.getWrappedObject(resultArray[0], CCCommandWrapper.WRAPPER));
    }
    Object result;
    try {
        result = match.invoke(object, arguments);
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof Exception) {
            throw new RuntimeException(e.getTargetException());
        }
        throw new RuntimeException(e);
    } catch (IllegalAccessException | IllegalArgumentException e) {
        throw new RuntimeException(e);
    }
    return CCObjectWrapper.createArray(CCObjectWrapper.getWrappedObject(result, CCCommandWrapper.WRAPPER));
}
Also used : PermissionException(logisticspipes.security.PermissionException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PermissionException(logisticspipes.security.PermissionException) CCQueued(logisticspipes.proxy.computers.interfaces.CCQueued) ILuaObject(dan200.computercraft.api.lua.ILuaObject) CCDirectCall(logisticspipes.proxy.computers.interfaces.CCDirectCall) CCCommand(logisticspipes.proxy.computers.interfaces.CCCommand)

Example 2 with PermissionException

use of logisticspipes.security.PermissionException in project LogisticsPipes by RS485.

the class CoreRoutedPipe method getPipeForUUID.

@CCCommand(description = "Returns the access to the pipe of the givven router UUID")
@ModDependentMethod(modId = LPConstants.computerCraftModID)
@CCDirectCall
public Object getPipeForUUID(String sUuid) throws PermissionException {
    if (!getUpgradeManager().hasCCRemoteControlUpgrade()) {
        throw new PermissionException();
    }
    UUID uuid = UUID.fromString(sUuid);
    int id = SimpleServiceLocator.routerManager.getIDforUUID(uuid);
    IRouter router = SimpleServiceLocator.routerManager.getRouter(id);
    if (router == null) {
        return null;
    }
    CoreRoutedPipe pipe = router.getPipe();
    return pipe;
}
Also used : PermissionException(logisticspipes.security.PermissionException) IRouter(logisticspipes.routing.IRouter) UUID(java.util.UUID) ModDependentMethod(logisticspipes.asm.ModDependentMethod) CCCommand(logisticspipes.proxy.computers.interfaces.CCCommand) CCDirectCall(logisticspipes.proxy.computers.interfaces.CCDirectCall)

Example 3 with PermissionException

use of logisticspipes.security.PermissionException in project LogisticsPipes by RS485.

the class BaseWrapperClass method invokeMethod.

public Object[] invokeMethod(String methodName, Context context, Arguments args) throws Exception {
    if (object == null) {
        throw new Exception("This LP object is not persistable");
    }
    int length = args.count();
    Object[] arguments = new Object[length];
    for (int i = 0; i < length; i++) {
        if (args.isString(i)) {
            arguments[i] = args.checkString(i);
        } else {
            Object tmp = args.checkAny(i);
            if (tmp instanceof BaseWrapperClass) {
                tmp = ((BaseWrapperClass) tmp).getObject();
            }
            if (tmp instanceof ICCTypeWrapped) {
                tmp = ((ICCTypeWrapped) tmp).getObject();
            }
            arguments[i] = tmp;
        }
    }
    Method match = null;
    for (Method method : info.commands.values()) {
        if (!method.getName().equalsIgnoreCase(methodName)) {
            continue;
        }
        if (!argumentsMatch(method, arguments)) {
            continue;
        }
        match = method;
        break;
    }
    if (match == null) {
        StringBuilder error = new StringBuilder();
        error.append("No such method.");
        boolean handled = false;
        for (Method method : info.commands.values()) {
            if (!method.getName().equalsIgnoreCase(methodName)) {
                continue;
            }
            if (handled) {
                error.append("\n");
            }
            handled = true;
            error.append(method.getName());
            error.append("(");
            boolean a = false;
            for (Class<?> clazz : method.getParameterTypes()) {
                if (a) {
                    error.append(", ");
                }
                error.append(clazz.getName());
                a = true;
            }
            error.append(")");
        }
        if (!handled) {
            error = new StringBuilder();
            error.append("Internal Excption (Code: 1, ");
            error.append(methodName);
            error.append(")");
        }
        throw new UnsupportedOperationException(error.toString());
    }
    if (match.getAnnotation(CCDirectCall.class) != null) {
        if (!isDirectCall) {
            throw new PermissionException();
        }
    }
    if (match.getAnnotation(CCCommand.class).needPermission()) {
        if (info.securityMethod != null) {
            try {
                info.securityMethod.invoke(object);
            } catch (InvocationTargetException e) {
                if (e.getTargetException() instanceof Exception) {
                    throw (Exception) e.getTargetException();
                }
                throw e;
            }
        }
    }
    Object result;
    try {
        result = match.invoke(object, arguments);
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof Exception) {
            throw (Exception) e.getTargetException();
        }
        throw e;
    }
    return CCObjectWrapper.createArray(CCObjectWrapper.getWrappedObject(result, BaseWrapperClass.WRAPPER));
}
Also used : PermissionException(logisticspipes.security.PermissionException) ICCTypeWrapped(logisticspipes.proxy.computers.interfaces.ICCTypeWrapped) Method(java.lang.reflect.Method) PermissionException(logisticspipes.security.PermissionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CCDirectCall(logisticspipes.proxy.computers.interfaces.CCDirectCall) CCCommand(logisticspipes.proxy.computers.interfaces.CCCommand)

Aggregations

CCCommand (logisticspipes.proxy.computers.interfaces.CCCommand)3 CCDirectCall (logisticspipes.proxy.computers.interfaces.CCDirectCall)3 PermissionException (logisticspipes.security.PermissionException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 ILuaObject (dan200.computercraft.api.lua.ILuaObject)1 UUID (java.util.UUID)1 ModDependentMethod (logisticspipes.asm.ModDependentMethod)1 CCQueued (logisticspipes.proxy.computers.interfaces.CCQueued)1 ICCTypeWrapped (logisticspipes.proxy.computers.interfaces.ICCTypeWrapped)1 IRouter (logisticspipes.routing.IRouter)1