dsql-admin/dsql-app/.local_dist/server/pages/api/addUserTable.js
Benjamin Toby 885c11f674 Updates
2025-01-14 07:42:13 +01:00

1 line
5.6 KiB
JavaScript

"use strict";(()=>{var e={};e.id=8275,e.ids=[8275],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")},47122:(e,a,r)=>{r.r(a),r.d(a,{config:()=>E,default:()=>h,routeModule:()=>N});var s={};r.r(s),r.d(s,{default:()=>p});var t=r(89947),l=r(2706),d=r(96762),i=r(67619),n=r(69842),o=r(4820),u=r(27825),c=r.n(u),b=r(21724),_=r(48786),m=r(98e3),f=r(76856);async function p(e,a){if("POST"!==e.method)return a.json({msg:"Failed!"});let r=await (0,n.A)(e,a,!0);if(!r)return console.log("No User"),a.json({success:!1,msg:"Unauthorized"});let{payload:s}=e.body,t=e.body.dbFullName.replace(/[^a-z\_0-9]/g,""),l=e.body.dbSlug,d=e.body.dbId,u=e.body.parentDatabase,p=e.body.parentTable;try{let e=(0,o.A)({userId:r.id});if(!e)throw Error("No User Schema found!");let n=e.findIndex(e=>e.dbFullName===t),h=e[n];if(!h)throw Error("No Target Database Found!");let E=h.tables.findIndex(e=>e.tableName===s.tableName);if(E>=0?e[n].tables[E]=s:e[n].tables.push(s),s?.childTable){let a=e.find(e=>e.dbFullName===u),r=a?.tables.find(e=>{if(e.tableName===p)return!0});if(!r)throw Error("Child Table Parent Doesn't Exist!");r.childrenTables?.[0]?r.childrenTables.push({dbNameFull:t,tableName:s.tableName,tableNameFull:s.tableNameFull}):r.childrenTables=[{dbNameFull:t,tableName:s.tableName,tableNameFull:s.tableFullName}]}if(h.childrenDatabases)for(let a=0;a<h.childrenDatabases.length;a++){let t=h.childrenDatabases[a];try{let a=e.findIndex(e=>e.dbFullName===t.dbFullName);e[a]&&(e[a].tables=c().cloneDeep(h.tables));let l=await (0,_.A)("SELECT * FROM user_databases WHERE user_id = ? AND db_full_name = ?",[r.id,t.dbFullName]),d=await (0,_.A)("SELECT * FROM user_database_tables WHERE db_id=? AND user_id=? AND table_slug=?",[l[0].id,r.id,s.tableName]);if(d?.[0])continue;await (0,m.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:r.id,db_id:l[0].id,db_slug:l[0].db_slug,table_name:s.tableFullName,table_slug:s.tableName}})}catch(e){(0,i.A)({component:"/api/addUserTable/lines-124-140",message:e.message,user:r})}}(0,b.A)({userId:r.id,schemaData:e});let N={user_id:r.id,db_id:d,db_slug:l,table_name:s.tableFullName,table_slug:s.tableName};s?.childTable&&(N.child_table="1",N.child_table_parent_database=u,N.child_table_parent_table=p),await (0,m.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:N}),await (0,f.A)({userId:r.id,targetDatabase:t}),a.json({success:!0})}catch(e){console.log(e.message),(0,i.A)({component:"/api/addUserTable/main-catch-error",message:e.message,user:r}),a.json({success:!1,msg:"Database Files could not be written!"})}}let h=(0,d.M)(s,"default"),E=(0,d.M)(s,"config"),N=new t.PagesAPIRouteModule({definition:{kind:l.A.PAGES_API,page:"/api/addUserTable",pathname:"/api/addUserTable",bundlePath:"",filename:""},userland:s})},69842:(e,a,r)=>{r.d(a,{A:()=>n});var s=r(48786),t=r(95969),l=r(29021),d=r.n(l),i=r(89159);async function n(e,a,r){try{let{keyCookieName:l,csrfCookieName:n}=(0,i.A)();if(!e.cookies?.[l]?.match(/./))throw Error("Request Cookies not Found!");let o=(0,t.A)({encryptedString:e.cookies[l]});if(!o)throw Error("Couldn't decrypt auth cookie!");let u=JSON.parse(o);if(!u.csrf_k)throw Error("No `csrf_k` present in user Object!");if(r&&!Object.keys(e.headers).find(e=>e.includes(u.csrf_k)))throw console.log("req.headers",e.headers),console.log("userObject",u),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(r&&!d().existsSync(`${c}/${u.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");if(0==u.verification_status&&!r){let e=await (0,s.A)(`SELECT verification_status FROM users WHERE id='${u.id}'`);e&&e[0]&&1==e[0].verification_status&&a.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,a,r)=>{r.d(a,{A:()=>s});function s(e){let a=process.env.DSQL_COOKIES_PREFIX||"dsql_",r=process.env.DSQL_COOKIES_KEY_NAME||"key",s=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",t=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",l=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),d=a;e?.userId&&(d+=`user_${e.userId}_`),l&&(d+=`${l}_`),d+=r;let i=a;e?.userId&&(i+=`user_${e.userId}_`),l&&(i+=`${l}_`),i+=s;let n=a;return e?.userId&&(n+=`user_${e.userId}_`),l&&(n+=`${l}_`),{keyCookieName:d,csrfCookieName:i,oneTimeCodeName:n+=t}}},4820:(e,a,r)=>{r.d(a,{A:()=>n});var s=r(67619),t=r(29021),l=r.n(t),d=r(33873),i=r.n(d);function n({userId:e}){try{let a=i().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return JSON.parse(l().readFileSync(a,"utf-8"))}catch(e){return(0,s.A)({component:"grabUserSchemaData",message:e.message}),null}}},21724:(e,a,r)=>{r.d(a,{A:()=>n});var s=r(67619),t=r(29021),l=r.n(t),d=r(33873),i=r.n(d);function n({userId:e,schemaData:a}){try{let r=i().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return l().writeFileSync(r,JSON.stringify(a),"utf8"),!0}catch(e){return(0,s.A)({component:"/functions/backend/setUserSchemaData",message:e.message}),!1}}}};var a=require("../../webpack-api-runtime.js");a.C(e);var r=e=>a(a.s=e),s=a.X(0,[1251,2282],()=>r(47122));module.exports=s})();