Imports System
Imports System.ComponentModel

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports DevExpress.ExpressApp.Security

Public Class WinChangeDatabaseHelper
    Public Shared DatabaseName As String
    Public Shared AuthenticatedUserLogonFailed As Boolean = False
    Public Shared SkipLogonDialog As Boolean = False
End Class
Public Class WinChangeDatabaseStandardAuthentication
    Inherits AuthenticationStandard(Of SimpleUser, ChangeLogon)
    Public Shared AuthenticatedUserName As String
    Public Overloads Overrides ReadOnly Property AskLogonParametersViaUI() As Boolean
        Get
            If WinChangeDatabaseHelper.SkipLogonDialog Then
                Return False
            End If
            Return MyBase.AskLogonParametersViaUI
        End Get
    End Property
    Public Overloads Function Authenticate(ByVal objectSpace As DevExpress.ExpressApp.IObjectSpace) As Object
        WinChangeDatabaseHelper.AuthenticatedUserLogonFailed = False
        If String.IsNullOrEmpty(AuthenticatedUserName) Then
            Return MyBase.Authenticate(objectSpace)
        Else
            Dim logonParameters As ChangeLogon = CType(Me.LogonParameters, ChangeLogon)
            Dim result As Object = objectSpace.FindObject(UserType, New BinaryOperator("UserName", logonParameters.UserName))
            If result Is Nothing Then
                WinChangeDatabaseHelper.AuthenticatedUserLogonFailed = True
                WinChangeDatabaseHelper.SkipLogonDialog = False
                Throw New AuthenticationException(logonParameters.UserName, SecurityExceptionLocalizer.GetExceptionMessage(SecurityExceptionId.RetypeTheInformation))
            End If
            AuthenticatedUserName = ""
            Return result
        End If
    End Function
End Class
