IPISecurity2 Example

 

This example on load connects to the default server and gets the security table attribute names.

 

When the button 1 is clicked it obtains an IPISecurity2 interface from the PIModuleDB object and displays the current security attributes. Then it changes them and displays the changed attributes. Finally it resets the security attributes to their original values.

 

When the button 2 is clicked it obtains an IPISecurity2 interface from the "%OSI" PIModule object and displays the current security attributes. Then it changes them and displays the changed attributes. Finally it resets the security attributes to their original values.

 

Build the IPISecurity2 example as follows:

1. Create a new project and add a reference to the PISDK library.

2. Add two buttons. Make sure the button names (Command1 and Command2) match those shown.

3. Cut and paste the code below into the project.

 

Option Explicit
'
' This example gets and sets the security attributes for the PIModule Database
' and a PIModule
'
' 2004-Dec-13 KEC> Written
'

'
' Globals
'
Dim g_PIServer As Server
Dim g_OwnerStr As String
Dim g_GroupStr As String
Dim g_AccessStr As String

Private Sub Command1_Click()
'
' Change security of the Module Database and then change it back
'
Dim IPIS2 As IPISecurity2
Dim curSecurity As NamedValues
Dim newSecurity As NamedValues
Dim changedSecurity As NamedValues
Dim curNV As NamedValue

On Error GoTo handler
'
' Get current security settings
'
Set IPIS2 = g_PIServer.PIModuleDB
Set curSecurity = IPIS2.GetSecurity
'
' Display current security
'
For Each curNV In curSecurity
MsgBox "Item name: " & curNV.Name & " value: " & curNV.Value, vbOKOnly, "Existing PIModuleDB Security"
Next
'
' Change security
'
Set newSecurity = New NamedValues
newSecurity.Add g_OwnerStr, "pidemo"
newSecurity.Add g_GroupStr, "piuser"
newSecurity.Add g_AccessStr, "o:r g: w:rw"
IPIS2.PutSecurity newSecurity
'
' Display new security
'
Set changedSecurity = IPIS2.GetSecurity
For Each curNV In changedSecurity
MsgBox "Item name: " & curNV.Name & " value: " & curNV.Value, vbOKOnly, "Changed PIModuleDB Security"
Next
'
' Set it back to the original security
'
IPIS2.PutSecurity curSecurity
Exit Sub
handler:
MsgBox Err.Description
End Sub

Private Sub Command2_Click()
'
' Change security of a Module then change it back
'
Dim myMod As PIModule
Dim IPIS2 As IPISecurity2
Dim curSecurity As NamedValues
Dim newSecurity As NamedValues
Dim changedSecurity As NamedValues
Dim curNV As NamedValue

On Error GoTo handler
'
' Get the current security settings the %OSI module
'
Set myMod = g_PIServer.PIModuleDB.PIModules("%OSI")
Set IPIS2 = myMod
Set curSecurity = IPIS2.GetSecurity
'
' Display current security
'
For Each curNV In curSecurity
MsgBox "Item name: " & curNV.Name & " value: " & curNV.Value, vbOKOnly, "Existing " & myMod.Name & " Security"
Next
'
' Change security
'
Set newSecurity = New NamedValues
newSecurity.Add g_OwnerStr, "pidemo"
newSecurity.Add g_GroupStr, "piuser"
newSecurity.Add g_AccessStr, "o:r g: w:rw"
IPIS2.PutSecurity newSecurity
'
' Display new security
'
Set changedSecurity = IPIS2.GetSecurity
For Each curNV In changedSecurity
MsgBox "Item name: " & curNV.Name & " value: " & curNV.Value, vbOKOnly, "Changed " & myMod.Name & " Security"
Next
'
' Set it back to the original security
'
IPIS2.PutSecurity curSecurity
Exit Sub
handler:
MsgBox Err.Description
End Sub

Private Sub Form_Load()
Dim secConstants As PIConstant
Dim nvSecConst As NamedValue
'
' Open default server
'
Set g_PIServer = Servers.DefaultServer
g_PIServer.Open
'
' Get these once
'
Set secConstants = PISDK.PIConstants.Item("MDBBDBSecConstants")
For Each nvSecConst In secConstants
If (mbsAccess = nvSecConst.Value) Then
g_AccessStr = nvSecConst.Name
ElseIf (mbsOwner = nvSecConst.Value) Then
g_OwnerStr = nvSecConst.Name
ElseIf (mbsGroup = nvSecConst.Value) Then
g_GroupStr = nvSecConst.Name
Else
MsgBox "Unknown security constant. Name: " & nvSecConst.Name & " Value: " & nvSecConst.Value
End If
Next

End Sub
 

Enabling Operational Intelligence