This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
user.go
503 lines (438 loc) · 11.4 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package interactor
import (
"bytes"
"context"
_ "embed"
"errors"
htmlTmpl "html/template"
"net/mail"
textTmpl "text/template"
"github.com/reearth/reearth-backend/internal/usecase"
"github.com/reearth/reearth-backend/internal/usecase/gateway"
"github.com/reearth/reearth-backend/internal/usecase/interfaces"
"github.com/reearth/reearth-backend/internal/usecase/repo"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/log"
"github.com/reearth/reearth-backend/pkg/project"
"github.com/reearth/reearth-backend/pkg/rerror"
"github.com/reearth/reearth-backend/pkg/user"
)
type User struct {
common
userRepo repo.User
teamRepo repo.Team
projectRepo repo.Project
sceneRepo repo.Scene
sceneLockRepo repo.SceneLock
layerRepo repo.Layer
propertyRepo repo.Property
datasetRepo repo.Dataset
datasetSchemaRepo repo.DatasetSchema
transaction repo.Transaction
file gateway.File
authenticator gateway.Authenticator
mailer gateway.Mailer
signupSecret string
authSrvUIDomain string
}
type mailContent struct {
UserName string
Message string
Suffix string
ActionLabel string
ActionURL htmlTmpl.URL
}
var (
//go:embed emails/auth_html.tmpl
autHTMLTMPLStr string
//go:embed emails/auth_text.tmpl
authTextTMPLStr string
authTextTMPL *textTmpl.Template
authHTMLTMPL *htmlTmpl.Template
passwordResetMailContent mailContent
)
func init() {
var err error
authTextTMPL, err = textTmpl.New("passwordReset").Parse(authTextTMPLStr)
if err != nil {
log.Panicf("password reset email template parse error: %s\n", err)
}
authHTMLTMPL, err = htmlTmpl.New("passwordReset").Parse(autHTMLTMPLStr)
if err != nil {
log.Panicf("password reset email template parse error: %s\n", err)
}
passwordResetMailContent = mailContent{
Message: "Thank you for using Re:Earth. We’ve received a request to reset your password. If this was you, please click the link below to confirm and change your password.",
Suffix: "If you did not mean to reset your password, then you can ignore this email.",
ActionLabel: "Confirm to reset your password",
}
}
func NewUser(r *repo.Container, g *gateway.Container, signupSecret, authSrcUIDomain string) interfaces.User {
return &User{
userRepo: r.User,
teamRepo: r.Team,
projectRepo: r.Project,
sceneRepo: r.Scene,
sceneLockRepo: r.SceneLock,
layerRepo: r.Layer,
propertyRepo: r.Property,
datasetRepo: r.Dataset,
datasetSchemaRepo: r.DatasetSchema,
transaction: r.Transaction,
file: g.File,
authenticator: g.Authenticator,
signupSecret: signupSecret,
authSrvUIDomain: authSrcUIDomain,
mailer: g.Mailer,
}
}
func (i *User) Fetch(ctx context.Context, ids []id.UserID, operator *usecase.Operator) ([]*user.User, error) {
if err := i.OnlyOperator(operator); err != nil {
return nil, err
}
res, err := i.userRepo.FindByIDs(ctx, ids)
if err != nil {
return res, err
}
// filter
for k, u := range res {
teams, err := i.teamRepo.FindByUser(ctx, u.ID())
if err != nil {
return res, err
}
teamIDs := make([]id.TeamID, 0, len(teams))
for _, t := range teams {
if t != nil {
teamIDs = append(teamIDs, t.ID())
}
}
if !operator.IsReadableTeam(teamIDs...) {
res[k] = nil
}
}
return res, nil
}
func (i *User) GetUserByCredentials(ctx context.Context, inp interfaces.GetUserByCredentials) (u *user.User, err error) {
u, err = i.userRepo.FindByNameOrEmail(ctx, inp.Email)
if err != nil && !errors.Is(rerror.ErrNotFound, err) {
return nil, err
} else if u == nil {
return nil, interfaces.ErrInvalidUserEmail
}
matched, err := u.MatchPassword(inp.Password)
if err != nil {
return nil, err
}
if !matched {
return nil, interfaces.ErrSignupInvalidPassword
}
if u.Verification() == nil || !u.Verification().IsVerified() {
return nil, interfaces.ErrNotVerifiedUser
}
return u, nil
}
func (i *User) GetUserBySubject(ctx context.Context, sub string) (u *user.User, err error) {
u, err = i.userRepo.FindByAuth0Sub(ctx, sub)
if err != nil {
return nil, err
}
return u, nil
}
func (i *User) StartPasswordReset(ctx context.Context, email string) error {
tx, err := i.transaction.Begin()
if err != nil {
return err
}
defer func() {
if err2 := tx.End(ctx); err == nil && err2 != nil {
err = err2
}
}()
u, err := i.userRepo.FindByEmail(ctx, email)
if err != nil {
return err
}
pr := user.NewPasswordReset()
u.SetPasswordReset(pr)
if err := i.userRepo.Save(ctx, u); err != nil {
return err
}
var TextOut, HTMLOut bytes.Buffer
link := i.authSrvUIDomain + "/?pwd-reset-token=" + pr.Token
passwordResetMailContent.UserName = u.Name()
passwordResetMailContent.ActionURL = htmlTmpl.URL(link)
if err := authTextTMPL.Execute(&TextOut, passwordResetMailContent); err != nil {
return err
}
if err := authHTMLTMPL.Execute(&HTMLOut, passwordResetMailContent); err != nil {
return err
}
err = i.mailer.SendMail([]gateway.Contact{
{
Email: u.Email(),
Name: u.Name(),
},
}, "Password reset", TextOut.String(), HTMLOut.String())
if err != nil {
return err
}
tx.Commit()
return nil
}
func (i *User) PasswordReset(ctx context.Context, password, token string) error {
tx, err := i.transaction.Begin()
if err != nil {
return err
}
defer func() {
if err2 := tx.End(ctx); err == nil && err2 != nil {
err = err2
}
}()
u, err := i.userRepo.FindByPasswordResetRequest(ctx, token)
if err != nil {
return err
}
passwordReset := u.PasswordReset()
ok := passwordReset.Validate(token)
if !ok {
return interfaces.ErrUserInvalidPasswordReset
}
u.SetPasswordReset(nil)
if err := u.SetPassword(password); err != nil {
return err
}
if err := i.userRepo.Save(ctx, u); err != nil {
return err
}
tx.Commit()
return nil
}
func (i *User) UpdateMe(ctx context.Context, p interfaces.UpdateMeParam, operator *usecase.Operator) (u *user.User, err error) {
if err := i.OnlyOperator(operator); err != nil {
return nil, err
}
if p.Password != nil {
if p.PasswordConfirmation == nil || *p.Password != *p.PasswordConfirmation {
return nil, interfaces.ErrUserInvalidPasswordConfirmation
}
}
tx, err := i.transaction.Begin()
if err != nil {
return
}
defer func() {
if err2 := tx.End(ctx); err == nil && err2 != nil {
err = err2
}
}()
var team *user.Team
u, err = i.userRepo.FindByID(ctx, operator.User)
if err != nil {
return nil, err
}
if p.Name != nil && *p.Name != u.Name() {
// username should not be a valid mail
if _, err := mail.ParseAddress(*p.Name); err == nil {
return nil, interfaces.ErrSignupInvalidName
}
// make sure the username is not exists
if userByName, _ := i.userRepo.FindByName(ctx, *p.Name); userByName != nil {
return nil, interfaces.ErrSignupInvalidName
}
oldName := u.Name()
u.UpdateName(*p.Name)
team, err = i.teamRepo.FindByID(ctx, u.Team())
if err != nil && !errors.Is(err, rerror.ErrNotFound) {
return nil, err
}
tn := team.Name()
if tn == "" || tn == oldName {
team.Rename(*p.Name)
} else {
team = nil
}
}
if p.Email != nil {
if err := u.UpdateEmail(*p.Email); err != nil {
return nil, err
}
}
if p.Lang != nil {
u.UpdateLang(*p.Lang)
}
if p.Theme != nil {
u.UpdateTheme(*p.Theme)
}
if p.Password != nil && u.HasAuthProvider("reearth") {
if err := u.SetPassword(*p.Password); err != nil {
return nil, err
}
}
// Update Auth0 users
if p.Name != nil || p.Email != nil || p.Password != nil {
for _, a := range u.Auths() {
if a.Provider != "auth0" {
continue
}
if _, err := i.authenticator.UpdateUser(gateway.AuthenticatorUpdateUserParam{
ID: a.Sub,
Name: p.Name,
Email: p.Email,
Password: p.Password,
}); err != nil {
return nil, err
}
}
}
if team != nil {
err = i.teamRepo.Save(ctx, team)
if err != nil {
return nil, err
}
}
err = i.userRepo.Save(ctx, u)
if err != nil {
return nil, err
}
tx.Commit()
return u, nil
}
func (i *User) RemoveMyAuth(ctx context.Context, authProvider string, operator *usecase.Operator) (u *user.User, err error) {
if err := i.OnlyOperator(operator); err != nil {
return nil, err
}
tx, err := i.transaction.Begin()
if err != nil {
return
}
defer func() {
if err2 := tx.End(ctx); err == nil && err2 != nil {
err = err2
}
}()
u, err = i.userRepo.FindByID(ctx, operator.User)
if err != nil {
return nil, err
}
u.RemoveAuthByProvider(authProvider)
err = i.userRepo.Save(ctx, u)
if err != nil {
return nil, err
}
tx.Commit()
return u, nil
}
func (i *User) SearchUser(ctx context.Context, nameOrEmail string, operator *usecase.Operator) (u *user.User, err error) {
u, err = i.userRepo.FindByNameOrEmail(ctx, nameOrEmail)
if err != nil && !errors.Is(err, rerror.ErrNotFound) {
return nil, err
}
return u, nil
}
func (i *User) DeleteMe(ctx context.Context, userID id.UserID, operator *usecase.Operator) (err error) {
if operator == nil || operator.User.IsNil() {
return nil
}
if userID.IsNil() || userID != operator.User {
return errors.New("invalid user id")
}
tx, err := i.transaction.Begin()
if err != nil {
return
}
defer func() {
if err2 := tx.End(ctx); err == nil && err2 != nil {
err = err2
}
}()
u, err := i.userRepo.FindByID(ctx, userID)
if err != nil && !errors.Is(err, rerror.ErrNotFound) {
return err
}
if u == nil {
return nil
}
teams, err := i.teamRepo.FindByUser(ctx, u.ID())
if err != nil {
return err
}
deleter := ProjectDeleter{
SceneDeleter: SceneDeleter{
Scene: i.sceneRepo,
SceneLock: i.sceneLockRepo,
Layer: i.layerRepo,
Property: i.propertyRepo,
Dataset: i.datasetRepo,
DatasetSchema: i.datasetSchemaRepo,
},
File: i.file,
Project: i.projectRepo,
}
updatedTeams := make([]*user.Team, 0, len(teams))
deletedTeams := []id.TeamID{}
for _, team := range teams {
if !team.IsPersonal() && !team.Members().IsOnlyOwner(u.ID()) {
_ = team.Members().Leave(u.ID())
updatedTeams = append(updatedTeams, team)
continue
}
// Delete all projects
err := repo.IterateProjectsByTeam(i.projectRepo, ctx, team.ID(), 50, func(projects []*project.Project) error {
for _, prj := range projects {
if err := deleter.Delete(ctx, prj, true, operator); err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
deletedTeams = append(deletedTeams, team.ID())
}
// Save teams
if err := i.teamRepo.SaveAll(ctx, updatedTeams); err != nil {
return err
}
// Delete teams
if err := i.teamRepo.RemoveAll(ctx, deletedTeams); err != nil {
return err
}
// Delete user
if err := i.userRepo.Remove(ctx, u.ID()); err != nil {
return err
}
tx.Commit()
return nil
}
func (i *User) CreateVerification(ctx context.Context, email string) error {
u, err := i.userRepo.FindByEmail(ctx, email)
if err != nil {
return err
}
if u.Verification().IsVerified() {
return nil
}
return i.createVerification(ctx, u)
}
func (i *User) VerifyUser(ctx context.Context, code string) (*user.User, error) {
tx, err := i.transaction.Begin()
if err != nil {
return nil, err
}
u, err := i.userRepo.FindByVerification(ctx, code)
if err != nil {
return nil, err
}
if u.Verification().IsExpired() {
return nil, errors.New("verification expired")
}
u.Verification().SetVerified(true)
err = i.userRepo.Save(ctx, u)
if err != nil {
return nil, err
}
tx.Commit()
return u, nil
}