Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

Added get current account info from context. Fixed some bugs.

dreambladeflagc25dc3c4f1c2
diff --git a/core/internal/controller/domains/domains_v1_apply_cert.go b/core/internal/controller/domains/domains_v1_apply_cert.go
index 64789640..ff23a1b8 100644
--- a/core/internal/controller/domains/domains_v1_apply_cert.go
+++ b/core/internal/controller/domains/domains_v1_apply_cert.go
@@ -1,14 +1,31 @@
package domains
import (
+ "billionmail-core/internal/service/domains"
+ "billionmail-core/internal/service/rbac"
"context"
- "github.com/gogf/gf/v2/errors/gcode"
- "github.com/gogf/gf/v2/errors/gerror"
-
"billionmail-core/api/domains/v1"
)
func (c *ControllerV1) ApplyCert(ctx context.Context, req *v1.ApplyCertReq) (res *v1.ApplyCertRes, err error) {
- return nil, gerror.NewCode(gcode.CodeNotImplemented)
+ res = &v1.ApplyCertRes{}
+
+ acc, err := rbac.GetCurrentAccount(ctx)
+
+ if err != nil {
+ res.SetError(err)
+ return
+ }
+
+ // Apply for the certificate
+ err = domains.ApplyLetsEncryptCertWithHttp(ctx, req.Domain, acc.Email)
+
+ if err != nil {
+ res.SetError(err)
+ return
+ }
+
+ res.SetSuccess("Certificate applied successfully")
+ return
}
diff --git a/core/internal/service/acme/renew.go b/core/internal/service/acme/renew.go
index c2d5f4b8..e8738647 100644
--- a/core/internal/service/acme/renew.go
+++ b/core/internal/service/acme/renew.go
@@ -70,7 +70,7 @@ func StartRenew(certId int, accountId int, certPath string, domains []string, em
progress := GetAcmeLogBody(ctx)
if err != nil {
// Update renewal log
- public.MR("ssl", "renew_logs").Where("renew_id = ?", renewId).Update(g.Map{"status": 2, "progress": progress, "error_info": err.Error()})
+ public.MR("ssl", "renew_logs").Where("renew_id = ?", renewId).Update(g.Map{"status": 2, "error_info": err.Error()})
g.Log().Error(context.Background(), "Renew certificate failed: ", err.Error())
return err
}
@@ -78,13 +78,13 @@ func StartRenew(certId int, accountId int, certPath string, domains []string, em
// Certificate is empty
if certificate == "" || privateKey == "" {
// Update renewal log
- public.MR("ssl", "renew_logs").Where("renew_id = ?", renewId).Update(g.Map{"status": 2, "progress": progress, "error_info": "let's encrypt certificate is empty"})
+ public.MR("ssl", "renew_logs").Where("renew_id = ?", renewId).Update(g.Map{"status": 2, "error_info": "let's encrypt certificate is empty"})
g.Log().Error(context.Background(), "Renew certificate failed: let's encrypt certificate is empty")
return errors.New("let's encrypt certificate is empty")
}
// Update renewal log
- public.MR("ssl", "renew_logs").Where("renew_id = ?", renewId).Update(g.Map{"status": 1, "progress": progress, "error_info": ""})
+ public.MR("ssl", "renew_logs").Where("renew_id = ?", renewId).Update(g.Map{"status": 1, "progress": 100, "error_info": ""})
g.Log().Info(context.Background(), "Renew certificate success for domains: ", domains)
dnsNames := "[]"
diff --git a/core/internal/service/domains/ssl.go b/core/internal/service/domains/ssl.go
new file mode 100644
index 00000000..6439df77
--- /dev/null
+++ b/core/internal/service/domains/ssl.go
@@ -0,0 +1,20 @@
+package domains
+
+import (
+ "context"
+ "github.com/gogf/gf/v2/errors/gerror"
+)
+
+// ApplyLetsEncryptCertWithHttp applies for a Let's Encrypt certificate for the given domain.
+func ApplyLetsEncryptCertWithHttp(ctx context.Context, domain string, email string) error {
+ return gerror.New("Not implemented")
+
+ //crt, key, err := acme.ApplySSLWithExistingServer(ctx, []string{domain}, email, "http", "", nil, public.AbsPath("../ssl"))
+ //
+ //if err != nil {
+ // return err
+ //}
+ //
+ //// Save the certificate and key to the database
+ //g.DB().Model("letsencrypts").Insert()
+}
diff --git a/core/internal/service/rbac/account.go b/core/internal/service/rbac/account.go
index 78a00cb2..97cd11d6 100644
--- a/core/internal/service/rbac/account.go
+++ b/core/internal/service/rbac/account.go
@@ -3,6 +3,7 @@ package rbac
import (
"billionmail-core/internal/model"
"context"
+ "fmt"
"time"
"github.com/gogf/gf/v2/frame/g"
@@ -302,3 +303,18 @@ func GetCurrentRoles(ctx context.Context) []string {
return roles
}
+
+// GetCurrentAccount gets the current user account from context
+func GetCurrentAccount(ctx context.Context) (acc *model.Account, err error) {
+ accountId := GetCurrentAccountId(ctx)
+
+ if accountId == 0 {
+ return nil, fmt.Errorf("account ID not found in context")
+ }
+
+ if err = g.DB().Model("account").Where("account_id = ?", accountId).Scan(&acc); err != nil {
+ return nil, fmt.Errorf("failed to get account: %w", err)
+ }
+
+ return
+}