// Retrieve the user who is to be modified.
User user = userService.getUser(userId);
// Create and configure a user filter.
UserFilter filterToAdd = new UserFilter();
// The following field has been filled in to make a filter that allows a
// user to access only the assigned objects.
// This value was determined using GetUserFilterCriteriaTypes.java.
filterToAdd.setUserFilterCriteriaId(2);
// Because this filter used the criteria type "Assigned" it is necessary
// to specify what advertisers this user has access to. This next step
// would be skipped for the criteria types "All" and "None".
// Create an object filter to represent each object the user has access
// to. Since this is an advertiser filter, an object filter represents an
// advertiser. The total of object filter objects will need to match the
// total of advertisers the user is assigned.
ObjectFilter allowedObject = new ObjectFilter();
// Insert the advertiser ID of an advertiser assigned to this user.
allowedObject.setId(advertiserId);
// Create any additional object filters that are needed, then create an
// array of all of the object filters for this filter.
ObjectFilter[] objectFilters = {allowedObject};
// Add these settings to the user filter
filterToAdd.setObjectFilters(objectFilters);
// Add the filter to the user. The following method is specific to
// advertiser filters. See the User class documentation for the names of
// methods for other filters.
user.setAdvertiserUserFilter(filterToAdd);