UsernameConsumersPermission implements Permission, Serializable { @Override public boolean canAccess(Object target, SubResource subResource, Access required) { if (target.getClass().equals(Consumer.class)) { return ((Consumer) target).getOwnerId().equals(owner.getId()) && ((Consumer) target).getUsername().equals(user.getUsername()) && Access.ALL.provides(required); } if (target.getClass().equals(Owner.class) && subResource.equals(SubResource.CONSUMERS) && Access.CREATE.provides(required)) { return true; } if (target.getClass().equals(Entitlement.class)) { Entitlement ent = (Entitlement) target; if (ent.getConsumer().getUsername().equals(user.getUsername()) && ent.getOwner().getKey().equals(owner.getKey())) { return true; } } return false; } UsernameConsumersPermission(UserInfo user, Owner owner); @Override boolean canAccess(Object target, SubResource subResource, Access required); @Override Criterion getCriteriaRestrictions(Class entityClass); @Override Owner getOwner(); String getUsername();  }
@Test public void allowsUsernameConsumersModification() { Consumer c = new Consumer("consumer", username, owner, null); assertTrue(perm.canAccess(c, SubResource.NONE, Access.ALL)); assertTrue(perm.canAccess(c, SubResource.NONE, Access.CREATE)); assertTrue(perm.canAccess(c, SubResource.NONE, Access.READ_ONLY)); }
@Test public void allowsRegisterOrgConsumers() { Consumer c = new Consumer("consumer", username, owner, null); assertTrue(perm.canAccess(owner, SubResource.CONSUMERS, Access.CREATE)); }
@Test public void allowsListOrgConsumers() { Consumer c = new Consumer("consumer", username, owner, null); assertTrue(perm.canAccess(owner, SubResource.CONSUMERS, Access.READ_ONLY)); }
@Test public void blocksAccessToOrgPools() { assertFalse(perm.canAccess(owner, SubResource.POOLS, Access.READ_ONLY)); }
@Test public void blocksAccessToOrg() { assertFalse(perm.canAccess(owner, SubResource.NONE, Access.READ_ONLY)); assertFalse(perm.canAccess(owner, SubResource.NONE, Access.ALL)); assertFalse(perm.canAccess(owner, SubResource.NONE, Access.CREATE)); }
@Test public void blocksConsumersInOtherOrgDespiteSameUsername() { Owner other = new Owner("ownerkey2", "My Org 2"); other.setId(TestUtil.randomString()); Consumer c = new Consumer("consumer", username, other, null); assertFalse(perm.canAccess(c, SubResource.NONE, Access.READ_ONLY)); assertFalse(perm.canAccess(c, SubResource.NONE, Access.ALL)); }
@Test public void blocksOtherUsernameConsumers() { Consumer c = new Consumer("consumer", "somebodyelse", owner, null); assertFalse(perm.canAccess(c, SubResource.NONE, Access.READ_ONLY)); assertFalse(perm.canAccess(c, SubResource.NONE, Access.ALL)); }
@Test public void allowsUsernameConsumersUnbind() { Consumer c = new Consumer("consumer", username, owner, null); Entitlement e = new Entitlement(); e.setOwner(owner); e.setConsumer(c); assertTrue(perm.canAccess(e, SubResource.NONE, Access.ALL)); }
@Test public void blocksOtherUsernameConsumersUnbind() { Consumer c = new Consumer("consumer", "somebodyelse", owner, null); Entitlement e = new Entitlement(); e.setOwner(owner); e.setConsumer(c); assertFalse(perm.canAccess(e, SubResource.NONE, Access.ALL)); }