1 line
7.7 KiB
JavaScript
1 line
7.7 KiB
JavaScript
"use strict";(()=>{var e={};e.id=861,e.ids=[861],e.modules={39908:e=>{e.exports=require("@moduletrace/datasquirel/dist/users/user-auth")},27825:e=>{e.exports=require("lodash")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},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}}})},15679:(e,r,t)=>{t.r(r),t.d(r,{config:()=>E,default:()=>m,routeModule:()=>A});var s={};t.r(s),t.d(s,{default:()=>b});var a=t(89947),o=t(2706),n=t(96762),l=t(48786),i=t(60125),d=t(67619),c=t(69842),u=t(4820),_=t(29021),p=t.n(_),f=t(27825),D=t.n(f),S=t(76856);async function b(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,c.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let s=e.body.schema,a=e.body.database,o=e.body.table;e.body.delegated;try{let n;let d=await (0,i.A)({request:e,user:t,databaseSlug:a,query:e.query}),c=d?.rootUserId?d.rootUserId:t.id,_=`datasquirel_user_${c}_${a}`.replace(/[^a-z\_0-9]/g,"");if(d?.delegated){let e=await (0,l.A)("SELECT * FROM user_users WHERE invited_user_id=? AND user_id=? AND user_priviledge LIKE '%Edit Tables%'",[t.id,d.rootUserId]);e&&e[0]&&(n=!0)}else n=!0;if(!n)return r.json({success:!1,msg:"Unauthorized"});let f=(0,u.A)({userId:c});if(!f)throw Error("Database schema not found!");if(!function({dbSchemaData:e,delegatedUser:r,dbFullName:t,schema:s,table:a,userId:o}){let n=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${o}/main.json`;try{if(!e)throw Error("Database schema not found!");let o=D().cloneDeep(e),l=o.find(e=>e.dbFullName==t);if(!l)throw Error("Old Database schema not found!");let i=o.findIndex(e=>e.dbFullName==t);if(i<0)throw Error("Target Database index not found!");let d=s.find(e=>e.dbFullName===t);if(!d)throw Error("New Database schema not found!");if(r?.delegated&&(r.priviledges?.match(/create.?table/i),!r.priviledges?.match(/edit.?table/i)))throw Error("Unauthorized!");let c=d.tables.find(e=>e.tableName==a.tableName);if(!c)throw Error("Table schema not found!");let u=l.tables.findIndex(e=>e.tableName==a.tableName);c&&(o[i].tables[u]=c);let _=l?.childrenDatabases;_?.forEach(e=>{let r=o.findIndex(r=>r.dbFullName===e.dbFullName);o[r]&&l&&(o[r].tables=l.tables.map(e=>{let r=D().cloneDeep(e);return delete r.childrenTables,r}))});let f=o?.[i]?.tables?.[u]?.childrenTables;return f&&f.forEach(e=>{let r=o.findIndex(r=>r.dbFullName==e.dbNameFull),t=o[r]?.tables?.findIndex(r=>r.tableName===e.tableName);o[r]?.tables[t]&&(o[r].tables[t].fields=D().cloneDeep(c.fields),o[r].tables[t].indexes=D().cloneDeep(c.indexes))}),p().writeFileSync(`${n}`,JSON.stringify(o),"utf8"),!0}catch(e){return console.log("updateSchemaData.ts ERROR: ",e.message),!1}}({dbFullName:_,dbSchemaData:f,delegatedUser:d,schema:s,table:o,userId:c}))throw Error("Update Failed!");return await (0,S.A)({userId:c,targetDatabase:_}),r.json({success:!0})}catch(e){console.log("updateDbSchemaDataForUser ERROR:",e.message),(0,d.A)({component:"/api/updateDbSchemaDataForUser/main-catch-error",message:e.message,user:t}),r.json({success:!1,msg:e.message})}}let m=(0,n.M)(s,"default"),E=(0,n.M)(s,"config"),A=new a.PagesAPIRouteModule({definition:{kind:o.A.PAGES_API,page:"/api/updateDbSchemaDataForUser",pathname:"/api/updateDbSchemaDataForUser",bundlePath:"",filename:""},userland:s})},60125:(e,r,t)=>{t.d(r,{A:()=>a});var s=t(95969);async function a({request:e,databaseSlug:r,user:t,query:a}){try{let o=t.id,n=null;if(!a?.delegated)return{dbUserId:o};let l=a.dbUserId,i=`${process.env.DSQL_USER_DB_PREFIX}${l}_${r}`,d=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${i}`;try{if(!e.cookies?.[d])throw Error("Cookie not present");let t=(0,s.A)({encryptedString:e.cookies[d]});if(!t)throw Error("Invalid Token");if((n=JSON.parse(t)).databaseSlug===r)return{dbUserId:o=n.rootUserId,rootUserId:n.rootUserId,rootUserName:n.rootUserName,rootUserEmail:n.rootUserEmail,rootUserImage:n.rootUserImage,databaseFullName:n.databaseFullName,databaseSlug:n.databaseSlug,allowedTables:n.allowedTables,priviledges:n.priviledges,database:n.databaseSlug,delegated:!0}}catch(e){}return{dbUserId:o}}catch(e){return null}}},69842:(e,r,t)=>{t.d(r,{A:()=>o});var s=t(39908),a=t.n(s);async function o(e,r,t){let s=a()({req:e,level:t?"deep":void 0});return console.log(s),s.payload}},4820:(e,r,t)=>{t.d(r,{A:()=>i});var s=t(67619),a=t(29021),o=t.n(a),n=t(33873),l=t.n(n);function i({userId:e}){try{let r=l().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return JSON.parse(o().readFileSync(r,"utf-8"))}catch(e){return(0,s.A)({component:"grabUserSchemaData",message:e.message}),null}}},95969:(e,r,t)=>{t.d(r,{A:()=>n});var s=t(55511),a=t(79428),o=t(32443);function n({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let{key:n,salt:l,keyLen:i,algorithm:d,bufferAllocSize:c}=(0,o.A)({encryptionKey:r});if(!n?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!l?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let u=(0,s.scryptSync)(n,l,i),_=a.Buffer.alloc(c,0),p=(0,s.createDecipheriv)(d,u,_);try{let r=p.update(e,"hex","utf8");return r+=p.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}},48786:(e,r,t)=>{t.d(r,{A:()=>a});var s=t(7447);async function a(...e){let r=(0,s.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()}}},7447:(e,r,t)=>{t.d(r,{A:()=>o});var s=t(9973),a=t.n(s);function o(e){return e?.ro?DSQL_READ_ONLY_DB_CONN||a()({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||a()({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||a()({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:()=>s});function s(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),s=r.X(0,[9814],()=>t(15679));module.exports=s})(); |