Security group

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

Security group

Post by Hi-Volt »

Hi. I need to save into a string at which security group the actual logged user belongs to. For example, if the logged user is an amministrator, the string will contain "Group_01" value. Could you help me?
Thank you!
Best regards.

User avatar
Ron L.
Posts: 214
Joined: Fri Jul 15, 2011 3:21 pm

Re: Security group

Post by Ron L. »

First create a "System Tag" for the "Current User".
add_system_tag.JPG
add_system_tag.JPG (38.06 KiB) Viewed 20955 times
add_system_tag_curr_user.JPG
add_system_tag_curr_user.JPG (83.75 KiB) Viewed 20955 times
Then add some script to copy the current user string to another tag when it's value changes.
system_curr_user_change.JPG
system_curr_user_change.JPG (74.43 KiB) Viewed 20955 times
Best Regards,

Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer

User avatar
Ron L.
Posts: 214
Joined: Fri Jul 15, 2011 3:21 pm

Re: Security group

Post by Ron L. »

I just realized, this only helps you determine the current user, but not the current users group. I don't know of a way to do that except with an if else statement.

Code: Select all

void SystemTagCurrentUser_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
		{
			if (Globals.Tags.SystemTagCurrentUser.Value == "user1")
			{
				Globals.Tags.Tag1.Value = "group1";			
			}
		}
I will do some more research and see if there is a more elegant solution.
Best Regards,

Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer

User avatar
Ron L.
Posts: 214
Joined: Fri Jul 15, 2011 3:21 pm

Re: Security group

Post by Ron L. »

There is a more elegant solution, but it requires some helper functions. You can place this in a script module.

Code: Select all

namespace Neo.ApplicationFramework.Generated
{
	using System.Windows.Forms;
	using System;
	using System.Drawing;
	using Neo.ApplicationFramework.Tools;
	using Neo.ApplicationFramework.Common.Graphics.Logic;
	using Neo.ApplicationFramework.Controls;
	using Neo.ApplicationFramework.Interfaces;
    
	using System.Collections.Generic;
    
	public partial class SecurityFunctions
	{
		
		public string GetUserGroups(string userName)
		{
			IList<ISecurityUser> HMIUsers = Globals.Security.Users;
			int userIndex = GetUserIndex(HMIUsers, userName);
			
			IList<ISecurityGroup> userGroups = HMIUsers[userIndex].SecurityGroups;
			string groupNames = string.Empty;
			foreach(ISecurityGroup securityGroup in userGroups)
			{
				groupNames += string.Format("{0}, ", securityGroup.GroupName);
			}
			groupNames = groupNames.Remove(groupNames.Length-2, 2);
			return groupNames;
		}
		
		private int GetUserIndex(IList<ISecurityUser> users, string userName)
		{
			bool foundUser=false;
			int index = 0;
			while(index < users.Count-1 && !foundUser)
			{
				if(users[index].Username == userName)
				{
					foundUser = true;
				}
				else
				{
					index++;
				}
			}
			return index;
		}
	}
}
With these helper functions, you can then use code similar to this.

Code: Select all

void SystemTagCurrentUser_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
		{
			if(Globals.Tags.SystemTagCurrentUser.Value != string.Empty)
			{
				Globals.Tags.Tag1.Value =
					Globals.SecurityFunctions.GetUserGroups(Globals.Tags.SystemTagCurrentUser.Value);
			}				
		}
Attachments
UserNameAndGroup.zip
(25.69 KiB) Downloaded 1109 times
Best Regards,

Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer

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

Re: Security group

Post by Hi-Volt »

Thank you very much!!!

ajack
Posts: 44
Joined: Wed Feb 22, 2012 12:01 am

Re: Security group

Post by ajack »

Dear Ron,

I guess I found another way to get Security Group:

Code: Select all

using Neo.ApplicationFramework.Tools.Security;
and then call class SecurityGroup

Code: Select all

public SecurityGroup _securityGroup;

Code: Select all

namespace Neo.ApplicationFramework.Generated
{
    using System.Windows.Forms;
    using System;
    using System.Drawing;
    using Neo.ApplicationFramework.Tools;
    using Neo.ApplicationFramework.Common.Graphics.Logic;
    using Neo.ApplicationFramework.Controls;
    using Neo.ApplicationFramework.Interfaces;
	
	using Neo.ApplicationFramework.Tools.Security;
    
    public partial class Screen1
    {
		public SecurityGroup _securityGroup;
		public string temp;
		
		void Button2_Click(System.Object sender, System.EventArgs e)
		{
			temp = _securityGroup.GroupName.ToString();
			Text1.Text = temp;
		}
    }
}
but it turned out that thing did NOT work that way... It got error whenever I click the button to get GroupName. It really confuses me: what's wrong with this code?

Attached in this post is my project.

Best regards,

Phong Duong
Attachments
SecurityGroup.zip
(26.4 KiB) Downloaded 994 times

ajack
Posts: 44
Joined: Wed Feb 22, 2012 12:01 am

Re: Security group

Post by ajack »

Dear Ron,

Moreover, this is what made me confused:
Ron L. wrote: IList<ISecurityUser> HMIUsers = Globals.Security.Users;
int userIndex = GetUserIndex(HMIUsers, userName);

IList<ISecurityGroup> userGroups = HMIUsers[userIndex].SecurityGroups;
Actually, this security group pop up in my head in the first time, so I did some search in Script help and user manual. How could you find out that Globals.Security.Users object is an array?

Thanks and best regards,

Phong Duong

ajack
Posts: 44
Joined: Wed Feb 22, 2012 12:01 am

Re: Security group

Post by ajack »

ajack wrote:Dear Ron,

Moreover, this is what made me confused:
Ron L. wrote: IList<ISecurityUser> HMIUsers = Globals.Security.Users;
int userIndex = GetUserIndex(HMIUsers, userName);

IList<ISecurityGroup> userGroups = HMIUsers[userIndex].SecurityGroups;
Actually, this security group pop up in my head in the first time, so I did some search in Script help and user manual. How could you find out that Globals.Security.Users object is an array?
So silly me, I did found out about Globals.Security.User. Actually, that class implement IList interface, so it must be a collection of data.

Thanks!

Phong Duong

ajack
Posts: 44
Joined: Wed Feb 22, 2012 12:01 am

Re: Security group

Post by ajack »

ajack wrote: I guess I found another way to get Security Group:

Code: Select all

using Neo.ApplicationFramework.Tools.Security;
and then call class SecurityGroup

Code: Select all

public SecurityGroup _securityGroup;
Dear Ron,

I was so silly, SecurityGroup and SecurityUser is just a class, I need to call an instance, which has the Group and User properties of current user. But I just don't know which instance has those properties...

Thanks,

Phong Duong

Post Reply