dsql-admin/dsql-app/.local_dist/server/pages/api/sync-table-data.js
Benjamin Toby 5900263b62 Updates
2025-01-14 07:19:19 +01:00

16 lines
7.9 KiB
JavaScript

"use strict";(()=>{var e={};e.id=538,e.ids=[538],e.modules={27825:e=>{e.exports=require("lodash")},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")},69653:(e,r,t)=>{t.r(r),t.d(r,{config:()=>S,default:()=>b,routeModule:()=>g});var a={};t.r(a),t.d(a,{default:()=>m});var s=t(89947),o=t(2706),i=t(96762),n=t(67619),l=t(69842),d=t(27825),u=t.n(d),c=t(48786),p=t(33170);async function _({tbls:e,user:r,query:t}){let a;if(!t?.delegated)return{isPriviledged:!0,rootUserId:r.id};try{for(let t=0;t<e.length;t++){let s=e[t],o=await (0,c.A)(`SELECT * FROM delegated_user_tables WHERE delegated_user_id='${r.id}' AND \`database\`='${s.db}' AND \`table\`='${s.table}'`);if(!o?.[0]?.root_user_id)throw Error("Unauthorized!");let i=o[0];a||(a=i.root_user_id)}return{isPriviledged:!0,rootUserId:a}}catch(e){return{isPriviledged:!1,rootUserId:a,error:e.message}}}var f=t(4820);async function m(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,l.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{paradigm:a,src:s,dst:o}=e.body;try{let i=await _({tbls:[{db:s.db,table:s.table},{db:o.db,table:o.table}],user:t,query:e.query});if(!i.rootUserId||!i.isPriviledged)throw Error(i.error||"Unauthorized!");let n=(0,f.A)({userId:i.rootUserId});if(!n)throw Error("User Schema Data not Found!");let l=`${process.env.DSQL_USER_DB_PREFIX}${i.rootUserId}_${o.db}`,d=`${process.env.DSQL_USER_DB_PREFIX}${i.rootUserId}_${s.db}`,m=n.find(e=>e.dbFullName==l),b=n.find(e=>e.dbFullName==d),S=m?.tables.find(e=>e.tableName==o.table),g=b?.tables.find(e=>e.tableName==s.table);if(!S||!g)throw Error("Operation Failed! Tables Schemas not present.");if(!u().isEqual(g.fields,S.fields))throw Error("Tables incompatible");switch(a){case"Push":let y=await (0,c.A)(`SELECT * FROM ${d}.${s.table}`),h=await (0,p.A)({dbName:o.db,paradigm:"import",payload:y,tableName:o.table,userId:i.rootUserId,payloadType:"object"});return await E({dbName:o.db,tableName:o.table,data:y,userId:i.rootUserId}),r.json({success:!0,payload:h});case"Pull":let $=await (0,c.A)(`SELECT * FROM ${l}.${o.table}`),v=await (0,p.A)({dbName:s.db,paradigm:"import",payload:$,tableName:s.table,userId:i.rootUserId,payloadType:"object"});return await E({dbName:s.db,tableName:s.table,data:$,userId:i.rootUserId}),r.json({success:!0,payload:v})}r.json({success:!1})}catch(e){console.log(e.message),(0,n.default)({component:"/api/sync-table-data",message:e.message,user:t}),r.json({success:!1,msg:e.message})}}async function E({dbName:e,tableName:r,data:t,userId:a}){let s=`${process.env.DSQL_USER_DB_PREFIX}${a}_${e.replace(/ /g,"")}`,o=t.map(e=>e.id),i=`DELETE FROM ${s}.${r}`;o?.[0]&&(i+=` WHERE id NOT IN (${o.join(",")})`),await (0,c.A)(i)}let b=(0,i.M)(a,"default"),S=(0,i.M)(a,"config"),g=new s.PagesAPIRouteModule({definition:{kind:o.A.PAGES_API,page:"/api/sync-table-data",pathname:"/api/sync-table-data",bundlePath:"",filename:""},userland:a})},33170:(e,r,t)=>{t.d(r,{A:()=>i});var a=t(98e3),s=t(67619),o=t(48786);async function i({paradigm:e,userId:r,dbName:t,tableName:i,payload:n,payloadType:l}){let d={};try{let s=`${process.env.DSQL_USER_DB_PREFIX}${r}_${t.replace(/ /g,"")}`;switch(e){case"export":let u=await (0,o.A)(`SELECT * FROM \`${s}\`.\`${i}\``);d.tableData=u;break;case"import":let c="base64"==l?Buffer.from(n,"base64").toString():n,p="object"===l?n:JSON.parse(c);for(let e=0;e<p.length;e++){let r=p[e],t=await (0,a.A)({data:r,dbFullName:s,tableName:i,dbContext:"Dsql User",paradigm:"Full Access",duplicateColumnName:"id",duplicateColumnValue:r?.id,update:!0});if(t.error)throw Error(t.error)}break;default:return null}return d}catch(e){return(0,s.default)({component:"/functions/backend/importExportTableDataFn",message:e.message}),null}}},69842:(e,r,t)=>{t.d(r,{A:()=>l});var a=t(48786),s=t(95969),o=t(29021),i=t.n(o),n=t(89159);async function l(e,r,t){try{let{keyCookieName:o,csrfCookieName:l}=(0,n.A)();if(!e.cookies?.[o]?.match(/./))throw Error("Request Cookies not Found!");let d=(0,s.A)({encryptedString:e.cookies[o]});if(!d)throw Error("Couldn't decrypt auth cookie!");let u=JSON.parse(d);if(!u.csrf_k)throw Error("No `csrf_k` present in user Object!");if(t&&!Object.keys(e.headers).find(e=>e.includes(u.csrf_k)))throw Error("Deep Auth Failed. `csrf` key not found in request headers!");let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)throw Error("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.");if(t&&!i().existsSync(`${c}/${u.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");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"])}if(u?.date&&Date.now()-u.date>6048e5)throw Error("Key expired!");return u}catch(e){return console.log(`userAuth Error: ${e.message}`),null}}},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",o=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),i=r;e?.userId&&(i+=`user_${e.userId}_`),o&&(i+=`${o}_`),i+=t;let n=r;e?.userId&&(n+=`user_${e.userId}_`),o&&(n+=`${o}_`),n+=a;let l=r;return e?.userId&&(l+=`user_${e.userId}_`),o&&(l+=`${o}_`),{keyCookieName:i,csrfCookieName:n,oneTimeCodeName:l+=s}}},4820:(e,r,t)=>{t.d(r,{A:()=>l});var a=t(67619),s=t(29021),o=t.n(s),i=t(33873),n=t.n(i);function l({userId:e}){try{let r=n().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,a.default)({component:"grabUserSchemaData",message:e.message}),null}}},67619:(e,r,t)=>{t.r(r),t.d(r,{default:()=>o});var a=t(29021),s=t.n(a);async function o({user:e,message:r,component:t,noMail:a,req:o}){let i=new Date,n=(()=>{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 a=`🚀 SERVER ERROR ===========================
Error Message: ${r}
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(a+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),o?.url&&(a+=`
URL: ${o.url}`),o?.body&&(a+=`
Request Body: ${JSON.stringify(o.body,null,4)}`),n&&(a+=`
IP: ${n}`),a+=`
Date: ${i.toDateString()}
========================================`,s().existsSync("./.tmp/error.log")||s().writeFileSync("./.tmp/error.log","","utf-8");let l=s().readFileSync("./.tmp/error.log","utf-8");s().writeFileSync("./.tmp/error.log",a),s().appendFileSync("./.tmp/error.log",`
${l}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},95969:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(55511),s=t(79428);function o({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let o=r||process.env.DSQL_ENCRYPTION_PASSWORD,i=t||process.env.DSQL_ENCRYPTION_SALT,n=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;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 l=(0,a.scryptSync)(o,i,n),d=s.Buffer.alloc(16,0),u=(0,a.createDecipheriv)("aes-192-cbc",l,d);try{let r=u.update(e,"hex","utf8");return r+=u.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),a=r.X(0,[1251],()=>t(69653));module.exports=a})();