/
user.go
245 lines (213 loc) · 6.19 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package store
import (
"context"
"time"
"github.com/cuigh/auxo/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
UserStatusBlocked = 0
UserStatusNormal = 1
)
type User struct {
Id string `json:"id" bson:"_id"`
Name string `json:"name" bson:"name" valid:"required"`
LoginName string `json:"login_name" bson:"login_name" valid:"required"`
Roles []string `json:"roles" bson:"roles"`
Email string `json:"email,omitempty" bson:"email" valid:"email"`
Phone string `json:"phone,omitempty" bson:"phone"`
Wecom string `json:"wecom,omitempty" bson:"wecom"`
Admin bool `json:"admin" bson:"admin"`
Status int32 `json:"status" bson:"status"` // 0-禁用, 1-正常
Password string `json:"-" bson:"password"`
Salt string `json:"-" bson:"salt" mongo:"create:true,update:false,deep:false"`
CreateTime time.Time `json:"create_time" bson:"create_time"`
ModifyTime time.Time `json:"modify_time" bson:"modify_time"`
}
type UserStore interface {
Create(u *User) error
Modify(t *User) error
ModifyProfile(t *User) error
Find(id string) (u *User, err error)
FindByName(loginName string) (*User, error)
Search(name, loginName, filter string, pageIndex, pageSize int64) (users []*User, total int64, err error)
Fetch(ids []string) (users []*User, err error)
SetPassword(id string, password, salt string) error
SetStatus(id string, status int32) error
Count(ctx context.Context) (int64, error)
CreateIndexes(ctx context.Context) error
}
type userStore struct {
c *mongo.Collection
}
func NewUserStore(db *mongo.Database) UserStore {
return &userStore{db.Collection("user")}
}
func (s *userStore) Find(id string) (u *User, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
r := s.c.FindOne(ctx, bson.M{"_id": id})
t := &User{}
if err := r.Decode(&t); err == nil {
return t, nil
} else if err == mongo.ErrNoDocuments {
return nil, nil
} else {
return nil, err
}
}
func (s *userStore) Fetch(ids []string) (users []*User, err error) {
if len(ids) == 0 {
return nil, nil
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
filter := bson.M{"_id": bson.M{"$in": ids}}
opts := options.Find().SetProjection(bson.D{
{"password", 0},
{"salt", 0},
})
cur, err := s.c.Find(ctx, filter, opts)
if err != nil {
return nil, err
}
defer cur.Close(ctx)
err = cur.All(ctx, &users)
if err != nil {
return nil, err
}
return users, nil
}
func (s *userStore) FindByName(loginName string) (*User, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
r := s.c.FindOne(ctx, bson.M{"login_name": loginName})
t := &User{}
if err := r.Decode(&t); err == nil {
return t, nil
} else if err == mongo.ErrNoDocuments {
return nil, nil
} else {
return nil, err
}
}
func (s *userStore) Search(name, loginName, filter string, pageIndex, pageSize int64) (users []*User, total int64, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
args := bson.M{}
if name != "" {
args["name"] = name
}
if loginName != "" {
args["login_name"] = loginName
}
switch filter {
case "admin":
args["admin"] = true
case "active":
args["status"] = UserStatusNormal
case "blocked":
args["status"] = UserStatusBlocked
}
// fetch total count
total, err = s.c.CountDocuments(ctx, args)
if err != nil {
return nil, 0, err
}
opts := options.Find().SetProjection(bson.D{
{"password", 0},
{"salt", 0},
}).SetSkip(pageSize * (pageIndex - 1)).SetLimit(pageSize).SetSort(bson.M{"login_name": 1})
cur, err := s.c.Find(ctx, args, opts)
if err != nil {
return nil, 0, err
}
defer cur.Close(ctx)
err = cur.All(ctx, &users)
if err != nil {
return nil, 0, err
}
return users, total, nil
}
func (s *userStore) SetPassword(id string, pwd, salt string) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
update := bson.M{
"password": pwd,
"salt": salt,
}
_, err = s.c.UpdateByID(ctx, id, bson.M{"$set": update})
return
}
func (s *userStore) SetStatus(id string, status int32) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = s.c.UpdateByID(ctx, id, bson.M{"$set": bson.M{"status": status}})
return
}
func (s *userStore) Create(u *User) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
u.Id = createId()
u.CreateTime = time.Now()
u.ModifyTime = u.CreateTime
_, err := s.c.InsertOne(ctx, u)
if mongo.IsDuplicateKeyError(err) {
return errors.Format("登录名 %s 已经存在", u.LoginName)
}
return err
}
func (s *userStore) Modify(u *User) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
update := bson.M{
"name": u.Name,
"login_name": u.LoginName,
"roles": u.Roles,
"email": u.Email,
"phone": u.Phone,
"wecom": u.Wecom,
"admin": u.Admin,
"status": u.Status,
"modify_time": time.Now(),
}
r, err := s.c.UpdateByID(ctx, u.Id, bson.M{"$set": update})
if err != nil {
return err
} else if r.MatchedCount == 0 {
return errors.Format("can't find user '%d'", u.Id)
}
return nil
}
func (s *userStore) ModifyProfile(u *User) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
update := bson.M{
"name": u.Name,
"login_name": u.LoginName,
"email": u.Email,
"phone": u.Phone,
"modify_time": time.Now(),
}
r, err := s.c.UpdateByID(ctx, u.Id, bson.M{"$set": update})
if err != nil {
return err
} else if r.MatchedCount == 0 {
return errors.Format("can't find user '%d'", u.Id)
}
return nil
}
func (s *userStore) Count(ctx context.Context) (int64, error) {
filter := bson.M{}
return s.c.CountDocuments(ctx, filter)
}
func (s *userStore) CreateIndexes(ctx context.Context) error {
index := mongo.IndexModel{
Keys: bson.D{{"login_name", 1}},
Options: options.Index().SetUnique(true),
}
_, err := s.c.Indexes().CreateOne(ctx, index)
return err
}