Search in sources :

Example 1 with Permission

use of org.structr.common.Permission in project structr by structr.

the class Services method doInitialize.

private void doInitialize() {
    configurationClass = Settings.Configuration.getValue();
    configuredServiceNames = Settings.Services.getValue();
    // create set of configured services
    configuredServiceClasses.addAll(Arrays.asList(configuredServiceNames.split("[ ,]+")));
    if (!isTesting()) {
        // read license
        licenseManager = new StructrLicenseManager(Settings.getBasePath() + "license.key");
    }
    // if configuration is not yet established, instantiate it
    // this is the place where the service classes get the
    // opportunity to modify the default configuration
    getConfigurationProvider();
    // do simple heap size check
    final Runtime runtime = Runtime.getRuntime();
    final long max = runtime.maxMemory() / 1024 / 1024 / 1024;
    final int processors = runtime.availableProcessors();
    logger.info("{} processors available, {} GB max heap memory", processors, max);
    if (max < 8) {
        logger.warn("Maximum heap size is smaller than recommended, this can lead to problems with large databases!");
        logger.warn("Please configure AT LEAST 8 GBs of heap memory using -Xmx8g.");
    }
    logger.info("Starting services..");
    // initialize other services
    for (final String serviceClassName : configuredServiceClasses) {
        Class serviceClass = getServiceClassForName(serviceClassName);
        if (serviceClass != null) {
            startService(serviceClass);
        }
    }
    logger.info("{} service(s) processed", serviceCache.size());
    logger.info("Registering shutdown hook.");
    // register shutdown hook
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            shutdown();
        }
    });
    // read permissions for ownerless nodes
    final String configForOwnerlessNodes = Settings.OwnerlessNodes.getValue();
    if (StringUtils.isNotBlank(configForOwnerlessNodes)) {
        for (final String permission : configForOwnerlessNodes.split("[, ]+")) {
            final String trimmed = permission.trim();
            if (StringUtils.isNotBlank(trimmed)) {
                final Permission val = Permissions.valueOf(trimmed);
                if (val != null) {
                    permissionsForOwnerlessNodes.add(val);
                } else {
                    logger.warn("Invalid permisson {}, ignoring.", trimmed);
                }
            }
        }
    } else {
        // default
        permissionsForOwnerlessNodes.add(Permission.read);
    }
    // a configuration file, i.e. when this is NOT this first start.
    try {
        final ExecutorService service = Executors.newSingleThreadExecutor();
        service.submit(new Runnable() {

            @Override
            public void run() {
                // wait a second
                try {
                    Thread.sleep(100);
                } catch (Throwable ignore) {
                }
                // call initialization callbacks from a different thread
                for (final InitializationCallback callback : singletonInstance.callbacks) {
                    callback.initializationDone();
                }
            }
        }).get();
    } catch (Throwable t) {
        logger.warn("Exception while executing post-initialization tasks", t);
    }
    // Don't use logger here because start/stop scripts rely on this line.
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.ms").format(new Date()) + "  ---------------- Initialization complete ----------------");
    setOverridingSchemaTypesAllowed(false);
    initializationDone = true;
}
Also used : InitializationCallback(org.structr.api.service.InitializationCallback) Date(java.util.Date) StructrLicenseManager(org.structr.util.StructrLicenseManager) Permission(org.structr.common.Permission) ExecutorService(java.util.concurrent.ExecutorService) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with Permission

use of org.structr.common.Permission in project structr by structr.

the class CMISAclService method applyAce.

// ----- private methods -----
private void applyAce(final AccessControllable node, final Ace toAdd, final boolean revoke) throws FrameworkException {
    final String principalId = toAdd.getPrincipalId();
    final List<String> permissions = toAdd.getPermissions();
    final Principal principal = CMISObjectWrapper.translateUsernameToPrincipal(principalId);
    if (principal != null) {
        for (final String permissionString : permissions) {
            final Permission permission = Permissions.valueOf(permissionString);
            if (permission != null) {
                if (revoke) {
                    node.revoke(permission, principal);
                } else {
                    node.grant(permission, principal);
                }
            } else {
                throw new CmisInvalidArgumentException("Permission with ID " + permissionString + " does not exist");
            }
        }
    } else {
        throw new CmisObjectNotFoundException("Principal with ID " + principalId + " does not exist");
    }
}
Also used : CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) Permission(org.structr.common.Permission) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) Principal(org.structr.core.entity.Principal)

Example 3 with Permission

use of org.structr.common.Permission in project structr by structr.

the class IsAllowedFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 3)) {
            return false;
        }
        if (sources[0] instanceof Principal) {
            final Principal principal = (Principal) sources[0];
            if (sources[1] instanceof AbstractNode) {
                final AbstractNode node = (AbstractNode) sources[1];
                if (sources[2] instanceof String) {
                    final String[] parts = ((String) sources[2]).split("[,]+");
                    boolean allowed = true;
                    for (final String part : parts) {
                        final String trimmedPart = part.trim();
                        if (trimmedPart.length() > 0) {
                            final Permission permission = Permissions.valueOf(trimmedPart);
                            if (permission != null) {
                                allowed &= node.isGranted(permission, SecurityContext.getInstance(principal, AccessMode.Backend));
                            } else {
                                logger.warn("Error: unknown permission \"{}\". Parameters: {}", new Object[] { trimmedPart, getParametersAsString(sources) });
                                return "Error: unknown permission " + trimmedPart;
                            }
                        }
                    }
                    return allowed;
                } else {
                    logger.warn("Error: third argument is not a string. Parameters: {}", getParametersAsString(sources));
                    return "Error: third argument is not a string.";
                }
            } else {
                logger.warn("Error: second argument is not a node. Parameters: {}", getParametersAsString(sources));
                return "Error: second argument is not a node.";
            }
        } else {
            logger.warn("Error: first argument is not of type Principal. Parameters: {}", getParametersAsString(sources));
            return "Error: first argument is not of type Principal.";
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) Permission(org.structr.common.Permission) Principal(org.structr.core.entity.Principal)

Aggregations

Permission (org.structr.common.Permission)3 Principal (org.structr.core.entity.Principal)2 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 ExecutorService (java.util.concurrent.ExecutorService)1 CmisInvalidArgumentException (org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)1 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)1 InitializationCallback (org.structr.api.service.InitializationCallback)1 AbstractNode (org.structr.core.entity.AbstractNode)1 StructrLicenseManager (org.structr.util.StructrLicenseManager)1