blob: 9d33a98ab8feb0af575b5e0de81d2b48c3ab635d [file] [log] [blame]
Joe Gregorio53806d02011-11-23 09:08:31 -05001#!/usr/bin/python
2#
Craig Citro751b7fb2014-09-23 11:20:38 -07003# Copyright 2014 Google Inc. All Rights Reserved.
Joe Gregorio53806d02011-11-23 09:08:31 -05004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Sample for the Group Settings API demonstrates get and update method.
18
19Usage:
20 $ python groupsettings.py
21
22You can also get help on all the command-line flags the program understands
23by running:
24
25 $ python groupsettings.py --help
26"""
INADA Naokie8d87822014-08-20 15:25:24 +090027from __future__ import print_function
Joe Gregorio53806d02011-11-23 09:08:31 -050028
29__author__ = 'Shraddha Gupta <shraddhag@google.com>'
30
31from optparse import OptionParser
32import os
33
34import pprint
35import sys
John Asmuth864311d2014-04-24 15:46:08 -040036from googleapiclient.discovery import build
Joe Gregorio53806d02011-11-23 09:08:31 -050037import httplib2
38from oauth2client.client import flow_from_clientsecrets
39from oauth2client.file import Storage
Chaz Reide8e6c732018-07-14 06:45:07 -070040from oauth2client.tools import run_flow
Joe Gregorio53806d02011-11-23 09:08:31 -050041
42
43# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
44# application, including client_id and client_secret, which are found
45# on the API Access tab on the Google APIs
46# Console <http://code.google.com/apis/console>
47CLIENT_SECRETS = 'client_secrets.json'
48
49# Helpful message to display in the browser if the CLIENT_SECRETS file
50# is missing.
51MISSING_CLIENT_SECRETS_MESSAGE = """
52WARNING: Please configure OAuth 2.0
53
54To make this sample run you will need to populate the client_secrets.json file
55found at:
56
57 %s
58
59with information from the APIs Console <https://code.google.com/apis/console>.
60
61""" % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS)
62
63
64def access_settings(service, groupId, settings):
65 """Retrieves a group's settings and updates the access permissions to it.
66
67 Args:
68 service: object service for the Group Settings API.
69 groupId: string identifier of the group@domain.
70 settings: dictionary key-value pairs of properties of group.
71 """
72
73 # Get the resource 'group' from the set of resources of the API.
74 # The Group Settings API has only one resource 'group'.
75 group = service.groups()
76
77 # Retrieve the group properties
78 g = group.get(groupUniqueId=groupId).execute()
INADA Naokie8d87822014-08-20 15:25:24 +090079 print('\nGroup properties for group %s\n' % g['name'])
Joe Gregorio53806d02011-11-23 09:08:31 -050080 pprint.pprint(g)
81
82 # If dictionary is empty, return without updating the properties.
83 if not settings.keys():
INADA Naokie8d87822014-08-20 15:25:24 +090084 print('\nGive access parameters to update group access permissions\n')
Joe Gregorio53806d02011-11-23 09:08:31 -050085 return
86
87 body = {}
88
89 # Settings might contain null value for some keys(properties).
90 # Extract the properties with values and add to dictionary body.
91 for key in settings.iterkeys():
92 if settings[key] is not None:
93 body[key] = settings[key]
94
95 # Update the properties of group
96 g1 = group.update(groupUniqueId=groupId, body=body).execute()
97
INADA Naokie8d87822014-08-20 15:25:24 +090098 print('\nUpdated Access Permissions to the group\n')
Joe Gregorio53806d02011-11-23 09:08:31 -050099 pprint.pprint(g1)
100
101
102def main(argv):
103 """Demos the setting of the access properties by the Groups Settings API."""
104 usage = 'usage: %prog [options]'
105 parser = OptionParser(usage=usage)
106 parser.add_option('--groupId',
107 help='Group email address')
108 parser.add_option('--whoCanInvite',
109 help='Possible values: ALL_MANAGERS_CAN_INVITE, '
110 'ALL_MEMBERS_CAN_INVITE')
111 parser.add_option('--whoCanJoin',
112 help='Possible values: ALL_IN_DOMAIN_CAN_JOIN, '
113 'ANYONE_CAN_JOIN, CAN_REQUEST_TO_JOIN, '
114 'CAN_REQUEST_TO_JOIN')
115 parser.add_option('--whoCanPostMessage',
116 help='Possible values: ALL_IN_DOMAIN_CAN_POST, '
117 'ALL_MANAGERS_CAN_POST, ALL_MEMBERS_CAN_POST, '
118 'ANYONE_CAN_POST, NONE_CAN_POST')
119 parser.add_option('--whoCanViewGroup',
120 help='Possible values: ALL_IN_DOMAIN_CAN_VIEW, '
121 'ALL_MANAGERS_CAN_VIEW, ALL_MEMBERS_CAN_VIEW, '
122 'ANYONE_CAN_VIEW')
123 parser.add_option('--whoCanViewMembership',
124 help='Possible values: ALL_IN_DOMAIN_CAN_VIEW, '
125 'ALL_MANAGERS_CAN_VIEW, ALL_MEMBERS_CAN_VIEW, '
126 'ANYONE_CAN_VIEW')
127 (options, args) = parser.parse_args()
128
129 if options.groupId is None:
INADA Naokie8d87822014-08-20 15:25:24 +0900130 print('Give the groupId for the group')
Joe Gregorio53806d02011-11-23 09:08:31 -0500131 parser.print_help()
132 return
133
134 settings = {}
135
136 if (options.whoCanInvite or options.whoCanJoin or options.whoCanPostMessage
137 or options.whoCanPostMessage or options.whoCanViewMembership) is None:
INADA Naokie8d87822014-08-20 15:25:24 +0900138 print('No access parameters given in input to update access permissions')
Joe Gregorio53806d02011-11-23 09:08:31 -0500139 parser.print_help()
140 else:
141 settings = {'whoCanInvite': options.whoCanInvite,
142 'whoCanJoin': options.whoCanJoin,
143 'whoCanPostMessage': options.whoCanPostMessage,
144 'whoCanViewGroup': options.whoCanViewGroup,
145 'whoCanViewMembership': options.whoCanViewMembership}
146
147 # Set up a Flow object to be used if we need to authenticate.
148 FLOW = flow_from_clientsecrets(CLIENT_SECRETS,
149 scope='https://www.googleapis.com/auth/apps.groups.settings',
150 message=MISSING_CLIENT_SECRETS_MESSAGE)
151
152 storage = Storage('groupsettings.dat')
153 credentials = storage.get()
154
155 if credentials is None or credentials.invalid:
INADA Naokie8d87822014-08-20 15:25:24 +0900156 print('invalid credentials')
Joe Gregorio53806d02011-11-23 09:08:31 -0500157 # Save the credentials in storage to be used in subsequent runs.
Chaz Reide8e6c732018-07-14 06:45:07 -0700158 credentials = run_flow(FLOW, storage)
Joe Gregorio53806d02011-11-23 09:08:31 -0500159
160 # Create an httplib2.Http object to handle our HTTP requests and authorize it
161 # with our good Credentials.
162 http = httplib2.Http()
163 http = credentials.authorize(http)
164
165 service = build('groupssettings', 'v1', http=http)
166
167 access_settings(service=service, groupId=options.groupId, settings=settings)
168
169if __name__ == '__main__':
170 main(sys.argv)