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

1 line
11 KiB
JavaScript
Raw Normal View History

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