-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbusers.go
110 lines (100 loc) · 3.22 KB
/
dbusers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"strings"
"gorm.io/gorm"
)
func FixEmptyEmails(db *gorm.DB) {
// alternative to be cool
// db.Exec("update harbor_user set email=username where email='' AND username LIKE='%@%'")
var users []DbHarborUser
db.Where("Email = ?", "").Find(&users)
for _, user := range users {
fmt.Printf("Fix empty email: %s", user)
if strings.Contains(user.Username, "@") {
user.Email = user.Username
// db.Model(&user).Update("Email", user.Username)
} else {
user.Email = fmt.Sprintf("%s@nodamain.io", user.Username)
// db.Model(&user).Update("Email", username)
}
db.Save(&user)
fmt.Printf("email updated: %s\n", user)
}
}
// update TargetUser if the data has been modified
func UpdateTargerUser(uSource DbHarborUser, uTarget DbHarborUser, db *gorm.DB) {
updated := false
if uSource.UpdateTime != uTarget.UpdateTime {
// change by a copy using Reflection
uTarget.Email = uSource.Email
// uTarget.Password = uSource.Password
uTarget.Realname = uSource.Realname
uTarget.Deleted = uSource.Deleted
uTarget.SysadminFlag = uSource.SysadminFlag
uTarget.CreationTime = uSource.CreationTime
uTarget.UpdateTime = uSource.UpdateTime
db.Save(&uTarget)
updated = true
}
if uSource.OidcUser.Secret != uTarget.OidcUser.Secret {
uTarget.OidcUser.Secret = uSource.OidcUser.Secret
db.Save(&uTarget.OidcUser)
updated = true
}
if updated {
fmt.Printf("User updated: %s\n", uTarget)
}
}
func CreateNewUser(user DbHarborUser, db *gorm.DB) {
newUser := DbHarborUser{
// change by a copy using Reflection
Username: user.Username,
Email: user.Email,
Password: user.Password,
Realname: user.Realname,
Comment: user.Comment,
Deleted: user.Deleted,
ResetUuid: user.ResetUuid,
Salt: user.Salt,
SysadminFlag: user.SysadminFlag,
CreationTime: user.CreationTime,
UpdateTime: user.UpdateTime,
PasswordVersion: user.PasswordVersion,
}
oidc := DBOidcUser{
Secret: user.OidcUser.Secret,
Subiss: user.OidcUser.Subiss,
Token: user.OidcUser.Token,
CreationTime: user.OidcUser.CreationTime,
UpdateTime: user.OidcUser.UpdateTime,
}
newUser.OidcUser = &oidc
db.Save(&newUser)
fmt.Printf("User created: %s", newUser)
}
func SyncUsersDatabase(dbSource *gorm.DB, dbTarget *gorm.DB) {
var usersTarget []DbHarborUser
var mUsers = make(map[string]DbHarborUser)
// dbTarget.Preload("OidcUser").Where("Username not in ( 'admin' , 'anonymous')").Find(&usersTarget)
dbTarget.Joins("OidcUser").Find(&usersTarget, "Username not in ( 'admin' , 'anonymous')")
for _, user := range usersTarget {
if user.OidcUser == nil {
fmt.Printf(" %s no Oidc\n", user)
} else {
mUsers[user.Username] = user
}
}
var usersSource []DbHarborUser
// dbSource.Preload("DbOidcUser").Where("Username not in ( 'admin' , 'anonymous')").Find(&usersSource)
dbSource.Joins("OidcUser").Find(&usersSource, "Username not in ( 'admin' , 'anonymous')")
for _, userSource := range usersSource {
if userTarget, ok := mUsers[userSource.Username]; ok {
// check if the registry needs to be updated
UpdateTargerUser(userSource, userTarget, dbTarget)
} else {
// create new record
CreateNewUser(userSource, dbTarget)
}
}
}