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.
Security group
Re: Security group
First create a "System Tag" for the "Current User".
Then add some script to copy the current user string to another tag when it's value changes.
Then add some script to copy the current user string to another tag when it's value changes.
Best Regards,
Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer
Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer
Re: Security group
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.
I will do some more research and see if there is a more elegant solution.
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";
}
}
Best Regards,
Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer
Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer
Re: Security group
There is a more elegant solution, but it requires some helper functions. You can place this in a script module.
With these helper functions, you can then use code similar to this.
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;
}
}
}
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
Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer
Re: Security group
Dear Ron,
I guess I found another way to get Security Group:
and then call class SecurityGroup
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
I guess I found another way to get Security Group:
Code: Select all
using Neo.ApplicationFramework.Tools.Security;
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;
}
}
}
Attached in this post is my project.
Best regards,
Phong Duong
- Attachments
-
- SecurityGroup.zip
- (26.4 KiB) Downloaded 994 times
Re: Security group
Dear Ron,
Moreover, this is what made me confused:
Thanks and best regards,
Phong Duong
Moreover, this is what made me confused:
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?Ron L. wrote: IList<ISecurityUser> HMIUsers = Globals.Security.Users;
int userIndex = GetUserIndex(HMIUsers, userName);
IList<ISecurityGroup> userGroups = HMIUsers[userIndex].SecurityGroups;
Thanks and best regards,
Phong Duong
Re: Security group
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.ajack wrote:Dear Ron,
Moreover, this is what made me confused: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?Ron L. wrote: IList<ISecurityUser> HMIUsers = Globals.Security.Users;
int userIndex = GetUserIndex(HMIUsers, userName);
IList<ISecurityGroup> userGroups = HMIUsers[userIndex].SecurityGroups;
Thanks!
Phong Duong
Re: Security group
Dear Ron,ajack wrote: I guess I found another way to get Security Group:and then call class SecurityGroupCode: Select all
using Neo.ApplicationFramework.Tools.Security;
Code: Select all
public SecurityGroup _securityGroup;
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