権限を持つメモの共同編集者を追加、削除する

メモの作成者がオーナーになります。オーナーは、個々のユーザーまたはグループに付与された権限を使用して、共同編集者を追加できます。共同編集者はメモの編集権限を取得します。API を使用して共同編集者の権限を取り消すこともできます。このページでは、権限を使用して共同編集者を追加および削除する方法について説明します。

共同編集者を追加する権限を追加する

次の例は、ユーザーまたはグループの権限を使用してメモに共同編集者を追加する方法を示しています。

REST

権限と permission が割り当てられた親メモを使用して notes.permissions.batchCreate() を呼び出します。メンバー(ユーザー、グループ)に割り当てることができるのは WRITER ロールのみです。

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();
}

権限を取り消して共同編集者を削除する

次のサンプルは、権限を使用してメモからすべての共同編集者を削除する方法を示しています。

REST

権限と permission が割り当てられた親メモを使用して、notes.permissions.batchDelete() を呼び出します。

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();
}