Skip to content

Commit f1fc760

Browse files
committed
implemented set new password
1 parent bfa7413 commit f1fc760

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

controllers/UpdateUserSettingsController.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,21 @@ func UpdateUserSettingsController(c echo.Context) error {
3636
return c.NoContent(http.StatusInternalServerError)
3737
}
3838

39+
// if new password is set, update it
40+
if validater.NewPassword != nil {
41+
// hash the new password
42+
rawhash, err := helpers.HashPassword(*validater.NewPassword)
43+
if err != nil {
44+
log.Println("Failed to hash new password", err)
45+
return c.NoContent(http.StatusInternalServerError)
46+
}
47+
user.Hash = rawhash
48+
49+
if res := inits.DB.Save(&user); res.Error != nil {
50+
log.Println("Failed to update user password", res.Error)
51+
return c.NoContent(http.StatusInternalServerError)
52+
}
53+
}
54+
3955
return c.NoContent(http.StatusOK)
4056
}

inits/Models.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package inits
22

33
import (
44
"ch/kirari04/videocms/models"
5+
"fmt"
56
"log"
7+
8+
"golang.org/x/crypto/bcrypt"
69
)
710

811
func Models() {
@@ -24,6 +27,29 @@ func Models() {
2427
mustRun(DB.AutoMigrate(&models.SystemResource{}))
2528
mustRun(DB.AutoMigrate(&models.Tag{}))
2629
mustRun(DB.AutoMigrate(&models.TagLinks{}))
30+
31+
// init default admin user
32+
// if no user exists
33+
var count int64
34+
if err := DB.Model(&models.User{}).Count(&count).Error; err != nil {
35+
log.Fatalln("Failed to count users: ", err)
36+
return
37+
}
38+
if count == 0 {
39+
rawhash, _ := bcrypt.GenerateFromPassword([]byte("12345678"), 14)
40+
user := models.User{
41+
Username: "admin",
42+
Hash: string(rawhash),
43+
Admin: true,
44+
Settings: models.UserSettings{
45+
WebhooksEnabled: true,
46+
WebhooksMax: 100,
47+
},
48+
}
49+
if res := DB.Create(&user); res.Error != nil {
50+
fmt.Printf("error while creating admin user: %s\n", res.Error.Error())
51+
}
52+
}
2753
}
2854

2955
func mustRun(err error) {

models/UserSettings.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ func (j UserSettings) Value() (driver.Value, error) {
3737
}
3838

3939
type UserSettingsUpdateValidation struct {
40-
EnablePlayerCaptcha *bool `validate:"required,boolean"`
40+
EnablePlayerCaptcha *bool `validate:"required,boolean"`
41+
NewPassword *string `validate:"omitempty,min=8,max=64"`
4142
}

0 commit comments

Comments
 (0)