/** * Sets a default ACL that will be applied to all {@link ParseObject}s when they are created. * * @param acl The ACL to use as a template for all {@link ParseObject}s created after set * has been called. This value will be copied and used as a template for the creation of * new ACLs, so changes to the instance after {@code set(ParseACL, boolean)} * has been called will not be reflected in new {@link ParseObject}s. * @param withAccessForCurrentUser If {@code true}, the {@code ParseACL} that is applied to newly-created * {@link ParseObject}s will provide read and write access to the * {@link ParseUser#getCurrentUser()} at the time of creation. If {@code false}, the * provided ACL will be used without modification. If acl is {@code null}, this value is * ignored. */ public void set(ParseACL acl, boolean withAccessForCurrentUser) { defaultACLWithCurrentUser = null; lastCurrentUser = null; if (acl != null) { ParseACL newDefaultACL = acl.copy(); newDefaultACL.setShared(true); defaultACL = newDefaultACL; defaultACLUsesCurrentUser = withAccessForCurrentUser; } else { defaultACL = null; } }
public ParseACL get() { if (defaultACLUsesCurrentUser && defaultACL != null) { ParseUser currentUser = ParseUser.getCurrentUser(); if (currentUser != null) { // If the currentUser has changed, generate a new ACL from the defaultACL. ParseUser last = lastCurrentUser != null ? lastCurrentUser.get() : null; if (last != currentUser) { ParseACL newDefaultACLWithCurrentUser = defaultACL.copy(); newDefaultACLWithCurrentUser.setShared(true); newDefaultACLWithCurrentUser.setReadAccess(currentUser, true); newDefaultACLWithCurrentUser.setWriteAccess(currentUser, true); defaultACLWithCurrentUser = newDefaultACLWithCurrentUser; lastCurrentUser = new WeakReference<>(currentUser); } return defaultACLWithCurrentUser; } } return defaultACL; }
@Test public void testSetDefaultACLWithACL() { ParseACL acl = mock(ParseACL.class); ParseACL copiedACL = mock(ParseACL.class); when(acl.copy()).thenReturn(copiedACL); ParseDefaultACLController controller = new ParseDefaultACLController(); controller.set(acl, true); assertNull(controller.defaultACLWithCurrentUser); assertNull(controller.lastCurrentUser); assertTrue(controller.defaultACLUsesCurrentUser); verify(copiedACL, times(1)).setShared(true); assertEquals(copiedACL, controller.defaultACL); }
@Test public void testGetDefaultACLWithCurrentUserAndLastCurrentUserNotSame() { ParseDefaultACLController controller = new ParseDefaultACLController(); ParseACL acl = mock(ParseACL.class); ParseACL copiedACL = mock(ParseACL.class); when(acl.copy()).thenReturn(copiedACL); controller.defaultACL = acl; controller.defaultACLUsesCurrentUser = true; controller.defaultACLWithCurrentUser = new ParseACL(); // Register currentUser ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); ParseUser currentUser = mock(ParseUser.class); when(currentUserController.getAsync(anyBoolean())).thenReturn(Task.forResult(currentUser)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); ParseACL defaultACL = controller.get(); verify(copiedACL, times(1)).setShared(true); verify(copiedACL, times(1)).setReadAccess(eq(currentUser), eq(true)); verify(copiedACL, times(1)).setWriteAccess(eq(currentUser), eq(true)); assertSame(currentUser, controller.lastCurrentUser.get()); assertNotSame(acl, defaultACL); assertSame(copiedACL, defaultACL); }
@Test public void testIsShared() { ParseACL acl = new ParseACL(); acl.setShared(true); assertTrue(acl.isShared()); }
@Test public void testGetACLWithSharedACL() { ParseObject object = new ParseObject("Test"); ParseACL acl = new ParseACL(); acl.setShared(true); acl.setPublicReadAccess(true); object.put("ACL", acl); ParseACL aclAgain = object.getACL(); assertTrue(aclAgain.getPublicReadAccess()); }
@Test public void testParcelable() { ParseACL acl = new ParseACL(); acl.setReadAccess("userId", true); ParseUser user = new ParseUser(); user.setObjectId("userId2"); acl.setReadAccess(user, true); acl.setRoleWriteAccess("role", true); acl.setShared(true); Parcel parcel = Parcel.obtain(); acl.writeToParcel(parcel, 0); parcel.setDataPosition(0); acl = ParseACL.CREATOR.createFromParcel(parcel); assertTrue(acl.getReadAccess("userId")); assertTrue(acl.getReadAccess(user)); assertTrue(acl.getRoleWriteAccess("role")); assertTrue(acl.isShared()); assertFalse(acl.getPublicReadAccess()); assertFalse(acl.getPublicWriteAccess()); }