-
Notifications
You must be signed in to change notification settings - Fork 45
/
user.go
124 lines (112 loc) · 2.71 KB
/
user.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package dao
import (
"fmt"
"github.com/VaalaCat/frp-panel/models"
"github.com/VaalaCat/frp-panel/utils"
"github.com/samber/lo"
)
func AdminGetAllUsers() ([]*models.UserEntity, error) {
db := models.GetDBManager().GetDefaultDB()
users := make([]*models.User, 0)
err := db.Find(&users).Error
if err != nil {
return nil, err
}
return lo.Map(users,
func(u *models.User, _ int) *models.UserEntity {
return u.UserEntity
}), nil
}
func AdminCountUsers() (int64, error) {
db := models.GetDBManager().GetDefaultDB()
var count int64
err := db.Model(&models.User{}).Count(&count).Error
if err != nil {
return 0, err
}
return count, nil
}
func GetUserByUserID(userID int) (*models.UserEntity, error) {
if userID == 0 {
return nil, fmt.Errorf("invalid user id")
}
db := models.GetDBManager().GetDefaultDB()
u := &models.User{}
err := db.Where(&models.User{
UserEntity: &models.UserEntity{
UserID: userID,
},
}).First(u).Error
if err != nil {
return nil, err
}
return u.UserEntity, nil
}
func UpdateUser(userInfo models.UserInfo, user *models.UserEntity) error {
db := models.GetDBManager().GetDefaultDB()
user.UserID = userInfo.GetUserID()
return db.Model(&models.User{}).Where(
&models.User{
UserEntity: &models.UserEntity{
UserID: userInfo.GetUserID(),
},
},
).Save(&models.User{
UserEntity: user,
}).Error
}
func GetUserByUserName(userName string) (*models.UserEntity, error) {
if userName == "" {
return nil, fmt.Errorf("invalid user name")
}
db := models.GetDBManager().GetDefaultDB()
u := &models.User{}
err := db.Where(&models.User{
UserEntity: &models.UserEntity{
UserName: userName,
},
}).First(u).Error
if err != nil {
return nil, err
}
return u.UserEntity, nil
}
func CheckUserPassword(userNameOrEmail, password string) (bool, models.UserInfo, error) {
var user models.User
db := models.GetDBManager().GetDefaultDB()
if err := db.Where(&models.User{
UserEntity: &models.UserEntity{
UserName: userNameOrEmail,
},
}).Or(&models.User{
UserEntity: &models.UserEntity{
Email: userNameOrEmail,
},
}).First(&user).Error; err != nil {
return false, nil, err
}
return utils.CheckPasswordHash(password, user.Password), user, nil
}
func CheckUserNameAndEmail(userName, email string) error {
var user models.User
db := models.GetDBManager().GetDefaultDB()
if err := db.Where(&models.User{
UserEntity: &models.UserEntity{
UserName: userName,
},
}).Or(&models.User{
UserEntity: &models.UserEntity{
Email: email,
},
}).First(&user).Error; err != nil {
return err
}
return nil
}
func CreateUser(user *models.UserEntity) error {
u := &models.User{
UserEntity: user,
}
db := models.GetDBManager().GetDefaultDB()
return db.Create(u).Error
}