הגנה על תוכן הקבצים

ב-Google Drive API יש תמיכה במספר דרכים כדי למנוע שינויים בקבצים, כולל הגבלת תוכן של קבצים וחסימת האפשרות להוריד, להדפיס או להעתיק קבצים.

הגדרת קבצים במצב קריאה בלבד עם הגבלות תוכן ב-Drive

אפשר להוסיף הגבלת תוכן לקובץ ב-Google Drive כדי למנוע מהמשתמשים לבצע את הפעולות הבאות:

  • שינוי הכותרת
  • ביצוע שינויי עריכה בתוכן
  • העלאת גרסה קודמת
  • הוספה או שינוי של הערות

החלת הגבלות תוכן היא מנגנון שמאפשר להגדיר תוכן של פריט ב-Drive לקריאה בלבד, בלי לשנות את הרשאות הגישה של הפריט. כלומר לא מדובר בהגבלת גישה. המשתמשים לא יכולים לשנות את תוכן הקובץ, אבל פעולות אחרות עדיין מותרות בהתאם לרמת הגישה (לדוגמה, משתמש עם גישת עריכה עדיין יכול להעביר פריט או לשנות את הגדרות השיתוף שלו).

כדי להוסיף או להסיר הגבלת תוכן בקובץ ב-Drive, למשתמש צריכות להיות הרשאות משויכות. קובץ או תיקייה ב'אחסון שלי' או באחסון שיתופי עם capabilities.canModifyEditorContentRestriction צריך להקצות את role=writer. עבור קובץ או תיקייה ב'אחסון שלי' או באחסון שיתופי עם הגבלת תוכן ל-ownerRestricted, עליכם להיות הבעלים של הקובץ או להחזיק ב-role=organizer. כדי להציג פריט עם הגבלת תוכן, למשתמשים צריך להיות role=reader ומעלה. רשימה מלאה של התפקידים זמינה במאמר תפקידים והרשאות. במאמר שינוי הרשאות מוסבר איך משנים את ההרשאות של הקובץ.

אפשר להשתמש בשדה הבוליאני contentRestrictions.readOnly במשאב files כדי להגדיר הגבלת תוכן. שימו לב: הגדרה של הגבלת תוכן לפריט מסוים מחליפה את ההגבלה הקיימת.

תרחישים של הגבלות תוכן

הגבלת תוכן בפריט ב-Drive מאותת למשתמשים שלא צריך לשנות את התוכן. יכולות להיות לכך כמה סיבות:

  • השהיית עבודה על מסמך שיתופי במהלך תקופות בדיקה או ביקורת.
  • הגדרת פריט למצב סופי, כגון 'אושר'.
  • מניעת שינויים במהלך פגישה רגישה.
  • איסור על שינויים חיצוניים בתהליכי עבודה שטופלו על ידי מערכות אוטומטיות.
  • הגבלת העריכות של Google Apps Script ותוספים של Google Workspace.
  • יש להימנע מעריכות שבוצעו בטעות במסמך.

שימו לב: הגבלות תוכן יכולות לעזור בניהול התוכן, אבל המטרה היא לא למנוע ממשתמשים שיש להם מספיק הרשאות להמשיך לעבוד על פריט מסוים. בנוסף, זו לא דרך ליצור רשומה שלא ניתנת לשינוי. הגבלות התוכן ב-Drive ניתנות לשינוי, כך שהגבלת תוכן על פריט לא מבטיחה שהפריט לא ישתנה אף פעם.

איך מנהלים קבצים עם הגבלות תוכן

קובצי Google Docs , Google Sheets ו-Google Slides יכולים לכלול הגבלות תוכן, וכן כל קובץ אחר.

הגבלת תוכן על פריט מונעת שינויים בשם ובתוכן שלו, כולל:

  • תגובות והצעות (ב-Docs, ב-Sheets, ב-Slides ובקבצים בינאריים)
  • גרסאות של קובץ בינארי
  • טקסט ועיצוב ב-Docs
  • טקסט או נוסחאות ב-Sheets, פריסת Sheets ומופעים ב-Sheets
  • כל התוכן ב-Slides, וגם הסדר והמספר של השקפים

יש סוגים מסוימים של קבצים שלא יכולים לכלול הגבלת תוכן. הנה כמה דוגמאות:

הוספת הגבלת תוכן

כדי להוסיף הגבלת תוכן לקובץ, משתמשים ב-method files.update כשהשדה contentRestrictions.readOnly מוגדר ל-true. מוסיפים reason (אופציונלי) כדי לציין את הסיבה להוספת ההגבלה, למשל 'חוזה סופי'. דוגמת הקוד הבאה מראה איך להוסיף הגבלת תוכן:

Java

File updatedFile =
  new File()
      .setContentRestrictions(
          ImmutableList.of(new ContentRestriction().setReadOnly(true).setReason("Finalized contract."));

File response = driveService.files().update("FILE_ID", updatedFile).setFields("contentRestrictions").execute();

Python

content_restriction = {'readOnly': True, 'reason':'Finalized contract.'}

response = drive_service.files().update(fileId="FILE_ID", body = {'contentRestrictions' : [content_restriction]}, fields = "contentRestrictions").execute();

Node.js

/**
* Set a content restriction on a file.
* @return{obj} updated file
**/
async function addContentRestriction() {
  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app

  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});
  const service = google.drive({version: 'v3', auth});
  const contentRestriction = {
    'readOnly': True,
    'reason': 'Finalized contract.',
  };
  const updatedFile = {
    'contentRestrictions': [contentRestriction],
  };
  try {
    const response = await service.files.update({
      fileId: 'FILE_ID',
      resource: updatedFile,
      fields: 'contentRestrictions',
    });
    return response;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

מחליפים את FILE_ID ב-fileId של הקובץ שרוצים לשנות.

כשמריצים את הקוד לדוגמה, הקובץ מוגבל לתוכן ומופיע סמל נעילה () לצד שם הקובץ בממשק המשתמש (UI) של Google Drive. עכשיו הקובץ מוגדר לקריאה בלבד.

קובץ עם הגבלת תוכן בתוך רשימת קבצים ב-Drive.
איור 1. קובץ עם הגבלת תוכן בתוך רשימת קבצים ב-Drive.

הסרה של הגבלת תוכן

כדי להסיר הגבלת תוכן של קובץ, משתמשים בשיטה files.update כאשר השדה contentRestrictions.readOnly מוגדר ל-false. דוגמת הקוד הבאה ממחישה איך להסיר הגבלת תוכן:

Java

File updatedFile =
new File()
    .setContentRestrictions(
        ImmutableList.of(new ContentRestriction().setReadOnly(false));

File response = driveService.files().update("FILE_ID", updatedFile).setFields("contentRestrictions").execute();

Python

content_restriction = {'readOnly': False}

response = drive_service.files().update(fileId="FILE_ID", body = {'contentRestrictions' : [content_restriction]}, fields = "contentRestrictions").execute();

Node.js

/**
* Remove a content restriction on a file.
* @return{obj} updated file
**/
async function removeContentRestriction() {
  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app

  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});
  const service = google.drive({version: 'v3', auth});
  const contentRestriction = {
    'readOnly': False,
  };
  const updatedFile = {
    'contentRestrictions': [contentRestriction],
  };
  try {
    const response = await service.files.update({
      fileId: 'FILE_ID',
      resource: updatedFile,
      fields: 'contentRestrictions',
    });
    return response;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

מחליפים את FILE_ID ב-fileId של הקובץ שרוצים לשנות.

כשמריצים את הקוד לדוגמה, הקובץ כבר לא מוגבל לתוכן.

תוכלו גם להשתמש בממשק המשתמש של Drive כדי להסיר הגבלת תוכן ולהתיר לערוך תוכן (בתנאי שיש לכם את ההרשאות המתאימות). אפשר לעשות את זה בשתי דרכים:

  1. ב-Drive, לוחצים לחיצה ימנית על הקובץ שיש בו הגבלת תוכן ואז לוחצים על Unlock .

    הסרה של הגבלת תוכן של קובץ מתוך רשימת קבצים ב-Drive.
    איור 2. צריך להסיר הגבלה על תוכן של קובץ מתוך רשימת הקבצים ב-Drive.
  2. פותחים את הקובץ עם הגבלת תוכן ולוחצים על (מצב נעילה) > ביטול הנעילה של הקובץ.

    הסרת הגבלה על תוכן של קובץ בתוך מסמך
    איור 3. צריך להסיר את הגבלת התוכן של קובץ מסוים מתוך המסמך.

בדיקה של הגבלת תוכן

כדי לבדוק אם יש הגבלת תוכן, משתמשים ב-method files.get עם השדה המוחזר של contentRestrictions. דוגמת הקוד הבאה מראה איך בודקים את הסטטוס של הגבלת תוכן:

Java

File response = driveService.files().get("FILE_ID").setFields("contentRestrictions").execute();

Python

response = drive_service.files().get(fileId="FILE_ID", fields = "contentRestrictions").execute();

Node.js

/**
* Get content restrictions on a file.
* @return{obj} updated file
**/
async function fetchContentRestrictions() {
  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app

  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});
  const service = google.drive({version: 'v3', auth});
  try {
    const response = await service.files.get({
      fileId: 'FILE_ID',
      fields: 'contentRestrictions',
    });
    return response;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

מחליפים את FILE_ID ב-fileId של הקובץ שרוצים לבדוק.

כשמריצים את הקוד לדוגמה, השיטה מחזירה את המשאב ContentRestriction, אם הוא קיים.

הוספת הגבלת תוכן שרק הבעלים של הקובץ יכול לשנות

כדי להוסיף הגבלה על תוכן הקובץ כך שרק בעלי הקבצים יוכלו להחליף את מצב המנגנון, עליכם להשתמש ב-method files.update כשהשדה הבוליאני contentRestrictions.ownerRestricted מוגדר ל-true. דוגמת הקוד הבאה מראה איך להוסיף הגבלת תוכן לבעלי קבצים בלבד:

Java

File updatedFile =
  new File()
      .setContentRestrictions(
          ImmutableList.of(new ContentRestriction().setReadOnly(true).setOwnerRestricted(true).setReason("Finalized contract."));

File response = driveService.files().update("FILE_ID", updatedFile).setFields("contentRestrictions").execute();

Python

content_restriction = {'readOnly': True, 'ownerRestricted': True, 'reason':'Finalized contract.'}

response = drive_service.files().update(fileId="FILE_ID", body = {'contentRestrictions' : [content_restriction]}, fields = "contentRestrictions").execute();

Node.js

/**
* Set an owner restricted content restriction on a file.
* @return{obj} updated file
**/
async function addOwnerRestrictedContentRestriction() {
  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app

  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});
  const service = google.drive({version: 'v3', auth});
  const contentRestriction = {
    'readOnly': True,
    'ownerRestricted': True,
    'reason': 'Finalized contract.',
  };
  const updatedFile = {
    'contentRestrictions': [contentRestriction],
  };
  try {
    const response = await service.files.update({
      fileId: 'FILE_ID',
      resource: updatedFile,
      fields: 'contentRestrictions',
    });
    return response;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

מחליפים את FILE_ID ב-fileId של הקובץ שרוצים לשנות.

כשמריצים את הקוד לדוגמה, הקובץ מוגבל לתוכן ורק בעלי הקובץ יכולים להסיר אותו. אם אתם הבעלים של הקובץ, סמל נעילה פעילה () מופיע לצד שם הקובץ ב ממשק המשתמש של Drive (ממשק המשתמש). אם אתם לא הבעלים, סמל המנעול מעומעם.

כדי להסיר את הדגל ownerRestricted, משתמשים בשיטה files.update כשהשדה contentRestrictions.ownerRestricted מוגדר ל-false.

יכולות הגבלת תוכן

משאב files מכיל אוסף של שדות capabilities בוליאניים שמאפשרים לציין אם אפשר לבצע פעולה בקובץ.

הגבלות התוכן כוללות את capabilities:

  • capabilities.canModifyEditorContentRestriction: האם המשתמש הנוכחי יכול להוסיף או לשנות הגבלת תוכן.
  • capabilities.canModifyOwnerContentRestriction: האם המשתמש הנוכחי יכול להוסיף או לשנות הגבלת תוכן של בעלים.
  • capabilities.canRemoveContentRestriction: האם המשתמש הנוכחי יכול להסיר את הגבלת התוכן שהוחלה (אם קיימת).

למידע נוסף, ראו יכולות.

דוגמה לאחזור קובץ capabilities זמינה במאמר אימות הרשאות משתמש.

איך מונעים ממשתמשים להוריד, להדפיס או להעתיק את הקובץ

אפשר להגביל את האופן שבו משתמשים עם הרשאות role=commenter או role=reader יכולים להוריד, להדפיס ולהעתיק קבצים ב-Drive, ב-Docs, ב-Sheets וב-Slides.

כדי להסיר את האפשרויות של הורדה, הדפסה והעתקה של קבצים, צריך להשתמש ב-method files.update כשהשדה הבוליאני copyRequiresWriterPermission מוגדר ל-true.