dsql-admin/dsql-app/.local_dist/server/pages/api/updateTableEntry.js
Benjamin Toby 81cf010cb5 Updates
2025-01-14 16:27:08 +01:00

16 lines
12 KiB
JavaScript

"use strict";(()=>{var e={};e.id=1132,e.ids=[1132],e.modules={39908:e=>{e.exports=require("@moduletrace/datasquirel/dist/users/user-auth")},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")},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}}})},18885:(e,r,t)=>{t.r(r),t.d(r,{config:()=>g,default:()=>f,routeModule:()=>S});var a={};t.r(a),t.d(a,{default:()=>_});var n=t(89947),s=t(2706),o=t(96762),i=t(77377),l=t(31495),u=t(60125),c=t(67619),d=t(69842),p=t(30802);async function _(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{dbSlug:a,tableName:n,payload:s,entryId:o,tableSchema:_,delegated:f,dbId:g}=e.body;delete s.id,delete s.uuid,delete s.date_created,delete s.date_created_code,delete s.date_created_timestamp,delete s.date_updated,delete s.date_updated_code,delete s.date_updated_timestamp;try{let c=await (0,u.A)({databaseSlug:a,request:e,user:t,query:e.query}),d=c?.rootUserId?`datasquirel_user_${c.rootUserId}_${a}`:`datasquirel_user_${t.id}_${a}`;if(!await (0,l.A)({user:t,confirmedDelegetedUser:c,database:a,table:n,dbId:g,priviledgeRegex:/Update Entries/i}))return r.json({success:!1,msg:"Unauthorized"});let f=await (0,p.A)({dbContext:"Dsql User",paradigm:"Full Access",dbFullName:d,tableName:n,identifierColumnName:"id",identifierValue:o,data:s,tableSchema:_});if(f?.error)throw Error(f.error);if(!f)throw Error("No result returned!");await (0,i.A)({dbName:d,entryId:o,entryOrder:s.order,tableName:n,tableSchema:_}),r.json({success:!0,payload:f})}catch(e){(0,c.A)({component:"/api/updateTableEntry/main-catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!",err:e.message})}}let f=(0,o.M)(a,"default"),g=(0,o.M)(a,"config"),S=new n.PagesAPIRouteModule({definition:{kind:s.A.PAGES_API,page:"/api/updateTableEntry",pathname:"/api/updateTableEntry",bundlePath:"",filename:""},userland:a})},31495:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(48786),n=t(67619);async function s({user:e,confirmedDelegetedUser:r,database:t,table:s,priviledgeRegex:o,dbId:i}){let l,u;try{r?.delegated?(l=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,s]),u=l[0]?.priviledge?.match(o)):(l=await (0,a.A)("SELECT table_slug FROM user_database_tables WHERE user_id=? AND table_slug=? AND db_id=?",[e.id,s,i]),u=!0)}catch(r){(0,n.A)({component:"checkUserRights",message:r.message,user:e})}return!!l&&!!l[0]&&!!u}},60125:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(95969);async function n({request:e,databaseSlug:r,user:t,query:n}){try{let s=t.id,o=null;if(!n?.delegated)return{dbUserId:s};let i=n.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,a.A)({encryptedString:e.cookies[u]});if(!t)throw Error("Invalid Token");if((o=JSON.parse(t)).databaseSlug===r)return{dbUserId:s=o.rootUserId,rootUserId:o.rootUserId,rootUserName:o.rootUserName,rootUserEmail:o.rootUserEmail,rootUserImage:o.rootUserImage,databaseFullName:o.databaseFullName,databaseSlug:o.databaseSlug,allowedTables:o.allowedTables,priviledges:o.priviledges,database:o.databaseSlug,delegated:!0}}catch(e){}return{dbUserId:s}}catch(e){return null}}},77377:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(37897);async function n({dbName:e,entryId:r,entryOrder:t,tableName:n,tableSchema:s}){try{if(s.fields.find(e=>!!e.fieldName?.match(/^order$/i))&&t){let e=await (0,a.A)({paradigm:"Full Access",queryString:`SELECT * FROM ${n} WHERE \`order\` = '${t}' AND id != ?`,queryValues:[String(r)]});if(!e?.[0])throw Error("No Existing Order");await (0,a.A)({paradigm:"Full Access",queryString:`UPDATE ${n} SET \`order\` = \`order\` + 1 WHERE \`order\` >= ${t} AND id != ?`,queryValues:[String(r)]})}return!0}catch(e){return!1}}},69842:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(39908),n=t.n(a);async function s(e,r,t){let a=n()({req:e,level:t?"deep":void 0});return console.log(a),a.payload}},30802:(e,r,t)=>{t.d(r,{A:()=>c});var a=t(72583),n=t.n(a),s=t(92652),o=t(48786),i=t(37897),l=t(68829),u=t(62050);async function c({dbContext:e,paradigm:r,dbFullName:t,tableName:a,data:c,tableSchema:d,identifierColumnName:p,identifierValue:_,encryptionKey:f,encryptionSalt:g,useLocal:S}){if(!c||!Object.keys(c).length)return null;let y=!!S||!e?.match(/dsql.user/i)&&(!t||!!t.match(/^datasquirel$/)),m=S?u.A:y?o.A:i.A,E=Object.keys(c),A=[],h=[];for(let e=0;e<E.length;e++)try{let r=E[e],t=c[r],a=d?d?.fields?.filter(e=>e.fieldName===r):null,o=a&&a[0]?a[0]:null;if(null==t||void 0==t)continue;let i=/<[^>]+>/g;(o?.richText||String(t).match(i))&&(t=n()(t,s.A)),o?.encrypted&&(t=(0,l.A)({data:t,encryptionKey:f,encryptionSalt:g})),"object"==typeof t&&(t=JSON.stringify(t)),o?.pattern&&!new RegExp(o.pattern,o.patternFlags||"").test(t)&&(console.log("DSQL: Pattern not matched =>",t),t=""),"string"==typeof t&&t.match(/^null$/i)&&(t={toSqlString:function(){return"NULL"}}),"string"!=typeof t||t.match(/./i)||(t={toSqlString:function(){return"NULL"}}),A.push(`\`${r}\`=?`),"number"==typeof t?h.push(String(t)):h.push(t)}catch(e){console.log("DSQL: Error in parsing data keys in update function =>",e.message);continue}A.push(`date_updated='${Date()}'`),A.push(`date_updated_code='${Date.now()}'`);let D=`UPDATE \`${t}\`.\`${a}\` SET ${A.join(",")} WHERE \`${p}\`=?`;return h.push(_),y?await m(D,h):await m({paradigm:r,queryString:D,queryValues:h})}},92652:(e,r,t)=>{t.d(r,{A:()=>a});let a={allowedTags:["b","i","em","strong","a","p","span","ul","ol","li","h1","h2","h3","h4","h5","h6","img","div","button","pre","code","br"],allowedAttributes:{a:["href"],img:["src","alt","width","height","class","style"],"*":["style","class"]}}},67619:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(29021),n=t.n(a);async function s({user:e,message:r,component:t,noMail:a,req:s}){let o=new Date,i=(()=>{if(!s)return null;try{let e=s.headers["x-forwarded-for"],r=s.headers["x-real-ip"],t=s.headers["cf-connecting-ip"],a=Array.isArray(e)?e[0]:e?.split(",")[0],n=t||a||r||s.socket.remoteAddress;if(!n)return null;return String(n)}catch(e){return null}})();try{let a=`🚀 SERVER ERROR ===========================
Error Message: ${r}
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(a+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),s?.url&&(a+=`
URL: ${s.url}`),s?.body&&(a+=`
Request Body: ${JSON.stringify(s.body,null,4)}`),i&&(a+=`
IP: ${i}`),a+=`
Date: ${o.toDateString()}
========================================`,n().existsSync("./.tmp/error.log")||n().writeFileSync("./.tmp/error.log","","utf-8");let l=n().readFileSync("./.tmp/error.log","utf-8");n().writeFileSync("./.tmp/error.log",a),n().appendFileSync("./.tmp/error.log",`
${l}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},95969:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(55511),n=t(79428),s=t(32443);function o({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let{key:o,salt:i,keyLen:l,algorithm:u,bufferAllocSize:c}=(0,s.A)({encryptionKey:r});if(!o?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!i?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let d=(0,a.scryptSync)(o,i,l),p=n.Buffer.alloc(c,0),_=(0,a.createDecipheriv)(u,d,p);try{let r=_.update(e,"hex","utf8");return r+=_.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}},68829:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(55511),n=t(79428),s=t(32443);function o({data:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encryption string is invalid"),e;let{key:o,salt:i,keyLen:l,algorithm:u,bufferAllocSize:c}=(0,s.A)({encryptionKey:r});if(!o?.match(/.{8,}/))return console.log("Encryption key is invalid"),e;if(!i?.match(/.{8,}/))return console.log("Encryption salt is invalid"),e;let d=(0,a.scryptSync)(o,i,l),p=n.Buffer.alloc(c,0),_=(0,a.createCipheriv)(u,d,p);try{let r=_.update(e,"utf8","hex");return r+=_.final("hex")}catch(r){return console.log("Error in encrypting =>",r.message),e}}},48786:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(7447);async function n(...e){let r=(0,a.A)();try{if(!r)throw Error("No Connection provided to DB_HANDLER function!");let t=await r.query(...e);return JSON.parse(JSON.stringify(t))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}finally{await r?.end()}}},37897:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(64292),n=t(7447);async function s({paradigm:e,queryString:r,queryValues:t}){let s="Read Only"==e?(0,n.A)({ro:!0}):(0,n.A)({fa:!0});try{return await (0,a.A)(s,r,t)}catch(e){return console.log(`DSQL_USER_DB_HANDLER Error: ${e.message}`),null}finally{s?.end()}}},62050:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(7447);async function n(...e){let r=(0,a.A)();console.log("Querying ...");try{let t=await r.query(...e);return JSON.parse(JSON.stringify(t))}catch(e){return console.log("DB Error =>",e.message),{success:!1,error:e.message}}finally{await r?.end()}}},64292:(e,r,t)=>{async function a(e,r,t){try{if("string"==typeof r){let a=await e.query(n(r),t);return JSON.parse(JSON.stringify(a))}if("object"!=typeof r)return null;{let t=[];for(let a=0;a<r.length;a++)try{let s=r[a],o=await e.query(n(s.query),s.values);t.push(JSON.parse(JSON.stringify(o)))}catch(e){t.push(null)}return t}}catch(e){return null}finally{e.end()}}function n(e){return e.replace(/\n/gm,"").replace(/ {2,}/g,"").trim()}t.d(r,{A:()=>a})},7447:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(9973),n=t.n(a);function s(e){return e?.ro?DSQL_READ_ONLY_DB_CONN||n()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_READ_ONLY_USERNAME,password:process.env.DSQL_DB_READ_ONLY_PASSWORD,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}}):e?.fa?global.DSQL_FULL_ACCESS_DB_CONN||n()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_FULL_ACCESS_USERNAME,password:process.env.DSQL_DB_FULL_ACCESS_PASSWORD,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}}):global.DSQL_DB_CONN||n()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:e?.noDb?void 0:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}})}},32443:(e,r,t)=>{t.d(r,{A:()=>a});function a(e){return{key:e?.encryptionKey||process.env.DSQL_ENCRYPTION_PASSWORD,keyLen:process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24,salt:e?.encryptionSalt||process.env.DSQL_ENCRYPTION_SALT,apiKey:e?.apiKey||process.env.DSQL_API_KEY,algorithm:e?.algorithm||process.env.DSQL_ENCRYPTION_ALGORITHM||"aes-192-cbc",bufferAllocSize:e?.bufferAllocSize||(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE?function(e,r){try{let r=Number(e);if("number"!=typeof r||isNaN(r))return 0;return Math.round(r)}catch(e){return console.log(`Numberfy ERROR: ${e.message}`),0}}(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE):void 0)||16}}},2706:(e,r)=>{Object.defineProperty(r,"A",{enumerable:!0,get:function(){return t}});var t=function(e){return e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE",e}({})},89947:(e,r,t)=>{e.exports=t(75600)}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=r(r.s=18885);module.exports=t})();