backend-plus 2.5.1-betha.0 → 2.5.2-betha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- create or replace function get_app_user() returns text
1
+ create or replace function get_app_user(p_var text default 'user') returns text
2
2
  stable language sql
3
3
  as
4
4
  $sql$
5
- select split_part(current_setting('application_name'),' ',1);
5
+ select current_setting('backend_plus._' || p_var);
6
6
  $sql$;
@@ -415,6 +415,7 @@ export interface AppConfigLogin
415
415
  {
416
416
  schema: string // schema of the user table
417
417
  table: string // user table
418
+ from: string // complete expression to get table or join where get the user
418
419
  userFieldname: string // fieldname in user table that stores the user name
419
420
  passFieldname: string // fieldname in user table that stores the password hash
420
421
  rolFieldname: string // fieldname in user table that stores the rol
@@ -780,16 +780,17 @@ AppBackend.prototype.start = function start(opts){
780
780
  }
781
781
  be.config.db.search_path = be.config.db.search_path ?? [be.config.db.schema, 'public'];
782
782
  be.getDbClient = function getDbClient(req){
783
- var paramsDb = be.DoubleDragon?.dbParams?.[req?.user?.[be.config.login.userFieldName]] ?? be.config.db;
783
+ var username = req?.user?.[be.config.login.userFieldName];
784
+ var paramsDb = be.DoubleDragon?.dbParams?.[username] ?? be.config.db;
784
785
  return pg.connect(paramsDb).then(function(client){
785
786
  var dbAppName=req?(
786
787
  ((req.user||{})[be.config.login.userFieldName]||'!logged')+
787
788
  ' '+(req.machineId||'?')+
788
789
  ' '+(((req.useragent)||{}).shortDescription||'?')
789
790
  ):'!app local internal';
790
- return client.query(
791
- "SET application_name = "+be.db.quoteLiteral(dbAppName)
792
- ).execute().then(function(){
791
+ return client.executeSentences([
792
+ "SET application_name = "+be.db.quoteLiteral(dbAppName),
793
+ ]).then(function(){
793
794
  var search_path = be.config.db.search_path;
794
795
  if(search_path.length>0){
795
796
  return client.query("set SEARCH_PATH TO "+be.db.quoteIdentList(search_path)).execute().then(function(){
@@ -798,6 +799,14 @@ AppBackend.prototype.start = function start(opts){
798
799
  }else{
799
800
  return client;
800
801
  }
802
+ }).then(function(client){
803
+ if(username){
804
+ return client.query(`SELECT set_app_user(${be.db.quoteLiteral(username)})`).execute().then(function(){
805
+ return client;
806
+ });
807
+ }else{
808
+ return client;
809
+ }
801
810
  }).then(function(client){
802
811
  if(be.config["client-setup"].lang=='es'){
803
812
  return client.query("set datestyle TO iso,dmy").execute().then(function(){
@@ -1012,13 +1021,17 @@ AppBackend.prototype.start = function start(opts){
1012
1021
  }
1013
1022
  be.getDbClient(req).then(function(cli){
1014
1023
  client = cli;
1024
+ return client.query("select set_app_user('!login')").execute();
1025
+ }).then(function(){
1015
1026
  var infoFieldList=be.config.login.infoFieldList||(be.config.login.rolFieldName?[be.config.login.userFieldName,be.config.login.rolFieldName]:[be.config.login.userFieldName]);
1016
1027
  return client.query(
1017
1028
  "SELECT "+infoFieldList.map(function(fieldOrPair){ return fieldOrPair.split(' as ').map(function(ident){ return be.db.quoteIdent(ident)}).join(' as '); })+
1018
1029
  ", "+be.config.login.activeClausule+" as active "+
1019
1030
  ", "+be.config.login.lockedClausule+" as locked "+
1020
- " FROM "+(be.config.login.schema?be.db.quoteIdent(be.config.login.schema)+'.':'')
1021
- +be.db.quoteIdent(be.config.login.table)+
1031
+ " FROM "+(be.config.login.from ?? (
1032
+ (be.config.login.schema?be.db.quoteIdent(be.config.login.schema)+'.':'')+
1033
+ be.db.quoteIdent(be.config.login.table)
1034
+ ))+
1022
1035
  " WHERE "+be.db.quoteIdent(be.config.login.userFieldName)+" = $1 "+
1023
1036
  " AND "+be.db.quoteIdent(be.config.login.passFieldName)+" = $2 ",
1024
1037
  [username, md5(password+username)]
@@ -2935,6 +2948,37 @@ AppBackend.prototype.dumpDbSchemaPartial = async function dumpDbSchemaPartial(pa
2935
2948
  throw err;
2936
2949
  }
2937
2950
  }
2951
+ lines.push(`
2952
+ create or replace function set_app_user(p_user text) returns text
2953
+ secirotu definer volatile language plpgsql
2954
+ as
2955
+ $body$
2956
+ declare
2957
+ ${be.config.login.infoFieldList.map(fieldName => `
2958
+ ${db.quoteIdent('v_'+fieldName)} text;`
2959
+ ).join('')}
2960
+ begin
2961
+ if p_user = '!login' then
2962
+ ${be.config.login.infoFieldList.map(fieldName => `
2963
+ set backend_plus._${fieldName} = '!';`).join('')}
2964
+
2965
+ set backend_plus._mode = login;
2966
+ else
2967
+ select ${be.config.login.infoFieldList.map(fieldName => db.quoteIdent(fieldName)).join(', ')}
2968
+ into ${be.config.login.infoFieldList.map(fieldName => db.quoteIdent('v_'+fieldName)).join(', ')}
2969
+ from usuarios u left join personas p using (idper)
2970
+ where u."usuario" = p_user;
2971
+ ${be.config.login.infoFieldList.map(fieldName => `
2972
+ perform set_config('backend_plus._${fieldName}', v_${fieldName}, false);`).join('')}
2973
+
2974
+ set backend_plus._mode = normal;
2975
+ end if;
2976
+ perform set_config('backend_plus._user', p_user, false);
2977
+ return p_user;
2978
+ end;
2979
+ $body$;
2980
+
2981
+ `)
2938
2982
  var enancePart= 'do $SQL_ENANCE$\n begin\n' + enanceLines.join('\n')+'\n' + 'end\n$SQL_ENANCE$;';
2939
2983
  var someNotFound=false;
2940
2984
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "backend-plus",
3
3
  "description": "Backend for the anti Pareto rule",
4
- "version": "2.5.1-betha.0",
4
+ "version": "2.5.2-betha.4",
5
5
  "author": "Codenautas <codenautas@googlegroups.com>",
6
6
  "license": "MIT",
7
7
  "repository": "codenautas/backend-plus",
@@ -83,7 +83,7 @@
83
83
  "@types/js-yaml": "^4.0.9",
84
84
  "@types/mocha": "^10.0.10",
85
85
  "@types/multiparty": "~4.2.1",
86
- "@types/node": "^22.15.21",
86
+ "@types/node": "^22.15.29",
87
87
  "@types/nodemailer": "^6.4.17",
88
88
  "@types/numeral": "~2.0.5",
89
89
  "@types/session-file-store": "^1.2.5",