dsql-admin/dsql-app/.local_dist/server/pages/api/sync-table-data.js
Benjamin Toby 7a06e83a46 Updates
2024-12-08 20:56:57 +01:00

16 lines
7.3 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")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},41848:(e,r,t)=>{t.r(r),t.d(r,{config:()=>h,default:()=>$,routeModule:()=>N});var a={};t.r(a),t.d(a,{default:()=>g});var s=t(89947),l=t(2706),o=t(96762),n=t(48225),i=t.n(n),d=t(6420),u=t.n(d),c=t(27825),p=t.n(c),m=t(51348),b=t.n(m),f=t(27444);t(81630),t(48225);let _=t(51348);async function E({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],l=await _(`SELECT * FROM delegated_user_tables WHERE delegated_user_id='${r.id}' AND \`database\`='${s.db}' AND \`table\`='${s.table}'`);if(!l?.[0]?.root_user_id)throw Error("Unauthorized!");let o=l[0];a||(a=o.root_user_id)}return{isPriviledged:!0,rootUserId:a}}catch(e){return{isPriviledged:!1,rootUserId:a,error:e.message}}}var S=t(2258);async function g(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await u()(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{paradigm:a,src:s,dst:l}=e.body;try{let o=await E({tbls:[{db:s.db,table:s.table},{db:l.db,table:l.table}],user:t,query:e.query});if(!o.rootUserId||!o.isPriviledged)throw Error(o.error||"Unauthorized!");let n=(0,S.default)({userId:o.rootUserId});if(!n)throw Error("User Schema Data not Found!");let i=`${process.env.DSQL_USER_DB_PREFIX}${o.rootUserId}_${l.db}`,d=`${process.env.DSQL_USER_DB_PREFIX}${o.rootUserId}_${s.db}`,u=n.find(e=>e.dbFullName==i),c=n.find(e=>e.dbFullName==d),m=u?.tables.find(e=>e.tableName==l.table),_=c?.tables.find(e=>e.tableName==s.table);if(!m||!_)throw Error("Operation Failed! Tables Schemas not present.");if(!p().isEqual(_.fields,m.fields))throw Error("Tables incompatible");switch(a){case"Push":let g=await b()(`SELECT * FROM ${d}.${s.table}`),$=await (0,f.A)({dbName:l.db,paradigm:"import",payload:g,tableName:l.table,userId:o.rootUserId,payloadType:"object"});return await y({dbName:l.db,tableName:l.table,data:g,userId:o.rootUserId}),r.json({success:!0,payload:$});case"Pull":let h=await b()(`SELECT * FROM ${i}.${l.table}`),N=await (0,f.A)({dbName:s.db,paradigm:"import",payload:h,tableName:s.table,userId:o.rootUserId,payloadType:"object"});return await y({dbName:s.db,tableName:s.table,data:h,userId:o.rootUserId}),r.json({success:!0,payload:N})}r.json({success:!1})}catch(e){console.log(e.message),i()({component:"/api/sync-table-data",message:e.message,user:t}),r.json({success:!1,msg:e.message})}}async function y({dbName:e,tableName:r,data:t,userId:a}){let s=`${process.env.DSQL_USER_DB_PREFIX}${a}_${e.replace(/ /g,"")}`,l=t.map(e=>e.id),o=`DELETE FROM ${s}.${r}`;l?.[0]&&(o+=` WHERE id NOT IN (${l.join(",")})`),await b()(o)}let $=(0,o.M)(a,"default"),h=(0,o.M)(a,"config"),N=new s.PagesAPIRouteModule({definition:{kind:l.A.PAGES_API,page:"/api/sync-table-data",pathname:"/api/sync-table-data",bundlePath:"",filename:""},userland:a})},27444:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(47670),s=t.n(a);let l=t(48225),o=t(51348);async function n({paradigm:e,userId:r,dbName:t,tableName:a,payload:n,payloadType:i}){let d={};try{let l=`${process.env.DSQL_USER_DB_PREFIX}${r}_${t.replace(/ /g,"")}`;switch(e){case"export":let u=await o(`SELECT * FROM \`${l}\`.\`${a}\``);d.tableData=u;break;case"import":let c="base64"==i?Buffer.from(n,"base64").toString():n,p="object"===i?n:JSON.parse(c);for(let e=0;e<p.length;e++){let r=p[e],t=await s()({data:r,dbFullName:l,tableName:a,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 l({component:"/functions/backend/importExportTableDataFn",message:e.message}),null}}},6420:(e,r,t)=>{t(81630);let a=t(51348),s=t(72835),l=t(29021),o=t(31341);async function n(e,r,t,n){let{keyCookieName:i,csrfCookieName:d}=o();if(!e.cookies?.[i]?.match(/./))return null;let u=s({encryptedString:e.cookies[i]});if(!u)return null;let c=JSON.parse(u);if(!c.csrf_k||t&&!e.headers["x-csrf-auth"]?.match(RegExp(`${c.csrf_k}`)))return null;let p=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!p)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(t&&!l.existsSync(`${p}/${c.csrf_k}`))return null;if(0==c.verification_status&&!t){let e=await a(`SELECT verification_status FROM users WHERE id='${c.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return c?.date&&Date.now()-c.date>6048e5?null:c}e.exports=n},31341:e=>{e.exports=function(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=r;e?.userId&&(s+=`user_${e.userId}_`),e?.database&&(s+=`${e.database}_`),s+=t;let l=r;return e?.userId&&(l+=`user_${e.userId}_`),e?.database&&(l+=`${e.database}_`),{keyCookieName:s,csrfCookieName:l+=a}}},2258:(e,r,t)=>{t.r(r),t.d(r,{default:()=>o});let a=t(48225),s=t(29021),l=t(33873);function o({userId:e}){try{let r=l.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:"grabUserSchemaData",message:e.message}),null}}},48225:(e,r,t)=>{let a=t(29021),{IncomingMessage:s}=t(81630);e.exports=async function({user:e,message:r,component:t,noMail:s,req:l}){let o=new Date,n=(()=>{if(!l)return null;try{let e=l.headers["x-forwarded-for"],r=l.headers["x-real-ip"],t=l.headers["cf-connecting-ip"],a=Array.isArray(e)?e[0]:e?.split(",")[0],s=t||a||r||l.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}`),l?.url&&(s+=`
URL: ${l.url}`),l?.body&&(s+=`
Request Body: ${JSON.stringify(l.body,null,4)}`),n&&(s+=`
IP: ${n}`),s+=`
Date: ${o.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)}}},72835:(e,r,t)=>{let{scryptSync:a,createDecipheriv:s}=t(55511),{Buffer:l}=t(79428);e.exports=({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,n=t||process.env.DSQL_ENCRYPTION_SALT,i=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(!n?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let d=s("aes-192-cbc",a(o,n,i),l.alloc(16,0));try{let r=d.update(e,"hex","utf8");return r+=d.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,[4050],()=>t(41848));module.exports=a})();