wizards/source/sfdatabases/SF_Register.xba | 20 ++++++++++++++------ wizards/source/sfdocuments/SF_Form.xba | 3 +++ wizards/source/sfdocuments/SF_FormDocument.xba | 5 ++++- 3 files changed, 21 insertions(+), 7 deletions(-)
New commits: commit d2c1a4fa508572cc2908787deeebc5baa9d6e0c3 Author: Jean-Pierre Ledure <j...@ledure.be> AuthorDate: Wed Aug 9 12:33:39 2023 +0200 Commit: Jean-Pierre Ledure <j...@ledure.be> CommitDate: Wed Aug 9 14:45:23 2023 +0200 SCriptForge (SFDatabases) manage database credentials When not embedded, databases usually require user and password to grant access to scripts. When the access was refused, so far the execution was stopped brutally with an error message provided by LO internal code. This was made visible during some tests with a MySql database server. The code to be reviewed is located in the creation of the "database" service. This service has the sungularity that it can be called from several other service instances. As from this commit unsuccessful tentatives to login to a database from code are rejected with a clean SF error message. The different scenarios to create a database service have been (re)tested. Change-Id: I695d108242872d27671688af76916784908aeebe Reviewed-on: https://gerrit.libreoffice.org/c/core/+/155507 Reviewed-by: Jean-Pierre Ledure <j...@ledure.be> Tested-by: Jenkins diff --git a/wizards/source/sfdatabases/SF_Register.xba b/wizards/source/sfdatabases/SF_Register.xba index 04c76ff59e94..cee09f94f3f3 100644 --- a/wizards/source/sfdatabases/SF_Register.xba +++ b/wizards/source/sfdatabases/SF_Register.xba @@ -25,6 +25,7 @@ Option Explicit REM ================================================================== EXCEPTIONS Private Const BASEDOCUMENTOPENERROR = "BASEDOCUMENTOPENERROR" +Private Const DBCONNECTERROR = "DBCONNECTERROR" REM ============================================================== PUBLIC METHODS @@ -65,7 +66,8 @@ Public Function _NewDatabase(Optional ByVal pvArgs As Variant) As Object ''' Returns: ''' The instance or Nothing ''' Exceptions: -''' BASEDOCUMENTOPENERROR The database file could not be opened or connected +''' BASEDOCUMENTOPENERROR The database file could not be opened +''' DBCONNECTERROR The database could not be connected, credentials are probably wrong Dim oDatabase As Object ' Return value Dim vFileName As Variant ' alias of pvArgs(0) @@ -116,7 +118,9 @@ Try: Set .[Me] = oDatabase ._Location = ConvertToUrl(vFileName) Set ._DataSource = oDBContext.getByName(._Location) + If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo CatchConnect Set ._Connection = ._DataSource.getConnection(vUser, vPassword) + If IsNull(._Connection) Then GoTo CatchConnect ._ReadOnly = vReadOnly Set ._MetaData = ._Connection.MetaData ._URL = ._MetaData.URL @@ -130,20 +134,23 @@ Catch: CatchError: ScriptForge.SF_Exception.RaiseFatal(BASEDOCUMENTOPENERROR, "FileName", vFileName, "RegistrationName", vRegistration) GoTo Finally +CatchConnect: + ScriptForge.SF_Exception.RaiseFatal(DBCONNECTERROR, "User", vUser, "Password", vPassword, vFileName) + GoTo Finally End Function ' SFDatabases.SF_Register._NewDatabase REM ----------------------------------------------------------------------------- Public Function _NewDatabaseFromSource(Optional ByVal pvArgs As Variant) As Object -' ByRef poDataSource As Object _ -' , ByVal psUser As String _ -' , ByVal psPassword As String _ +' ByRef oDataSource As Object _ +' , ByVal sUser As String _ +' , ByVal sPassword As String _ ' ) As Object ''' Create a new instance of the SF_Database class from the given datasource ''' established in the SFDocuments.Base service ''' THIS SERVICE MUST NOT BE CALLED FROM A USER SCRIPT ''' Args: -''' DataSource: com.sun.star.sdbc.XDataSource -''' User, Password : connection parameters +''' oDataSource: com.sun.star.sdbc.XDataSource +''' sUser, sPassword : connection parameters ''' Returns: ''' The instance or Nothing ''' Exceptions: @@ -189,6 +196,7 @@ Finally: Set _NewDatabaseFromSource = oDatabase Exit Function Catch: + ScriptForge.SF_Exception.Clear() GoTo Finally End Function ' SFDatabases.SF_Register._NewDatabaseFromSource diff --git a/wizards/source/sfdocuments/SF_Form.xba b/wizards/source/sfdocuments/SF_Form.xba index cc7c2aecf34f..a5559ae06a8a 100644 --- a/wizards/source/sfdocuments/SF_Form.xba +++ b/wizards/source/sfdocuments/SF_Form.xba @@ -81,6 +81,7 @@ REM ================================================================== EXCEPTION Private Const FORMDEADERROR = "FORMDEADERROR" Private Const SUBFORMNOTFOUNDERROR = "SUBFORMNOTFOUNDERROR" +Private Const DBCONNECTERROR = "DBCONNECTERROR" REM ============================================================= PRIVATE MEMBERS @@ -668,6 +669,8 @@ Public Function GetDatabase(Optional ByVal User As Variant _ ''' User, Password: the login parameters as strings. Defaults = "" ''' Returns: ''' A SFDatabases.Database instance or Nothing +''' Exceptions: +''' DBCONNECTERROR The database could not be connected, credentials are probably wrong ''' Example: ''' Dim myDb As Object ''' Set myDb = oForm.GetDatabase() diff --git a/wizards/source/sfdocuments/SF_FormDocument.xba b/wizards/source/sfdocuments/SF_FormDocument.xba index 4270c08e2907..5269cea09c1c 100644 --- a/wizards/source/sfdocuments/SF_FormDocument.xba +++ b/wizards/source/sfdocuments/SF_FormDocument.xba @@ -65,6 +65,7 @@ Option Explicit REM ================================================================== EXCEPTIONS Private Const BASEFORMNOTFOUNDERROR = "BASEFORMNOTFOUNDERROR" +Private Const DBCONNECTERROR = "DBCONNECTERROR" REM ============================================================= PRIVATE MEMBERS @@ -244,6 +245,8 @@ Public Function GetDatabase(Optional ByVal User As Variant _ ''' User, Password: the login parameters as strings. Defaults = "" ''' Returns: ''' A SFDatabases.Database instance or Nothing +''' Exceptions: +''' DBCONNECTERROR The database could not be connected, credentials are probably wrong ''' Example: ''' Dim myDb As Object ''' Set myDb = oFormDoc.GetDatabase() @@ -267,7 +270,7 @@ Check: Try: If IsNull(_DataSource) Then GoTo CatchConnect Set oDatabase = ScriptForge.SF_Services.CreateScriptService("SFDatabases.DatabaseFromDocument" _ - , _DataSource, Iif(User = "", _User, User), Iif(Password = "", _Password, "")) + , _DataSource, Iif(User = "", _User, User), Iif(Password = "", _Password, Password)) If IsNull(oDatabase) Then GoTo CatchConnect oDatabase._Location = _DataSource.Name