|
| 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 | +# [START admin_sdk_groups_settings_quickstart] |
| 16 | +""" |
| 17 | +Shows basic usage of the Admin SDK Groups Settings API. Outputs a group's |
| 18 | +settings identified by the group's email address. |
| 19 | +""" |
| 20 | + |
| 21 | +""" |
| 22 | +Outputs all the groups in the domain which have 'external' to the domain access. |
| 23 | +Also outputs their access settings. |
| 24 | +""" |
| 25 | +from __future__ import print_function |
| 26 | +import httplib2 |
| 27 | +import os |
| 28 | + |
| 29 | +from apiclient import discovery |
| 30 | +from oauth2client import client |
| 31 | +from oauth2client import tools |
| 32 | +from oauth2client.file import Storage |
| 33 | + |
| 34 | +try: |
| 35 | + import argparse |
| 36 | + flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() |
| 37 | +except ImportError: |
| 38 | + flags = None |
| 39 | + |
| 40 | +# If modifying these scopes, delete your previously saved credentials |
| 41 | +# at ~/.credentials/group-settings-public.json |
| 42 | +SCOPES = ['https://www.googleapis.com/auth/admin.directory.group', |
| 43 | + 'https://www.googleapis.com/auth/apps.groups.settings'] |
| 44 | +CLIENT_SECRET_FILE = 'client_secret.json' |
| 45 | +APPLICATION_NAME = 'List Groups with external access' |
| 46 | + |
| 47 | +# External access for view group |
| 48 | +ANYONE_CAN_VIEW_GROUP = 'ANYONE_CAN_VIEW' |
| 49 | + |
| 50 | +# External access for who can join |
| 51 | +ANYONE_CAN_JOIN_GROUP = 'ANYONE_CAN_JOIN' |
| 52 | + |
| 53 | +# Group can have external members |
| 54 | +EXTERNAL_MEMBERS_CAN_JOIN = 'true' |
| 55 | + |
| 56 | +# External access for who can post messages |
| 57 | +ANYONE_CAN_POST_MESSAGE = 'ANYONE_CAN_POST' |
| 58 | + |
| 59 | +# External access for who can view members |
| 60 | +ANYONE_CAN_VIEW_MEMBERSHIP = 'ANYONE_CAN_VIEW' |
| 61 | + |
| 62 | +def get_credentials(): |
| 63 | + """Gets valid user credentials from storage. |
| 64 | +
|
| 65 | + If nothing has been stored, or if the stored credentials are invalid, |
| 66 | + the OAuth2 flow is completed to obtain the new credentials. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + Credentials, the obtained credential. |
| 70 | + """ |
| 71 | + home_dir = os.path.expanduser('~') |
| 72 | + credential_dir = os.path.join(home_dir, '.credentials') |
| 73 | + if not os.path.exists(credential_dir): |
| 74 | + os.makedirs(credential_dir) |
| 75 | + credential_path = os.path.join(credential_dir, |
| 76 | + 'group-settings-public.json') |
| 77 | + |
| 78 | + store = Storage(credential_path) |
| 79 | + credentials = store.get() |
| 80 | + if not credentials or credentials.invalid: |
| 81 | + flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) |
| 82 | + flow.user_agent = APPLICATION_NAME |
| 83 | + if flags: |
| 84 | + credentials = tools.run_flow(flow, store, flags) |
| 85 | + else: # Needed only for compatibility with Python 2.6 |
| 86 | + credentials = tools.run(flow, store) |
| 87 | + print('Storing credentials to ' + credential_path) |
| 88 | + return credentials |
| 89 | + |
| 90 | + |
| 91 | +def print_if_external_access_enabled(groupEmail, settings): |
| 92 | + """ |
| 93 | + Given the group email and its settings, checks some of its settings and prints |
| 94 | + them if the group has external access. |
| 95 | + """ |
| 96 | + whoCanViewGroup = settings['whoCanViewGroup'] |
| 97 | + whoCanJoin = settings['whoCanJoin'] |
| 98 | + allowExternalMembers = settings['allowExternalMembers'] |
| 99 | + whoCanPostMessage = settings['whoCanPostMessage'] |
| 100 | + whoCanViewMembership = settings['whoCanViewMembership'] |
| 101 | + if (whoCanViewGroup == ANYONE_CAN_VIEW_GROUP |
| 102 | + or whoCanJoin == ANYONE_CAN_JOIN_GROUP |
| 103 | + or allowExternalMembers == EXTERNAL_MEMBERS_CAN_JOIN |
| 104 | + or whoCanPostMessage == ANYONE_CAN_POST_MESSAGE |
| 105 | + or whoCanViewMembership == ANYONE_CAN_VIEW_MEMBERSHIP): |
| 106 | + print(groupEmail) |
| 107 | + print(' whoCanViewGroup - {0}'.format(whoCanViewGroup)) |
| 108 | + print(' whoCanJoin - {0}'.format(whoCanJoin)) |
| 109 | + print(' allowExternalMembers - {0}'.format(allowExternalMembers)) |
| 110 | + print(' whoCanPostMessage - {0}'.format(whoCanPostMessage)) |
| 111 | + print(' whoCanViewMembership - {0}'.format(whoCanViewMembership)) |
| 112 | + |
| 113 | + |
| 114 | +def get_group_settings(group_settings_service, groupEmail): |
| 115 | + """ |
| 116 | + Gets the group settings for the given groupEmail and prints the group |
| 117 | + if it has external access enabled. |
| 118 | + """ |
| 119 | + try: |
| 120 | + settings = group_settings_service.groups().get( |
| 121 | + groupUniqueId=groupEmail).execute() |
| 122 | + print_if_external_access_enabled(groupEmail, settings) |
| 123 | + except: |
| 124 | + print('Unable to read group: {0}'.format(groupEmail)) |
| 125 | + |
| 126 | + |
| 127 | +def get_groups(group_service, group_settings_service, pageToken): |
| 128 | + """ |
| 129 | + Gets the groups in the domain, gets group settings for each group and prints |
| 130 | + the ones which have external access enabled. |
| 131 | +
|
| 132 | + Returns: |
| 133 | + pageToken to get the next page of groups |
| 134 | + """ |
| 135 | + results = group_service.groups().list( |
| 136 | + customer='my_customer', pageToken=pageToken, orderBy='email').execute() |
| 137 | + groups = results.get('groups', []) |
| 138 | + |
| 139 | + if groups: |
| 140 | + for group in groups: |
| 141 | + get_group_settings(group_settings_service, group['email']) |
| 142 | + return results.get('nextPageToken', None) |
| 143 | + |
| 144 | + |
| 145 | +def main(): |
| 146 | + credentials = get_credentials() |
| 147 | + http = credentials.authorize(httplib2.Http()) |
| 148 | + group_service = discovery.build('admin', 'directory_v1', http=http) |
| 149 | + group_settings_service = discovery.build('groupssettings', 'v1', http=http) |
| 150 | + |
| 151 | + pageToken = None |
| 152 | + while True: |
| 153 | + pageToken = get_groups(group_service=group_service, |
| 154 | + group_settings_service=group_settings_service, |
| 155 | + pageToken=pageToken) |
| 156 | + if pageToken is None: |
| 157 | + break |
| 158 | + |
| 159 | +if __name__ == '__main__': |
| 160 | + main() |
0 commit comments