dsql-admin/dsql-app/.local_dist/server/pages/api/sql-users/create.js
Benjamin Toby e0a030f10d Updates
2025-01-13 09:00:21 +01:00

1 line
2.4 KiB
JavaScript

"use strict";(()=>{var e={};e.id=7889,e.ids=[7889],e.modules={60818:e=>{e.exports=require("dotenv")},47795:e=>{e.exports=require("generate-password")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},79428:e=>{e.exports=require("buffer")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},52795:(e,r,s)=>{s.r(r),s.d(r,{config:()=>f,default:()=>h,routeModule:()=>E});var t={};s.r(t),s.d(t,{default:()=>g});var a=s(89947),i=s(2706),o=s(96762),u=s(47795),n=s.n(u),d=s(69842),l=s(67619),c=s(48786),m=s(68829),p=s(5437);async function g(e,r){if("POST"!==e.method)return r.json({user:null,msg:"Registration Failed!"});let s=await (0,d.A)(e,r,!0);if(!s)return r.json({success:!1,msg:"Unauthorized"});let{data:t,edit:a,grants:i}=e.body;try{let e=process.env.DSQL_DB_HOST||"127.0.0.1",a=s?.id==Number(process.env.DSQL_SU_USER_ID),{host:o,password:u,username:d}=t,l=a&&d?.match(/./)?d:`dsql_user_${s.id}`;if(!o.match(/./))throw Error("Invalid host");let g=String(o).replace(/[^a-zA-Z0-9-\.\:\/\%]/g,""),h=u?.match(/./)?u:n().generate({length:16,numbers:!0,symbols:!0,uppercase:!0,exclude:"*#.'`\""}),f=await (0,c.A)("SELECT * FROM mysql.user WHERE User = ? AND (Host = ? OR Host = ?)",[l,g,e]);if(f?.[0]?.User)throw Error("SQL User already exists. Remember if you have the '%' host on your primary user, it catches all other hosts.");let E=await (0,c.A)("SELECT COUNT(*) FROM mariadb_users WHERE user_id = ?",[s.id]);if(E?.[0]?.["COUNT(*)"]>=10)throw Error("User limit reached");let S=(0,m.default)({data:h}),w=await (0,c.A)("INSERT INTO mariadb_users (user_id, username, host, password, grants) VALUES (?, ?, ?, ?, ?)",[s.id,l,g,S,JSON.stringify(i)]);if(!w?.insertId)throw Error("Error in adding SQL user");await (0,p.A)({userId:s.id,mariadbUsername:l,mariadbUserHost:g}),r.json({success:!0})}catch(e){console.log("Create MariaDB user ERROR:",e.message),(0,l.default)({component:"/api/acceptUserInvitation/catch-error",message:e.message,user:s}),r.json({success:!1,msg:"Error in adding SQL user =>"+e.message,err:e.message})}}let h=(0,o.M)(t,"default"),f=(0,o.M)(t,"config"),E=new a.PagesAPIRouteModule({definition:{kind:i.A.PAGES_API,page:"/api/sql-users/create",pathname:"/api/sql-users/create",bundlePath:"",filename:""},userland:t})}};var r=require("../../../webpack-api-runtime.js");r.C(e);var s=e=>r(r.s=e),t=r.X(0,[3496],()=>s(52795));module.exports=t})();