Adding a user (Python)

Adding a user (Python)
 1# This example creates some new users in the database.
 2
 3# Create a local variable for the output toolbox
 4ot = impact.gui.output_toolbox
 5db = impact.active_database
 6
 7# Clear the output toolbox
 8ot.clear()
 9
10# Retrieve the users from the DB
11users = db.users
12
13# get the first site in the DB
14sites = db.sites
15
16if sites.count > 0:
17    site = None
18    try:
19        site = sites.item(1)
20    except Exception as exc:
21        pass
22    impact.gui.output_toolbox.add(f"Failed to create object via sites.item(): {exc}")
23
24# get the first user group from the DB
25mastersettings = db.find_master_tool_settings(ipMasterSettingType.mstUserGroup)
26usergroup = None
27try:
28    usergroup = mastersettings.item(1)
29except Exception as exc:
30    impact.gui.output_toolbox.add(f"Failed to create object via mastersettings.item(): {exc}")
31
32ot.add("UserGroup: " + usergroup.name)
33if not site.isNone():
34    ot.add("Site: " + site.full_name)
35
36publishToEnterprise = False
37
38# IUsers.add is deprecated and IUsers.add2 should be used instead
39newCADUser = None
40try:
41    newCADUser = users.add2("LOGINID1", "full_name", "Password", "email@email.com", ipUserType.utCAD, None, usergroup, publishToEnterprise)
42except Exception as exc:
43    impact.gui.output_toolbox.add(f"Failed to create object via users.add2(): {exc}")
44
45if not newCADUser.isNone():
46    ot.add("Successfully created user LOGINID1")
47else:
48    ot.add("Unable to create user LOGINID1")
49newNServerUser = None
50try:
51    newNServerUser = users.add2("LOGINID2", "full_name", "Password", "email@email.com", ipUserType.utNServer, site, usergroup, publishToEnterprise)
52except Exception as exc:
53    impact.gui.output_toolbox.add(f"Failed to create object via users.add2(): {exc}")
54
55if not newNServerUser.isNone():
56    ot.add("Successfully created user LOGINID2")
57else:
58    ot.add("Unable to create user LOGINID2")