1 line
6.8 KiB
JavaScript
1 line
6.8 KiB
JavaScript
"use strict";(()=>{var e={};e.id=861,e.ids=[861],e.modules={27825:e=>{e.exports=require("lodash")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},79428:e=>{e.exports=require("buffer")},79646:e=>{e.exports=require("child_process")},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:()=>g,default:()=>h,routeModule:()=>D});var a={};t.r(a),t.d(a,{default:()=>S});var s=t(89947),n=t(2706),o=t(96762),l=t(48786),i=t(60125),d=t(67619),u=t(69842),c=t(4820),f=t(29021),_=t.n(f),b=t(27825),E=t.n(b),m=t(76856);t(29021),t(33873);let{execSync:p}=t(79646);async function S(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,u.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let a=e.body.schema,s=e.body.database,n=e.body.table;e.body.delegated;try{let o;let d=await (0,i.A)({request:e,user:t,databaseSlug:s,query:e.query}),u=d?.rootUserId?d.rootUserId:t.id,f=`datasquirel_user_${u}_${s}`.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]&&(o=!0)}else o=!0;if(!o)return r.json({success:!1,msg:"Unauthorized"});let b=(0,c.A)({userId:u});if(!b)throw Error("Database schema not found!");if(!function({dbSchemaData:e,delegatedUser:r,dbFullName:t,schema:a,table:s,userId:n}){let o=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${n}/main.json`;try{if(!e)throw Error("Database schema not found!");let n=E().cloneDeep(e),l=n.find(e=>e.dbFullName==t);if(!l)throw Error("Old Database schema not found!");let i=n.findIndex(e=>e.dbFullName==t);if(i<0)throw Error("Target Database index not found!");let d=a.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 u=d.tables.find(e=>e.tableName==s.tableName);if(!u)throw Error("Table schema not found!");let c=l.tables.findIndex(e=>e.tableName==s.tableName);u&&(n[i].tables[c]=u);let f=l?.childrenDatabases;f?.forEach(e=>{let r=n.findIndex(r=>r.dbFullName===e.dbFullName);n[r]&&l&&(n[r].tables=l.tables.map(e=>{let r=E().cloneDeep(e);return delete r.childrenTables,r}))});let b=n?.[i]?.tables?.[c]?.childrenTables;return b&&b.forEach(e=>{let r=n.findIndex(r=>r.dbFullName==e.dbNameFull),t=n[r]?.tables?.findIndex(r=>r.tableName===e.tableName);n[r]?.tables[t]&&(n[r].tables[t].fields=E().cloneDeep(u.fields),n[r].tables[t].indexes=E().cloneDeep(u.indexes))}),_().writeFileSync(`${o}`,JSON.stringify(n),"utf8"),!0}catch(e){return console.log("updateSchemaData.ts ERROR: ",e.message),!1}}({dbFullName:f,dbSchemaData:b,delegatedUser:d,schema:a,table:n,userId:u}))throw Error("Update Failed!");return await (0,m.A)({userId:u,targetDatabase:f}),r.json({success:!0})}catch(e){console.log("updateDbSchemaDataForUser ERROR:",e.message),(0,d.default)({component:"/api/updateDbSchemaDataForUser/main-catch-error",message:e.message,user:t}),r.json({success:!1,msg:e.message})}}let h=(0,o.M)(a,"default"),g=(0,o.M)(a,"config"),D=new s.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/updateDbSchemaDataForUser",pathname:"/api/updateDbSchemaDataForUser",bundlePath:"",filename:""},userland:a})},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 l=s.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,a.A)({encryptedString:e.cookies[d]});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:()=>i});var a=t(48786),s=t(95969),n=t(29021),o=t.n(n),l=t(89159);async function i(e,r,t){let{keyCookieName:n,csrfCookieName:i}=(0,l.A)();if(!e.cookies?.[n]?.match(/./))return null;let d=(0,s.A)({encryptedString:e.cookies[n]});if(!d)return null;let u=JSON.parse(d);if(!u.csrf_k||t&&!Object.keys(e.headers).find(e=>e.includes(u.csrf_k)))return null;let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(t&&!o().existsSync(`${c}/${u.csrf_k}`))return null;if(0==u.verification_status&&!t){let e=await (0,a.A)(`SELECT verification_status FROM users WHERE id='${u.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return u?.date&&Date.now()-u.date>6048e5?null:u}},89159:(e,r,t)=>{t.d(r,{A:()=>a});function a(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=process.env.DSQL_COOKIES_KEY_NAME||"key",a=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",s=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",n=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),o=r;e?.userId&&(o+=`user_${e.userId}_`),n&&(o+=`${n}_`),o+=t;let l=r;e?.userId&&(l+=`user_${e.userId}_`),n&&(l+=`${n}_`),l+=a;let i=r;return e?.userId&&(i+=`user_${e.userId}_`),n&&(i+=`${n}_`),{keyCookieName:o,csrfCookieName:l,oneTimeCodeName:i+=s}}},4820:(e,r,t)=>{t.d(r,{A:()=>i});var a=t(67619),s=t(29021),n=t.n(s),o=t(33873),l=t.n(o);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(n().readFileSync(r,"utf-8"))}catch(e){return(0,a.default)({component:"grabUserSchemaData",message:e.message}),null}}},48786:(e,r,t)=>{t.d(r,{A:()=>a});async function a(...e){let r=global.DSQL_DB_CONN;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()}}},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,[2282],()=>t(15679));module.exports=a})(); |