dsql-admin/dsql-app/.local_dist/server/pages/api/updateDbSchemaDataForUser.js

1 line
7.0 KiB
JavaScript
Raw Normal View History

2024-12-09 12:27:08 +00:00
"use strict";(()=>{var e={};e.id=861,e.ids=[861],e.modules={60818:e=>{e.exports=require("dotenv")},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")},79646:e=>{e.exports=require("child_process")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},81630:e=>{e.exports=require("http")},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}}})},23249:(e,r,t)=>{t.r(r),t.d(r,{config:()=>P,default:()=>N,routeModule:()=>A});var s={};t.r(s),t.d(s,{default:()=>I});var a=t(89947),o=t(2706),n=t(96762),l=t(51348),i=t.n(l),d=t(39831),u=t.n(d),c=t(48225),f=t.n(c),p=t(6420),_=t.n(p),b=t(2258),m=t(29021),S=t.n(m),E=t(27825),h=t.n(E),D=t(19261),g=t.n(D);t(29021),t(33873);let{execSync:v}=t(79646);async function I(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await _()(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 l=await u()({request:e,user:t,databaseSlug:a,query:e.query}),d=l?.rootUserId?l.rootUserId:t.id,c=`datasquirel_user_${d}_${a}`.replace(/[^a-z\_0-9]/g,"");if(l?.delegated){let e=await i()("SELECT * FROM user_users WHERE invited_user_id=? AND user_id=? AND user_priviledge LIKE '%Edit Tables%'",[t.id,l.rootUserId]);e&&e[0]&&(n=!0)}else n=!0;if(!n)return r.json({success:!1,msg:"Unauthorized"});let f=(0,b.default)({userId:d});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=h().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 u=d.tables.find(e=>e.tableName==a.tableName);if(!u)throw Error("Table schema not found!");let c=l.tables.findIndex(e=>e.tableName==a.tableName);u&&(o[i].tables[c]=u);let f=l?.childrenDatabases;f?.forEach(e=>{let r=o.findIndex(r=>r.dbFullName===e.dbFullName);o[r]&&l&&(o[r].tables=l.tables.map(e=>{let r=h().cloneDeep(e);return delete r.childrenTables,r}))});let p=o?.[i]?.tables?.[c]?.childrenTables;return p&&p.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=h().cloneDeep(u.fields),o[r].tables[t].indexes=h().cloneDeep(u.indexes))}),S().writeFileSync(`${n}`,JSON.stringify(o),"utf8"),!0}catch(e){return console.log("updateSchemaData.js ERROR: ",e.message),!1}}({dbFullName:c,dbSchemaData:f,delegatedUser:l,schema:s,table:o,userId:d}))throw Error("Update Failed!");return await g()({userId:d,targetDatabase:c}),r.json({success:!0})}catch(e){console.log("updateDbSchemaDataForUser ERROR:",e.message),f()({component:"/api/updateDbSchemaDataForUser/main-catch-error",message:e.message,user:t}),r.json({success:!1,msg:e.message})}}let N=(0,n.M)(s,"default"),P=(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})},39831:(e,r,t)=>{t(81630);let s=t(72835);e.exports=async function({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("Cook