@Override /* package */ void validateSave() { synchronized (mutex) { if (this.getObjectId() == null && getName() == null) { throw new IllegalStateException("New roles must specify a name."); } super.validateSave(); } }
/** * Gets the {@link ParseRelation} for the {@link ParseUser}s that are direct children of this * role. These users are granted any privileges that this role has been granted (e.g. read or * write access through ACLs). You can add or remove users from the role through this relation. * * @return the relation for the users belonging to this role. */ public ParseRelation<ParseUser> getUsers() { return getRelation("users"); }
/** * Sets the name for a role. This value must be set before the role has been saved to the server, * and cannot be set once the role has been saved.<br /> * <br /> * A role's name can only contain alphanumeric characters, _, -, and spaces. * * @param name The name of the role. * @throws IllegalStateException if the object has already been saved to the server. */ public void setName(String name) { this.put("name", name); }
@Test public void testGetRoleWriteAccess() { ParseACL acl = new ParseACL(); ParseRole role = new ParseRole(); role.setName("Player"); role.setObjectId("test"); acl.setRoleWriteAccess(role, true); assertTrue(acl.getRoleWriteAccess(role)); }
@Test(expected = IllegalArgumentException.class) public void testPutFailureWithNameAndObjectIdSet() { ParseRole role = new ParseRole("Test"); role.setObjectId("objectId"); role.put("name", "value"); }
@Test public void testGetUsers() { ParseRole role = new ParseRole("Test"); assertThat(role.getUsers(), instanceOf(ParseRelation.class)); assertSame(role.getUsers(), role.getRelation("users")); }
@Test public void testGetRoles() { ParseRole role = new ParseRole("Test"); assertThat(role.getRoles(), instanceOf(ParseRelation.class)); assertSame(role.getRoles(), role.getRelation("roles")); }
/** * Set whether users belonging to the given role are allowed to read this object. The role must * already be saved on the server and its data must have been fetched in order to use this method. * * @param role The role to assign access. * @param allowed Whether the given role can read this object. */ public void setRoleReadAccess(ParseRole role, boolean allowed) { validateRoleState(role); setRoleReadAccess(role.getName(), allowed); }
@Test public void testValidateSaveSuccessWithNoName() { ParseRole role = new ParseRole("Test"); role.setObjectId("test"); // objectId != null and name == null should not fail role.validateSave(); }
@Test(expected = IllegalStateException.class) public void testValidateSaveFailureWithNoObjectIdAndName() { ParseRole role = new ParseRole(); role.validateSave(); }
/** * Constructs a new ParseRole with the given name. If no default ACL has been specified, you must * provide an ACL for the role. * * @param name The name of the Role to create. */ public ParseRole(String name) { this(); setName(name); }
private static void validateRoleState(ParseRole role) { if (role == null || role.getObjectId() == null) { throw new IllegalArgumentException( "Roles must be saved to the server before they can be used in an ACL."); } }
@Test public void testGetQuery() { ParseQuery<?> query = ParseRole.getQuery(); assertEquals(ParseCorePlugins.getInstance().getSubclassingController().getClassName(ParseRole.class), query.getBuilder().getClassName()); }
@Test public void testGetRoleReadAccess() { ParseACL acl = new ParseACL(); ParseRole role = new ParseRole(); role.setName("Player"); role.setObjectId("test"); acl.setRoleReadAccess(role, true); assertTrue(acl.getRoleReadAccess(role)); }