Update calendar to allow access to user's secondary calendars (#499)

* Update calendar to allow access to user's secondary calendars

* Code cleanup

* Code cleanup

* Code cleanup
This commit is contained in:
Ross Scroggs 2017-05-20 12:34:25 -07:00 committed by Jay Lee
parent d5af189125
commit 1a96622366
2 changed files with 45 additions and 50 deletions

View File

@ -772,7 +772,7 @@ gam <UserTypeEntity> delete|del backupcodes|backupcode|verificationcodes
gam <UserTypeEntity> show backupcodes|backupcode|verificationcodes gam <UserTypeEntity> show backupcodes|backupcode|verificationcodes
gam <UserTypeEntity> add calendar <CalendarItem> <CalendarAttributes>* gam <UserTypeEntity> add calendar <CalendarItem> <CalendarAttributes>*
gam <UserTypeEntity> update calendar <CalendarItem> <CalendarAttributes>+ gam <UserTypeEntity> update calendar <CalendarItem>|primary <CalendarAttributes>+
gam <UserTypeEntity> delete|del calendar <CalendarItem> gam <UserTypeEntity> delete|del calendar <CalendarItem>
gam <UserTypeEntity> show calendars gam <UserTypeEntity> show calendars
gam <UserTypeEntity> info calendar <CalendarItem>|primary gam <UserTypeEntity> info calendar <CalendarItem>|primary

View File

@ -818,12 +818,27 @@ def buildActivityGAPIObject(user):
userEmail = convertUserUIDtoEmailAddress(user) userEmail = convertUserUIDtoEmailAddress(user)
return (userEmail, buildGAPIServiceObject(u'appsactivity', userEmail)) return (userEmail, buildGAPIServiceObject(u'appsactivity', userEmail))
def buildCalendarGAPIObject(calname): def normalizeCalendarId(calname, checkPrimary=False):
calname = calname.lower()
if checkPrimary and calname == u'primary':
return calname
if not GC_Values[GC_DOMAIN]: if not GC_Values[GC_DOMAIN]:
GC_Values[GC_DOMAIN] = _getValueFromOAuth(u'hd').lower() GC_Values[GC_DOMAIN] = _getValueFromOAuth(u'hd')
calendarId = convertUserUIDtoEmailAddress(calname) return convertUserUIDtoEmailAddress(calname)
def buildCalendarGAPIObject(calname):
calendarId = normalizeCalendarId(calname)
return (calendarId, buildGAPIServiceObject(u'calendar', calendarId)) return (calendarId, buildGAPIServiceObject(u'calendar', calendarId))
def buildCalendarDataGAPIObject(calname):
calendarId, cal = buildCalendarGAPIObject(calname)
try:
# Force service account token request. If we fail fall back to using admin for authentication
cal._http.request.credentials.refresh(httplib2.Http(disable_ssl_certificate_validation=GC_Values[GC_NO_VERIFY_SSL]))
except oauth2client.client.HttpAccessTokenRefreshError:
_, cal = buildCalendarGAPIObject(_getValueFromOAuth(u'email'))
return (calendarId, cal)
def buildDriveGAPIObject(user): def buildDriveGAPIObject(user):
userEmail = convertUserUIDtoEmailAddress(user) userEmail = convertUserUIDtoEmailAddress(user)
return (userEmail, buildGAPIServiceObject(u'drive', userEmail)) return (userEmail, buildGAPIServiceObject(u'drive', userEmail))
@ -1407,13 +1422,9 @@ def doGetCustomerInfo():
try_date = _adjustDate(message) try_date = _adjustDate(message)
print u'User counts as of %s:' % try_date print u'User counts as of %s:' % try_date
for item in usage[0][u'parameters']: for item in usage[0][u'parameters']:
if not u'intValue' in item or int(item[u'intValue']) == 0: api_name = user_counts_map.get(item[u'name'])
continue api_value = int(item.get(u'intValue', 0))
api_value = int(item[u'intValue']) if api_name and api_value:
try:
api_name = user_counts_map[item[u'name']]
except KeyError:
continue
print u' {}: {:,}'.format(api_name, api_value) print u' {}: {:,}'.format(api_name, api_value)
def doUpdateCustomer(): def doUpdateCustomer():
@ -2506,9 +2517,7 @@ def changeCalendarAttendees(users):
break break
def deleteCalendar(users): def deleteCalendar(users):
calendarId = sys.argv[5] calendarId = normalizeCalendarId(sys.argv[5])
if calendarId.find(u'@') == -1:
calendarId = u'%s@%s' % (calendarId, GC_Values[GC_DOMAIN])
for user in users: for user in users:
user, cal = buildCalendarGAPIObject(user) user, cal = buildCalendarGAPIObject(user)
if not cal: if not cal:
@ -2588,9 +2597,7 @@ def getCalendarAttributes(i, body, function):
return colorRgbFormat return colorRgbFormat
def addCalendar(users): def addCalendar(users):
calendarId = sys.argv[5] calendarId = normalizeCalendarId(sys.argv[5])
if calendarId.find(u'@') == -1:
calendarId = u'%s@%s' % (calendarId, GC_Values[GC_DOMAIN])
body = {u'id': calendarId, u'selected': True, u'hidden': False} body = {u'id': calendarId, u'selected': True, u'hidden': False}
colorRgbFormat = getCalendarAttributes(6, body, u'add') colorRgbFormat = getCalendarAttributes(6, body, u'add')
i = 0 i = 0
@ -2604,9 +2611,7 @@ def addCalendar(users):
callGAPI(cal.calendarList(), u'insert', soft_errors=True, body=body, colorRgbFormat=colorRgbFormat) callGAPI(cal.calendarList(), u'insert', soft_errors=True, body=body, colorRgbFormat=colorRgbFormat)
def updateCalendar(users): def updateCalendar(users):
calendarId = sys.argv[5] calendarId = normalizeCalendarId(sys.argv[5], checkPrimary=True)
if calendarId.find(u'@') == -1:
calendarId = u'%s@%s' % (calendarId, GC_Values[GC_DOMAIN])
body = {} body = {}
colorRgbFormat = getCalendarAttributes(6, body, u'update') colorRgbFormat = getCalendarAttributes(6, body, u'update')
i = 0 i = 0
@ -3018,13 +3023,9 @@ def formatACLRule(rule):
return u'(Scope: {0}, Role: {1})'.format(rule[u'scope'][u'type'], rule[u'role']) return u'(Scope: {0}, Role: {1})'.format(rule[u'scope'][u'type'], rule[u'role'])
def doCalendarShowACL(): def doCalendarShowACL():
calendarId, cal = buildCalendarGAPIObject(sys.argv[2]) calendarId, cal = buildCalendarDataGAPIObject(sys.argv[2])
try: if not cal:
# Force service account token request. If we fail fall back to return
# using admin for delegation
cal._http.request.credentials.refresh(httplib2.Http(disable_ssl_certificate_validation=GC_Values[GC_NO_VERIFY_SSL]))
except oauth2client.client.HttpAccessTokenRefreshError:
_, cal = buildCalendarGAPIObject(_getValueFromOAuth(u'email'))
acls = callGAPIitems(cal.acl(), u'list', u'items', calendarId=calendarId) acls = callGAPIitems(cal.acl(), u'list', u'items', calendarId=calendarId)
i = 0 i = 0
count = len(acls) count = len(acls)
@ -3033,18 +3034,14 @@ def doCalendarShowACL():
print u'Calendar: {0}, ACL: {1}{2}'.format(calendarId, formatACLRule(rule), currentCount(i, count)) print u'Calendar: {0}, ACL: {1}{2}'.format(calendarId, formatACLRule(rule), currentCount(i, count))
def doCalendarAddACL(calendarId=None, act_as=None, role=None, scope=None, entity=None): def doCalendarAddACL(calendarId=None, act_as=None, role=None, scope=None, entity=None):
if not GC_Values[GC_DOMAIN]:
GC_Values[GC_DOMAIN] = _getValueFromOAuth(u'hd').lower()
if calendarId is None: if calendarId is None:
calendarId = sys.argv[2] calendarId = sys.argv[2]
if calendarId.find(u'@') == -1:
calendarId = u'%s@%s' % (calendarId, GC_Values[GC_DOMAIN])
if not act_as: if not act_as:
calendarId = normalizeCalendarId(calendarId)
act_as = calendarId act_as = calendarId
_, cal = buildCalendarGAPIObject(act_as) _, cal = buildCalendarGAPIObject(act_as)
try: try:
# Force service account token request. If we fail fall back to # Force service account token request. If we fail fall back to using admin for authentication
# using admin for delegation
cal._http.request.credentials.refresh(httplib2.Http(disable_ssl_certificate_validation=GC_Values[GC_NO_VERIFY_SSL])) cal._http.request.credentials.refresh(httplib2.Http(disable_ssl_certificate_validation=GC_Values[GC_NO_VERIFY_SSL]))
except oauth2client.client.HttpAccessTokenRefreshError: except oauth2client.client.HttpAccessTokenRefreshError:
_, cal = buildCalendarGAPIObject(_getValueFromOAuth(u'email')) _, cal = buildCalendarGAPIObject(_getValueFromOAuth(u'email'))
@ -3053,8 +3050,8 @@ def doCalendarAddACL(calendarId=None, act_as=None, role=None, scope=None, entity
body[u'role'] = role body[u'role'] = role
else: else:
body[u'role'] = sys.argv[4].lower() body[u'role'] = sys.argv[4].lower()
if body[u'role'] not in [u'freebusy', u'read', u'reader', u'editor', u'owner', u'none']: if body[u'role'] not in [u'freebusy', u'read', u'reader', u'editor', u'writer', u'owner', u'none']:
print u'ERROR: Role must be one of freebusy, read, editor, owner, none; got %s' % body[u'role'] print u'ERROR: Role must be one of freebusy, reader, editor, writer, owner, none; got %s' % body[u'role']
sys.exit(2) sys.exit(2)
if body[u'role'] == u'freebusy': if body[u'role'] == u'freebusy':
body[u'role'] = u'freeBusyReader' body[u'role'] = u'freeBusyReader'
@ -3108,13 +3105,13 @@ def doCalendarDelACL():
doCalendarAddACL(calendarId=calendarId, role=u'none', scope=scope, entity=entity) doCalendarAddACL(calendarId=calendarId, role=u'none', scope=scope, entity=entity)
def doCalendarWipeData(): def doCalendarWipeData():
calendarId, cal = buildCalendarGAPIObject(sys.argv[2]) calendarId, cal = buildCalendarDataGAPIObject(sys.argv[2])
if not cal: if not cal:
return return
callGAPI(cal.calendars(), u'clear', calendarId=calendarId) callGAPI(cal.calendars(), u'clear', calendarId=calendarId)
def doCalendarDeleteEvent(): def doCalendarDeleteEvent():
calendarId, cal = buildCalendarGAPIObject(sys.argv[2]) calendarId, cal = buildCalendarDataGAPIObject(sys.argv[2])
if not cal: if not cal:
return return
events = [] events = []
@ -3150,7 +3147,7 @@ def doCalendarDeleteEvent():
print u' would delete eventId %s. Add doit to command to actually delete event' % eventId print u' would delete eventId %s. Add doit to command to actually delete event' % eventId
def doCalendarAddEvent(): def doCalendarAddEvent():
calendarId, cal = buildCalendarGAPIObject(sys.argv[2]) calendarId, cal = buildCalendarDataGAPIObject(sys.argv[2])
if not cal: if not cal:
return return
sendNotifications = timeZone = None sendNotifications = timeZone = None
@ -3412,9 +3409,7 @@ def _showCalendar(userCalendar, j, jcount):
print u' Method: {0}, Type: {1}'.format(notification[u'method'], notification[u'type']) print u' Method: {0}, Type: {1}'.format(notification[u'method'], notification[u'type'])
def infoCalendar(users): def infoCalendar(users):
calendarId = sys.argv[5].lower() calendarId = normalizeCalendarId(sys.argv[5], checkPrimary=True)
if calendarId != u'primary' and calendarId.find(u'@') == -1:
calendarId = u'%s@%s' % (calendarId, GC_Values[GC_DOMAIN])
i = 0 i = 0
count = len(users) count = len(users)
for user in users: for user in users: