Search in sources :

Example 61 with Principal

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

the class StructrFileAttributes method owner.

@Override
public UserPrincipal owner() {
    if (file == null) {
        return null;
    }
    UserPrincipal owner = null;
    try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
        final Principal fileOwner = file.getOwnerNode();
        if (fileOwner == null) {
            owner = securityContext.getUser(false)::getName;
        } else {
            owner = fileOwner::getName;
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("", fex);
    }
    return owner;
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) UserPrincipal(java.nio.file.attribute.UserPrincipal) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) UserPrincipal(java.nio.file.attribute.UserPrincipal) Principal(org.structr.core.entity.Principal)

Example 62 with Principal

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

the class DOMNode method getSecurityInstructions.

static void getSecurityInstructions(final DOMNode thisNode, final Set<String> instructions) {
    final Principal _owner = thisNode.getOwnerNode();
    if (_owner != null) {
        instructions.add("@structr:owner(" + _owner.getProperty(AbstractNode.name) + ")");
    }
    for (final Security security : thisNode.getSecurityRelationships()) {
        if (security != null) {
            final Principal grantee = security.getSourceNode();
            final Set<String> perms = security.getPermissions();
            final StringBuilder shortPerms = new StringBuilder();
            // first character only
            for (final String perm : perms) {
                if (perm.length() > 0) {
                    shortPerms.append(perm.substring(0, 1));
                }
            }
            if (shortPerms.length() > 0) {
                // ignore SECURITY-relationships without permissions
                instructions.add("@structr:grant(" + grantee.getProperty(AbstractNode.name) + "," + shortPerms.toString() + ")");
            }
        }
    }
}
Also used : Security(org.structr.core.entity.Security) Principal(org.structr.core.entity.Principal)

Example 63 with Principal

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

the class AbstractPrimitiveProperty method updateAccessInformation.

// ----- private methods -----
private void updateAccessInformation(final SecurityContext securityContext, final PropertyContainer propertyContainer) throws FrameworkException {
    try {
        if (securityContext.modifyAccessTime()) {
            final Principal user = securityContext.getUser(false);
            String modifiedById = null;
            if (user != null) {
                if (user instanceof SuperUser) {
                    // "virtual" UUID of superuser
                    modifiedById = Principal.SUPERUSER_ID;
                } else {
                    modifiedById = user.getUuid();
                }
                propertyContainer.setProperty(AbstractNode.lastModifiedBy.dbName(), modifiedById);
            }
            propertyContainer.setProperty(AbstractNode.lastModifiedDate.dbName(), System.currentTimeMillis());
        }
    } catch (Throwable t) {
        // fail without throwing an exception here
        logger.warn("", t);
    }
}
Also used : SuperUser(org.structr.core.entity.SuperUser) Principal(org.structr.core.entity.Principal)

Example 64 with Principal

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

the class MeResource method doGet.

@Override
public Result doGet(PropertyKey sortKey, boolean sortDescending, int pageSize, int page) throws FrameworkException {
    Principal user = securityContext.getUser(true);
    if (user != null) {
        List<GraphObject> resultList = new LinkedList<>();
        resultList.add(user);
        return new Result(resultList, null, isCollectionResource(), isPrimitiveArray());
    } else {
        throw new NotAllowedException("No user");
    }
}
Also used : NotAllowedException(org.structr.rest.exception.NotAllowedException) GraphObject(org.structr.core.GraphObject) Principal(org.structr.core.entity.Principal) LinkedList(java.util.LinkedList) RestMethodResult(org.structr.rest.RestMethodResult) Result(org.structr.core.Result)

Example 65 with Principal

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

the class MeResource method checkAndConfigure.

@Override
public boolean checkAndConfigure(String part, SecurityContext securityContext, HttpServletRequest request) throws FrameworkException {
    this.securityContext = securityContext;
    if ("me".equalsIgnoreCase(part)) {
        this.typeResource = new TypeResource();
        this.typeResource.setSecurityContext(securityContext);
        this.typeResource.checkAndConfigure("user", securityContext, request);
        Principal user = securityContext.getUser(true);
        if (user != null) {
            // we need to create synthetic nested constraints
            this.idResource = new UuidResource();
            this.idResource.setSecurityContext(securityContext);
            this.idResource.checkAndConfigure(user.getProperty(GraphObject.id), securityContext, request);
        }
    }
    return true;
}
Also used : 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