Using your own user table in WebApps

From DataFlex Wiki
Jump to navigationJump to search

In most cases when creating a WebApp for an existing (Windows) system, that system will already have a mechanism for checking user credentials (i.e. user-name and password) in existance. The Data Access supplied mechanism for WebApps uses its own tables for this and for session management within that. So how do we integrate the two?

Actually it is reasonably simple, but I'm going to go through it line-by-line, so it will seem like a lot, but it really isn't.

The instructions below assume that your system has a user table; that it has a userID column of some sort; that column is the primary (unique) key; and that column is the sole element of Index.1 on that table. (If that last is not the case, you will have to also modify the "Send Find of hoUserDD EQ Index.1" line in the UserLogin procedure in your sub-class to use the correct index.)

Text in {italics} should be replaced with the appropriate values for your own system.

Step 1. If you have not already done it, Create a WebApp Project in your workspace.

Step 2. From {DataFlexInstalledLocation}\Pkg:

  1. Copy cWebSessionManagerStandard.pkg (note: not cWebSessionManager.pkg) to your AppSrc directory, renaming it to cWebSessionManager{YourSystemName}.pkg
  2. Copy cWebAppUserDataDictionary.pkg and cWebAppSessionDataDictionary.pkg to your DDSrc directory (the former will not actually be used, but will be modified by the next step of the process, so best to copy it too)

Step 3. Change the relationship of WebAppSession to WebAppUser to point to your user table instead:

  1. The relationship needs to be on identically defined columns (same type and size). I don't think that we ought to impact the existing system when that can be avoided, so if a change is needed (and it usually will be) the change should be at the WebAppSession table end. Modify (in the Studio, right-click WebAppSession in the Table Explorer pane/tab, select "Edit Table" and make the required change to the LoginName column) that so that it matches the (unique) primary key (the user ID) of your current user table. (So if your current user table has a column - say, UserID - which is ASCII 10, modify WebAppSession.LoginName to be ASCII 10 from its original ASCII 20.)
  2. In the "Relationships" tab, delete the relationship to WebAppUser (the only one there) and add a replacement pointing to your user table, from LoginName on WebAppSession, to whatever the user ID column is on your user table.

Step 4. Modify your cWebSessionManager sub-class:

  1. Change the use statement:
    "Use cWebSessionManager.pkg"
    "Use cWebSessionManagerStandard.pkg"
  2. Replace the use statement:
    "Use cWebAppUserDataDictionary.dd"
    "Use c{YourUserTable}DataDictionary.dd"
  3. Change the classname:
    "Class cWebSessionManagerStandard is a cWebSessionManager"
    "Class cWebSessionManager{YourSystemName} is a cWebSessionManagerStandard" (so sub-classing cWebSessionManagerStandard)
  4. Change the user data dictionary instance: in the Construct_Object procedure
    "Get Create (RefClass(cWebAppUserDataDictionary)) to hoUserDD"
    "Get Create (RefClass(c{YourUserTable}DataDictionary)) to hoUserDD"
  5. Modify the UserLogin function:
    1. Replace the line:
      "Move sLoginName to WebAppUser.LoginName"
      "Move sLoginName to {YourUserTable}.{YourIdCol}"
    2. Replace the line:
      "If (Found and (Lowercase(sLoginName) = Lowercase(Trim(WebAppUser.LoginName)))) Begin"
      "If (Found and (Lowercase(sLoginName) = Lowercase(Trim({YourUserTable}.{YourIdCol})))) Begin"
    3. Replace the line:
      "Get Field_Current_Value of hoUserDD Field WebAppUser.Password to sUserPassword"
      "Get Field_Current_Value of hoUserDD Field {YourUserTable}.{YourPasswordCol} to sUserPassword"
    4. Optional. Replace the line:
      "Set Field_Changed_Value of hoUserDD Field WebAppUser.LastLogin to (CurrentDateTime())"
      "Set Field_Changed_Value of hoUserDD Field {YourUserTable}.{YourLastLoginCol} to (CurrentDateTime())" // This one is optional because you may not have such a column (a date, by default) and if you do not wish to create one just remove the line.
  6. Modify the UpdateSessionProperties function:
    1. Optional - otherwise just remove the line. Replace the line:
      "Set psUsername to (Trim(WebAppUser.FullName))"
      "Set psUsername to (Trim({YourUserTable}.{YourUserFullname}))"
    2. Replace the line:
      "Set psLoginName to (Trim(WebAppUser.LoginName))"
      "Set psLoginName to (Trim({YourUserTable}.{YourIdColumn}))"
    3. Optional - if you don't have a rights column, remove the line. Replace the line:
      "Set piUserRights to WebAppUser.Rights"
      "Set piUserRights to {YourUserTable}.{YourRightsColumn}" // Note: if you use an ASCII column, rather than a numeric one for this, you will need to add a different property in the Construct_Object procedure of the class - something like "Property String psUserType" and "Set" that instead
  7. Remove all the other methods from your sub-class (they are covered in its super-class cWebSessionManagerStandard):
    1. CreateSession
    2. ValidateSession
    3. IsLoggedIn
    4. ComparePasswords
    5. OnSessionPropertiesSet
    6. OnSessionPropertiesClear
    7. EndSession

Step 5. Modify SessionManager.wo to use your sub-class:

  1. Change:
    "Use cWebSessionManagerStandard.pkg"
    "Use cWebSessionManager{YourSystemName}.pkg"
  2. Change:
    "Object oSessionManager is a cWebSessionManagerStandard"
    "Object oSessionManager is a cWebSessionManager{YourSystemName}"

Now you should be good to go and users of your Windows app will/can also be users of your WebApp!