dsql-admin/dsql-app/.local_dist/server/pages/api/sync-table-data.js
Benjamin Toby 8b7ea057d0 Updates
2024-12-06 14:24:26 +01:00

16 lines
7.2 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:()=>N,default:()=>h,routeModule:()=>$});var a={};t.r(a),t.d(a,{default:()=>g});var s=t(89947),o=t(2706),l=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),f=t.n(m),b=t(27444);t(81630),t(48225);let E=t(51348);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 E(`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 l=o[0];a||(a=l.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:o}=e.body;try{let l=await _({tbls:[{db:s.db,table:s.table},{db:o.db,table:o.table}],user:t,query:e.query});if(!l.rootUserId||!l.isPriviledged)throw Error(l.error||"Unauthorized!");let n=(0,S.default)({userId:l.rootUserId});if(!n)throw Error("User Schema Data not Found!");let i=`${process.env.DSQL_USER_DB_PREFIX}${l.rootUserId}_${o.db}`,d=`${process.env.DSQL_USER_DB_PREFIX}${l.rootUserId}_${s.db}`,u=n.find(e=>e.dbFullName==i),c=n.find(e=>e.dbFullName==d),m=u?.tables.find(e=>e.tableName==o.table),E=c?.tables.find(e=>e.tableName==s.table);if(!m||!E)throw Error("Operation Failed! Tables Schemas not present.");if(!p().isEqual(E.fields,m.fields))throw Error("Tables incompatible");switch(a){case"Push":let g=await f()(`SELECT * FROM ${d}.${s.table}`),h=await (0,b.A)({dbName:o.db,paradigm:"import",payload:g,tableName:o.table,userId:l.rootUserId,payloadType:"object"});return await y({dbName:o.db,tableName:o.table,data:g,userId:l.rootUserId}),r.json({success:!0,payload:h});case"Pull":let N=await f()(`SELECT * FROM ${i}.${o.table}`),$=await (0,b.A)({dbName:s.db,paradigm:"import",payload:N,tableName:s.table,userId:l.rootUserId,payloadType:"object"});return await y({dbName:s.db,tableName:s.table,data:N,userId:l.rootUserId}),r.json({success:!0,payload:$})}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,"")}`,o=t.map(e=>e.id),l=`DELETE FROM ${s}.${r}`;o?.[0]&&(l+=` WHERE id NOT IN (${o.join(",")})`),await f()(l)}let h=(0,l.M)(a,"default"),N=(0,l.M)(a,"config"),$=new s.PagesAPIRouteModule({definition:{kind:o.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 o=t(48225),l=t(51348);async function n({paradigm:e,userId:r,dbName:t,tableName:a,payload:n,payloadType:i}){let d={};try{let o=`${process.env.DSQL_USER_DB_PREFIX}${r}_${t.replace(/ /g,"")}`;switch(e){case"export":let u=await l(`SELECT * FROM \`${o}\`.\`${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:o,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 o({component:"/functions/backend/importExportTableDataFn",message:e.message}),null}}},6420:(e,r,t)=>{t(81630);let a=t(51348),s=t(72835),o=t(29021),l=t(31341);async function n(e,r,t,n){let{keyCookieName:i,csrfCookieName:d}=l();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&&!o.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(){let e=process.env.DSQL_COOKIES_PREFIX||"dsql_";return{keyCookieName:e+(process.env.DSQL_COOKIES_KEY_NAME||"key"),csrfCookieName:e+(process.env.DSQL_COOKIES_CSRF_NAME||"csrf")}}},2258:(e,r,t)=>{t.r(r),t.d(r,{default:()=>l});let a=t(48225),s=t(29021),o=t(33873);function l({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:"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:o}){let l=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 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)}`),n&&(s+=`
IP: ${n}`),s+=`
Date: ${l.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:o}=t(79428);e.exports=({encryptedString:e,encryptionKey:r,encryptionSalt:t})=>{if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let l=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(!l?.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(l,n,i),o.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})();