Пример #1
0
  @Test
  public void testConvertToCmisAcl() {
    Ace ace = aceN.toCommonsAce();
    assertEquals(ANDREAS, ace.getPrincipalId());
    assertEquals(1, ace.getPermissions().size());
    assertEquals("", ace.getPermissions().get(0));

    ace = aceR.toCommonsAce();
    assertEquals(BERTA, ace.getPrincipalId());
    assertEquals(1, ace.getPermissions().size());
    assertEquals(EnumBasicPermissions.CMIS_READ.value(), ace.getPermissions().get(0));

    ace = aceW.toCommonsAce();
    assertEquals(CHRISTIAN, ace.getPrincipalId());
    assertEquals(1, ace.getPermissions().size());
    assertEquals(EnumBasicPermissions.CMIS_WRITE.value(), ace.getPermissions().get(0));

    ace = aceA.toCommonsAce();
    assertEquals(DOROTHEE, ace.getPrincipalId());
    assertEquals(1, ace.getPermissions().size());
    assertEquals(EnumBasicPermissions.CMIS_ALL.value(), ace.getPermissions().get(0));

    InMemoryAcl acl = createDefaultAcl();
    Acl commonsAcl = acl.toCommonsAcl();
    assertEquals(4, commonsAcl.getAces().size());
    assertTrue(hasCommonsAce(commonsAcl, ANDREAS, ""));
    assertFalse(hasCommonsAce(commonsAcl, ANDREAS, EnumBasicPermissions.CMIS_READ.value()));
    assertFalse(hasCommonsAce(commonsAcl, ANDREAS, EnumBasicPermissions.CMIS_WRITE.value()));
    assertFalse(hasCommonsAce(commonsAcl, ANDREAS, EnumBasicPermissions.CMIS_ALL.value()));
    assertTrue(hasCommonsAce(commonsAcl, BERTA, EnumBasicPermissions.CMIS_READ.value()));
    assertTrue(hasCommonsAce(commonsAcl, CHRISTIAN, EnumBasicPermissions.CMIS_WRITE.value()));
    assertTrue(hasCommonsAce(commonsAcl, DOROTHEE, EnumBasicPermissions.CMIS_ALL.value()));
  }
Пример #2
0
  public int getAclId(StoredObjectImpl so, Acl addACEs, Acl removeACEs) {
    InMemoryAcl newAcl;
    boolean removeDefaultAcl = false;
    int aclId = 0;

    if (so == null) {
      newAcl = new InMemoryAcl();
    } else {
      aclId = so.getAclId();
      newAcl = getInMemoryAcl(aclId);
      if (null == newAcl) {
        newAcl = new InMemoryAcl();
      } else {
        // copy list so that we can safely change it without effecting
        // the original
        newAcl = new InMemoryAcl(newAcl.getAces());
      }
    }

    if (newAcl.size() == 0 && addACEs == null && removeACEs == null) {
      return 0;
    }

    if (null != removeACEs) {
      for (Ace ace : removeACEs.getAces()) {
        InMemoryAce inMemAce = new InMemoryAce(ace);
        if (inMemAce.equals(InMemoryAce.getDefaultAce())) {
          removeDefaultAcl = true;
        }
      }
    }

    if (so != null && 0 == aclId && !removeDefaultAcl) {
      return 0; // if object grants full access to everyone and it will
      // not be removed we do nothing
    }

    // add ACEs
    if (null != addACEs) {
      for (Ace ace : addACEs.getAces()) {
        InMemoryAce inMemAce = new InMemoryAce(ace);
        if (inMemAce.equals(InMemoryAce.getDefaultAce())) {
          return 0; // if everyone has full access there is no need to
        }
        // add additional ACLs.
        newAcl.addAce(inMemAce);
      }
    }

    // remove ACEs
    if (null != removeACEs) {
      for (Ace ace : removeACEs.getAces()) {
        InMemoryAce inMemAce = new InMemoryAce(ace);
        newAcl.removeAce(inMemAce);
      }
    }

    if (newAcl.size() > 0) {
      return addAcl(newAcl);
    } else {
      return 0;
    }
  }
Пример #3
0
public class InMemoryAce {

  private final String principalId;
  private Permission permission;
  private static InMemoryAce DEFAULT_ACE =
      new InMemoryAce(InMemoryAce.getAnyoneUser(), Permission.ALL);

  public static final String getAnyoneUser() {
    return "anyone";
  }

  public static final String getAnonymousUser() {
    return "anonymous";
  }

  public static final InMemoryAce getDefaultAce() {
    return DEFAULT_ACE;
  }

  public InMemoryAce(Ace commonsAce) {
    if (null == commonsAce
        || null == commonsAce.getPrincipalId()
        || null == commonsAce.getPermissions())
      throw new IllegalArgumentException("Cannot create InMemoryAce with null value");
    List<String> perms = commonsAce.getPermissions();
    if (perms.size() != 1)
      throw new IllegalArgumentException("InMemory only supports ACEs with a single permission.");
    String perm = perms.get(0);
    this.principalId = commonsAce.getPrincipalId();
    this.permission = Permission.fromCmisString(perm);
  }

  public InMemoryAce(String prinicpalId, Permission permission) {
    if (null == prinicpalId || null == permission)
      throw new IllegalArgumentException("Cannot create InMemoryAce with null value");

    this.principalId = prinicpalId;
    this.permission = permission;
  }

  public String getPrincipalId() {
    return principalId;
  }

  public Permission getPermission() {
    return permission;
  }

  public void setPermission(Permission newPermission) {
    permission = newPermission;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((permission == null) ? 0 : permission.hashCode());
    result = prime * result + ((principalId == null) ? 0 : principalId.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    InMemoryAce other = (InMemoryAce) obj;
    if (permission != other.permission) return false;
    if (principalId == null) {
      if (other.principalId != null) return false;
    } else if (!principalId.equals(other.principalId)) return false;
    return true;
  }

  public boolean hasPermission(Permission permission2) {
    return this.permission.compareTo(permission2) >= 0;
  }

  @Override
  public String toString() {
    return "InMemoryAce [principalId=" + principalId + ", permission=" + permission + "]";
  }

  public Ace toCommonsAce() {
    return new AccessControlEntryImpl(
        new AccessControlPrincipalDataImpl(principalId),
        Collections.singletonList(permission.toCmisString()));
  }
}