-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGetUserNonOwnerDriveACLs.py
More file actions
executable file
·58 lines (52 loc) · 2.93 KB
/
GetUserNonOwnerDriveACLs.py
File metadata and controls
executable file
·58 lines (52 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
"""
# Purpose: For a Google Drive User, delete all drive file ACls except those indicating the user as owner
# Note: This script can use basic GAM: https://github.com/jay0lee/GAM or advanced GAM: https://github.com/taers232c/GAMADV-X
# Usage:
# 1: Use print filelist to get selected ACLS
# Suntax, basic GAM: gam <UserTypeEntity> print filelist [anyowner] [query <QueryDriveFile>] [fullquery <QueryDriveFile>]
# Example, basic GAM: gam user testuser@domain.com print filelist id title permissions > filelistperms.csv
# Syntax, advanced GAM: gam <UserTypeEntity> print filelist [anyowner|(showownedby any|me|others)]
# [query <QueryDriveFile>] [fullquery <QueryDriveFile>] [select <DriveFileEntity>|orphans] [depth <Number>] [showparent]
# For a full description of print filelist, see: https://github.com/taers232c/GAMADV-XTD/wiki/Users---Drive---Files
# Example, advanced GAM: gam redirect csv ./filelistperms.csv user testuser@domain.com print filelist id permissions
# 2: From that list of ACLs, output a CSV file with headers "Owner,driveFileId,driveFileTitle,permissionId,emailAddress"
# that lists the driveFileIds and permissionIds for all ACls except those indicating the user as owner
# (n.b., emailAddress and driveFileTitle are not used in the next step, they are included for documentation purposes)
# $ python GetUserNonOwnerDriveACLs.py filelistperms.csv deleteperms.csv
# 3: Inspect deleteperms.csv, verify that it makes sense and then proceed
# 4: Delete the ACLS
# $ gam csv deleteperms.csv gam user "~Owner" delete drivefileacl "~driveFileId" "~permissionId"
"""
import csv
import re
import sys
id_n_address = re.compile(r"permissions.(\d+).id")
if (len(sys.argv) > 2) and (sys.argv[2] != '-'):
outputFile = open(sys.argv[2], 'wb')
else:
outputFile = sys.stdout
outputCSV = csv.DictWriter(outputFile, ['Owner', 'driveFileId', 'driveFileTitle', 'permissionId', 'emailAddress'], lineterminator='\n')
outputCSV.writeheader()
if (len(sys.argv) > 1) and (sys.argv[1] != '-'):
inputFile = open(sys.argv[1], 'rb')
else:
inputFile = sys.stdin
for row in csv.DictReader(inputFile):
for k, v in row.iteritems():
mg = id_n_address.match(k)
if mg:
perm_group = mg.group(1)
if v:
if (row['permissions.{0}.type'.format(perm_group)] != 'user'
or row['permissions.{0}.role'.format(perm_group)] != 'owner'
or row.get('permissions.{0}.emailAddress'.format(perm_group), '') != row['Owner']):
outputCSV.writerow({'Owner': row['Owner'],
'driveFileId': row['id'],
'driveFileTitle': row['title'],
'permissionId': 'id:{0}'.format(row['permissions.{0}.id'.format(perm_group)]),
'emailAddress': row.get('permissions.{0}.emailAddress'.format(perm_group), '')})]
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()