Develop a NetLogic for managing user accounts
This NetLogic contains methods for adding and removing users.
Prerequisites
Set the default external code editor. See Set the default code editor.
- InProject view, right-click theNetLogicfolder and select .
- Hover-over the NetLogic, select , and enterUserManagementScript.
- Double-click the NetLogic.The external code editor opens.
- Edit the NetLogic code:
- Add theAddUsermethod:[ExportMethod] public void AddUser(string name, string password, string localeId) { // Get the current project folder. var currentProject = Project.Current; var securityFolder = currentProject.GetObject("Security"); var usersFolder = securityFolder.GetObject("Users"); // Check the password length. if (password.Length < 1) return; // Creating a new User and set its locale and password. var newUser = InformationModel.MakeObject<User>(name); Session.ChangePassword(name, password, string.Empty); newUser.LocaleIds = new string[] { localeId }; if (usersFolder == null) { Log.Error("Add User", "Missing Users folder"); return; } usersFolder.Children.Add(newUser.NodeId); }TheAddUsermethod takes the following parameters:
- name
- The name of the new user account.
- password
- The password of the new user account.
- localeId
- The locale of the user account. For example,en-USorit-IT
- Add theRemoveUsermethod:[ExportMethod] public void RemoveUser(string name) { // Get the current project folder. var currentProject = Project.Current; var securityFolder = currentProject.GetObject("Security"); var usersFolder = securityFolder.GetObject("Users"); if (usersFolder == null) { Log.Error("Add User", "Missing Users folder"); return; } if (usersFolder.Children.Count <= 0) { Log.Error("Users folder is Empty"); return; } // Remove the User by the name. usersFolder.Children.Remove(name); }TheRemoveUsermethod takes the following parameter:
- name
- The name of the user account to remove.
- Save the code.
Provide Feedback