Skip to content

Commit 7f71f65

Browse files
author
Eric Koleda
authored
Merge pull request googleworkspace#7 from gsuitedevs/groupssettings
Add a sample for listing groups with external access.
2 parents e5220a6 + 2712996 commit 7f71f65

File tree

2 files changed

+157
-3
lines changed

2 files changed

+157
-3
lines changed
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
1-
# Google Admin SDK Groups Settings Python Quickstart
1+
# Google Admin SDK Groups Settings Python Samples
2+
3+
## Quickstart
24

35
Complete the steps described in the [Google Admin SDK Groups Settings Python
46
Quickstart](https://developers.google.com/admin-sdk/groups-settings/quickstart/python),
57
and in about five minutes you'll have a simple Python command-line application
68
that makes requests to the Google Admin SDK Groups Settings API.
79

8-
## Install
10+
### Install
911

1012
```
1113
pip install --upgrade google-api-python-client
1214
```
1315

14-
## Run
16+
### Run
1517

1618
```
1719
python quickstart.py
1820
```
21+
22+
## Detect External Access
23+
24+
This script lists all groups in your domain with some form of external access
25+
enabled.
26+
27+
### Install
28+
29+
```
30+
pip install --upgrade google-api-python-client
31+
```
32+
33+
### Run
34+
35+
```
36+
detect_external_access.py
37+
```
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
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.
18+
"""
19+
from __future__ import print_function
20+
import httplib2
21+
22+
from apiclient import discovery, errors
23+
from oauth2client import client, tools
24+
from oauth2client.file import Storage
25+
26+
# If modifying these scopes, delete your previously saved credentials
27+
# at credentials.json
28+
SCOPES = ['https://www.googleapis.com/auth/admin.directory.group',
29+
'https://www.googleapis.com/auth/apps.groups.settings']
30+
CLIENT_SECRET_FILE = 'client_secret.json'
31+
APPLICATION_NAME = 'List Groups with external access'
32+
33+
# External access for view group
34+
ANYONE_CAN_VIEW_GROUP = 'ANYONE_CAN_VIEW'
35+
36+
# External access for who can join
37+
ANYONE_CAN_JOIN_GROUP = 'ANYONE_CAN_JOIN'
38+
39+
# Group can have external members
40+
EXTERNAL_MEMBERS_CAN_JOIN = 'true'
41+
42+
# External access for who can post messages
43+
ANYONE_CAN_POST_MESSAGE = 'ANYONE_CAN_POST'
44+
45+
# External access for who can view members
46+
ANYONE_CAN_VIEW_MEMBERSHIP = 'ANYONE_CAN_VIEW'
47+
48+
def get_credentials():
49+
"""
50+
Gets valid user credentials from storage.
51+
52+
If nothing has been stored, or if the stored credentials are invalid,
53+
the OAuth2 flow is completed to obtain the new credentials.
54+
55+
Returns:
56+
Credentials, the obtained credential.
57+
"""
58+
store = Storage('credentials.json')
59+
creds = store.get()
60+
if not creds or creds.invalid:
61+
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
62+
creds = tools.run_flow(flow, store)
63+
return creds
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 or
76+
who_can_join == ANYONE_CAN_JOIN_GROUP or
77+
allow_external_members == EXTERNAL_MEMBERS_CAN_JOIN or
78+
who_can_post_message == ANYONE_CAN_POST_MESSAGE or
79+
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+
def check_group_settings(group_settings_service, group_email):
88+
"""
89+
Gets the group settings for the given group_email and prints the group
90+
if it has external access enabled.
91+
"""
92+
try:
93+
settings = group_settings_service.groups().get(
94+
groupUniqueId=group_email).execute()
95+
print_group_settings(group_email, settings)
96+
except errors.HttpError:
97+
print('Unable to read group: {0}'.format(group_email))
98+
99+
def check_groups(group_service, group_settings_service, page_token):
100+
"""
101+
Gets the groups in the domain, gets group settings for each group and prints
102+
the ones which have external access enabled.
103+
104+
Returns:
105+
page_token to get the next page of groups
106+
"""
107+
results = group_service.groups().list(customer='my_customer',
108+
pageToken=page_token,
109+
orderBy='email').execute()
110+
groups = results.get('groups', [])
111+
112+
if groups:
113+
for group in groups:
114+
check_group_settings(group_settings_service, group['email'])
115+
return results.get('nextPageToken', None)
116+
117+
def main():
118+
"""
119+
Runs the script.
120+
"""
121+
credentials = get_credentials()
122+
http = credentials.authorize(httplib2.Http())
123+
group_service = discovery.build('admin', 'directory_v1', http=http)
124+
group_settings_service = discovery.build('groupssettings', 'v1', http=http)
125+
126+
page_token = None
127+
while True:
128+
page_token = check_groups(group_service=group_service,
129+
group_settings_service=group_settings_service,
130+
page_token=page_token)
131+
if page_token is None:
132+
break
133+
134+
if __name__ == '__main__':
135+
main()

0 commit comments

Comments
 (0)