เพิ่มและลบผู้ทำงานร่วมกันในโน้ตที่มีสิทธิ์

ผู้สร้างโน้ตจะเป็นเจ้าของ เจ้าของสามารถเพิ่มผู้ทำงานร่วมกัน โดยใช้สิทธิ์ที่มอบให้แก่ผู้ใช้หรือกลุ่มแต่ละราย ผู้ทำงานร่วมกันจะได้รับ สิทธิ์แก้ไขโน้ต นอกจากนี้ คุณยังเพิกถอนสิทธิ์ของผู้ทำงานร่วมกันโดยใช้ API ได้ด้วย หน้านี้แสดงวิธีเพิ่มและลบผู้ทำงานร่วมกันโดยใช้สิทธิ์

เพิ่มสิทธิ์เพื่อเพิ่มผู้ทำงานร่วมกัน

ตัวอย่างต่อไปนี้แสดงวิธีใช้สิทธิ์ในผู้ใช้หรือกลุ่มเพื่อเพิ่ม ผู้ทำงานร่วมกันในโน้ต

REST

เรียกใช้ notes.permissions.batchCreate() โดยกำหนดสิทธิ์ให้กับโน้ตหลักและ permission โปรดทราบว่าคุณจะมอบหมายได้เฉพาะ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

เรียกใช้ notes.permissions.batchDelete() โดยกำหนดสิทธิ์ให้กับโน้ตหลักและ 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();
}