mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Fix missing admin user setup
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -12,12 +13,14 @@ import (
|
|||||||
"github.com/go-chi/cors"
|
"github.com/go-chi/cors"
|
||||||
"github.com/go-chi/httprate"
|
"github.com/go-chi/httprate"
|
||||||
"github.com/unrolled/secure"
|
"github.com/unrolled/secure"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
"novamd/internal/api"
|
"novamd/internal/api"
|
||||||
"novamd/internal/auth"
|
"novamd/internal/auth"
|
||||||
"novamd/internal/config"
|
"novamd/internal/config"
|
||||||
"novamd/internal/db"
|
"novamd/internal/db"
|
||||||
"novamd/internal/handlers"
|
"novamd/internal/handlers"
|
||||||
|
"novamd/internal/models"
|
||||||
"novamd/internal/secrets"
|
"novamd/internal/secrets"
|
||||||
"novamd/internal/storage"
|
"novamd/internal/storage"
|
||||||
)
|
)
|
||||||
@@ -47,6 +50,12 @@ func NewServer(cfg *config.Config) (*Server, error) {
|
|||||||
// Initialize filesystem
|
// Initialize filesystem
|
||||||
storageManager := storage.NewService(cfg.WorkDir)
|
storageManager := storage.NewService(cfg.WorkDir)
|
||||||
|
|
||||||
|
// Setup admin user
|
||||||
|
err = setupAdminUser(database, storageManager, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to setup admin user: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize router
|
// Initialize router
|
||||||
router := initRouter(cfg)
|
router := initRouter(cfg)
|
||||||
|
|
||||||
@@ -168,3 +177,47 @@ func (s *Server) setupRoutes(jwtManager auth.JWTManager, sessionService *auth.Se
|
|||||||
// Handle all other routes with static file server
|
// Handle all other routes with static file server
|
||||||
s.router.Get("/*", handlers.NewStaticHandler(s.config.StaticPath).ServeHTTP)
|
s.router.Get("/*", handlers.NewStaticHandler(s.config.StaticPath).ServeHTTP)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupAdminUser(db db.Database, w storage.WorkspaceManager, cfg *config.Config) error {
|
||||||
|
|
||||||
|
adminEmail := cfg.AdminEmail
|
||||||
|
adminPassword := cfg.AdminPassword
|
||||||
|
|
||||||
|
// Check if admin user exists
|
||||||
|
adminUser, err := db.GetUserByEmail(adminEmail)
|
||||||
|
if adminUser != nil {
|
||||||
|
return nil // Admin user already exists
|
||||||
|
} else if err != sql.ErrNoRows {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash the password
|
||||||
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(adminPassword), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to hash password: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create admin user
|
||||||
|
adminUser = &models.User{
|
||||||
|
Email: adminEmail,
|
||||||
|
DisplayName: "Admin",
|
||||||
|
PasswordHash: string(hashedPassword),
|
||||||
|
Role: models.RoleAdmin,
|
||||||
|
}
|
||||||
|
|
||||||
|
createdUser, err := db.CreateUser(adminUser)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create admin user: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize workspace directory
|
||||||
|
err = w.InitializeUserWorkspace(createdUser.ID, createdUser.LastWorkspaceID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to initialize admin workspace: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Created admin user with ID: %d and default workspace with ID: %d", createdUser.ID, createdUser.LastWorkspaceID)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
package user
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
|
|
||||||
"novamd/internal/db"
|
|
||||||
"novamd/internal/models"
|
|
||||||
"novamd/internal/storage"
|
|
||||||
)
|
|
||||||
|
|
||||||
type UserService struct {
|
|
||||||
DB db.Database
|
|
||||||
Storage storage.Manager
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewUserService(database db.Database, s storage.Manager) *UserService {
|
|
||||||
return &UserService{
|
|
||||||
DB: database,
|
|
||||||
Storage: s,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *UserService) SetupAdminUser(adminEmail, adminPassword string) (*models.User, error) {
|
|
||||||
// Check if admin user exists
|
|
||||||
adminUser, err := s.DB.GetUserByEmail(adminEmail)
|
|
||||||
if adminUser != nil {
|
|
||||||
return adminUser, nil // Admin user already exists
|
|
||||||
} else if err != sql.ErrNoRows {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hash the password
|
|
||||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(adminPassword), bcrypt.DefaultCost)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to hash password: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create admin user
|
|
||||||
adminUser = &models.User{
|
|
||||||
Email: adminEmail,
|
|
||||||
DisplayName: "Admin",
|
|
||||||
PasswordHash: string(hashedPassword),
|
|
||||||
Role: models.RoleAdmin,
|
|
||||||
}
|
|
||||||
|
|
||||||
createdUser, err := s.DB.CreateUser(adminUser)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create admin user: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize workspace directory
|
|
||||||
err = s.Storage.InitializeUserWorkspace(createdUser.ID, createdUser.LastWorkspaceID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to initialize admin workspace: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Created admin user with ID: %d and default workspace with ID: %d", createdUser.ID, createdUser.LastWorkspaceID)
|
|
||||||
|
|
||||||
return adminUser, nil
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user