mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Add user.UpdateUserHash()
This commit is contained in:
parent
e3b3a76088
commit
c76a432003
@ -15,8 +15,11 @@
|
||||
package object
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/casdoor/casdoor/util"
|
||||
"xorm.io/core"
|
||||
@ -40,6 +43,7 @@ type User struct {
|
||||
IsAdmin bool `json:"isAdmin"`
|
||||
IsGlobalAdmin bool `json:"isGlobalAdmin"`
|
||||
IsForbidden bool `json:"isForbidden"`
|
||||
Hash string `xorm:"varchar(100)" json:"hash"`
|
||||
|
||||
Github string `xorm:"varchar(100)" json:"github"`
|
||||
Google string `xorm:"varchar(100)" json:"google"`
|
||||
@ -92,6 +96,8 @@ func UpdateUser(id string, user *User) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
user.UpdateUserHash()
|
||||
|
||||
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(user)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -102,6 +108,8 @@ func UpdateUser(id string, user *User) bool {
|
||||
|
||||
func AddUser(user *User) bool {
|
||||
user.Id = util.GenerateId()
|
||||
user.UpdateUserHash()
|
||||
|
||||
affected, err := adapter.Engine.Insert(user)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -111,6 +119,10 @@ func AddUser(user *User) bool {
|
||||
}
|
||||
|
||||
func AddUsers(users []*User) bool {
|
||||
for _, user := range users {
|
||||
user.UpdateUserHash()
|
||||
}
|
||||
|
||||
affected, err := adapter.Engine.Insert(users)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -224,3 +236,18 @@ func GetMaskedUsers(users []*User) []*User {
|
||||
}
|
||||
return users
|
||||
}
|
||||
|
||||
func getMd5Hash(text string) string {
|
||||
hash := md5.Sum([]byte(text))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
func calculateHash(user *User) string {
|
||||
s := strings.Join([]string{user.Id, user.Password, user.DisplayName, user.Avatar, user.Phone}, "|")
|
||||
return getMd5Hash(s)
|
||||
}
|
||||
|
||||
func (user *User) UpdateUserHash() {
|
||||
hash := calculateHash(user)
|
||||
user.Hash = hash
|
||||
}
|
||||
|
@ -45,6 +45,20 @@ func TestSyncIds(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncHashes(t *testing.T) {
|
||||
InitConfig()
|
||||
|
||||
users := GetGlobalUsers()
|
||||
for _, user := range users {
|
||||
if user.Hash != "" {
|
||||
continue
|
||||
}
|
||||
|
||||
user.UpdateUserHash()
|
||||
updateUserColumn("hash", user)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSaltedPassword(t *testing.T) {
|
||||
password := "123456"
|
||||
salt := "123"
|
||||
|
Loading…
x
Reference in New Issue
Block a user