forked from piotrkowalczuk/charon
/
registration.go
131 lines (107 loc) · 3.63 KB
/
registration.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
package web
import (
"net/http"
"strconv"
"github.com/go-soa/charon/controller/web/request"
"github.com/go-soa/charon/lib"
"github.com/go-soa/charon/lib/routing"
"github.com/go-soa/charon/lib/security"
"github.com/go-soa/charon/model"
uuid "github.com/satori/go.uuid"
"golang.org/x/net/context"
)
// RegistrationIndex ...
func (h *Handler) RegistrationIndex(ctx context.Context, rw http.ResponseWriter, r *http.Request) context.Context {
return h.renderTemplate(rw, ctx)
}
// RegistrationProcess ...
func (h *Handler) RegistrationProcess(ctx context.Context, rw http.ResponseWriter, r *http.Request) context.Context {
r.ParseForm()
validationErrorBuilder := lib.NewValidationErrorBuilder()
registrationRequest := request.NewRegistrationRequestFromForm(r.Form)
registrationRequest.Validate(validationErrorBuilder)
if validationErrorBuilder.HasErrors() {
rw.WriteHeader(http.StatusBadRequest)
return h.renderTemplateWithData(rw, ctx, map[string]interface{}{
"validationErrors": validationErrorBuilder.Errors(),
"request": registrationRequest,
})
}
user, err := createAndRegisterUser(h.Container.PasswordHasher, h.Container.RM.User, registrationRequest)
if err != nil {
switch err {
case lib.ErrUserUniqueConstraintViolationUsername:
validationErrorBuilder.Add("email", "User with given email already exists.")
return h.renderTemplateWithData(rw, ctx, map[string]interface{}{
"validationErrors": validationErrorBuilder.Errors(),
"request": registrationRequest,
})
default:
return h.renderTemplate500(rw, ctx, err)
}
}
err = h.Container.ConfirmationMailer.Send(user.Username, map[string]interface{}{
"user": user,
})
if err != nil {
return h.renderTemplate500(rw, ctx, err)
}
h.redirect(rw, r, "registration_success", http.StatusFound)
return ctx
}
// RegistrationSuccess ...
func (h *Handler) RegistrationSuccess(ctx context.Context, rw http.ResponseWriter, r *http.Request) context.Context {
return h.renderTemplate(rw, ctx)
}
// RegistrationConfirmation ...
func (h *Handler) RegistrationConfirmation(ctx context.Context, rw http.ResponseWriter, r *http.Request) context.Context {
var ok bool
var confirmationTokenParam string
var userIDParam string
if confirmationTokenParam, ok = routing.ParamFromContext(ctx, "confirmationToken"); !ok {
h.Container.Logger.Debug("Confirmation token param is missing.")
return h.renderTemplate400(rw, ctx)
}
if userIDParam, ok = routing.ParamFromContext(ctx, "userId"); !ok {
h.Container.Logger.Debug("User ID param is missing.")
return h.renderTemplate400(rw, ctx)
}
userID, err := strconv.ParseInt(userIDParam, 10, 64)
if err != nil {
h.Container.Logger.Debug("User ID param wrong type.")
return h.renderTemplate400(rw, ctx)
}
if err := h.Container.RM.User.RegistrationConfirmation(userID, confirmationTokenParam); err != nil {
switch err {
case lib.ErrUserNotFound:
h.Container.Logger.Debug("Registration confirmation failure, user not found.")
return h.renderTemplateWithStatus(rw, ctx, http.StatusMethodNotAllowed)
default:
return h.renderTemplate500(rw, ctx, err)
}
}
return h.renderTemplate(rw, ctx)
}
func createAndRegisterUser(
passwordHasher security.PasswordHasher,
repository lib.UserRepository,
request *request.RegistrationRequest,
) (*model.User, error) {
confirmationToken := uuid.NewV4().String()
hashedPassword, err := passwordHasher.Hash(request.Password)
if err != nil {
return nil, err
}
user := model.NewUser(
request.Email,
hashedPassword,
request.FirstName,
request.LastName,
confirmationToken,
)
err = repository.Insert(user)
if err != nil {
return nil, err
}
return user, nil
}