dsql-admin/dsql-app/.local_dist/server/pages/api/updateUserTable.js
Benjamin Toby 81cf010cb5 Updates
2025-01-14 16:27:08 +01:00

1 line
11 KiB
JavaScript

"use strict";(()=>{var e={};e.id=4371,e.ids=[4371],e.modules={39908:e=>{e.exports=require("@moduletrace/datasquirel/dist/users/user-auth")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},72583:e=>{e.exports=require("sanitize-html")},9973:e=>{e.exports=require("serverless-mysql")},79428:e=>{e.exports=require("buffer")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},96762:(e,r)=>{Object.defineProperty(r,"M",{enumerable:!0,get:function(){return function e(r,t){return t in r?r[t]:"then"in r&&"function"==typeof r.then?r.then(r=>e(r,t)):"function"==typeof r&&"default"===t?r:void 0}}})},81147:(e,r,t)=>{t.r(r),t.d(r,{config:()=>m,default:()=>A,routeModule:()=>b});var a={};t.r(a),t.d(a,{default:()=>S});var s=t(89947),n=t(2706),o=t(96762),i=t(29021),l=t.n(i),u=t(48786),c=t(31495),d=t(60125),p=t(67619),_=t(69842),f=t(30802),g=t(76856);async function S(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,_.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{dbFullName:a,dbSlug:s,dbId:n,tableNameOld:o,payload:i,delegated:S}=e.body,{tableName:A,tableFullName:m,tableDescription:b}=i;if(!A?.match(/./)||A.match(/[^a-zA-Z0-9-_]/))return r.json({success:!1,msg:"Bad Input"});try{let i=await (0,d.A)({databaseSlug:s,request:e,user:t,query:e.query});if(!await (0,c.A)({user:t,confirmedDelegetedUser:i,database:s,table:o,dbId:n,priviledgeRegex:/Edit Table/i}))return console.log("User Unauthorized"),r.json({success:!1,msg:"Unauthorized"});let _=i?.rootUserId?i.rootUserId:t.id,D=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${_}/main.json`,y=JSON.parse(l().readFileSync(D,"utf8")),E=y.filter(e=>e.dbFullName===a)[0].tables.filter((e,r)=>{if(e.tableName===o)return!0});if(E&&E[0])E[0].tableName=A,E[0].tableNameOld=o,E[0].tableFullName=m,b&&(E[0].tableDescription=b),E[0].childrenTables&&E[0].childrenTables.forEach(e=>{try{y.filter(r=>r.dbFullName===e.dbNameFull)[0].tables.filter(r=>{if(r.tableName===e.tableName)return!0})[0].childTableName=A}catch(e){(0,p.A)({component:"/api/updateUserTable/lines-131-138",message:e.message,user:t})}});else throw Error("Table Does not exist");l().writeFileSync(`${D}`,JSON.stringify(y),"utf8"),await (0,f.A)({dbFullName:"datasquirel",tableName:"user_database_tables",identifierColumnName:"table_slug",identifierValue:o,data:{table_name:m,table_slug:A,table_description:b}}),S&&i&&await (0,u.A)("UPDATE delegated_user_tables SET `table`=? WHERE delegated_user_id=? AND root_user_id=? AND `table`=? AND `database`=?",[A,t.id,i.rootUserId,o,s]);let h=a.replace(/[^a-z\_0-9]/g,"");await (0,g.A)({userId:_,targetDatabase:h}),r.json({success:!0})}catch(e){(0,p.A)({component:"/api/updateUserTable/main-catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!"})}}let A=(0,o.M)(a,"default"),m=(0,o.M)(a,"config"),b=new s.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/updateUserTable",pathname:"/api/updateUserTable",bundlePath:"",filename:""},userland:a})},31495:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(48786),s=t(67619);async function n({user:e,confirmedDelegetedUser:r,database:t,table:n,priviledgeRegex:o,dbId:i}){let l,u;try{r?.delegated?(l=await (0,a.A)("SELECT priviledge FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=? AND `database`=? AND `table`=?",[r.rootUserId,e.id,t,n]),u=l[0]?.priviledge?.match(o)):(l=await (0,a.A)("SELECT table_slug FROM user_database_tables WHERE user_id=? AND table_slug=? AND db_id=?",[e.id,n,i]),u=!0)}catch(r){(0,s.A)({component:"checkUserRights",message:r.message,user:e})}return!!l&&!!l[0]&&!!u}},60125:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(95969);async function s({request:e,databaseSlug:r,user:t,query:s}){try{let n=t.id,o=null;if(!s?.delegated)return{dbUserId:n};let i=s.dbUserId,l=`${process.env.DSQL_USER_DB_PREFIX}${i}_${r}`,u=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${l}`;try{if(!e.cookies?.[u])throw Error("Cookie not present");let t=(0,a.A)({encryptedString:e.cookies[u]});if(!t)throw Error("Invalid Token");if((o=JSON.parse(t)).databaseSlug===r)return{dbUserId:n=o.rootUserId,rootUserId:o.rootUserId,rootUserName:o.rootUserName,rootUserEmail:o.rootUserEmail,rootUserImage:o.rootUserImage,databaseFullName:o.databaseFullName,databaseSlug:o.databaseSlug,allowedTables:o.allowedTables,priviledges:o.priviledges,database:o.databaseSlug,delegated:!0}}catch(e){}return{dbUserId:n}}catch(e){return null}}},69842:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(39908),s=t.n(a);async function n(e,r,t){let a=s()({req:e,level:t?"deep":void 0});return console.log(a),a.payload}},30802:(e,r,t)=>{t.d(r,{A:()=>c});var a=t(72583),s=t.n(a),n=t(92652),o=t(48786),i=t(37897),l=t(68829),u=t(62050);async function c({dbContext:e,paradigm:r,dbFullName:t,tableName:a,data:c,tableSchema:d,identifierColumnName:p,identifierValue:_,encryptionKey:f,encryptionSalt:g,useLocal:S}){if(!c||!Object.keys(c).length)return null;let A=!!S||!e?.match(/dsql.user/i)&&(!t||!!t.match(/^datasquirel$/)),m=S?u.A:A?o.A:i.A,b=Object.keys(c),D=[],y=[];for(let e=0;e<b.length;e++)try{let r=b[e],t=c[r],a=d?d?.fields?.filter(e=>e.fieldName===r):null,o=a&&a[0]?a[0]:null;if(null==t||void 0==t)continue;let i=/<[^>]+>/g;(o?.richText||String(t).match(i))&&(t=s()(t,n.A)),o?.encrypted&&(t=(0,l.A)({data:t,encryptionKey:f,encryptionSalt:g})),"object"==typeof t&&(t=JSON.stringify(t)),o?.pattern&&!new RegExp(o.pattern,o.patternFlags||"").test(t)&&(console.log("DSQL: Pattern not matched =>",t),t=""),"string"==typeof t&&t.match(/^null$/i)&&(t={toSqlString:function(){return"NULL"}}),"string"!=typeof t||t.match(/./i)||(t={toSqlString:function(){return"NULL"}}),D.push(`\`${r}\`=?`),"number"==typeof t?y.push(String(t)):y.push(t)}catch(e){console.log("DSQL: Error in parsing data keys in update function =>",e.message);continue}D.push(`date_updated='${Date()}'`),D.push(`date_updated_code='${Date.now()}'`);let E=`UPDATE \`${t}\`.\`${a}\` SET ${D.join(",")} WHERE \`${p}\`=?`;return y.push(_),A?await m(E,y):await m({paradigm:r,queryString:E,queryValues:y})}},92652:(e,r,t)=>{t.d(r,{A:()=>a});let a={allowedTags:["b","i","em","strong","a","p","span","ul","ol","li","h1","h2","h3","h4","h5","h6","img","div","button","pre","code","br"],allowedAttributes:{a:["href"],img:["src","alt","width","height","class","style"],"*":["style","class"]}}},95969:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(55511),s=t(79428),n=t(32443);function o({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let{key:o,salt:i,keyLen:l,algorithm:u,bufferAllocSize:c}=(0,n.A)({encryptionKey:r});if(!o?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!i?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let d=(0,a.scryptSync)(o,i,l),p=s.Buffer.alloc(c,0),_=(0,a.createDecipheriv)(u,d,p);try{let r=_.update(e,"hex","utf8");return r+=_.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}},68829:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(55511),s=t(79428),n=t(32443);function o({data:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encryption string is invalid"),e;let{key:o,salt:i,keyLen:l,algorithm:u,bufferAllocSize:c}=(0,n.A)({encryptionKey:r});if(!o?.match(/.{8,}/))return console.log("Encryption key is invalid"),e;if(!i?.match(/.{8,}/))return console.log("Encryption salt is invalid"),e;let d=(0,a.scryptSync)(o,i,l),p=s.Buffer.alloc(c,0),_=(0,a.createCipheriv)(u,d,p);try{let r=_.update(e,"utf8","hex");return r+=_.final("hex")}catch(r){return console.log("Error in encrypting =>",r.message),e}}},48786:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(7447);async function s(...e){let r=(0,a.A)();try{if(!r)throw Error("No Connection provided to DB_HANDLER function!");let t=await r.query(...e);return JSON.parse(JSON.stringify(t))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}finally{await r?.end()}}},37897:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(64292),s=t(7447);async function n({paradigm:e,queryString:r,queryValues:t}){let n="Read Only"==e?(0,s.A)({ro:!0}):(0,s.A)({fa:!0});try{return await (0,a.A)(n,r,t)}catch(e){return console.log(`DSQL_USER_DB_HANDLER Error: ${e.message}`),null}finally{n?.end()}}},62050:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(7447);async function s(...e){let r=(0,a.A)();console.log("Querying ...");try{let t=await r.query(...e);return JSON.parse(JSON.stringify(t))}catch(e){return console.log("DB Error =>",e.message),{success:!1,error:e.message}}finally{await r?.end()}}},64292:(e,r,t)=>{async function a(e,r,t){try{if("string"==typeof r){let a=await e.query(s(r),t);return JSON.parse(JSON.stringify(a))}if("object"!=typeof r)return null;{let t=[];for(let a=0;a<r.length;a++)try{let n=r[a],o=await e.query(s(n.query),n.values);t.push(JSON.parse(JSON.stringify(o)))}catch(e){t.push(null)}return t}}catch(e){return null}finally{e.end()}}function s(e){return e.replace(/\n/gm,"").replace(/ {2,}/g,"").trim()}t.d(r,{A:()=>a})},7447:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(9973),s=t.n(a);function n(e){return e?.ro?DSQL_READ_ONLY_DB_CONN||s()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_READ_ONLY_USERNAME,password:process.env.DSQL_DB_READ_ONLY_PASSWORD,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}}):e?.fa?global.DSQL_FULL_ACCESS_DB_CONN||s()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_FULL_ACCESS_USERNAME,password:process.env.DSQL_DB_FULL_ACCESS_PASSWORD,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}}):global.DSQL_DB_CONN||s()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:e?.noDb?void 0:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}})}},32443:(e,r,t)=>{t.d(r,{A:()=>a});function a(e){return{key:e?.encryptionKey||process.env.DSQL_ENCRYPTION_PASSWORD,keyLen:process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24,salt:e?.encryptionSalt||process.env.DSQL_ENCRYPTION_SALT,apiKey:e?.apiKey||process.env.DSQL_API_KEY,algorithm:e?.algorithm||process.env.DSQL_ENCRYPTION_ALGORITHM||"aes-192-cbc",bufferAllocSize:e?.bufferAllocSize||(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE?function(e,r){try{let r=Number(e);if("number"!=typeof r||isNaN(r))return 0;return Math.round(r)}catch(e){return console.log(`Numberfy ERROR: ${e.message}`),0}}(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE):void 0)||16}}},2706:(e,r)=>{Object.defineProperty(r,"A",{enumerable:!0,get:function(){return t}});var t=function(e){return e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE",e}({})},89947:(e,r,t)=>{e.exports=t(75600)}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),a=r.X(0,[9814],()=>t(81147));module.exports=a})();