Assign 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
ParthDesai
Posts: 9
Joined: Fri Jan 15, 2016 4:00 am

Assign Security Group

Post by ParthDesai »

Hello,

On my screen 1 I am having multiple controls like button,textbox etc
What I am trying to do is I want to assign security group to controls present on my screen using script module.

AMitchneck
Posts: 137
Joined: Mon Jun 11, 2012 2:10 pm

Re: Assign Security Group

Post by AMitchneck »

Hi ParthDesai,

What is it exactly you are trying to do? On the screen you can configure controls to be hidden, disabled, etc. based on user access level. Are you trying to control what the user can do from the script side? For instance, if they are low-level user pressing button does A, but if they are high-level user it does B?

If you are looking at the latter case, you can determine if the user is in a specific group using a function like this:

Code: Select all

public bool hasAccess(string reqGroup)
{
	string curUser = Globals.Security.CurrentUser;
	foreach (ISecurityUser user in Globals.Security.Users)
		if (user.Username == curUser)
		{
			foreach (ISecurityGroup sgroup in user.SecurityGroups)
				if (sgroup.GroupName == reqGroup) return true;
			return false;
		}
	return false;
}
Then in your controls script you would have:

Code: Select all

if (hasAccess("Operators"))
{
	...
}
You can also popup a login window:

Code: Select all

Globals.Security.Login();
To log them out you can use:

Code: Select all

Globals.Security.Logout();
Adam M.
Controls Engineer
FlexEnergy

ParthDesai
Posts: 9
Joined: Fri Jan 15, 2016 4:00 am

Re: Assign Security Group

Post by ParthDesai »

Hello,

I have two system.windows.Form.Button control on my third party user control and wanted to give administrator access to one of the button using C#.
So wanted to know how I can give access to one of the button through script or through code in my usercontrol.

AMitchneck
Posts: 137
Joined: Mon Jun 11, 2012 2:10 pm

Re: Assign Security Group

Post by AMitchneck »

OK.

When you say "give administrator access" do you mean log them in as an administrator? To do this you could specify a username and password to the login function like so:

Code: Select all

Globals.Security.Login("userName", "password");
If you want, you can pre-check if the user is already logged in as administrator via the code I supplied in my prior post (also below). Note this requires that the user does not change the password for the account - I don't know if there is a way to determine the new password. (You can just supply the username and require the user to give the password.)

If you just want to make sure the user has Administrator privileges before the button does anything, you could use the following, using the function from my prior post in the button_click routine:

Code: Select all

if (hasAccess("Administrator"))
{
   [restricted code here]
}
Adam M.
Controls Engineer
FlexEnergy

Post Reply