This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
/
update_password.go
126 lines (101 loc) · 3.63 KB
/
update_password.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
package handlers
import (
"errors"
"net/http"
"os"
"strings"
"github.com/Coderockr/vitrine-social/server/mail"
"github.com/Coderockr/vitrine-social/server/model"
)
type (
// UpdatePasswordOrganizationRepository represet operations for organization repository.
UpdatePasswordOrganizationRepository interface {
Get(id int64) (*model.Organization, error)
GetByEmail(email string) (*model.Organization, error)
ResetPasswordTo(o *model.Organization, password string) error
ChangePassword(o model.Organization, cPassword, newPassword string) (model.Organization, error)
}
)
// UpdatePasswordHandler update user password
func UpdatePasswordHandler(repo UpdatePasswordOrganizationRepository) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var handlerForm map[string]string
err := requestToJSONObject(r, &handlerForm)
if err != nil {
HandleHTTPError(w, http.StatusBadRequest, err)
return
}
userID := GetUserID(r)
organization, err := repo.Get(userID)
var cPassword, nPassword string
var ok bool
if cPassword, ok = handlerForm["currentPassword"]; !ok {
HandleHTTPError(w, http.StatusBadRequest, errors.New("você deve informar senha atual"))
return
}
if nPassword, ok = handlerForm["newPassword"]; !ok {
HandleHTTPError(w, http.StatusBadRequest, errors.New("você deve informar a nova senha"))
return
}
if _, err = repo.ChangePassword(*organization, cPassword, nPassword); err != nil {
HandleHTTPError(w, http.StatusUnauthorized, err)
return
}
HandleHTTPSuccessNoContent(w)
}
}
// ResetPasswordHandler resets the current user password, if it has the permissions
func ResetPasswordHandler(repo UpdatePasswordOrganizationRepository) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var handlerForm map[string]string
err := requestToJSONObject(r, &handlerForm)
if err != nil {
HandleHTTPError(w, http.StatusBadRequest, err)
return
}
if HasPermission(r, model.PasswordResetPermission) == false {
HandleHTTPError(w, http.StatusBadRequest, errors.New("you do not have the right permissions to perform this action"))
return
}
userID := GetUserID(r)
organization, err := repo.Get(userID)
newPassword := strings.TrimSpace(handlerForm["newPassword"])
repo.ResetPasswordTo(organization, newPassword)
HandleHTTPSuccess(w, nil)
}
}
// ForgotPasswordHandler create a token to reset password and send it to email
func ForgotPasswordHandler(repo UpdatePasswordOrganizationRepository, mailer mail.Mailer, jm *JWTManager) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var handlerForm map[string]string
err := requestToJSONObject(r, &handlerForm)
if err != nil {
HandleHTTPError(w, http.StatusBadRequest, err)
return
}
organization, err := repo.GetByEmail(handlerForm["email"])
if organization == nil {
HandleHTTPError(w, http.StatusUnauthorized, errors.New("Não há organização com este email"))
return
}
p := []string{model.PasswordResetPermission}
token, err := jm.CreateToken(organization.User, &p)
if err != nil {
return
}
emailParams := mail.EmailParams{
To: organization.User.Email,
Subject: "Esqueci minha senha",
Template: mail.ForgotPasswordTemplate,
Variables: map[string]string{
"name": organization.Name,
"link": os.Getenv("FRONTEND_URL") + "/recover-password/" + token,
},
}
if err := mailer.SendEmail(emailParams); err != nil {
HandleHTTPError(w, http.StatusBadRequest, errors.New("Falha ao enviar o email"))
return
}
HandleHTTPSuccessNoContent(w)
}
}