forked from taers232c/GAM-Scripts3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetSharedFilePermissionsTypeRoleLists.py
executable file
·212 lines (198 loc) · 8.82 KB
/
GetSharedFilePermissionsTypeRoleLists.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
"""
# Purpose: For a Google Drive User(s), show all shared file permissions organized into lists by type and role
# Note: This script can use Basic or Advanced GAM:
# https://github.com/GAM-team/GAM
# https://github.com/taers232c/GAMADV-XTD3
# Customize: Set SHOW_USERS, SHOW_GROUPS, SHOW_DOMAINS, SHOW_ANYONES, SHOW_COUNTS, LIST_DELIMITER
# Python: Use python or python3 below as appropriate to your system; verify that you have version 3
# $ python -V or python3 -V
# Python 3.x.y
# Usage:
# 1: Get ACLs for all files, if you don't want all users, replace all users with your user selection in the command below
# $ Basic: gam all users print filelist id title permissions owners mimetype > filelistperms.csv
# $ Basic: gam user [email protected] print filelist id title permissions owners mimetype > filelistperms.csv
# $ Advanced: gam config auto_batch_min 1 redirect csv ./filelistperms.csv multiprocess all users print filelist fields id,name,mimetype,basicpermissions,owners.emailaddress pm not role owner em pmfilter
# $ Advanced: gam redirect csv ./filelistperms.csv user [email protected] print filelist fields id,name,mimetype,basicpermissions,owners.emailaddress pm not role owner em pmfilter
# 2: From that list of ACLs, output a CSV file that lists the shared file permissions organized into lists by type and role
# $ python3 GetSharedFilePermissionsTypeRoleLists.py filelistperms.csv filetyperoleperms.csv
"""
import copy
import csv
import re
import sys
SHOW_USERS = True # True: show user ACLs; False: do not show user ACLs
SHOW_GROUPS = True # True: show group ACLs; False: do not show group ACLs
SHOW_DOMAINS = True # True: show domain ACLs; False: do not show domain ACLs
SHOW_ANYONES = True # True: show anyone ACLs; False: do not show anyone ACLs
SHOW_COUNTS = False # True: show ACL counts columns; False: do not show ACL count columns
LIST_DELIMITER = ' '
FILE_NAME = 'name'
ALT_FILE_NAME = 'title'
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
PERMISSIONS_N_TYPE = re.compile(r"permissions.(\d+).type")
USER_GROUP_ROLES = ['commenter', 'reader', 'writer', 'fileOrganizer', 'organizer']
DOMAIN_ANYONE_ROLES = ['commenter', 'reader', 'writer']
ZERO_COUNTS = {
'user': {
'commenter': {'count': 0, 'addresses': []},
'reader': {'count': 0, 'addresses': []},
'writer': {'count': 0, 'addresses': []},
'fileOrganizer': {'count': 0, 'addresses': []},
'organizer': {'count': 0, 'addresses': []}
},
'group': {
'commenter': {'count': 0, 'addresses': []},
'reader': {'count': 0, 'addresses': []},
'writer': {'count': 0, 'addresses': []},
'fileOrganizer': {'count': 0, 'addresses': []},
'organizer': {'count': 0, 'addresses': []}
},
'domain': {
'commenter': {'count': 0, 'addresses': []},
'reader': {'count': 0, 'addresses': []},
'writer': {'count': 0, 'addresses': []}
},
'domainWithlink': {
'commenter': {'count': 0, 'addresses': []},
'reader': {'count': 0, 'addresses': []},
'writer': {'count': 0, 'addresses': []}
},
'anyone': {
'commenter': {'count': 0},
'reader': {'count': 0},
'writer': {'count': 0}
},
'anyoneWithlink': {
'commenter': {'count': 0},
'reader': {'count': 0},
'writer': {'count': 0}
},
}
fieldnames = ['Owner', 'driveFileId', 'driveFileTitle', 'mimeType']
if SHOW_COUNTS:
if SHOW_USERS:
fieldnames.extend(['userCommenterCount', 'userCommenter',
'userReaderCount', 'userReader',
'userWriterCount', 'userWriter',
'userFileorganizerCount', 'userFileorganizer',
'userOrganizerCount', 'userOrganizer'])
if SHOW_GROUPS:
fieldnames.extend(['groupCommenterCount', 'groupCommenter',
'groupReaderCount', 'groupReader',
'groupWriterCount', 'groupWriter',
'groupFileorganizerCount', 'groupFileorganizer',
'groupOrganizerCount', 'groupOrganizer'])
if SHOW_DOMAINS:
fieldnames.extend(['domainCommenterCount', 'domainCommenter',
'domainReaderCount', 'domainReader',
'domainWriterCount', 'domainWriter'])
fieldnames.extend(['domainWithlinkCommenterCount', 'domainWithlinkCommenter',
'domainWithlinkReaderCount', 'domainWithlinkReader',
'domainWithlinkWriterCount', 'domainWithlinkWriter'])
if SHOW_ANYONES:
fieldnames.extend(['anyoneCommenterCount', 'anyoneCommenter',
'anyoneReaderCount', 'anyoneReader',
'anyoneWriterCount', 'anyoneWriter'])
fieldnames.extend(['anyoneWithlinkCommenterCount', 'anyoneWithlinkCommenter',
'anyoneWithlinkReaderCount', 'anyoneWithlinkReader',
'anyoneWithlinkWriterCount', 'anyoneWithlinkWriter'])
else:
if SHOW_USERS:
fieldnames.extend(['userCommenter',
'userReader',
'userWriter',
'userFileorganizer',
'userOrganizer'])
if SHOW_GROUPS:
fieldnames.extend(['groupCommenter',
'groupReader',
'groupWriter',
'groupFileorganizer',
'groupOrganizer'])
if SHOW_DOMAINS:
fieldnames.extend(['domainCommenter',
'domainReader',
'domainWriter'])
fieldnames.extend(['domainWithlinkCommenter',
'domainWithlinkReader',
'domainWithlinkWriter'])
if SHOW_ANYONES:
fieldnames.extend(['anyoneCommenter',
'anyoneReader',
'anyoneWriter'])
fieldnames.extend(['anyoneWithlinkCommenter',
'anyoneWithlinkReader',
'anyoneWithlinkWriter'])
if (len(sys.argv) > 2) and (sys.argv[2] != '-'):
outputFile = open(sys.argv[2], 'w', encoding='utf-8', newline='')
else:
outputFile = sys.stdout
outputCSV = csv.DictWriter(outputFile, fieldnames, lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
if (len(sys.argv) > 1) and (sys.argv[1] != '-'):
inputFile = open(sys.argv[1], 'r', encoding='utf-8')
else:
inputFile = sys.stdin
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
permCounts = copy.deepcopy(ZERO_COUNTS)
for k, v in iter(row.items()):
mg = PERMISSIONS_N_TYPE.match(k)
if mg and v:
permissions_N = mg.group(1)
role = row[f'permissions.{permissions_N}.role']
if role == 'owner':
continue
if v in ['user', 'group']:
permCounts[v][role]['count'] += 1
permCounts[v][role]['addresses'].append(row[f'permissions.{permissions_N}.emailAddress'].lower())
continue
if v == 'domain':
if not row.get(f'permissions.{permissions_N}.allowFileDiscovery',
str(row.get(f'permissions.{permissions_N}.withLink') == 'False')):
v = 'domainWithlink'
permCounts[v][role]['count'] += 1
permCounts[v][role]['addresses'].append(row[f'permissions.{permissions_N}.domain'])
continue
# if v == 'anyone'
if not row.get(f'permissions.{permissions_N}.allowFileDiscovery',
str(row.get(f'permissions.{permissions_N}.withLink') == 'False')):
v = 'anyoneWithlink'
permCounts[v][role]['count'] += 1
orow = {'Owner': row['owners.0.emailAddress'],
'driveFileId': row['id'],
'driveFileTitle': row.get(FILE_NAME, row.get(ALT_FILE_NAME, 'Unknown')),
'mimeType': row['mimeType']}
if SHOW_USERS:
atype = 'user'
for role in USER_GROUP_ROLES:
atypeRole = f'{atype}{role.capitalize()}'
if SHOW_COUNTS:
orow[f'{atypeRole}Count'] = permCounts[atype][role]['count']
orow[atypeRole] = LIST_DELIMITER.join(permCounts[atype][role]['addresses'])
if SHOW_GROUPS:
atype = 'group'
for role in USER_GROUP_ROLES:
atypeRole = f'{atype}{role.capitalize()}'
if SHOW_COUNTS:
orow[f'{atypeRole}Count'] = permCounts[atype][role]['count']
orow[atypeRole] = LIST_DELIMITER.join(permCounts[atype][role]['addresses'])
if SHOW_DOMAINS:
for atype in ['domain', 'domainWithlink']:
for role in DOMAIN_ANYONE_ROLES:
atypeRole = f'{atype}{role.capitalize()}'
if SHOW_COUNTS:
orow[f'{atypeRole}Count'] = permCounts[atype][role]['count']
orow[atypeRole] = LIST_DELIMITER.join(permCounts[atype][role]['addresses'])
if SHOW_ANYONES:
for atype in ['anyone', 'anyoneWithlink']:
for role in DOMAIN_ANYONE_ROLES:
atypeRole = f'{atype}{role.capitalize()}'
if SHOW_COUNTS:
orow[f'{atypeRole}Count'] = permCounts[atype][role]['count']
outputCSV.writerow(orow)
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()