Add Users and Roles in Event Script

Hi There,

I am looking to do something relatively simple. I want to add an event script that will Add a new user, Add a new role and assign roles to that new user. For example when an manager clicks a button a new user and role is added for him to manipulate in the User Management page.

Is there a way of doing this in Ignition?

Kind Regards,
Adnan

Hi Adnan,

You can do this if you set up your User Source with a type of Database, meaning that the user and role details are stored in a SQL database. You can then create a script to write directly to the correct tables: user details go in the auth_users table, roles in the auth_roles table and users are mapped to roles in the auth_user_rl table.

The easiest thing to do is create a User Source with a type of Database and add a dummy user before looking at the tables to see how those details are stored. Note that the user’s password is stored as an MD5 hash. Creating a user in MySQL would look something like INSERT INTO auth_users SET username='John', passwd=MD5('password'), fname='John', lname='Smith', schedule='Always', language='en';

Any idea how to do this with SQL Server? I’ve tried using HASHBYTES(‘MD5’, ‘password’). It generates a hash, but SELECTing it returns a mishmash of chinese/thai characters and other random symbols. Comparing it to a password hash generated by modifying a password within the gateway yields completely different formats (top password is generated from gateway):
image

The SQL HASHBYTES passwords don’t work in Ignition.

There doesn’t seem to be any info whatsoever in the Ignition user manual for how passwords are stored :frowning:

Try HASHBYTES('SHA2_256', 'password') - although the character encoding might just be a problem with character encoding (on insert or retrieval) in that DB column/table.

Nope, using SHA2_256 encryption didn’t work either. I’ve tried all of the others as well (MD2, MD4, MD5, SHA, SHA1, SHA2_256, SHA2_512)

Looks like the auto DB user source does a SHA-1 encode before actually submitting the values into the database, and uses UTF-8 encoding to read the incoming string, then stores the bytes as B64 encoded data - I think that’s the step you’re missing.

I thought it looked like B64 encoding. I’ll add that onto it and see if it works, cheers

Ok, so i’ve tried going around SQL Server and instead using a few online encoders to:
1. encode password to SHA1 (www.sha1-online.com)
Test Password: 4081
Result hash: aab10d229a1280f85e8176ee16023d33280e7ab7
2. encode hash result to Base64 (www.base64encode.org)
Result hash: YWFiMTBkMjI5YTEyODBmODVlODE3NmVlMTYwMjNkMzMyODBlN2FiNw==

then pasted that into the user’s passwd field in the table, but still can’t login. Any ideas?

The way that I ended up doing this so that I could bulk create users is by using Excel and the following VBA, taken from here (https://en.wikibooks.org/wiki/Visual_Basic_for_Applications/String_Hashing_in_VBA).
Add code to a worksheet new Module, and then in the cell, use the function =SHA1_B64(A1) to encrpyt and encode your passwords to the format needed.

Sub asdas()
    SHA1 "4081", True
End Sub

Public Function SHA1_B64(sIn_R As Range)
    SHA1_B64 = SHA1(sIn_R.Value, 1)
End Function

Public Function SHA1(ByVal sIn As String, Optional bB64 As Boolean = 0) As String
    'Set a reference to mscorlib 4.0 64-bit
    
    'Test with empty string input:
    '40 Hex:   da39a3ee5e6...etc
    '28 Base-64:   2jmj7l5rSw0yVb...etc
    
    Dim oT As Object, oSHA1 As Object
    Dim TextToHash() As Byte
    Dim bytes() As Byte
            
    Set oT = CreateObject("System.Text.UTF8Encoding")
    Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed")
    
    TextToHash = oT.GetBytes_4(sIn)
    bytes = oSHA1.ComputeHash_2((TextToHash))
        
    If bB64 = True Then
       SHA1 = ConvToBase64String(bytes)
    Else
       SHA1 = ConvToHexString(bytes)
    End If
            
    Set oT = Nothing
    Set oSHA1 = Nothing
    
End Function

Private Function ConvToBase64String(vIn As Variant) As Variant

    Dim oD As Object
      
    Set oD = CreateObject("MSXML2.DOMDocument")
      With oD
        .LoadXML "<root />"
        .DocumentElement.DataType = "bin.base64"
        .DocumentElement.nodeTypedValue = vIn
      End With
    ConvToBase64String = Replace(oD.DocumentElement.Text, vbLf, "")
    
    Set oD = Nothing

End Function

Private Function ConvToHexString(vIn As Variant) As Variant

    Dim oD As Object
      
    Set oD = CreateObject("MSXML2.DOMDocument")
      
      With oD
        .LoadXML "<root />"
        .DocumentElement.DataType = "bin.Hex"
        .DocumentElement.nodeTypedValue = vIn
      End With
    ConvToHexString = Replace(oD.DocumentElement.Text, vbLf, "")
    
    Set oD = Nothing

End Function