Adding user via script

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
Hi-Volt
Posts: 29
Joined: Mon Sep 12, 2011 4:52 am

Adding user via script

Post by Hi-Volt »

Good morning. I'm trying to add a user to the panel via script. I have written the following code but it doesn't work fine:

SecurityUser su =new SecurityUser();
su.Username="Username";
su.Password="Password";
SecurityGroups sg=SecurityGroups.None;
su.Groups=sg;
Globals.Security.Users.Add(su);

If I execute the code above and then I try to login (with the new user), the panel auto-restarts the project. Else, if I execute the code above, I open & close the Users Dialog (where the new user is present) and in the end I try to login, it all works.

Could you help me please?
Thank you!

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: Adding user via script

Post by mark.monroe »

Take a look at the attached project. It uses a script to add users to the system.

Code: Select all

namespace Neo.ApplicationFramework.Generated
{
    using System.Windows.Forms;
    using System;
    using System.Drawing;
    using Neo.ApplicationFramework.Tools;
	using Neo.ApplicationFramework.Tools.Security;
    using Neo.ApplicationFramework.Common.Graphics.Logic;
    using Neo.ApplicationFramework.Controls;
    using Neo.ApplicationFramework.Interfaces;
    
	using Neo.ApplicationFramework.Common.Service;
    
    public partial class AddUsersFromTags
    {
		public static void AddUser(string username, string password)
		{
			//We will add all new users to the GROUP for lack of anything
			//better to do.
			const string GROUP = "Operators";
			
			ISecurityManager securityManager = Globals.Security as ISecurityManager;
			ISecurityUser newUser = new SecurityUser();
			PasswordHasherCF hasher = new PasswordHasherCF();
			
			//Create the new users here
			newUser.Username = username;
			newUser.Password = password;
			
			//The ISecurityUser will not create the password hash for us, so we need to do it
			newUser.PasswordHash = hasher.GetPasswordHash(password);
			
			//Because the group is a enum, and we only know the name of the group
			//we want our new user to be in, we need to cycle through the
			//security groups and find out what number is associated with the 
			//security group we want.
			foreach(ISecurityGroup aGroup in securityManager.Groups )
			{
				if(aGroup.GroupName == GROUP)
				{
					newUser.AddToGroup(aGroup.Group);
					Globals.Tags.Tag1.Value = aGroup.GroupName;
					break;
				}
			}
			
			//All done, lets add the new user to the HMI's security system
			securityManager.AddUser(newUser);
			
			//Now save the new security file. If you do not do this, then the above changes will
			//be lost on the next HMI power cycle!
			ISecurityServiceCF securityServiceCF = ServiceContainerCF.GetService<ISecurityServiceCF>();
			securityServiceCF.Save(securityManager.FilePath);
		}
    }
}
Attachments
BenSecurity_A7.zip
(28.77 KiB) Downloaded 1321 times
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

Hi-Volt
Posts: 29
Joined: Mon Sep 12, 2011 4:52 am

Re: Adding user via script

Post by Hi-Volt »

Thank you very much!
Best regards

wlederer
Posts: 175
Joined: Fri Jan 27, 2012 4:20 am

Re: Adding user via script

Post by wlederer »

Works fine. But, how can be user deleted via script?

Post Reply