The IsValidUser method determines if the specified user name is in the list of authorized users. Behind the scenes, it simply calls the Item method and returns True if that method returns a valid object.

Syntax

IsValidUser(UserName as String) as Boolean

Parameters
UserName
The name of the user to check.

Return Value
True if the specified user name is in the list of authorized users.

Example
This example, which could be used as the code for the Application.BeforeLogin script for a project, adds all valid users of the target application to the list of users for Stonefield Query (assuming the password is the same as the user name). In the case of the Visual FoxPro code, it assumes there's a table in the target application's directory called USERS containing the valid users of the application. The VBScript and JavaScript code uses the Employee table of the Northwind database instead. Note if the maximum number of licensed users has already been added, Users.AddItem returns a null value, so the script code handles this.

Visual FoxPro

lparameters toApplication as SQApplication
local lcDirectory, lnSelect, lcUserName, loUser as User
lcDirectory = toApplication.TargetApplicationDirectory
lnSelect = select()
select 0
use (lcDirectory + 'USERS')
scan
  lcUserName = trim(USERNAME)
  if not toApplication.Users.IsValidUser(lcUserName)
    loUser = toApplication.Users.AddItem(lcUserName)
    if vartype(loUser) = 'O'
      loUser.Password = lcUserName
    endif
  endif
endscan
use
select (lnSelect)

VBScript

function Main(Application)
dim Conn, RS, User
set Conn = createobject("ADODB.Connection")
set RS = createobject("ADODB.RecordSet")
Conn.ConnectionString = "provider=SQLOLEDB.1;" & _
  "data source=(local);initial catalog=Northwind;"
Conn.Open
RS.ActiveConnection = Conn
RS.Open("select * from employees")
on error resume next
do while not RS.EOF
  UserName = RS.Fields("LastName").Value & _
    RS.Fields("FirstName").Value
  if Application.Users.IsValidUser(UserName) = False then
    set User = Application.Users.AddItem(UserName)
    if Err.Number = 0 then
      User.FirstName = RS.Fields("FirstName").Value
      User.LastName = RS.Fields("LastName").Value
      User.Password = UserName
    end if
    RS.MoveNext
  end if
loop
on error goto 0
RS.Close
Conn.Close
Main = True
end function

JavaScript

function Main(Application) {
var Conn, RS, UserName, User, e ;
Conn = new ActiveXObject('ADODB.Connection') ;
RS = new ActiveXObject('ADODB.RecordSet') ;
Conn.ConnectionString = 'provider=SQLOLEDB.1;' +
  'data source=(local);initial catalog=Northwind;' ;
Conn.Open ;
RS.ActiveConnection = Conn ;
RS.Open('select * from employees') ;
do {
  UserName = RS.Fields('LastName').Value +
    RS.Fields('FirstName').Value ;
  if (Application.Users.IsValidUser(UserName) = false) {
    try {
      User = Application.Users.AddItem(UserName) ;
      User.FirstName = RS.Fields('FirstName').Value ;
      User.LastName = RS.Fields('LastName').Value ;
      User.Password = UserName ;
      }
    catch(e) { }
    }
    RS.MoveNext ;
  }
}
while (! RS.EOF) ;
RS.Close ;
Conn.Close ;
return true ;
}

C#

public static bool Application_BeforeLogin(SFQApplication sfqApplication)
{	
  string userName = String.Empty;
  Database database = SQApplication.DataEngine.Databases.GetMainDatabase();

  string resultSet = database.ExecuteSQLStatement("select firstname, " + 
    "lastname from employees");
  XmlDocument doc = new XmlDocument();
  doc.LoadXml(resultSet);
  string nodePath = "/DataSet/_resultset";
  XmlNode node = doc.SelectSingleNode(nodePath);

  if (node != null)
  {
    do
    {
      userName = node.Attributes[0].Value + " " + node.Attributes[1].Value;
      node = node.NextSibling;
      if (!SQApplication.Users.IsValidUser(userName)) 
      {
        try 
        {
          User user = SQApplication.Users.AddItem(userName.Substring(0, 10));
          user.FirstName = node.Attributes[0].Value ;
          user.LastName = node.Attributes[1].Value ;
          user.Password = userName.Substring(0, 10);
        }
        catch(Exception e) 
        { 
        }
      }
    }while (node != null);
  }
  return true;
}

VB.NET

public shared function Application_BeforeLogin(sfqApplication as SFQApplication) as Boolean

  Dim userName As String = String.Empty
  Dim database As Database = SQApplication.DataEngine.Databases.GetMainDatabase()
  Dim resultSet As String = database.ExecuteSQLStatement("select firstname, " +
    "lastname from employees")
  Dim doc As XmlDocument = New XmlDocument()
  doc.LoadXml(resultSet)
  Dim nodePath As String = "/DataSet/_resultset"
  Dim node As XmlNode = doc.SelectSingleNode(nodePath)

  If Not node Is Nothing Then
    Do
      userName = node.Attributes(0).Value + " " + node.Attributes(1).Value
      node = node.NextSibling
      If Not SQApplication.Users.IsValidUser(userName) Then
        Try
          Dim newUser as User = SQApplication.Users.AddItem(userName.Substring(0, 10))
          newUser.FirstName = node.Attributes(0).Value
          newUser.LastName = node.Attributes(1).Value
          newUser.Password = userName.Substring(0, 9)
        Catch e As Exception
        End Try
      End If
    Loop While Not node Is Nothing
  End If
  Return True
End Function

See also

Users Collection

© Stonefield Software Inc., 2024 • Updated: 06/06/16
Comment or report problem with topic