Ubah akses pengguna

Setelah dibuat, pengguna harus memiliki setidaknya satu peran pengguna yang ditetapkan. Namun, karena pengiklan baru dibuat atau organisasi memperluas ke beberapa mitra, pengguna akses perlu diperbarui untuk menyertakan sumber daya baru yang relevan.

Peran pengguna harus diperbarui melalui Metode users.bulkEditAssignedUserRoles, yang dapat menghapus dan menambahkan peran pengguna dengan satu tindakan. Hal ini memungkinkan Anda untuk mengubah tingkat akses pengguna ke resource dengan satu permintaan.

Berikut contoh cara mengedit peran pengguna yang ditetapkan dari pengguna yang sudah ada:

Java

// Retrieve the existing user.
User user = service.users().get(user-id).execute();

// Create the bulk edit request structure.
BulkEditAssignedUserRolesRequest bulkEditRequest =
    new BulkEditAssignedUserRolesRequest();

// Build list of user roles to add.
ArrayList<AssignedUserRole> addedUserRoles =
    new ArrayList<AssignedUserRole>();

// Add the user role for the new advertiser and the new user role
// for the existing advertiser to assign to the user.
addedUserRoles.add(new AssignedUserRole()
    .setAdvertiserId(new-advertiser-id)
    .setUserRole("STANDARD"));
addedUserRoles.add(new AssignedUserRole()
    .setAdvertiserId(existing-advertiser-id)
    .setUserRole("READ_ONLY"));

// Add list of user roles to add to the request.
bulkEditRequest.setCreatedAssignedUserRoles(addedUserRoles);

// Build list of user role IDs to delete.
ArrayList<String> deletedUserRoles = new ArrayList<String>();

// Create assigned user role ID to check for.
String existingAssignedUserRoleId = String.format(
    "advertiser-%d",
    existing-advertiser-id
);

// Check the existing user roles. If the user has an existing user role
// for the existing advertiser, add it to the list of roles to be deleted.
// Users cannot have multiple roles for a single resource.
for (AssignedUserRole userRole: user.getAssignedUserRoles()) {
  if (userRole.getAssignedUserRoleId() == existingAssignedUserRoleId) {
    deletedUserRoles.add(existingAssignedUserRoleId);
  }
}

// Add list of user roles to delete to the request.
bulkEditRequest.setDeletedAssignedUserRoles(deletedUserRoles);

// Build and execute the bulk edit request.
BulkEditAssignedUserRolesResponse response = service.users()
    .bulkEditAssignedUserRoles(
        user-id,
        bulkEditRequest
    ).execute();

// Check if response is empty.
// If not, iterate over created AssignedUserRoles.
if (response.isEmpty()) {
  System.out.print("Bulk edit request created no new AssignedUserRoles.");
} else {
  for (AssignedUserRole assignedRole : response.getCreatedAssignedUserRoles()) {
    System.out.printf("AssignedUserRole %s was created.\n",
        assignedRole.getAssignedUserRoleId());
  }
}

Python

# Retrieve the existing user.
user = service.users().get(userId=user-id).execute()

# Add the user role for the new advertiser and the new user role
# for the existing advertiser to assign to the user.
added_user_roles = [
    {
        'advertiser_id': new-advertiser-id,
        'user_role': 'STANDARD'
    },
    {
        'advertiser_id': existing-advertiser-id,
        'user_role': 'READ_ONLY'
    }
]

# Create empty deleted user role list to add to if necessary.
deleted_user_roles = []

# Check the existing user roles. If the user has an existing user role
# for the existing advertiser, add it to the list of roles to be deleted.
# Users cannot have multiple roles for a single resource.
for role in user['assignedUserRoles']:
  if role['assignedUserRoleId'] == ("advertiser-%s" % existing-advertiser-id):
    deleted_user_roles.append(role['assignedUserRoleId'])

# Create the bulk edit request.
bulk_edit_user_roles_request = {
    'deletedAssignedUserRoles': deleted_user_roles,
    'createdAssignedUserRoles': added_user_roles
}

# Edit the assigned user roles.
response = service.users().bulkEditAssignedUserRoles(
    userId=user-id,
    body=bulk_edit_user_roles_request
).execute()

# Check if response is empty.
# If not, iterate over and display new assigned targeting options.
if not response:
  print("Bulk edit request created no new AssignedUserRoles.")
else:
  for assigned_user_role in response['createdAssignedUserRoles']:
    print("AssignedUserRole %s was created.\n"
          % assigned_user_role['assignedUserRoleId'])

PHP

// Call the API, retrieving the existing user.
$user = $this->service->users->get(user-id);

// Create an array for user roles to add.
$addedUserRoles = array();

// Create new user role for new advertiser.
$newAdvertiserUserRole = new Google_Service_DisplayVideo_AssignedUserRole();
$newAdvertiserUserRole->setAdvertiserId(new-advertiser-id);
$newAdvertiserUserRole->setUserRole('STANDARD');
$addedUserRoles[] = $newAdvertiserUserRole;

// Create new user role for existing advertiser.
$existingAdvertiserUserRole =
    new Google_Service_DisplayVideo_AssignedUserRole();
$existingAdvertiserUserRole->setAdvertiserId(
    existing-advertiser-id
);
$existingAdvertiserUserRole->setUserRole('READ_ONLY');
$addedUserRoles[] = $existingAdvertiserUserRole;

// Create an array for user roles to delete.
$deletedUserRoles = array();

// Add user role to delete list if it is being replaced by a new user role.
foreach ($user->getAssignedUserRoles() as $role) {
    if ($role->getAdvertiserId() == existing-advertiser-id) {
        $deletedUserRoles[] = $role->getAssignedUserRoleId();
    }
}

// Create the bulk edit request.
$bulkEditUserRolesRequest =
    new Google_Service_DisplayVideo_BulkEditAssignedUserRolesRequest();
$bulkEditUserRolesRequest->setCreatedAssignedUserRoles($addedUserRoles);
$bulkEditUserRolesRequest->setDeletedAssignedUserRoles($deletedUserRoles);

// Call the API, editing the assigned user roles for the given user.
$response = $this->service->users->bulkEditAssignedUserRoles(
    user-id,
    $bulkEditUserRolesRequest
);

// Print created assigned user roles.
if (!empty($response->getCreatedAssignedUserRoles())) {
    foreach ($response->getCreatedAssignedUserRoles() as $role) {
        printf(
            'AssignedUserRole %s was created.\n',
            $role['assignedUserRoleId']
        );
    }
} else {
    print('Bulk edit request created no new AssignedUserRoles.');
}