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

16 lines
12 KiB
JavaScript
Raw Normal View History

2025-01-13 08:00:21 +00:00
"use strict";(()=>{var e={};e.id=1132,e.ids=[1132],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")},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:()=>E});var a={};t.r(a),t.d(a,{default:()=>p});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),_=t(30802);async function p(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:p,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,_.A)({dbContext:"Dsql User",paradigm:"Full Access",dbFullName:d,tableName:n,identifierColumnName:"id",identifierValue:o,data:s,tableSchema:p});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:p}),r.json({success:!0,payload:f})}catch(e){(0,c.default)({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"),E=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.default)({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});let a=t(37275);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 s=await a({database:e,paradigm:"Full Access",queryString:`SELECT * FROM ${n} WHERE \`order\` = '${t}' AND id != ?`,queryValues:[String(r)]});if(!s?.[0])throw Error("No Existing Order");await a({
2024-12-08 19:56:57 +00:00
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}
2025-01-13 08:00:21 +00:00
User Email: ${e?.email}`),s?.url&&(a+=`
URL: ${s.url}`),s?.body&&(a+=`
Request Body: ${JSON.stringify(s.body,null,4)}`),i&&(a+=`
2024-12-08 19:56:57 +00:00
IP: ${i}`),a+=`
Date: ${o.toDateString()}
2025-01-13 08:00:21 +00:00
========================================`,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",`
2024-12-08 19:56:57 +00:00
2025-01-13 08:00:21 +00:00
${l}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},95969:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(55511),n=t(79428);function s({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let s=r||process.env.DSQL_ENCRYPTION_PASSWORD,o=t||process.env.DSQL_ENCRYPTION_SALT,i=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!s?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!o?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let l=(0,a.scryptSync)(s,o,i),u=n.Buffer.alloc(16,0),c=(0,a.createDecipheriv)("aes-192-cbc",l,u);try{let r=c.update(e,"hex","utf8");return r+=c.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}},68829:(e,r,t)=>{t.r(r),t.d(r,{default:()=>s});var a=t(55511),n=t(79428);function s({data:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encryption string is invalid"),e;let s=r||process.env.DSQL_ENCRYPTION_PASSWORD,o=t||process.env.DSQL_ENCRYPTION_SALT,i=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!s?.match(/.{8,}/))return console.log("Encryption key is invalid"),e;if(!o?.match(/.{8,}/))return console.log("Encryption salt is invalid"),e;let l=(0,a.scryptSync)(s,o,i),u=n.Buffer.alloc(16,0),c=(0,a.createCipheriv)("aes-192-cbc",l,u);try{let r=c.update(e,"utf8","hex");return r+=c.final("hex")}catch(r){return console.log("Error in encrypting =>",r.message),e}}},48786:(e,r,t)=>{t.d(r,{A:()=>a});async function a(...e){let r=global.DSQL_DB_CONN;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()}}},37275:(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()}async function s({paradigm:e,queryString:r,queryValues:t}){try{switch(e){case"Read Only":return await a(global.DSQL_READ_ONLY_DB_CONN,r,t);case"Full Access":case"FA":return await a(global.DSQL_FULL_ACCESS_DB_CONN,r,t);default:return null}}catch(e){return console.log(`DSQL_USER_DB_HANDLER Error: ${e.message}`),null}}t.r(r),t.d(r,{default:()=>s})},7971:(e,r,t)=>{t.d(r,{A:()=>i});var a=t(9973),n=t.n(a),s=t(29021),o=t.n(s);async function i(...e){let r=n()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4",ssl:function(){let e=process.env.DSQL_SSL_DIR;if(!e?.match(/./))return;let r=`${e}/ca-cert.pem`;if(!o().existsSync(r)){console.log(`${r} does not exist`);return}return{ca:o().readFileSync(`${e}/ca-cert.pem`),rejectUnauthorized:!1}}()},onConnect:()=>{console.log("Connection Successful!")},onConnectError:e=>{console.log("Connection Error",e.message)},onError:e=>{console.log("Client Error",e.message)}});console.log("Querying ...");try{let t=await r.query(...e);return await r.end(),JSON.parse(JSON.stringify(t))}catch(e){return console.log("DB Error =>",e.message),{success:!1,error:e.message}}}},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})();