From fd3074d1ca789bad63ad9c172ec7a2aca87d3228 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 25 Jan 2023 13:01:03 +0100 Subject: [PATCH] refs #4974 Added dynamism and changes --- src/main.js | 440 ++++++++++++++++++++++------------------------ src/style.css | 2 + src/vn-access.hta | 10 +- 3 files changed, 217 insertions(+), 235 deletions(-) diff --git a/src/main.js b/src/main.js index adce86b..9d1788e 100755 --- a/src/main.js +++ b/src/main.js @@ -1,6 +1,5 @@ var Conf = { appName: 'Verdnatura', - dsName: 'verdnatura', odbcPath: 'HKCU\\Software\\ODBC\\ODBC.INI\\', regPath: 'HKCU\\SOFTWARE\\Verdnatura\\vn-access', defaultModule: 'vn', @@ -8,38 +7,14 @@ var Conf = { defaultBranch: 'master', defaultDatasource: 'production', defaultRemoteURL: 'https://salix.verdnatura.es', + defaultServer: 'db.verdnatura.es', dbName: 'vn2008', maxCorruptSize: 600, odbcDriver: 'MySQL ODBC 8.0 Unicode Driver', driverPath: '\\MySQL\\Connector ODBC 8.0\\myodbc8w.dll', version: 4, cdnURL: 'https://cdn.verdnatura.es/vn-access', - datasources: { - production: { - DESCRIPTION: 'production', - SERVER: 'db.verdnatura.es', - PORT: '3306', - SSLMODE: 'VERIFY_IDENTITY' - }, - test: { - DESCRIPTION: 'test', - SERVER: 'test-db.verdnatura.es', - PORT: '3306', - SSLMODE: 'VERIFY_IDENTITY' - }, - dev: { - DESCRIPTION: 'dev', - SERVER: 'dev-db.verdnatura.es', - PORT: '3307', - SSLMODE: 'DISABLED' - }, - localhost: { - DESCRIPTION: 'local', - SERVER: 'localhost', - PORT: '3306', - SSLMODE: 'DISABLED' - } - } + identifier: '.vn' }; var Locale = { @@ -116,31 +91,54 @@ var App = { // Creates the MySQL ODBC connection this.createODBC( Conf.odbcPath, - Conf.dsName, + Conf.defaultDatasource + Conf.identifier, Conf.odbcDriver ); - this.createDatasources(); this.regWrite(Conf.regPath, 'remoteURL', Conf.defaultRemoteURL); this.regWrite(Conf.regPath, 'lastExecutedVersion', Conf.version); // Delete old strings values (REMOVE IN VERSION 5) this.regDelete(Conf.regPath + 'configured'); this.regDelete(Conf.regPath + 'remember'); + // this.regDelete(Conf.odbcPath + 'verdnatura'); ToDo: Descomentar al subir } var notSignOut = this.regRead(Conf.regPath, 'notSignOut'); - var password = this.regRead(Conf.odbcPath + Conf.dsName, 'PWD'); - this.$('user').value = this.regRead(Conf.odbcPath + Conf.dsName, 'UID'); - this.$('password').value = password; // Branch options + this.request('GET', 'mdbBranches', null, function(err, res) { + if (err) + throw new Error ('Version could not be retrieved: '+ err.message +': '); + var selectBranch = document.querySelector('#branch'); + var option = []; + for (var x in res) { + option[x] = document.createElement('option'); + option[x].text = res[x].name + option[x].value = res[x].name + selectBranch.options.add(option[x]) + } + }); + this.$('branch').value = this.getBranch(); this.onChangeBranch(); // Datasource options - this.$('datasource').value = this.getDatasource(); + var selectDatarouce = document.querySelector('#datasource'); + var option = []; + var odbcName; + var allOdbc = this.EnumValues('HKCU\\SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources\\') + for (var y in allOdbc) { + if (allOdbc[y].slice(-3) == Conf.identifier) { + odbcName = allOdbc[y].replace(Conf.identifier, '') + option[y] = document.createElement('option'); + option[y].text = odbcName + option[y].value = odbcName + selectDatarouce.options.add(option[y]) + } + } + this.$('datasource').value = this.getDatasource(); if (notSignOut && password) { this.$('password').value = password; this.$('notSignOut').checked = true; @@ -149,7 +147,7 @@ var App = { } else { this.resetForm(true); this.onChangeDatasource(true, true); - } + } }, resetForm: function(clearPassword) { if (clearPassword) @@ -160,14 +158,13 @@ var App = { }, createODBC: function(path, dsName, driverName) { var driverPath = this.getEnv('ProgramFiles') + Conf.driverPath; - var datasource = Conf.defaultDatasource; var params = { Driver: driverPath, DESCRIPTION: Conf.appName, - SERVER: Conf.datasources[datasource].SERVER, + SERVER: Conf.defaultServer, DATABASE: Conf.dbName, SSLCA: this.certFile, - SSLMODE: Conf.datasources[datasource].SSLMODE, + SSLMODE: 'VERIFY_IDENTITY', SSLCIPHER: 'AES256-SHA', AUTO_RECONNECT: 1, NO_PROMPT: 1, @@ -177,17 +174,20 @@ var App = { this.regWrite(path + 'ODBC Data Sources', dsName, driverName); this.regWrites(path + dsName, params); }, - updateODBC: function(path, dsName, datasource) { - var params = Conf.datasources[datasource] - var datasourcePath = this.getDatasourcePath(); - var myUID = this.regRead(datasourcePath, 'UID'); - var myPWD = this.regRead(datasourcePath, 'PWD'); + updateODBC: function() { + var odbcPath = this.getOdbcPath(); + var myUID = this.regRead(odbcPath, 'remoteUser'); + var myPWD = this.regRead(odbcPath, 'remotePass'); + var params = []; if (myUID && myPWD) { params['UID'] = myUID; params['PWD'] = myPWD; + } else { + params['UID'] = this.regRead(odbcPath, 'UID'); + params['PWD'] = null; } - this.regWrites(path + dsName, params); + this.regWrites(odbcPath, params); }, disableUi: function(disabled, loadMessage) { if (disabled) @@ -253,21 +253,20 @@ var App = { this.$("datasource").className = null; }; if (!hasUpdate) { - this.updateDatasource(myDatasource); - this.updateODBC( - Conf.odbcPath, - Conf.dsName, - myDatasource - ); + this.regWrite(Conf.regPath, 'currentDatasource', myDatasource); + this.updateODBC(); }; if (hasChangeCredentials) { - var datasourcePath = this.getDatasourcePath(); - var myUID = this.regRead(datasourcePath, 'UID'); - var myPWD = this.regRead(datasourcePath, 'PWD'); + var odbcPath = this.getOdbcPath(); + var myUID = this.regRead(odbcPath, 'remoteUser'); + var myPWD = this.regRead(odbcPath, 'remotePass'); if (myUID && myPWD) { this.$('user').value = myUID; this.$('password').value = myPWD; - }; + } else { + this.$('user').value = this.regRead(odbcPath, 'UID') + this.$('password').value = this.regRead(odbcPath, 'PWD') + } }; this.$('user').focus(); }, @@ -320,8 +319,53 @@ var App = { if (!password) throw new Error(_('Enter a password')); - this.regWrite(Conf.odbcPath + Conf.dsName, 'UID', user); - this.regWrite(Conf.odbcPath + Conf.dsName, 'PWD', password); + this.regWrite(this.getOdbcPath(), 'UID', user); + this.regWrite(this.getOdbcPath(), 'PWD', password); + + var mysqlConn = new ActiveXObject('ADODB.Connection'); + var datasource = this.getDatasource(); + + // FIXME: Can't login in dev-db + if (datasource == 'dev') { + datasource = Conf.defaultDatasource + }; + + // Check credentials + try { + var odbcPath = this.getOdbcPath() + mysqlConn.open(this.getODBCString({ + Driver: '{'+ Conf.odbcDriver +'}', + SERVER: this.regRead(odbcPath, 'SERVER'), + DATABASE: Conf.dbName, + UID: user, + PWD: password, + SSLCA: this.certFile, + SSLMODE: this.regRead(odbcPath, 'SSLMODE'), + SSLCIPHER: 'AES256-SHA', + ENABLE_CLEARTEXT_PLUGIN : 1 + })); + } catch (err) { + var dbErrors = mysqlConn && mysqlConn.errors; + + if (dbErrors && dbErrors.count > 0) { + var dbError = dbErrors.item(0); + switch (dbError.NativeError) { + case 1045: // Access denied + clearPassword = true; + err = new Error(_('Bad login')); + err.name = 'BadLogin'; + break; + case 2003: // Can't connect + err = new Error(_('Server can\'t be reached')); + break; + default: + err = new Error(dbError.description); + } + dbErrors.clear(); + } + throw err; + } + mysqlConn.close(); // Check the cretentials and return the last version number var version = this.fetchVersion(); @@ -333,7 +377,7 @@ var App = { ? '.archive/'+ this.module +'/'+ version +'.7z' : this.module +'.7z?'+ new Date().getTime(); remoteFile = Conf.cdnURL +'/'+ remoteFile; - var request = new ActiveXObject("MSXML2.XMLHTTP.6.0"); + var request = new ActiveXObject('MSXML2.XMLHTTP.6.0'); request.open('GET', remoteFile, true); request.onreadystatechange = function() { App.onRequestReady(request); @@ -353,101 +397,52 @@ var App = { * fetch or %null if local is up-to-date */ fetchVersion: function() { - var mysqlConn = new ActiveXObject('ADODB.Connection'); - var datasource = this.getDatasource(); - var user = this.$('user').value; - var password = this.$('password').value; - - // FIXME: Can't login in dev-db - if (datasource == 'dev') { - datasource = Conf.defaultDatasource - }; - - // Check credentials - try { - mysqlConn.open(this.getODBCString({ - Driver: '{'+ Conf.odbcDriver +'}', - SERVER: Conf.datasources[datasource].SERVER, - DATABASE: Conf.dbName, - UID: user, - PWD: password, - SSLCA: this.certFile, - SSLMODE: Conf.datasources[datasource].SSLMODE, - SSLCIPHER: 'AES256-SHA', - ENABLE_CLEARTEXT_PLUGIN : 1 - })); - } catch (err) { - var dbErrors = mysqlConn && mysqlConn.errors; - - if (dbErrors && dbErrors.count > 0) { - var dbError = dbErrors.item(0); - switch (dbError.NativeError) { - case 1045: // Access denied - clearPassword = true; - err = new Error(_('Bad login')); - err.name = 'BadLogin'; - break; - case 2003: // Can't connect - err = new Error(_('Server can\'t be reached')); - break; - default: - err = new Error(dbError.description); - } - dbErrors.clear(); + // To obtain the lastest version of this module + var params = { + filter: { + fields: ['version'], + where: { + app: this.module, + branchFk: this.$('branch').value + }, } - throw err; - } - mysqlConn.close(); - - // Request to obtain the token and lastest version of this module - try { - var request = new ActiveXObject("MSXML2.XMLHTTP.6.0"); - urlLoginMethod = this.getRemoteURL() + '/api/Accounts/login' - request.open('POST', urlLoginMethod, false); - request.setRequestHeader('Content-Type', 'application/json') - var params = JSON.stringify({ - "user": user, - "password": password - }); - request.onreadystatechange = function() { - App.onRequestTokenReady(request); - } - request.send(params); - } catch (err) { - throw err; }; - - // Checks if it's already open - if (this.fso.fileExists(this.lockFile)) - try { - this.fso.deleteFile(this.lockFile); - return this.lastVersion; - } catch (e) { - throw new Error(_('Application it\'s already open')); - } + this.request('GET', 'MdbVersions/findOne', params, function(err, res) { + if (err) + throw new Error ('Version could not be retrieved: '+ err.message +': '); + alert(this.lockFile) + // Checks if it's already open + if (App.fso.fileExists(this.lockFile)) + try { + App.fso.deleteFile(this.lockFile); + return res.version; + } catch (e) { + throw new Error(_('Application it\'s already open')); + } - // Checks if MDB exists - if (!this.fso.fileExists(this.mdbFile)) - return this.lastVersion; + // Checks if MDB exists + if (!App.fso.fileExists(this.mdbFile)) + return res.version; - // If it's abnormaly bigger, maybe is corrupted, so force download - var file = this.fso.getFile(this.mdbFile); - - if (file.size > Conf.maxCorruptSize * 1024 * 1024) - return this.lastVersion; + // If it's abnormaly bigger, maybe is corrupted, so force download + var file = App.fso.getFile(this.mdbFile); + + if (file.size > Conf.maxCorruptSize * 1024 * 1024) + return res.version; - // Obtains the local version number from the MDB file - var localVersion = this.mdbGetValue( - 'SELECT Version FROM tblVariables', - 'Version', parseInt - ); - if (!localVersion) - localVersion = false; + // Obtains the local version number from the MDB file + var localVersion = this.mdbGetValue( + 'SELECT Version FROM tblVariables', + 'Version', parseInt + ); + if (!localVersion) + localVersion = false; - // Determines if should download - return !localVersion || this.lastVersion === false || localVersion != this.lastVersion - ? this.lastVersion - : null; + // Determines if should download + return !localVersion || res.version === false || localVersion != res.version + ? res.version + : null; + }); }, mdbGetValue: function(query, field, parseFn) { var value; @@ -516,53 +511,6 @@ var App = { this.catchError(e); } }, - onRequestTokenReady: function(request) { - if (request.readyState !== 4) - return; - - try { - if (request.status == 401) // Unhauthorized - throw new Error('Bad login in the remoteURL "' + this.getRemoteURL() + - '":\nThe latest version could not be obtained'); - else if (request.status !== 200 ) - throw new Error('HTTP: '+ request.statusText + ' ' + request.status); - - this.response = JSON.parse(request.responseText) - - if (this.response) { - var request = new ActiveXObject("MSXML2.ServerXMLHTTP"); - var filter = '&filter=' + JSON.stringify({ - "fields":["version"], - "where":{ - "app": this.module, - "branchFk": this.$('branch').value - }, - }); - var urlVersionMethod = this.getRemoteURL() + '/api/MdbVersions?access_token=' + this.response.token + filter - request.open('GET', urlVersionMethod, false); - request.onreadystatechange = function() { - App.onRequestVersionReady(request); - }; - request.send(); - } - } catch (err) { - throw err; - } - }, - onRequestVersionReady: function(request) { - if (request.readyState !== 4) - return; - - try { - if (request.status !== 200 ) - throw new Error('HTTP: '+ request.statusText + ' ' + request.status); - - var response = JSON.parse(request.responseText); - this.lastVersion = response[0].version - } catch (err) { - throw err; - } - }, openMdb: function() { var notSignOut = !!this.$('notSignOut').checked; this.regWrite(Conf.regPath, 'notSignOut', notSignOut); @@ -580,7 +528,7 @@ var App = { var clearPassword = err.name == 'BadLogin'; if (!this.$('notSignOut').checked || clearPassword) - this.regWrite(Conf.odbcPath + Conf.dsName, 'PWD', ''); + this.regWrite(this.getOdbcPath(), 'PWD', ''); this.disableUi(false); this.showMessage(err.message, 'error'); @@ -676,6 +624,51 @@ var App = { this.shell.regDelete(path); } catch (e) {} }, + request: function (method, url, data, cb) { + var fullUrl = this.getRemoteURL() +'/api/'+ url; + + var isGet = method == 'GET'; + if (isGet) { + var isFirst = true; + for (var param in data) { + fullUrl += isFirst ? '?' : '&'; + isFirst = false; + var value = data[param]; + if (typeof value == 'object') + value = JSON.stringify(value); + fullUrl += param +'='+ encodeURIComponent(value); + } + } + + var req = new ActiveXObject('MSXML2.XMLHTTP.6.0'); + req.open(method, fullUrl, false); + req.onreadystatechange = function() { + App.onRequestStateChange(req, cb) + }; + if (isGet) { + req.setRequestHeader('Content-Type', 'application/json'); + req.send(JSON.stringify(data)); + } else + req.send(); + }, + onRequestStateChange: function(req, cb) { + if (req.readyState !== 4) + return; + var status = req.status; + if (status >= 100 && status < 400) { + var res = JSON.parse(req.responseText); + cb(null, res); + } else { + err = new Error(req.statusText); + err.name = 'HttpError'; + err.status = status; + err.req = req; + if (status >= 400 && status < 600) + err.error = JSON.parse(req.responseText); + + cb(err); + } + }, /** * Gets information about the branch config in access * @@ -697,7 +690,8 @@ var App = { var datasource = this.regRead( Conf.regPath, 'currentDatasource' - ); + ); + return datasource || Conf.defaultDatasource; }, /** @@ -713,42 +707,36 @@ var App = { return remoteURL || Conf.defaultRemoteURL; }, /** - * Get the current datasource path + * Get the odbc path * - * @return {string} Datasource path + * @return {string} Remote server */ - getDatasourcePath: function() { - var datasourcePath = Conf.regPath + '\\datasources\\' + this.getDatasource(); - - return datasourcePath; + getOdbcPath: function() { + var odbcPath = Conf.odbcPath + + (this.$("datasource").value || Conf.defaultDatasource) + + Conf.identifier + return odbcPath; }, - /** - * Update the datasource - * - * @param {String} datasource The datasource - */ - updateDatasource: function(datasource) { - this.regWrite(Conf.regPath, 'currentDatasource', datasource); - }, - /** - * Create all datasources structure - */ - createDatasources: function() { - var params = { - 'UID': null, - 'PWD': null + EnumValues: function(RegKey) { + var RootKey = new Object() + RootKey["HKCR"] = RootKey["HKEY_CLASSES_ROOT"] = 0x80000000; + RootKey["HKCU"] = RootKey["HKEY_CURRENT_USER"] = 0x80000001; + RootKey["HKLM"] = RootKey["HKEY_LOCAL_MACHINE"] = 0x80000002; + RootKey["HKUS"] = RootKey["HKEY_USERS"] = 0x80000003; + RootKey["HKCC"] = RootKey["HKEY_CURRENT_CONFIG"] = 0x80000005; + var RootVal = RootKey[RegKey.substr(0, RegKey.indexOf("\\"))] + if (RootVal != undefined) { + Locator = new ActiveXObject("WbemScripting.SWbemLocator"); + ServerConn = Locator.ConnectServer(null, "root\\default"); + Registry = ServerConn.Get("StdRegProv"); + Method = Registry.Methods_.Item("EnumValues"); + p_In = Method.InParameters.SpawnInstance_(); + p_In.hDefKey = RootVal; + p_In.sSubKeyName = RegKey.substr(RegKey.indexOf("\\") + 1) + p_Out = Registry.ExecMethod_(Method.Name, p_In); + return p_Out.sNames.toArray(); } - for (var datasource in Conf.datasources) { - var myPath = Conf.regPath + '\\datasources\\' + datasource - for (var stringValues in Conf.datasources[datasource]) { - this.regWrite(myPath, - stringValues, - Conf.datasources[datasource][stringValues] - ); - } - this.regWrites(myPath, params); - }; - }, + } }; App.init(); diff --git a/src/style.css b/src/style.css index 7e9a8a3..6de4def 100755 --- a/src/style.css +++ b/src/style.css @@ -123,6 +123,7 @@ button:hover { display: none; padding-left: .3em; padding-right: .8em; + max-width: 133px; } #branchButton { float: left; @@ -184,6 +185,7 @@ select.dev { padding-right: .8em; margin-right: .5em; margin-top: -1.91em; + max-width: 133px; } #datasourceButton { float: right; diff --git a/src/vn-access.hta b/src/vn-access.hta index 782b6ce..d279c6f 100755 --- a/src/vn-access.hta +++ b/src/vn-access.hta @@ -61,11 +61,7 @@ App.onShowDatasourceOptionsClick()" alt="Change branch"/>
- +
@@ -79,10 +75,6 @@ alt="Change datasource"/>