Skip to content

Commit 0609c0e

Browse files
add_user supports custom attributes (#301)
* add user supports custom attributes * small fix * fail early
1 parent 25c9a43 commit 0609c0e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

duo_client/admin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ def get_users_by_ids(self, user_ids):
843843
def add_user(self, username, realname=None, status=None,
844844
notes=None, email=None, firstname=None, lastname=None,
845845
alias1=None, alias2=None, alias3=None, alias4=None,
846-
aliases=None):
846+
aliases=None, custom_attribute_map=None):
847847
"""
848848
Adds a user.
849849
@@ -856,6 +856,9 @@ def add_user(self, username, realname=None, status=None,
856856
lastname - User's surname for ID Proofing (optional)
857857
alias1..alias4 - Aliases for the user's primary username (optional)
858858
aliases - Aliases for the user's primary username (optional)
859+
custom_attribute_map - Map of custom attributes (optional). When provided this will be of type Dict[str|str]. e.g.
860+
{"attribute_name":"attribute_value"}
861+
Note: the custom attribute names have to be created prior to adding users
859862
860863
Returns newly created user object.
861864
@@ -886,6 +889,10 @@ def add_user(self, username, realname=None, status=None,
886889
params['alias4'] = alias4
887890
if aliases is not None:
888891
params['aliases'] = aliases
892+
if custom_attribute_map is not None:
893+
for key in custom_attribute_map:
894+
params[f'custom_attributes.{key}'] = custom_attribute_map[key]
895+
889896
response = self.json_api_call('POST',
890897
'/admin/v1/users',
891898
params)

examples/Admin/create_user_and_phone.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22
import pprint
33
import sys
4+
import json
45

56
import duo_client
67

@@ -20,6 +21,7 @@ def get_next_arg(prompt):
2021

2122
USERNAME = get_next_arg('user login name: ')
2223
REALNAME = get_next_arg('user full name: ')
24+
CUSTOM_ATTRIBUTE_MAP_STR = get_next_arg('custom attribute map (optional): ')
2325

2426
# Refer to http://www.duosecurity.com/docs/adminapi for more
2527
# information about phone types and platforms.
@@ -31,6 +33,7 @@ def get_next_arg(prompt):
3133
user = admin_api.add_user(
3234
username=USERNAME,
3335
realname=REALNAME,
36+
custom_attribute_map=json.loads(CUSTOM_ATTRIBUTE_MAP_STR) if CUSTOM_ATTRIBUTE_MAP_STR else None ,
3437
)
3538
print('Created user:')
3639
pprint.pprint(user)

0 commit comments

Comments
 (0)