dsql-admin/dsql-app/.local_dist/server/pages/api/updateUserTable.js

1 line
11 KiB
JavaScript
Raw Normal View History

2025-01-14 06:19:19 +00:00
"use strict";(()=>{var e={};e.id=4371,e.ids=[4371],e.modules={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")},79646:e=>{e.exports=require("child_process")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},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}}})},81147:(e,r,t)=>{t.r(r),t.d(r,{config:()=>h,default:()=>S,routeModule:()=>m});var a={};t.r(a),t.d(a,{default:()=>E});var s=t(89947),n=t(2706),o=t(96762),l=t(48786),i=t(31495),u=t(60125),c=t(67619),d=t(69842),_=t(30802),f=t(76856);let p=t(29021);t(33873);let{execSync:g}=t(79646);async function E(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,d.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{dbFullName:a,dbSlug:s,dbId:n,tableNameOld:o,payload:g,delegated:E}=e.body,{tableName:S,tableFullName:h,tableDescription:m}=g;if(!S?.match(/./)||S.match(/[^a-zA-Z0-9-_]/))return r.json({success:!1,msg:"Bad Input"});try{let d=await (0,u.A)({databaseSlug:s,request:e,user:t,query:e.query});if(!await (0,i.A)({user:t,confirmedDelegetedUser:d,database:s,table:o,dbId:n,priviledgeRegex:/Edit Table/i}))return console.log("User Unauthorized"),r.json({success:!1,msg:"Unauthorized"});let g=d?.rootUserId?d.rootUserId:t.id,b=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${g}/main.json`,y=JSON.parse(p.readFileSync(b,"utf8")),A=y.filter(e=>e.dbFullName===a)[0].tables.filter((e,r)=>{if(e.tableName===o)return!0});if(A&&A[0])A[0].tableName=S,A[0].tableNameOld=o,A[0].tableFullName=h,m&&(A[0].tableDescription=m),A[0].childrenTables&&A[0].childrenTables.forEach(e=>{try{y.filter(r=>r.dbFullName===e.dbNameFull)[0].tables.filter(r=>{if(r.tableName===e.tableName)return!0})[0].childTableName=S}catch(e){(0,c.default)({component:"/api/updateUserTable/lines-131-138",message:e.message,user:t})}});else throw Error("Table Does not exist");p.writeFileSync(`${b}`,JSON.stringify(y),"utf8"),await (0,_.A)({dbFullName:"datasquirel",tableName:"user_database_tables",identifierColumnName:"table_slug",identifierValue:o,data:{table_name:h,table_slug:S,table_description:m}}),E&&d&&await (0,l.A)("UPDATE delegated_user_tables SET `table`=? WHERE delegated_user_id=? AND root_user_id=? AND `table`=? AND `database`=?",[S,t.id,d.rootUserId,o,s]);let N=a.replace(/[^a-z\_0-9]/g,"");await (0,f.A)({userId:g,targetDatabase:N}),r.json({success:!0})}catch(e){(0,c.default)({component:"/api/updateUserTable/main-catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!"})}}let S=(0,o.M)(a,"default"),h=(0,o.M)(a,"config"),m=new s.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/updateUserTable",pathname:"/api/updateUserTable",bundlePath:"",filename:""},userland:a})},31495:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(48786),s=t(67619);async function n({user:e,confirmedDelegetedUser:r,database:t,table:n,priviledgeRegex:o,dbId:l}){let i,u;try{r?.delegated?(i=await (0,a.A)("SELECT priviledge FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=? AND `database`=? AND `table`=?",[r.rootUserId,e.id,t,n]),u=i[0]?.priviledge?.match(o)):(i=await (0,a.A)("SELECT table_slug FROM user_database_tables WHERE user_id=? AND table_slug=? AND db_id=?",[e.id,n,l]),u=!0)}catch(r){(0,s.default)({component:"checkUserRights",message:r.message,user:e})}return!!i&&!!i[0]&&!!u}},60125:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(95969);async function s({request:e,databaseSlug:r,user:t,query:s}){try{let n=t.id,o=null;if(!s?.delegated)return{dbUserId:n};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=(0,a.A)({encryptedString:e.cookies[u]});if(