mirror of
https://github.com/hay-kot/homebox.git
synced 2024-12-18 21:16:31 +00:00
96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/hay-kot/content/backend/ent"
|
|
"github.com/hay-kot/content/backend/internal/repo"
|
|
"github.com/hay-kot/content/backend/internal/types"
|
|
"github.com/hay-kot/content/backend/pkgs/hasher"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
const (
|
|
DefaultGroup = "Default"
|
|
DefaultName = "Admin"
|
|
DefaultEmail = "admin@admin.com"
|
|
DefaultPassword = "admin"
|
|
)
|
|
|
|
// EnsureAdministrator ensures that there is at least one superuser in the database
|
|
// if one isn't found a default is generate using the default credentials
|
|
func (a *app) EnsureAdministrator() {
|
|
superusers, err := a.repos.Users.GetSuperusers(context.Background())
|
|
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("failed to get superusers")
|
|
}
|
|
if len(superusers) > 0 {
|
|
return
|
|
}
|
|
|
|
pw, _ := hasher.HashPassword(DefaultPassword)
|
|
newSuperUser := types.UserCreate{
|
|
Name: DefaultName,
|
|
Email: DefaultEmail,
|
|
IsSuperuser: true,
|
|
Password: pw,
|
|
}
|
|
|
|
log.Info().
|
|
Str("name", newSuperUser.Name).
|
|
Str("email", newSuperUser.Email).
|
|
Msg("no superusers found, creating default superuser")
|
|
|
|
_, err = a.repos.Users.Create(context.Background(), newSuperUser)
|
|
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("failed to create default superuser")
|
|
}
|
|
|
|
}
|
|
|
|
func (a *app) SeedDatabase(repos *repo.AllRepos) {
|
|
if !a.conf.Seed.Enabled {
|
|
return
|
|
}
|
|
|
|
group, err := repos.Groups.Create(context.Background(), DefaultGroup)
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("failed to create default group")
|
|
}
|
|
|
|
for _, seedUser := range a.conf.Seed.Users {
|
|
|
|
// Check if User Exists
|
|
usr, err := repos.Users.GetOneEmail(context.Background(), seedUser.Email)
|
|
if err != nil && !ent.IsNotFound(err) {
|
|
log.Fatal().Err(err).Msg("failed to get user")
|
|
}
|
|
|
|
if usr != nil && usr.ID != uuid.Nil {
|
|
log.Info().Str("email", seedUser.Email).Msg("user already exists, skipping")
|
|
continue
|
|
}
|
|
|
|
hashedPw, err := hasher.HashPassword(seedUser.Password)
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("failed to hash password")
|
|
}
|
|
|
|
_, err = repos.Users.Create(context.Background(), types.UserCreate{
|
|
Name: seedUser.Name,
|
|
Email: seedUser.Email,
|
|
IsSuperuser: seedUser.IsSuperuser,
|
|
Password: hashedPw,
|
|
GroupID: group.ID,
|
|
})
|
|
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("failed to create user")
|
|
}
|
|
|
|
log.Info().Str("email", seedUser.Email).Msg("created user")
|
|
}
|
|
}
|