How to create new Principal Object using Jackrabbit/JCR

106 views Asked by At

I'm a new developer trying to tackle the Jackrabbit/JCR library. My team and I have been using the EveryonePrincipal for a couple of months now, but we've been wanting to implement more capabilities per user roles/principals so we can grant them necessary read/write access to each node. However, we're having some difficulties figuring out how to create a new Principal object.

I've been using:

PrincipalImpl newPrincipal = new PrincipalImpl("MyPrincipal");

Then creating a new RolePrincipal class matching the EveryonePrincipal, except the name would be "MyPrincipal" inside the RolePrincipal class. This method doesn't work unfortunately. Is there anything else we're missing from this? And how does the 'everyone' principal gets stored?

EveryonePrincipal.java

public final class EveryonePrincipal implements JackrabbitPrincipal, java.security.acl.Group {

public static final String NAME = "everyone";

private static final EveryonePrincipal INSTANCE = new EveryonePrincipal();

private EveryonePrincipal() { }

public static EveryonePrincipal getInstance() {
    return INSTANCE;
}

//----------------------------------------------------------< Principal >---
@Override
public String getName() {
    return NAME;
}

//--------------------------------------------------------------< Group >---
@Override
public boolean addMember(Principal user) {
    return false;
}

@Override
public boolean removeMember(Principal user) {
    throw new UnsupportedOperationException("Cannot remove a member from the everyone group.");
}

@Override
public boolean isMember(Principal member) {
    return !member.equals(this);
}

@Override
public Enumeration<? extends Principal> members() {
    throw new UnsupportedOperationException("Not implemented.");
}

//-------------------------------------------------------------< Object >---

@Override
public int hashCode() {
    return NAME.hashCode();
}

@Override
public boolean equals(Object obj) {
    if (obj == this) {
        return true;
    } else if (obj instanceof JackrabbitPrincipal && obj instanceof Group) {
        JackrabbitPrincipal other = (JackrabbitPrincipal) obj;
        return NAME.equals(other.getName());
    }
    return false;
}

@Override
public String toString() {
    return NAME + " principal";
} 
}
0

There are 0 answers