mirror of
https://github.com/GAM-team/GAM.git
synced 2026-07-03 20:31:35 +00:00
Improve user custom schema field handling (#303)
Allow entire schemas or schema fields to be cleared on update Allow specification of multivalued schema field type on update
This commit is contained in:
87
src/gam.py
87
src/gam.py
@@ -6295,7 +6295,21 @@ def appendItemToBodyList(body, itemName, itemValue):
|
|||||||
body.setdefault(itemName, [])
|
body.setdefault(itemName, [])
|
||||||
body[itemName].append(itemValue)
|
body[itemName].append(itemValue)
|
||||||
|
|
||||||
def getUserAttributes(i, updateCmd=False):
|
def getUserAttributes(i, cd, updateCmd=False):
|
||||||
|
def _splitSchemaNameDotFieldName(sn_fn, fnRequired=True):
|
||||||
|
if sn_fn.find(u'.') != -1:
|
||||||
|
schemaName, fieldName = sn_fn.split(u'.', 1)
|
||||||
|
schemaName = schemaName.strip()
|
||||||
|
fieldName = fieldName.strip()
|
||||||
|
if schemaName and fieldName:
|
||||||
|
return (schemaName, fieldName)
|
||||||
|
elif not fnRequired:
|
||||||
|
schemaName = sn_fn.strip()
|
||||||
|
if schemaName:
|
||||||
|
return (schemaName, None)
|
||||||
|
print u'ERROR: %s is not a valid custom schema.field name.' % sn_fn
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
if updateCmd:
|
if updateCmd:
|
||||||
body = {}
|
body = {}
|
||||||
need_password = False
|
need_password = False
|
||||||
@@ -6664,27 +6678,53 @@ def getUserAttributes(i, updateCmd=False):
|
|||||||
note[u'value'] = sys.argv[i].replace(u'\\n', u'\n')
|
note[u'value'] = sys.argv[i].replace(u'\\n', u'\n')
|
||||||
body[u'notes'] = note
|
body[u'notes'] = note
|
||||||
i += 1
|
i += 1
|
||||||
else:
|
elif myarg == u'clearschema':
|
||||||
if u'customSchemas' not in body:
|
if not updateCmd:
|
||||||
body[u'customSchemas'] = {}
|
print u'ERROR: %s is not a valid create user argument.' % sys.argv[i]
|
||||||
try:
|
|
||||||
(schemaName, fieldName) = sys.argv[i].split(u'.')
|
|
||||||
except ValueError:
|
|
||||||
print u'ERROR: %s is not a valid create/update user argument or custom schema name.' % sys.argv[i]
|
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
field_value = sys.argv[i+1]
|
schemaName, fieldName = _splitSchemaNameDotFieldName(sys.argv[i+1], False)
|
||||||
is_multivalue = False
|
up = u'customSchemas'
|
||||||
if field_value.lower() in [u'multivalue', u'multivalued', u'value']:
|
body.setdefault(up, {})
|
||||||
is_multivalue = True
|
body[up].setdefault(schemaName, {})
|
||||||
i += 1
|
if fieldName is None:
|
||||||
field_value = sys.argv[i+1]
|
schema = callGAPI(cd.schemas(), u'get',
|
||||||
body[u'customSchemas'].setdefault(schemaName, {})
|
soft_errors=True,
|
||||||
if is_multivalue:
|
customerId=GC_Values[GC_CUSTOMER_ID], schemaKey=schemaName, fields=u'fields(fieldName)')
|
||||||
body[u'customSchemas'][schemaName].setdefault(fieldName, [])
|
if not schema:
|
||||||
body[u'customSchemas'][schemaName][fieldName].append({u'value': field_value})
|
sys.exit(2)
|
||||||
|
for field in schema[u'fields']:
|
||||||
|
body[up][schemaName][field[u'fieldName']] = None
|
||||||
else:
|
else:
|
||||||
body[u'customSchemas'][schemaName][fieldName] = field_value
|
body[up][schemaName][fieldName] = None
|
||||||
i += 2
|
i += 2
|
||||||
|
elif myarg.find(u'.') >= 0:
|
||||||
|
schemaName, fieldName = _splitSchemaNameDotFieldName(sys.argv[i])
|
||||||
|
up = u'customSchemas'
|
||||||
|
body.setdefault(up, {})
|
||||||
|
body[up].setdefault(schemaName, {})
|
||||||
|
i += 1
|
||||||
|
if sys.argv[i].lower() in [u'multivalue', u'multivalued', u'value']:
|
||||||
|
i += 1
|
||||||
|
body[up][schemaName].setdefault(fieldName, [])
|
||||||
|
schemaValue = {}
|
||||||
|
if sys.argv[i].lower() == u'type':
|
||||||
|
i += 1
|
||||||
|
schemaValue[u'type'] = sys.argv[i].lower()
|
||||||
|
if schemaValue[u'type'] not in [u'custom', u'home', u'other', u'work']:
|
||||||
|
print u'ERROR: wrong type must be one of custom, home, other, work; got %s' % schemaValue[u'type']
|
||||||
|
sys.exit(2)
|
||||||
|
i += 1
|
||||||
|
if schemaValue[u'type'] == u'custom':
|
||||||
|
schemaValue[u'customType'] = sys.argv[i]
|
||||||
|
i += 1
|
||||||
|
schemaValue[u'value'] = sys.argv[i]
|
||||||
|
body[up][schemaName][fieldName].append(schemaValue)
|
||||||
|
else:
|
||||||
|
body[up][schemaName][fieldName] = sys.argv[i]
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
print u'ERROR: %s is not a valid argument for "gam %s user"' % (sys.argv[i], [u'create', u'update'][updateCmd])
|
||||||
|
sys.exit(2)
|
||||||
if need_password:
|
if need_password:
|
||||||
body[u'password'] = u''.join(random.sample(u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~`!@#$%^&*()-=_+:;"\'{}[]\\|', 25))
|
body[u'password'] = u''.join(random.sample(u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~`!@#$%^&*()-=_+:;"\'{}[]\\|', 25))
|
||||||
if u'password' in body and need_to_hash_password:
|
if u'password' in body and need_to_hash_password:
|
||||||
@@ -6694,7 +6734,7 @@ def getUserAttributes(i, updateCmd=False):
|
|||||||
|
|
||||||
def doCreateUser():
|
def doCreateUser():
|
||||||
cd = buildGAPIObject(u'directory')
|
cd = buildGAPIObject(u'directory')
|
||||||
body, admin_body = getUserAttributes(3, updateCmd=False)
|
body, admin_body = getUserAttributes(3, cd, updateCmd=False)
|
||||||
print u"Creating account for %s" % body[u'primaryEmail']
|
print u"Creating account for %s" % body[u'primaryEmail']
|
||||||
callGAPI(cd.users(), u'insert', body=body, fields=u'primaryEmail')
|
callGAPI(cd.users(), u'insert', body=body, fields=u'primaryEmail')
|
||||||
if admin_body:
|
if admin_body:
|
||||||
@@ -6831,7 +6871,7 @@ def doCreateResourceCalendar():
|
|||||||
|
|
||||||
def doUpdateUser(users, i):
|
def doUpdateUser(users, i):
|
||||||
cd = buildGAPIObject(u'directory')
|
cd = buildGAPIObject(u'directory')
|
||||||
body, admin_body = getUserAttributes(i, updateCmd=True)
|
body, admin_body = getUserAttributes(i, cd, updateCmd=True)
|
||||||
for user in users:
|
for user in users:
|
||||||
if user[:4].lower() == u'uid:':
|
if user[:4].lower() == u'uid:':
|
||||||
user = user[4:]
|
user = user[4:]
|
||||||
@@ -7507,7 +7547,10 @@ def doGetUserInfo(user_email=None):
|
|||||||
if isinstance(user[u'customSchemas'][schema][field], list):
|
if isinstance(user[u'customSchemas'][schema][field], list):
|
||||||
print u' %s:' % field
|
print u' %s:' % field
|
||||||
for an_item in user[u'customSchemas'][schema][field]:
|
for an_item in user[u'customSchemas'][schema][field]:
|
||||||
print convertUTF8(u' %s' % an_item[u'value'])
|
print convertUTF8(u' type: %s' % (an_item[u'type']))
|
||||||
|
if an_item[u'type'] == u'custom':
|
||||||
|
print convertUTF8(u' customType: %s' % (an_item[u'customType']))
|
||||||
|
print convertUTF8(u' value: %s' % (an_item[u'value']))
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u' %s: %s' % (field, user[u'customSchemas'][schema][field]))
|
print convertUTF8(u' %s: %s' % (field, user[u'customSchemas'][schema][field]))
|
||||||
print
|
print
|
||||||
|
|||||||
Reference in New Issue
Block a user