Skip to content

Commit

Permalink
fix: compatibility issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Nov 16, 2024
1 parent 459d94a commit a4ba3f0
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions core/user/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (svc *Service) Init() (err error) {
}

func (svc *Service) init() (err error) {
_, err = service.NewModelService[models.User]().GetOne(bson.M{"username": constants.DefaultAdminUsername}, nil)
u, err := service.NewModelService[models.User]().GetOne(bson.M{"username": constants.DefaultAdminUsername}, nil)
if err != nil {
if !errors2.Is(err, mongo.ErrNoDocuments) {
return err
Expand All @@ -41,20 +41,20 @@ func (svc *Service) init() (err error) {
}

// add user
u := models.User{
u = &models.User{
Username: constants.DefaultAdminUsername,
Password: utils.EncryptMd5(constants.DefaultAdminPassword),
Role: constants.RoleAdmin,
RootAdmin: true,
}
u.SetCreatedAt(time.Now())
u.SetUpdatedAt(time.Now())
_, err = service.NewModelService[models.User]().InsertOne(u)
_, err = service.NewModelService[models.User]().InsertOne(*u)
return err
}

func (svc *Service) initPro() (err error) {
_, err = service.NewModelService[models.User]().GetOne(bson.M{
u, err := service.NewModelService[models.User]().GetOne(bson.M{
"$or": []bson.M{
{"username": constants.DefaultAdminUsername},
{"root_admin": true},
Expand All @@ -65,19 +65,25 @@ func (svc *Service) initPro() (err error) {
return err
}
} else {
// exists
// exists, compatible with old versions
u.RootAdmin = true
u.SetUpdatedAt(time.Now())
err = service.NewModelService[models.User]().ReplaceById(u.Id, *u)
if err != nil {
log.Errorf("failed to update user: %v", err)
}
return
}

// add user
u := models.User{
u = &models.User{
Username: constants.DefaultAdminUsername,
Password: utils.EncryptMd5(constants.DefaultAdminPassword),
RootAdmin: true,
}
u.SetCreatedAt(time.Now())
u.SetUpdatedAt(time.Now())
_, err = service.NewModelService[models.User]().InsertOne(u)
_, err = service.NewModelService[models.User]().InsertOne(*u)
return err
}

Expand Down

0 comments on commit a4ba3f0

Please sign in to comment.