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

able to change admin username and password with .env file. adjust validation code font size. fixed bugs.

Jack664f613fcf00
diff --git a/core/internal/service/database_initialization/rbac.go b/core/internal/service/database_initialization/rbac.go
index 49141096..1bce3053 100644
--- a/core/internal/service/database_initialization/rbac.go
+++ b/core/internal/service/database_initialization/rbac.go
@@ -6,6 +6,7 @@ import (
"billionmail-core/internal/service/rbac"
"context"
"github.com/gogf/gf/v2/frame/g"
+ "time"
)
// Initialize RBAC related table structures
@@ -117,7 +118,14 @@ func init() {
// Create admin account if it doesn't exist
adminId := int64(0)
- adminIdVal, err := g.DB().Model("account").Where("username = ?", adminUsername).Value("account_id")
+
+ // Find admin account from account_role table
+ adminIdVal, err := g.DB().Model("account_role").Where("role_id = ?", adminRoleId).Value("account_id")
+
+ if err != nil || adminIdVal == nil {
+ // Find admin account from account table
+ adminIdVal, err = g.DB().Model("account").Where("username = ?", adminUsername).Value("account_id")
+ }
if err != nil {
g.Log().Error(context.Background(), "Failed to check admin role:", err)
@@ -138,6 +146,24 @@ func init() {
}
} else {
adminId = adminIdVal.Int64()
+ // Update admin account username and password
+ passwd, err := rbac.Account().GeneratePasswordHash(adminPassword)
+
+ if err != nil {
+ g.Log().Error(context.Background(), "Failed to hash admin password:", err)
+ return
+ }
+
+ _, err = g.DB().Model("account").Data(g.Map{
+ "username": adminUsername,
+ "password": passwd,
+ "update_time": time.Now().Unix(),
+ }).Where("account_id = ?", adminId).Update()
+
+ if err != nil {
+ g.Log().Error(context.Background(), "Failed to update admin account:", err)
+ return
+ }
}
// Assign admin role to admin account
diff --git a/core/internal/service/rbac/account.go b/core/internal/service/rbac/account.go
index e1e53258..1399ad0c 100644
--- a/core/internal/service/rbac/account.go
+++ b/core/internal/service/rbac/account.go
@@ -106,14 +106,14 @@ func (s *accountService) Delete(ctx context.Context, accountId int64) error {
// UpdatePassword updates account password
func (s *accountService) UpdatePassword(ctx context.Context, accountId int64, newPassword string) error {
- hashedPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
+ hashedPassword, err := s.GeneratePasswordHash(newPassword)
if err != nil {
return err
}
_, err = g.DB().Model("account").Data(g.Map{
- "password": string(hashedPassword),
- "updated_at": time.Now().Unix(),
+ "password": hashedPassword,
+ "update_time": time.Now().Unix(),
}).Where("account_id = ?", accountId).Update()
return err
}
@@ -124,6 +124,15 @@ func (s *accountService) VerifyPassword(hashedPassword, plainPassword string) bo
return err == nil
}
+// GeneratePasswordHash generates a hashed password
+func (s *accountService) GeneratePasswordHash(password string) (string, error) {
+ hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
+ if err != nil {
+ return "", err
+ }
+ return string(hashedPassword), nil
+}
+
// UsernameExists checks if a username already exists
func (s *accountService) UsernameExists(ctx context.Context, username string) (bool, error) {
count, err := g.DB().Model("account").Where("username = ?", username).Count()
@@ -156,9 +165,9 @@ func (s *accountService) GetAccountRoles(ctx context.Context, accountId int64) (
// AssignRole assigns a single role to an account
func (s *accountService) AssignRole(ctx context.Context, accountId int64, roleId int64) error {
_, err := g.DB().Model("account_role").Data(g.Map{
- "account_id": accountId,
- "role_id": roleId,
- "created_at": time.Now(),
+ "account_id": accountId,
+ "role_id": roleId,
+ "create_time": time.Now(),
}).Insert()
return err
}
@@ -187,9 +196,9 @@ func (s *accountService) BindRoles(ctx context.Context, accountId int64, roleIds
// Add new roles
for _, roleId := range roleIds {
_, err = g.DB().Model("account_role").Data(g.Map{
- "account_id": accountId,
- "role_id": roleId,
- "created_at": time.Now(),
+ "account_id": accountId,
+ "role_id": roleId,
+ "create_time": time.Now(),
}).Insert()
if err != nil {
return err
diff --git a/core/internal/service/rbac/permission.go b/core/internal/service/rbac/permission.go
index 2b797e4d..f329e3f7 100644
--- a/core/internal/service/rbac/permission.go
+++ b/core/internal/service/rbac/permission.go
@@ -72,8 +72,8 @@ func (s *permissionService) Create(ctx context.Context, name, description, modul
"action": action,
"resource": resource,
"status": status,
- "created_at": time.Now(),
- "updated_at": time.Now(),
+ "create_time": time.Now(),
+ "update_time": time.Now(),
}).Insert()
if err != nil {
return 0, err
@@ -96,7 +96,7 @@ func (s *permissionService) Update(ctx context.Context, permissionId int64, name
"action": action,
"resource": resource,
"status": status,
- "updated_at": time.Now(),
+ "update_time": time.Now(),
}).Where("id = ?", permissionId).Update()
return err
}
diff --git a/core/internal/service/rbac/role.go b/core/internal/service/rbac/role.go
index c5c36d2c..11c9aa6f 100644
--- a/core/internal/service/rbac/role.go
+++ b/core/internal/service/rbac/role.go
@@ -83,7 +83,7 @@ func (s *roleService) Update(ctx context.Context, roleId int64, name, descriptio
"name": name,
"description": description,
"status": status,
- "updated_at": time.Now(),
+ "update_time": time.Now(),
}).Where("id = ?", roleId).Update()
return err
}
@@ -107,7 +107,7 @@ func (s *roleService) BindPermissions(ctx context.Context, roleId int64, permiss
_, err = g.DB().Model("role_permission").Data(g.Map{
"role_id": roleId,
"permission_id": permId,
- "created_at": time.Now(),
+ "create_time": time.Now(),
}).Insert()
if err != nil {
return err
@@ -141,7 +141,7 @@ func (s *roleService) AssignPermission(ctx context.Context, roleId int64, permis
_, err := g.DB().Model("role_permission").Data(g.Map{
"role_id": roleId,
"permission_id": permissionId,
- "created_at": time.Now(),
+ "create_time": time.Now(),
}).Insert()
return err
}
diff --git a/core/internal/service/rbac/service.go b/core/internal/service/rbac/service.go
index b868855f..e0b1ba96 100644
--- a/core/internal/service/rbac/service.go
+++ b/core/internal/service/rbac/service.go
@@ -21,6 +21,8 @@ type IAccount interface {
UpdatePassword(ctx context.Context, accountId int64, newPassword string) error
// Verify password
VerifyPassword(hashedPassword, plainPassword string) bool
+ // GeneratePasswordHash generates a hashed password
+ GeneratePasswordHash(password string) (string, error)
// Check if username exists
UsernameExists(ctx context.Context, username string) (bool, error)
// Check if email exists
diff --git a/core/internal/service/rbac/validate_code.go b/core/internal/service/rbac/validate_code.go
index ee736178..8606016d 100644
--- a/core/internal/service/rbac/validate_code.go
+++ b/core/internal/service/rbac/validate_code.go
@@ -20,7 +20,7 @@ const (
// Captcha configuration structure
var (
defaultDriver = &base64Captcha.DriverString{
- Height: 50,
+ Height: 75,
Width: 200,
NoiseCount: 0,
ShowLineOptions: base64Captcha.OptionShowHollowLine,