add admin user creation
This commit is contained in:
@@ -44,9 +44,6 @@ var (
|
|||||||
|
|
||||||
func TestSuite(t *testing.T) {
|
func TestSuite(t *testing.T) {
|
||||||
_ = deleteTestDB("test.db")
|
_ = deleteTestDB("test.db")
|
||||||
if err := database.Open("test.db"); err != nil {
|
|
||||||
log.Fatalf("Failed to create DB: %#v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -77,6 +74,9 @@ func TestSuite(t *testing.T) {
|
|||||||
log.Fatalf("Error setting environment variable: %v", err)
|
log.Fatalf("Error setting environment variable: %v", err)
|
||||||
}
|
}
|
||||||
config.LoadConfig()
|
config.LoadConfig()
|
||||||
|
if err := database.Open("test.db", config.Recipients.AdminEmail); err != nil {
|
||||||
|
log.Fatalf("Failed to create DB: %#v", err)
|
||||||
|
}
|
||||||
utils.SMTPStart(Host, Port)
|
utils.SMTPStart(Host, Port)
|
||||||
emailService := services.NewEmailService(config.SMTP.Host, config.SMTP.Port, config.SMTP.User, config.SMTP.Password)
|
emailService := services.NewEmailService(config.SMTP.Host, config.SMTP.Port, config.SMTP.User, config.SMTP.Password)
|
||||||
var consentRepo repositories.ConsentRepositoryInterface = &repositories.ConsentRepository{}
|
var consentRepo repositories.ConsentRepositoryInterface = &repositories.ConsentRepository{}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"GoMembership/internal/models"
|
"GoMembership/internal/models"
|
||||||
"GoMembership/pkg/logger"
|
"GoMembership/pkg/logger"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/base64"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alexedwards/argon2id"
|
"github.com/alexedwards/argon2id"
|
||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
var DB *gorm.DB
|
var DB *gorm.DB
|
||||||
|
|
||||||
func Open(dbPath string) error {
|
func Open(dbPath string, adminMail string) error {
|
||||||
|
|
||||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -34,12 +34,60 @@ func Open(dbPath string) error {
|
|||||||
DB = db
|
DB = db
|
||||||
|
|
||||||
logger.Info.Print("Opened DB")
|
logger.Info.Print("Opened DB")
|
||||||
if err := seedDatabase(); err != nil {
|
|
||||||
return err
|
var count int64
|
||||||
|
db.Model(&models.User{}).Count(&count)
|
||||||
|
if count == 0 {
|
||||||
|
admin, err := seedAdmin(adminMail)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
result := db.Create(&admin)
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Landing page to create an admin
|
||||||
|
func seedAdmin(userMail string) (*models.User, error) {
|
||||||
|
passwordBytes := make([]byte, 12)
|
||||||
|
_, err := rand.Read(passwordBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode into a URL-safe base64 string
|
||||||
|
password, err := base64.URLEncoding.EncodeToString(passwordBytes)[:12], nil
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
hash, err := argon2id.CreateHash(password, argon2id.DefaultParams)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Error.Print("==============================================================")
|
||||||
|
logger.Error.Printf("Admin Email: %v", userMail)
|
||||||
|
logger.Error.Printf("Admin Password: %v", password)
|
||||||
|
logger.Error.Print("==============================================================")
|
||||||
|
|
||||||
|
return &models.User{
|
||||||
|
FirstName: "ad",
|
||||||
|
LastName: "min",
|
||||||
|
DateOfBirth: time.Now(),
|
||||||
|
Password: hash,
|
||||||
|
Address: "Downhill 4",
|
||||||
|
ZipCode: "9999",
|
||||||
|
City: "TechTown",
|
||||||
|
Email: userMail,
|
||||||
|
Status: constants.ActiveStatus,
|
||||||
|
RoleID: constants.Roles.Editor,
|
||||||
|
}, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func Close() error {
|
func Close() error {
|
||||||
logger.Info.Print("Closing DB")
|
logger.Info.Print("Closing DB")
|
||||||
db, err := DB.DB()
|
db, err := DB.DB()
|
||||||
@@ -48,42 +96,3 @@ func Close() error {
|
|||||||
}
|
}
|
||||||
return db.Close()
|
return db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func seedDatabase() error {
|
|
||||||
var count int64
|
|
||||||
DB.Model(&models.User{}).Count(&count)
|
|
||||||
if count == 0 {
|
|
||||||
bytes := make([]byte, 12)
|
|
||||||
_, err := rand.Read(bytes)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
password := hex.EncodeToString(bytes)
|
|
||||||
hash, err := argon2id.CreateHash(password, argon2id.DefaultParams)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
admin := models.User{
|
|
||||||
FirstName: "ad",
|
|
||||||
LastName: "min",
|
|
||||||
DateOfBirth: time.Now(),
|
|
||||||
Password: hash,
|
|
||||||
Address: "Downhill 4",
|
|
||||||
ZipCode: "9999",
|
|
||||||
City: "TechTown",
|
|
||||||
Status: constants.ActiveStatus,
|
|
||||||
RoleID: constants.Roles.Editor,
|
|
||||||
}
|
|
||||||
|
|
||||||
result := DB.Create(&admin)
|
|
||||||
if result.Error != nil {
|
|
||||||
return result.Error
|
|
||||||
}
|
|
||||||
logger.Error.Print("==============================================================")
|
|
||||||
logger.Error.Printf("Admin Password: %v", password)
|
|
||||||
logger.Error.Print("==============================================================")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user