Skip to content

Commit 11c43ea

Browse files
author
Eric Koleda
committed
Fix pylint errors.
1 parent c715ce5 commit 11c43ea

File tree

1 file changed

+90
-106
lines changed

1 file changed

+90
-106
lines changed

admin_sdk/groups_settings/detect_external_access.py

Lines changed: 90 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,24 +13,16 @@
1313
# limitations under the License.
1414

1515
"""
16-
Outputs all the groups in the domain which have 'external' to the domain access.
17-
Also outputs their access settings.
16+
print_group_settingss all the groups in the domain which have 'external' to the domain access.
17+
Also print_group_settingss their access settings.
1818
"""
1919
from __future__ import print_function
2020
import httplib2
21-
import os
2221

23-
from apiclient import discovery
24-
from oauth2client import client
25-
from oauth2client import tools
22+
from apiclient import discovery, errors
23+
from oauth2client import client, tools
2624
from oauth2client.file import Storage
2725

28-
try:
29-
import argparse
30-
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
31-
except ImportError:
32-
flags = None
33-
3426
# If modifying these scopes, delete your previously saved credentials
3527
# at ~/.credentials/group-settings-public.json
3628
SCOPES = ['https://www.googleapis.com/auth/admin.directory.group',
@@ -54,101 +46,93 @@
5446
ANYONE_CAN_VIEW_MEMBERSHIP = 'ANYONE_CAN_VIEW'
5547

5648
def get_credentials():
57-
"""Gets valid user credentials from storage.
58-
59-
If nothing has been stored, or if the stored credentials are invalid,
60-
the OAuth2 flow is completed to obtain the new credentials.
61-
62-
Returns:
63-
Credentials, the obtained credential.
64-
"""
65-
home_dir = os.path.expanduser('~')
66-
credential_dir = os.path.join(home_dir, '.credentials')
67-
if not os.path.exists(credential_dir):
68-
os.makedirs(credential_dir)
69-
credential_path = os.path.join(credential_dir,
70-
'group-settings-public.json')
71-
72-
store = Storage(credential_path)
73-
credentials = store.get()
74-
if not credentials or credentials.invalid:
75-
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
76-
flow.user_agent = APPLICATION_NAME
77-
if flags:
78-
credentials = tools.run_flow(flow, store, flags)
79-
else: # Needed only for compatibility with Python 2.6
80-
credentials = tools.run(flow, store)
81-
print('Storing credentials to ' + credential_path)
82-
return credentials
83-
84-
85-
def print_if_external_access_enabled(groupEmail, settings):
86-
"""
87-
Given the group email and its settings, checks some of its settings and prints
88-
them if the group has external access.
89-
"""
90-
whoCanViewGroup = settings['whoCanViewGroup']
91-
whoCanJoin = settings['whoCanJoin']
92-
allowExternalMembers = settings['allowExternalMembers']
93-
whoCanPostMessage = settings['whoCanPostMessage']
94-
whoCanViewMembership = settings['whoCanViewMembership']
95-
if (whoCanViewGroup == ANYONE_CAN_VIEW_GROUP
96-
or whoCanJoin == ANYONE_CAN_JOIN_GROUP
97-
or allowExternalMembers == EXTERNAL_MEMBERS_CAN_JOIN
98-
or whoCanPostMessage == ANYONE_CAN_POST_MESSAGE
99-
or whoCanViewMembership == ANYONE_CAN_VIEW_MEMBERSHIP):
100-
print(groupEmail)
101-
print(' whoCanViewGroup - {0}'.format(whoCanViewGroup))
102-
print(' whoCanJoin - {0}'.format(whoCanJoin))
103-
print(' allowExternalMembers - {0}'.format(allowExternalMembers))
104-
print(' whoCanPostMessage - {0}'.format(whoCanPostMessage))
105-
print(' whoCanViewMembership - {0}'.format(whoCanViewMembership))
106-
107-
108-
def get_group_settings(group_settings_service, groupEmail):
109-
"""
110-
Gets the group settings for the given groupEmail and prints the group
111-
if it has external access enabled.
112-
"""
113-
try:
114-
settings = group_settings_service.groups().get(
115-
groupUniqueId=groupEmail).execute()
116-
print_if_external_access_enabled(groupEmail, settings)
117-
except:
118-
print('Unable to read group: {0}'.format(groupEmail))
119-
120-
121-
def get_groups(group_service, group_settings_service, pageToken):
122-
"""
123-
Gets the groups in the domain, gets group settings for each group and prints
124-
the ones which have external access enabled.
125-
126-
Returns:
127-
pageToken to get the next page of groups
128-
"""
129-
results = group_service.groups().list(
130-
customer='my_customer', pageToken=pageToken, orderBy='email').execute()
131-
groups = results.get('groups', [])
132-
133-
if groups:
134-
for group in groups:
135-
get_group_settings(group_settings_service, group['email'])
136-
return results.get('nextPageToken', None)
49+
"""Gets valid user credentials from storage.
50+
51+
If nothing has been stored, or if the stored credentials are invalid,
52+
the OAuth2 flow is completed to obtain the new credentials.
53+
54+
Returns:
55+
Credentials, the obtained credential.
56+
"""
57+
store = Storage('credentials.json')
58+
creds = store.get()
59+
if not creds or creds.invalid:
60+
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
61+
creds = tools.run_flow(flow, store)
62+
return creds
63+
64+
65+
def print_group_settings(group_email, settings):
66+
"""
67+
Given the group email and its settings, checks some of its settings and
68+
prints them if the group has external access.
69+
"""
70+
who_can_view_group = settings['whoCanViewGroup']
71+
who_can_join = settings['whoCanJoin']
72+
allow_external_members = settings['allowExternalMembers']
73+
who_can_post_message = settings['whoCanPostMessage']
74+
who_can_view_membership = settings['whoCanViewMembership']
75+
if (who_can_view_group == ANYONE_CAN_VIEW_GROUP
76+
or who_can_join == ANYONE_CAN_JOIN_GROUP
77+
or allow_external_members == EXTERNAL_MEMBERS_CAN_JOIN
78+
or who_can_post_message == ANYONE_CAN_POST_MESSAGE
79+
or who_can_view_membership == ANYONE_CAN_VIEW_MEMBERSHIP):
80+
print(group_email)
81+
print('\twhoCanViewGroup - {0}'.format(who_can_view_group))
82+
print('\twhoCanJoin - {0}'.format(who_can_join))
83+
print('\tallowExternalMembers - {0}'.format(allow_external_members))
84+
print('\twhoCanPostMessage - {0}'.format(who_can_post_message))
85+
print('\twhoCanViewMembership - {0}'.format(who_can_view_membership))
86+
87+
88+
def check_group_settings(group_settings_service, group_email):
89+
"""
90+
Gets the group settings for the given group_email and prints the group
91+
if it has external access enabled.
92+
"""
93+
try:
94+
settings = group_settings_service.groups().get(
95+
groupUniqueId=group_email).execute()
96+
print_group_settings(group_email, settings)
97+
except errors.HttpError:
98+
print('Unable to read group: {0}'.format(group_email))
99+
100+
101+
def check_groups(group_service, group_settings_service, page_token):
102+
"""
103+
Gets the groups in the domain, gets group settings for each group and prints
104+
the ones which have external access enabled.
105+
106+
Returns:
107+
page_token to get the next page of groups
108+
"""
109+
results = group_service.groups().list(customer='my_customer',
110+
pageToken=page_token,
111+
orderBy='email').execute()
112+
groups = results.get('groups', [])
113+
114+
if groups:
115+
for group in groups:
116+
check_group_settings(group_settings_service, group['email'])
117+
return results.get('nextPageToken', None)
137118

138119

139120
def main():
140-
credentials = get_credentials()
141-
http = credentials.authorize(httplib2.Http())
142-
group_service = discovery.build('admin', 'directory_v1', http=http)
143-
group_settings_service = discovery.build('groupssettings', 'v1', http=http)
144-
145-
pageToken = None
146-
while True:
147-
pageToken = get_groups(group_service=group_service,
148-
group_settings_service=group_settings_service,
149-
pageToken=pageToken)
150-
if pageToken is None:
151-
break
121+
"""
122+
Runs the script.
123+
"""
124+
credentials = get_credentials()
125+
http = credentials.authorize(httplib2.Http())
126+
group_service = discovery.build('admin', 'directory_v1', http=http)
127+
group_settings_service = discovery.build('groupssettings', 'v1', http=http)
128+
129+
page_token = None
130+
while True:
131+
page_token = check_groups(group_service=group_service,
132+
group_settings_service=group_settings_service,
133+
page_token=page_token)
134+
if page_token is None:
135+
break
152136

153137
if __name__ == '__main__':
154-
main()
138+
main()

0 commit comments

Comments
 (0)