新增及移除具有權限的記事協作者

附註的建立者是該記事的擁有者。擁有者可以透過授予個別使用者或群組的權限,新增其他協作者。協作者可取得記事的編輯權限。協作者也可以使用 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 group 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. Owner of the note cannot 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) {
    // Note owner cannot be removed from the permissions of the note. Trying to remove the note
    // owner from the permissions will cause an exception.
    if (!permission.getRole().equals("OWNER")) {
      permissionsToDelete.add(permission.getName());
    }
  }

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