Thêm và xoá cộng tác viên có quyền trong ghi chú

Người tạo ghi chú là chủ sở hữu. Chủ sở hữu có thể thêm cộng tác viên bằng cách cấp quyền cho từng người dùng hoặc nhóm. Cộng tác viên có quyền chỉnh sửa ghi chú. Bạn cũng có thể thu hồi quyền của cộng tác viên bằng API. Trang này hướng dẫn cách thêm và xoá cộng tác viên bằng quyền.

Thêm quyền để thêm cộng tác viên

Ví dụ sau đây cho thấy cách sử dụng quyền đối với người dùng hoặc nhóm để thêm cộng tác viên vào một ghi chú:

REST

Gọi notes.permissions.batchCreate() với ghi chú gốc được chỉ định quyền và permission. Xin lưu ý rằng bạn chỉ có thể chỉ định vai trò WRITER cho thành viên (người dùng, nhóm).

Java

/**
 * Grants write access to a user and to a Google group for the given note.
 *
 * @param note The note whose permissions will be updated.
 * @param userEmail Email address of the user that will be added to the
 *     permissions of the note.
 * @param groupEmail Email address of the Google Groups that will be
 *     added to the permissions of the note.
 * @throws IOException
 * @return The response of the create permissions request.
 */
private BatchCreatePermissionsResponse addPermissions(
    Note note, String userEmail, String groupEmail) throws IOException {
  String noteName = note.getName();
  CreatePermissionRequest userPermission =
      new CreatePermissionRequest()
          .setParent(noteName)
          .setPermission(new Permission().setEmail(userEmail).setRole("WRITER"));

  CreatePermissionRequest groupPermission =
      new CreatePermissionRequest()
          .setParent(noteName)
          .setPermission(new Permission().setEmail(groupEmail).setRole("WRITER"));

  BatchCreatePermissionsRequest batchCreatePermissionsRequest =
      new BatchCreatePermissionsRequest()
          .setRequests(Arrays.asList(userPermission, groupPermission));

  return keepService
      .notes()
      .permissions()
      .batchCreate(noteName, batchCreatePermissionsRequest)
      .execute();
}

Thu hồi quyền để xoá cộng tác viên

Mẫu sau đây cho biết cách sử dụng quyền để xoá tất cả cộng tác viên khỏi một ghi chú:

REST

Gọi notes.permissions.batchDelete() với ghi chú gốc được chỉ định quyền và permission.

Java

/**
 * Deletes all permissions of a given note excluding the owner. The owner
 * can't be removed from a note's permissions.
 *
 * @param note The note whose permissions will be deleted.
 * @throws IOException
 */
private void deletePermissions(Note note) throws IOException {
  List<Permission> notePermissions =
      keepService.notes().get(note.getName()).execute().getPermissions();

  // List of users, groups or families that will be deleted from the
  // permissions of the note.
  List<String> permissionsToDelete = new ArrayList<>();

  for (Permission permission : notePermissions) {
    // The note owner can't be removed from the permissions. Trying to
    // remove the owner causes an exception.
    if (!permission.getRole().equals("OWNER")) {
      permissionsToDelete.add(permission.getName());
    }
  }

  keepService
      .notes()
      .permissions()
      .batchDelete(
          note.getName(),
          new BatchDeletePermissionsRequest().setNames(permissionsToDelete))
      .execute();
}