Page 1 of 1

Security group

Posted: Wed Oct 19, 2011 5:39 am
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.

Re: Security group

Posted: Wed Oct 19, 2011 10:08 am
by Ron L.
First create a "System Tag" for the "Current User".
add_system_tag.JPG
add_system_tag.JPG (38.06 KiB) Viewed 20962 times
add_system_tag_curr_user.JPG
add_system_tag_curr_user.JPG (83.75 KiB) Viewed 20962 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 20962 times

Re: Security group

Posted: Wed Oct 19, 2011 10:18 am
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.

Re: Security group

Posted: Thu Oct 20, 2011 9:18 am
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);
			}				
		}

Re: Security group

Posted: Fri Oct 21, 2011 1:23 am
by Hi-Volt
Thank you very much!!!

Re: Security group

Posted: Mon Mar 05, 2012 9:06 pm
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

Re: Security group

Posted: Tue Mar 06, 2012 11:00 am
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

Re: Security group

Posted: Wed Mar 07, 2012 7:47 pm
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

Re: Security group

Posted: Thu Mar 08, 2012 3:28 am
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