Search in sources :

Example 6 with Principal

use of org.structr.core.entity.Principal in project structr by structr.

the class FtpFilePageWrapper method getGroupName.

@Override
public String getGroupName() {
    String name = "";
    try (Tx tx = StructrApp.getInstance().tx()) {
        Principal owner = getOwner();
        if (owner != null) {
            List<Principal> parents = owner.getParents();
            if (!parents.isEmpty()) {
                name = parents.get(0).getProperty(AbstractNode.name);
            }
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("Error while getting group name of " + this, fex);
    }
    return name;
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Principal(org.structr.core.entity.Principal)

Example 7 with Principal

use of org.structr.core.entity.Principal in project structr by structr.

the class StructrUserManager method getAllUserNames.

@Override
public String[] getAllUserNames() throws FtpException {
    try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
        List<String> userNames = new ArrayList();
        Result<Principal> result = Result.EMPTY_RESULT;
        try {
            result = StructrApp.getInstance(securityContext).nodeQuery(Principal.class).getResult();
        } catch (FrameworkException ex) {
            logger.warn("Error while searching for principal", ex);
        }
        if (!result.isEmpty()) {
            for (Principal p : result.getResults()) {
                userNames.add(p.getProperty(Principal.name));
            }
        }
        tx.success();
        return (String[]) userNames.toArray(new String[userNames.size()]);
    } catch (FrameworkException fex) {
        logger.error("Unable to get user by its name", fex);
    }
    return null;
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ArrayList(java.util.ArrayList) Principal(org.structr.core.entity.Principal)

Example 8 with Principal

use of org.structr.core.entity.Principal in project structr by structr.

the class SSHService method authenticate.

@Override
public boolean authenticate(final String username, final PublicKey key, final ServerSession session) {
    boolean isValid = false;
    if (key == null) {
        return isValid;
    }
    try (final Tx tx = StructrApp.getInstance().tx()) {
        final Principal principal = StructrApp.getInstance().nodeQuery(Principal.class).andName(username).getFirst();
        if (principal != null) {
            securityContext = SecurityContext.getInstance(principal, AccessMode.Backend);
            // check single (main) pubkey
            final String pubKeyData = principal.getProperty(StructrApp.key(Principal.class, "publicKey"));
            if (pubKeyData != null) {
                final PublicKey pubKey = PublicKeyEntry.parsePublicKeyEntry(pubKeyData).resolvePublicKey(PublicKeyEntryResolver.FAILING);
                isValid = KeyUtils.compareKeys(pubKey, key);
            }
            // check array of pubkeys for this user
            final String[] pubKeysData = principal.getProperty(StructrApp.key(Principal.class, "publicKeys"));
            if (pubKeysData != null) {
                for (final String k : pubKeysData) {
                    if (k != null) {
                        final PublicKey pubKey = PublicKeyEntry.parsePublicKeyEntry(k).resolvePublicKey(PublicKeyEntryResolver.FAILING);
                        if (KeyUtils.compareKeys(pubKey, key)) {
                            isValid = true;
                            break;
                        }
                    }
                }
            }
        }
        tx.success();
    } catch (Throwable t) {
        logger.warn("", t);
        isValid = false;
    }
    try {
        if (isValid) {
            session.setAuthenticated();
        }
    } catch (IOException ex) {
        logger.error("Unable to authenticate session", ex);
    }
    return isValid;
}
Also used : Tx(org.structr.core.graph.Tx) PublicKey(java.security.PublicKey) IOException(java.io.IOException) Principal(org.structr.core.entity.Principal)

Example 9 with Principal

use of org.structr.core.entity.Principal in project structr by structr.

the class StructrFileAttributes method group.

@Override
public GroupPrincipal group() {
    if (file == null) {
        return null;
    }
    final List<Group> groups = new LinkedList<>();
    try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
        final Principal owner = file.getOwnerNode();
        if (owner != null) {
            groups.addAll(owner.getGroups());
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("", fex);
    }
    return groups.size() > 0 ? groups.get(0)::getName : null;
}
Also used : Group(org.structr.core.entity.Group) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkedList(java.util.LinkedList) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) UserPrincipal(java.nio.file.attribute.UserPrincipal) Principal(org.structr.core.entity.Principal)

Example 10 with Principal

use of org.structr.core.entity.Principal in project structr by structr.

the class StructrCMISServicesFactory method checkAuthentication.

// ----- private methods -----
private SecurityContext checkAuthentication(final CallContext callContext) {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        final String username = callContext.getUsername();
        final String password = callContext.getPassword();
        final Principal principal = AuthHelper.getPrincipalForPassword(Principal.name, username, password);
        SecurityContext securityContext = null;
        if (principal != null) {
            if (principal instanceof SuperUser) {
                securityContext = SecurityContext.getSuperUserInstance();
            } else {
                securityContext = SecurityContext.getInstance(principal, AccessMode.Backend);
            }
        }
        tx.success();
        if (securityContext != null) {
            return securityContext;
        }
    } catch (AuthenticationException aex) {
        throw new CmisUnauthorizedException(aex.getMessage());
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    throw new CmisUnauthorizedException();
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AuthenticationException(org.structr.core.auth.exception.AuthenticationException) SecurityContext(org.structr.common.SecurityContext) CmisUnauthorizedException(org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException) SuperUser(org.structr.core.entity.SuperUser) Principal(org.structr.core.entity.Principal)

Aggregations

Principal (org.structr.core.entity.Principal)112 FrameworkException (org.structr.common.error.FrameworkException)68 Tx (org.structr.core.graph.Tx)65 Test (org.junit.Test)41 App (org.structr.core.app.App)31 StructrApp (org.structr.core.app.StructrApp)31 TestOne (org.structr.core.entity.TestOne)16 Group (org.structr.core.entity.Group)14 NodeAttribute (org.structr.core.graph.NodeAttribute)13 PropertyMap (org.structr.core.property.PropertyMap)13 SecurityContext (org.structr.common.SecurityContext)10 LinkedList (java.util.LinkedList)9 Result (org.structr.core.Result)8 User (org.structr.web.entity.User)8 AbstractNode (org.structr.core.entity.AbstractNode)7 SuperUser (org.structr.core.entity.SuperUser)7 StructrUiTest (org.structr.web.StructrUiTest)7 Page (org.structr.web.entity.dom.Page)7 IOException (java.io.IOException)6 List (java.util.List)6