backend-plus 2.5.0-betha.0 → 2.5.0-betha.2

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$;
@@ -414,6 +414,7 @@ export interface AppConfigLogin
414
414
  {
415
415
  schema: string // schema of the user table
416
416
  table: string // user table
417
+ from: string // complete expression to get table or join where get the user
417
418
  userFieldname: string // fieldname in user table that stores the user name
418
419
  passFieldname: string // fieldname in user table that stores the password hash
419
420
  rolFieldname: string // fieldname in user table that stores the rol
@@ -779,16 +779,20 @@ AppBackend.prototype.start = function start(opts){
779
779
  }
780
780
  be.config.db.search_path = be.config.db.search_path ?? [be.config.db.schema, 'public'];
781
781
  be.getDbClient = function getDbClient(req){
782
- var paramsDb = be.DoubleDragon?.dbParams?.[req?.user?.[be.config.login.userFieldName]] ?? be.config.db;
782
+ var username = req?.user?.[be.config.login.userFieldName];
783
+ var paramsDb = be.DoubleDragon?.dbParams?.[username] ?? be.config.db;
783
784
  return pg.connect(paramsDb).then(function(client){
784
785
  var dbAppName=req?(
785
786
  ((req.user||{})[be.config.login.userFieldName]||'!logged')+
786
787
  ' '+(req.machineId||'?')+
787
788
  ' '+(((req.useragent)||{}).shortDescription||'?')
788
789
  ):'!app local internal';
789
- return client.query(
790
- "SET application_name = "+be.db.quoteLiteral(dbAppName)
791
- ).execute().then(function(){
790
+ return client.executeSentences([
791
+ "SET application_name = "+be.db.quoteLiteral(dbAppName),
792
+ ...(username ? [
793
+ `SELECT set_app_user(${be.db.quoteLiteral(username)})`
794
+ ] : [])
795
+ ]).then(function(){
792
796
  var search_path = be.config.db.search_path;
793
797
  if(search_path.length>0){
794
798
  return client.query("set SEARCH_PATH TO "+be.db.quoteIdentList(search_path)).execute().then(function(){
@@ -999,13 +1003,17 @@ AppBackend.prototype.start = function start(opts){
999
1003
  }
1000
1004
  be.getDbClient(req).then(function(cli){
1001
1005
  client = cli;
1006
+ return client.query("select set_app_user('!login')").execute();
1007
+ }).then(function(){
1002
1008
  var infoFieldList=be.config.login.infoFieldList||(be.config.login.rolFieldName?[be.config.login.userFieldName,be.config.login.rolFieldName]:[be.config.login.userFieldName]);
1003
1009
  return client.query(
1004
1010
  "SELECT "+infoFieldList.map(function(fieldOrPair){ return fieldOrPair.split(' as ').map(function(ident){ return be.db.quoteIdent(ident)}).join(' as '); })+
1005
1011
  ", "+be.config.login.activeClausule+" as active "+
1006
1012
  ", "+be.config.login.lockedClausule+" as locked "+
1007
- " FROM "+(be.config.login.schema?be.db.quoteIdent(be.config.login.schema)+'.':'')
1008
- +be.db.quoteIdent(be.config.login.table)+
1013
+ " FROM "+(be.config.login.from ?? (
1014
+ (be.config.login.schema?be.db.quoteIdent(be.config.login.schema)+'.':'')+
1015
+ be.db.quoteIdent(be.config.login.table)
1016
+ ))+
1009
1017
  " WHERE "+be.db.quoteIdent(be.config.login.userFieldName)+" = $1 "+
1010
1018
  " AND "+be.db.quoteIdent(be.config.login.passFieldName)+" = $2 ",
1011
1019
  [username, md5(password+username)]
@@ -2922,6 +2930,37 @@ AppBackend.prototype.dumpDbSchemaPartial = async function dumpDbSchemaPartial(pa
2922
2930
  throw err;
2923
2931
  }
2924
2932
  }
2933
+ lines.push(`
2934
+ create or replace function set_app_user(p_user text) returns text
2935
+ volatile language plpgsql
2936
+ as
2937
+ $body$
2938
+ declare
2939
+ ${be.config.login.infoFieldList.map(fieldName => `
2940
+ ${db.quoteIdent('v_'+fieldName)} text;`
2941
+ ).join('')}
2942
+ begin
2943
+ if p_user = '!login' then
2944
+ ${be.config.login.infoFieldList.map(fieldName => `
2945
+ set backend_plus._${fieldName} = '!';`).join('')}
2946
+
2947
+ set backend_plus._mode = login;
2948
+ else
2949
+ select ${be.config.login.infoFieldList.map(fieldName => db.quoteIdent(fieldName)).join(', ')}
2950
+ into ${be.config.login.infoFieldList.map(fieldName => db.quoteIdent('v_'+fieldName)).join(', ')}
2951
+ from usuarios u left join personas p using (idper)
2952
+ where u."usuario" = p_user;
2953
+ ${be.config.login.infoFieldList.map(fieldName => `
2954
+ perform set_config('backend_plus._${fieldName}', v_${fieldName}, false);`).join('')}
2955
+
2956
+ set backend_plus._mode = normal;
2957
+ end if;
2958
+ perform set_config('backend_plus._user', p_user, false);
2959
+ return p_user;
2960
+ end;
2961
+ $body$;
2962
+
2963
+ `)
2925
2964
  var enancePart= 'do $SQL_ENANCE$\n begin\n' + enanceLines.join('\n')+'\n' + 'end\n$SQL_ENANCE$;';
2926
2965
  var someNotFound=false;
2927
2966
  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.0-betha.0",
4
+ "version": "2.5.0-betha.2",
5
5
  "author": "Codenautas <codenautas@googlegroups.com>",
6
6
  "license": "MIT",
7
7
  "repository": "codenautas/backend-plus",