User Service
Wrapper for the '/user' API. Contains most of the user operations except profile functions, as profile service is disabled when Management API is used.
NB! If user is assigned to a group name (memberOf) that doesn't exist, then the new group will be created.
List of all options:
- Get all users
- Get one/create/modify/delete user
- Verify email/phone with code or link (primary ones)
- Get/update/delete custom user fields (dictionary with any keys/values)
- Get/update user enterprise
- Get password requirements (min and max password length)
- Get user's legal info
- Migrate user to other namespace
- Change password
- Get/update custom permission
- Get/update builtin and custom roles
- Get/report strong identification
- Get/update student info
User management
from trivoreid.utils.criteria import Filter
from trivoreid.models.email import EmailAddress
from trivoreid.models.user import (User,
Names,
Mobile,
Address)
# get list of users
users = api.user_service.get_all(Filter(Filter.EQUAL, 'nsCode', 'examplecode')).resources
# create new user object
user_to_create = User({
'nsCode' : 'testsdk',
'emails' : ['[email protected]'],
'memberOf' : ['gr003', 'gr004'] # if groups with user's nsCode and member
# names don't exist, they will be created
})
user_names = Names({
'givenName' : 'First Name',
'middleName' : 'Middle Name',
'familyName' : 'Last Name'})
user_addresses = [Address({
'addressName' : 'address',
'name' : 'home',
'country' : 'FI',
'locality' : 'fi',
'postalCode' : '20750',
'region' : 'Region',
'streetAddress' : 'Street Address 5C'})]
email1 = EmailAddress({'address' : '[email protected]'})
email2 = EmailAddress({'address' : '[email protected]',
'tags' : ['tag1', 'tag2'], 'name' : 'work'})
user_emails = [email1, email2]
mobile1 = Mobile({'number' : '+358402592078'})
mobile2 = Mobile({'number' : '+358402592077',
'tags' : ['tag1', 'tag2'], 'name' : 'work'})
user_mobiles = [mobile1, mobile2]
user_to_create.name = user_names
user_to_create.addresses = user_addresses
user_to_create.emails = user_emails
user_to_create.mobiles = user_mobiles
# user service returns new user object and a randomly generated password with
# the defined length (15 by default).
new_user = api.user_service.create(user_to_create, password_length=30)[0]
user = api.user_service.get(new_user.user_fields['id'])
print('Password : {}'.format(new_user.serialize()['password']))
print('\n User fields \n {}'.format(user.serialize()))
# deleting user account
api.user_service.delete(user.user_fields['id'])
# creating multiple users
user_list = []
for _ in range(5):
user_list.append(User({'nsCode' : 'testsdk'}))
# result is the list of created users
result = api.user_service.create(user_list)
Output
INFO:root:Found 11 users
Password : X0TYztjfszKCBVSbBG5krCCfMRnaEy
User fields
{'id': 'exampleId',
'username': '57334055',
'userAccountType': 'PERSON',
'nsCode': 'testsdk',
'emails': [{'address': '[email protected]', 'verified': False},
{'address': '[email protected]', 'verified': False}],
'mobiles': [{'number': '+35840123456', 'verified': False},
{'number': '+35840123457', 'verified': False}],
'email': '[email protected]',
'name': {'givenName': 'First Name',
'middleName': 'Middle Name',
'familyName': 'Last Name'},
'memberOf': [],
'addresses': [{'addressName': 'address',
'name': 'home',
'country': 'Finland',
'locality': 'fi',
'postalCode': '20750',
'region': 'Region',
'streetAddress': 'Street Address 5C'}]}
INFO:root:Successfully deleted user with id exampleId
INFO:root:Successfully created user with id exampleId1
INFO:root:Successfully created user with id exampleId2
INFO:root:Successfully created user with id exampleId3
INFO:root:Successfully created user with id exampleId4
INFO:root:Successfully created user with id exampleId5
Email / Mobile verification
userId = new_user.id
# send verification link to the user e-mail address
api.user_service.verify_email(userId, expiration_days=1)
# send verification code to the mobile
api.user_service.verify_sms_code(userId)
# verify mobile
api.user_service.verify_sms_code_check(userId, '1234')
#send verification link to the user mobile number
api.user_service.verify_sms_link(userId)
Custom user fields
custom_fields = {'test_field1' : 'test value 1',
'test_field2' : 'test value 2'}
fields = api.user_service.get_custom_fields(userId)
print('\nInitial fields : {}'.format(fields))
api.user_service.update_custom_fields(userId, custom_fields)
fields = api.user_service.get_custom_fields(userId)
print('\nFields after update : {}'.format(fields))
modified_field = {'test_field1' : 'modified value 1'}
api.user_service.update_custom_fields(userId, modified_field)
fields = api.user_service.get_custom_fields(userId)
print('\nFields after modifying one field : {}'.format(fields))
api.user_service.delete_custom_fields(userId)
fields = api.user_service.get_custom_fields(userId)
print('\nFields after delete : {}'.format(fields))
Output
Found custom fields of the user with id exampleId
Initial fields : b'{ }'
INFO:root:Successfully modified custom fields of the user id exampleId
INFO:root:Found custom fields of the user with id exampleId
Fields after update : b'{ "test_field1" : "test value 1", "test_field2" : "test value 2" }'
INFO:root:Successfully modified custom fields of the user id exampleId
INFO:root:Found custom fields of the user with id exampleId
Fields after modifying one field : b'{ "test_field1" : "modified value 1", "test_field2" : "test value 2" }'
INFO:root:Successfully deleted custom fields of the user with id exampleId
INFO:root:Found custom fields of the user with id exampleId
Fields after delete : b'{ }'
Enterprise and password requirements
from trivoreid.models.enterprise import Enterprise
userId = 'exampleId'
enterprise = api.user_service.get_enterprise(userId)
print('\nEnterprise fields : {} \n'.format(e.serialize()))
enterprise.businessId = 'test business ID'
enterprise.vatId = 'test vat ID'
enterprise.tradeName = 'tradeName'
enterprise.domicile = 'test domicicle'
api.user_service.update_enterprise(userId, enterprise)
enterprise2 = api.user_service.get_enterprise(userId)
print('\nEnterprise fields after modifying : {} \n'.format(enterprise2.serialize()))
pr = api.user_service.get_password_requirements(userId)
print('\nPassword requirements : {} \n'.format(pr.serialize()))
Output
INFO:root:Found enterprise from user with id exampleId
Enterprise fields : {'businessId': None,
'vatId': None,
'tradeName': None,
'domicile': None,
'leiCode': None,
'parallelTradeNames': None,
'auxiliaryTradeNames': None,
'tradeRegisterNumber': None,
'taxIdentificationNumber': None,
'general': {'phone': None, 'email': None, 'website': None, 'contacts': None},
'sales': {'phone': None, 'email': None, 'website': None, 'contacts': None},
'service': {'phone': None, 'email': None, 'website': None, 'contacts': None},
'customerSupport': {'phone': None,
'email': None,
'website': None,
'contacts': None}}
INFO:root:Successfully modified enterprise with the user id exampleId
INFO:root:Found enterprise from user with id exampleId
Enterprise fields after modifying : {'businessId': 'test business ID',
'vatId': 'test vat ID',
'tradeName': 'tradeName',
'domicile': 'test domicicle',
'leiCode': None,
'parallelTradeNames': [],
'auxiliaryTradeNames': [],
'tradeRegisterNumber': None,
'taxIdentificationNumber': None,
'general': {'phone': None, 'email': None, 'website': None, 'contacts': None},
'sales': {'phone': None, 'email': None, 'website': None, 'contacts': None},
'service': {'phone': None, 'email': None, 'website': None, 'contacts': None},
'customerSupport': {'phone': None,
'email': None,
'website': None,
'contacts': None}}
INFO:root:Found password requirements of the user with id exampleId
Password requirements : {'minLength': 9, 'maxLength': 360}
Email / Mobile tags
user = api.user_service.get('userID')
number=user.mobiles[0].number
address=user.emails[0].address
# Modify email and mobile tags
user.mobiles[0].tags.extend(['tag1', 'tag2'])
user.emails[0].tags.append('tag1')
User's Consents
from trivoreid.models.user import Consents
user = api.user_servise.get('userID')
# modify user consents
user.consents.marketingPost = True
user.consents.marketingOther = True
user.consents.profiling = True
api.user_service.update(user)
Migrate namespace
Warning: Original user will be deleted and another will be created in the target namespace. Not all information will be migrated!
from trivoreid.models.user import NamespaceMigrationOptions
user = api.user_servise.get('userID')
# migrate user to the exampleCode namespace
options = NamespaceMigrationOptions()
options.keepMobiles = False
options.targetNsCode = 'exampleCode'
#
user = api.user_servise.migrate_namespace(user.id, options)
Legal info
user = api.user_servise.get('userID')
# get user's legal info
info = api.user_servise.get_legal_info(user.id)
Student state
from trivoreid.models.user import StudentState, StudentStatus
user = api.user_servise.get('userID')
# get user's legal info
state = api.user_service.get_student_state(user.id)
# update student state of the user
state.state = StudentStatus.FULL_TIME
state = api.user_service.update_student_state(user.id, state)