How Exactly Do You Use security.login()?

Hello,

I would like to use a custom login form, and it is my understanding that I can use the security.login() to help me do this, but after looking at the sample code for the function, I’m still confused as to the arguments I need to pass.

In my case I have a login form with two global fields on it (login_username and login_password). According to the function, I need to pass three arguments:

  1. username - that would be globals.login_username
  2. user_uid - do I need to use getUserId(globals.login_username)?
  3. groups - do I need to use getUserGroups(globals.login_username)?

I can’t figure out where to specify the password. Do I need to use checkPassword() before calling security.login()? I’m really new to Servoy security, so please forgive my ignorance.

I think the following code works OK:

var username   = globals.login_username;
var passwd     = globals.login_password;

var userID     = security.getUserId(username);
var ok         = security.checkPassword(userID, passwd);
var userGroups = null;

if (!ok)
{
	application.beep();
	plugins.dialogs.showWarningDialog("Red Alert",  "Your login is incorrect.", "OK");
}
else
{
	userGroups = security.getUserGroups(userID); // returns dataset (group id, group name)
	security.login(username, userID, userGroups.getColumnAsArray(2)); // "officially" logging in
	plugins.dialogs.showInfoDialog("Woohooooooo!",  "Your login is CORRECT!", "OK");
	
	// do startup stuff here
}

The code in this thread gave me a head start:
http://forum.servoy.com/viewtopic.php?t=2148

The login method must be used in combination with loginForm property, Servoy will prevent that this form is left before login is called successfully.

The arguments to pass to the login method are:

  1. username, is for display/autoentervalue reasons only
  2. an string ID, Servoy accepts anything here (can be LDAP of ActiveDirectory service ID, or Servoy user_uid)
  3. the Servoy user group names the user is part of. (which defines the rights the user will have after login)

So when using LDAP for example (via a plugin) you need to retrieve the groups info from your LDAP.

It is also possible to use this function with Servoy its own security as you correctly suggested.

Hi Jan,

Thanks for the additional explanation on how security.login() works.

The login method must be used in combination with loginForm property, Servoy will prevent that this form is left before login is called successfully.

Are you referring to the Login Form solution setting? In my solution I currently have that property set to “none”, and the First Form set to my login form. Are you saying I should also set the Login Form property to my login form? Now that I think about it some more, I suppose you could bypass my login form simply by using the Window menu, unless the Login Form property was enabled?

Login Form solution setting yes,correct,true.

I must be doing something wrong. Despite having my custom login form selected as both the Login Form and the First Form under Solution Settings, I am still able to access the other forms in the CRM demo using the Window menu. Any ideas?

I am not sure but I can’t imagine this is correct:

Despite having my custom login form selected as both the Login Form and the First Form

.

I don’t use this yet but will when the ldap plugin is ready. From what I ‘know’ about this function I can imagine that you should not use the same form for both login and as first form…

Hi Marcel,

Initially I only had my login form selected as the First Form, but then Jan Blok mentioned that you could possibly leave my login form before “officially” logging in to Servoy, which is why I then also selected my login form as Login Form.

However, that has no effect, so there currently is a security hole in my setup. I haven’t used your menu plugin, but perhaps I can avoid this security hole by not having a Window menu in the first place?

I don’t understand completely what you are doing I guess. But… Shouldn’t you use the login form attached at login and not as first form.

after loginForm you will pass on to the firstForm, making those the same form will lead to weird behavior in all cases.

Hi Marcel,

I don’t completely understand what I’m doing either LOL. I’m just trying to modify the CRM demo so that you have to log in first using a custom login form. I think what initially confused me is that Servoy Developer always goes to the First Form, regardless if there’s a Login Form selected. Is this correct? Anyway, the Login Form and First Form are now different.

Jan Blok said:

after loginForm you will pass on to the firstForm, making those the same form will lead to weird behavior in all cases.

That’s what I thought, too. Does the onOpen method run before or after Login Form?

A couple of other questions:

  1. If I’m on my login form and then try and switch to the Orders form via the Window menu, should I get an error? Currently I can still switch without getting an error message.

  2. There are a few duplicates when selecting an onOpen method (like “solution_startup”). Is this normal?

Sean, you should consider the login form as a 100% replacement of the default login dialog. When you picture that the onOpen method will run AFTER the login, it will/should also NOT allow you to navigate to another form ‘manually’ without really being logged in e.g. getting past the login check…

Hope this helps

Hi Marcel,

When you picture that the onOpen method will run AFTER the login, it will/should also NOT allow you to navigate to another form ‘manually’ without really being logged in e.g. getting past the login check…

Right, so because I can navigate to another form manually before actually logging in, does that mean I found a bug, or is there something wrong on my end?

Right, so because I can navigate to another form manually before actually logging in, does that mean I found a bug, or is there something wrong on my end?

What answer do you want :) I really can’t tell. Based on the information you supplied I tend to think you are doing something wrong but that is just guessing…

I suggest that you create a small and simple sample solution. When you than still encounter the issue you upload the sample solution to the forum so somebody can check what you did…

Hi Marcel,

Here’s a sample solution, based on the example_data DB server. I really must be doing something wrong. I’ve set the Login Form property to my custom login form, and the First Form to the orders form, but when I open the solution, I go directly to the First Form.

login_test.servoy.zip (3.85 KB)

:)

How about adding a user and set ‘solution login required’.

Will work guaranteed…

Hi Marcel,

I didn’t export any users from my sample solution. I certainly could use the built-in login form, but I was trying to avoid that. Were you able to confirm if there was a problem? If so, then I’ll file a bug report with Servoy support.

No, that is not what I meant!

Did you try it?

Because when you do that you will see that everything works like expected with your login form and not the default one…

::scratching head:: Hmmmm…I did try it. That’s why I uploaded the sample solution in the first place. Did you import the solution into Servoy Developer or Servoy Server?

Developer, but try again. Before that set the property to show in menu unchecked as well as the default navigator. That is at least what I did… Really it works!