hub.eb?material_id=556&track_id=547

Logging On and Off


The Security Manager interface contains functions to logon and logoff users.

You can authenticate a user using the system.securitymanager.logon() function that takes two parameters:

  1. webServiceName - specifies which System Logon Service to use.
  2. inParameters - an array of arrays containing up to three pairs of parameters. For example:
    [["Script", fields.email.value], ["Script", fields.password.value]]​​

Each pair contains two strings corresponding to the parameter's source and value. The source can be used to distinguish the origin of the authentication request.

The source and value of the first array item are mapped to PARAM1_SOURCE and PARAM1_VALUE respectively. The source and value of the second and third array items also have corresponding fields.

Should the logon fail for any reason, a LogonException is thrown. The LogonException contains the error code and description returned from the Logon Service.

Further methods are available from the system.securityManager interface that can be used throughout the user's session:

  • getUserName() - returns the value stored in the USERID field for the current user or null when the user is not logged on.
  • isUserLoggedOn() - returns true when a user is logged on.
  • logoff() - logs a user off the system and clears authorizations, credentials and roles from the current session. Future calls to isUserLoggedOn() return false.

Steps


1

Complete the following steps using the Logon Service you created in the Introduction to Logon Services tutorial.

2

Create a form and name it logon. Create two fields called email and password and add these to the page.

3

Add a button to the form to perform a logon event, attach a script to its On Click event and paste the following code:

// try and log the user in
try {
   system.securityManager.logon("myLogonService", [["Script", fields.email.value], ["Script", fields.password.value]]);
}
catch (error) {
   // diplay an error message to the user explaining why the logon failed
   event.owner.addErrorMessage("Something went wrong: " + error.message);
}
4

Create another form called myApp and add a button to log the user off, attach a script to its On Click event and paste the following code:

system.securityManager.logoff();

 

5

Edit the logon script on the logon button so that if the logon call is successful the user is directed to the myApp form.

Paste the following at the bottom of the logon script:

// check the user is logged on
if (system.securityManager.isUserLoggedOn()) {
   // direct the user to the myApp form
   form.gotoForm("myApp");
}
6

Edit the logoff script on the logoff button so the user is directed to the logon form. Paste the following at the bottom of the logoff script:

// check the user is logged off
if (!system.securityManager.isUserLoggedOn()) {
   // direct the user to the logon form
   form.gotoForm("logon");
}
7

Run the logon form and enter valid details and click the logon button and you should be directed to the myApp form.

Current Module

Related