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' : ['example1@trivore.com'], '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' : 'example1@trivore.com'}) email2 = EmailAddress({'address' : 'example2@trivore.com', '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)
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))
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()))
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('userID')
Student state
Under development..
User Service Models
/wiki/spaces/TISpubdoc/pages/20515245
/wiki/spaces/TISpubdoc/pages/20515276
/wiki/spaces/TISpubdoc/pages/20515250
/wiki/spaces/TISpubdoc/pages/20515251
/wiki/spaces/TISpubdoc/pages/20515255
/wiki/spaces/TISpubdoc/pages/20515262
/wiki/spaces/TISpubdoc/pages/20515256
/wiki/spaces/TISpubdoc/pages/20515252
/wiki/spaces/TISpubdoc/pages/75825224