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

1 line
11 KiB
JavaScript
Raw Normal View History

2024-12-15 11:27:16 +00:00
"use strict";(()=>{var e={};e.id=4371,e.ids=[4371],e.modules={60818:e=>{e.exports=require("dotenv")},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")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},96762:(e,t)=>{Object.defineProperty(t,"M",{enumerable:!0,get:function(){return function e(t,r){return r in t?t[r]:"then"in t&&"function"==typeof t.then?t.then(t=>e(t,r)):"function"==typeof t&&"default"===r?t:void 0}}})},56363:(e,t,r)=>{r.r(t),r.d(t,{config:()=>A,default:()=>v,routeModule:()=>L});var s={};r.r(s),r.d(s,{default:()=>y});var a=r(89947),n=r(2706),o=r(96762),l=r(51348),i=r.n(l),c=r(73677),u=r.n(c),d=r(39831),_=r.n(d),p=r(48225),S=r.n(p),f=r(6420),g=r.n(f),E=r(33364),D=r.n(E),m=r(19261),b=r.n(m);let h=r(29021);r(33873);let{execSync:N}=r(79646);async function y(e,t){if("POST"!==e.method)return t.json({msg:"Failed!"});let r=await g()(e,t,!0);if(!r)return t.json({success:!1,msg:"Unauthorized"});let{dbFullName:s,dbSlug:a,dbId:n,tableNameOld:o,payload:l,delegated:c}=e.body,{tableName:d,tableFullName:p,tableDescription:f}=l;if(!d?.match(/./)||d.match(/[^a-zA-Z0-9-_]/))return t.json({success:!1,msg:"Bad Input"});try{let l=await _()({databaseSlug:a,request:e,user:r,query:e.query});if(!await u()({user:r,confirmedDelegetedUser:l,database:a,table:o,dbId:n,priviledgeRegex:/Edit Table/i}))return console.log("User Unauthorized"),t.json({success:!1,msg:"Unauthorized"});let g=l?.rootUserId?l.rootUserId:r.id,E=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${g}/main.json`,m=JSON.parse(h.readFileSync(E,"utf8")),N=m.filter(e=>e.dbFullName===s)[0].tables.filter((e,t)=>{if(e.tableName===o)return!0});if(N&&N[0])N[0].tableName=d,N[0].tableNameOld=o,N[0].tableFullName=p,f&&(N[0].tableDescription=f),N[0].childrenTables&&N[0].childrenTables.forEach(e=>{try{m.filter(t=>t.dbFullName===e.dbNameFull)[0].tables.filter(t=>{if(t.tableName===e.tableName)return!0})[0].childTableName=d}catch(e){S()({component:"/api/updateUserTable/lines-131-138",message:e.message,user:r})}});else throw Error("Table Does not exist");h.writeFileSync(`${E}`,JSON.stringify(m),"utf8"),await D()({dbFullName:"datasquirel",tableName:"user_database_tables",identifierColumnName:"table_slug",identifierValue:o,data:{table_name:p,table_slug:d,table_description:f}}),c&&l&&await i()("UPDATE delegated_user_tables SET `table`=? WHERE delegated_user_id=? AND root_user_id=? AND `table`=? AND `database`=?",[d,r.id,l.rootUserId,o,a]);let y=s.replace(/[^a-z\_0-9]/g,"");await b()({userId:g,targetDatabase:y}),t.json({success:!0})}catch(e){S()({component:"/api/updateUserTable/main-catch-error",message:e.message,user:r}),t.json({success:!1,msg:"Database Files could not be written!"})}}let v=(0,o.M)(s,"default"),A=(0,o.M)(s,"config"),L=new a.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/updateUserTable",pathname:"/api/updateUserTable",bundlePath:"",filename:""},userland:s})},73677:(e,t,r)=>{let s=r(51348),a=r(48225);e.exports=async function({user:e,confirmedDelegetedUser:t,database:r,table:n,priviledgeRegex:o,dbId:l}){let i,c;try{t?.delegated?(i=await s("SELECT priviledge FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=? AND `database`=? AND `table`=?",[t.rootUserId,e.id,r,n]),c=i[0]?.priviledge?.match(o)):(i=await s("SELECT table_slug FROM user_database_tables WHERE user_id=? AND table_slug=? AND db_id=?",[e.id,n,l]),c=!0)}catch(t){a({component:"checkUserRights",message:t.message,user:e})}return!!i&&!!i[0]&&!!c}},39831:(e,t,r)=>{r(81630);let s=r(72835);e.exports=async function({request:e,databaseSlug:t,user:r,query:a}){try{let n=r.id,o=null;if(!a?.delegated)return{dbUserId:n};let l=a.dbUserId,i=`${process.env.DSQL_USER_DB_PREFIX}${l}_${t}`,c=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${i}`;try{if(!e.cookies?.[c])throw Error("Cookie no