dsql-admin/dsql-app/.local_dist/server/pages/api/updateDbSchemaDataForUser.js
Benjamin Toby 3ffd2d6777 Updates
2024-12-05 08:03:33 +01:00

16 lines
8.0 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")},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:()=>A,default:()=>v,routeModule:()=>P});var a={};t.r(a),t.d(a,{default:()=>y});var s=t(89947),o=t(2706),n=t(96762),l=t(51348),i=t.n(l),u=t(39831),d=t.n(u),c=t(77538),f=t.n(c),p=t(6420),m=t.n(p),b=t(49827),S=t(29021),_=t.n(S),h=t(27825),E=t.n(h);t(29021);let g=t(33873),{execSync:D}=t(79646);async function y(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await m()(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let a=e.body.schema,s=e.body.database,o=e.body.table;e.body.delegated;try{let n;let l=await d()({request:e,user:t,databaseSlug:s,query:e.query}),u=l?.rootUserId?l.rootUserId:t.id,c=`datasquirel_user_${u}_${s}`.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:u});if(!f)throw Error("Database schema not found!");if(!function({dbSchemaData:e,delegatedUser:r,dbFullName:t,schema:a,table:s,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=E().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 u=a.find(e=>e.dbFullName===t);if(!u)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 d=u.tables.find(e=>e.tableName==s.tableName);if(!d)throw Error("Table schema not found!");let c=l.tables.findIndex(e=>e.tableName==s.tableName);d&&(o[i].tables[c]=d);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=E().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=E().cloneDeep(d.fields),o[r].tables[t].indexes=E().cloneDeep(d.indexes))}),_().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:a,table:o,userId:u}))throw Error("Update Failed!");let p=g.join(process.cwd(),"/shell");D(`node createDbFromSchema.js --user ${u} --database ${c}`,{cwd:p}),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 v=(0,n.M)(a,"default"),A=(0,n.M)(a,"config"),P=new s.PagesAPIRouteModule({definition:{kind:o.A.PAGES_API,page:"/api/updateDbSchemaDataForUser",pathname:"/api/updateDbSchemaDataForUser",bundlePath:"",filename:""},userland:a})},39831:(e,r,t)=>{t(81630);let a=t(72907);e.exports=async function({request:e,databaseSlug:r,user:t,query:s}){try{let o=t.id,n=null;if(!s?.delegated)return{dbUserId:o};let l=s.dbUserId,i=`${process.env.DSQL_USER_DB_PREFIX}${l}_${r}`,u=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${i}`;try{if(!e.cookies?.[u])throw Error("Cookie not present");let t=a(e.cookies[u]);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}}},49827:(e,r,t)=>{t.r(r),t.d(r,{default:()=>n});let a=t(77538),s=t(29021),o=t(33873);function n({userId:e}){try{let r=o.resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return JSON.parse(s.readFileSync(r,"utf-8"))}catch(e){return a({component:"/functions/backend/grabUserSchemaData",message:e.message}),null}}},77538:(e,r,t)=>{let a=t(29021),{IncomingMessage:s}=t(81630);e.exports=async function({user:e,message:r,component:t,noMail:s,req:o}){let n=new Date,l=(()=>{if(!o)return null;try{let e=o.headers["x-forwarded-for"],r=o.headers["x-real-ip"],t=o.headers["cf-connecting-ip"],a=Array.isArray(e)?e[0]:e?.split(",")[0],s=t||a||r||o.socket.remoteAddress;if(!s)return null;return String(s)}catch(e){return null}})();try{let s=`🚀 SERVER ERROR ===========================
Error Message: ${r}
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(s+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),o?.url&&(s+=`
URL: ${o.url}`),o?.body&&(s+=`
Request Body: ${JSON.stringify(o.body,null,4)}`),l&&(s+=`
IP: ${l}`),s+=`
Date: ${n.toDateString()}
========================================`,a.existsSync("./.tmp/error.log")||a.writeFileSync("./.tmp/error.log","","utf-8");let i=a.readFileSync("./.tmp/error.log","utf-8");a.writeFileSync("./.tmp/error.log",s),a.appendFileSync("./.tmp/error.log",`
${i}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},6420:(e,r,t)=>{t(81630);let a=t(51348),s=t(72907),o=t(29021);async function n(e,r,t,n){if(!e.cookies?.datasquirelAuthKey?.match(/./))return null;let l=s(e.cookies.datasquirelAuthKey);if(!l)return null;let i=JSON.parse(l);if(!i.csrf_k||t&&!e.headers["x-csrf-auth"]?.match(RegExp(`${i.csrf_k}`)))return null;let u=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!u)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(t&&!o.existsSync(`${u}/${i.csrf_k}`))return null;if(0==i.verification_status&&!t){let e=await a(`SELECT verification_status FROM users WHERE id='${i.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return i?.date&&Date.now()-i.date>6048e5?null:i}e.exports=n},72907:(e,r,t)=>{let{scryptSync:a,createDecipheriv:s}=t(55511),{Buffer:o}=t(79428);e.exports=e=>{let r=s("aes-192-cbc",a(process.env.DSQL_ENCRYPTION_PASSWORD||"",process.env.DSQL_ENCRYPTION_SALT||"",24),o.alloc(16,0));try{let t=r.update(e,"hex","utf8");return t+=r.final("utf8")}catch(e){return null}}},51348:(e,r,t)=>{let a=t(9973),s=t(21580),o=a({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4",ssl:s()}});async function n(...e){try{let r=await o.query(...e);return await o.end(),JSON.parse(JSON.stringify(r))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}}e.exports=n},21580:(e,r,t)=>{let a=t(29021);e.exports=function(){let e=process.env.DSQL_SSL_DIR;if(!e?.match(/./))return;let r=`${e}/ca-cert.pem`;if(!a.existsSync(r)){console.log(`${r} does not exist`);return}return{ca:a.readFileSync(`${e}/ca-cert.pem`)}}},2706:(e,r)=>{var t;Object.defineProperty(r,"A",{enumerable:!0,get:function(){return t}}),function(e){e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE"}(t||(t={}))},89947:(e,r,t)=>{e.exports=t(75600)}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=r(r.s=23249);module.exports=t})();