Skip to content

Commit aa4d60c

Browse files
committed
Worked on reset password views, refactored out password strength to a reusable directive
1 parent e40b462 commit aa4d60c

File tree

18 files changed

+259
-112
lines changed

18 files changed

+259
-112
lines changed

pkg/api/api.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ func Register(r *macaron.Macaron) {
4141
r.Get("/signup", Index)
4242
r.Post("/api/user/signup", bind(m.CreateUserCommand{}), SignUp)
4343

44+
// reset password
45+
r.Get("/user/password/send-reset-email", Index)
46+
r.Get("/user/password/reset", Index)
47+
48+
r.Post("/api/user/password/send-reset-email", bind(dtos.SendResetPasswordEmailForm{}), wrap(SendResetPasswordEmail))
49+
r.Post("/api/user/password/reset", wrap(ViewResetPasswordForm))
50+
4451
// dashboard snapshots
4552
r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot)
4653
r.Get("/dashboard/snapshot/*", Index)

pkg/api/dtos/user.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ type AdminUserListItem struct {
2727
Login string `json:"login"`
2828
IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
2929
}
30+
31+
type SendResetPasswordEmailForm struct {
32+
UserOrEmail string `json:"userOrEmail" binding:"Required"`
33+
}

pkg/api/password.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package api
2+
3+
import (
4+
"github.com/grafana/grafana/pkg/api/dtos"
5+
"github.com/grafana/grafana/pkg/bus"
6+
"github.com/grafana/grafana/pkg/middleware"
7+
m "github.com/grafana/grafana/pkg/models"
8+
)
9+
10+
func SendResetPasswordEmail(c *middleware.Context, form dtos.SendResetPasswordEmailForm) Response {
11+
userQuery := m.GetUserByLoginQuery{LoginOrEmail: form.UserOrEmail}
12+
13+
if err := bus.Dispatch(&userQuery); err != nil {
14+
return ApiError(404, "User does not exist", err)
15+
}
16+
17+
emailCmd := m.SendResetPasswordEmailCommand{User: userQuery.Result}
18+
if err := bus.Dispatch(&emailCmd); err != nil {
19+
return ApiError(500, "Failed to send email", err)
20+
}
21+
22+
return ApiSuccess("Email sent")
23+
}
24+
25+
func ViewResetPasswordForm(c *middleware.Context) Response {
26+
return ApiSuccess("Email sent")
27+
}

pkg/models/emails.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ type SendEmailCommand struct {
55
From string
66
Subject string
77
Body string
8-
Type string
98
Massive bool
109
Info string
1110
}
@@ -16,13 +15,7 @@ type SendResetPasswordEmailCommand struct {
1615

1716
// create mail content
1817
func (m *SendEmailCommand) Content() string {
19-
// set mail type
20-
contentType := "text/plain; charset=UTF-8"
21-
if m.Type == "html" {
22-
contentType = "text/html; charset=UTF-8"
23-
}
24-
25-
// create mail content
18+
contentType := "text/html; charset=UTF-8"
2619
content := "From: " + m.From + "\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body
2720
return content
2821
}
@@ -34,6 +27,5 @@ func NewSendEmailCommand(To []string, From, Subject, Body string) SendEmailComma
3427
From: From,
3528
Subject: Subject,
3629
Body: Body,
37-
Type: "html",
3830
}
3931
}

pkg/models/user.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ type User struct {
3030
Updated time.Time
3131
}
3232

33+
func (u *User) NameOrFallback() string {
34+
if u.Name != "" {
35+
return u.Name
36+
} else if u.Login != "" {
37+
return u.Login
38+
} else {
39+
return u.Email
40+
}
41+
}
42+
3343
// ---------------------
3444
// COMMANDS
3545

pkg/services/mailer/mailer.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ func processMailQueue() {
5555
}
5656
}
5757

58-
func encodeRFC2047(text string) string {
59-
// use mail's rfc2047 to encode any string
60-
addr := mail.Address{Address: text}
61-
return strings.Trim(addr.String(), " <>")
62-
}
63-
6458
func handleEmailCommand(cmd *m.SendEmailCommand) error {
6559
log.Info("Sending on queue")
6660
mailQueue <- cmd
@@ -166,47 +160,6 @@ func sendToSmtpServer(recipients []string, msgContent []byte) error {
166160
}
167161

168162
return client.Quit()
169-
// smtpServer := "smtp.gmail.com"
170-
// auth := smtp.PlainAuth(
171-
// "",
172-
// "torkel.odegaard@gmail.com",
173-
// "peslpwstnnloiksq",
174-
// smtpServer,
175-
// )
176-
//
177-
// from := mail.Address{Name: "test", Address: "torkel@test.com"}
178-
// to := mail.Address{Name: "Torkel Ödegaard", Address: "torkel@raintank.io"}
179-
// title := "Message from Grafana"
180-
//
181-
// body := "Testing email sending"
182-
//
183-
// header := make(map[string]string)
184-
// header["From"] = from.String()
185-
// header["To"] = to.String()
186-
// header["Subject"] = encodeRFC2047(title)
187-
// header["MIME-Version"] = "1.0"
188-
// header["Content-Type"] = "text/plain; charset=\"utf-8\""
189-
// header["Content-Transfer-Encoding"] = "base64"
190-
//
191-
// message := ""
192-
// for k, v := range header {
193-
// message += fmt.Sprintf("%s: %s\r\n", k, v)
194-
// }
195-
// message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))
196-
//
197-
// // Connect to the server, authenticate, set the sender and recipient,
198-
// // and send the email all in one step.
199-
// err := smtp.SendMail(
200-
// smtpServer+":587",
201-
// auth,
202-
// from.Address,
203-
// []string{to.Address},
204-
// []byte(message),
205-
// )
206-
// if err != nil {
207-
// log.Info("Failed to send email: %v", err)
208-
// }
209-
// kkkk
210163
}
211164

212165
func buildAndSend(msg *m.SendEmailCommand) (int, error) {

pkg/services/notifications/email.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ func getMailTmplData(u *m.User) map[interface{}]interface{} {
2020
data["AppUrl"] = setting.AppUrl
2121
data["BuildVersion"] = setting.BuildVersion
2222
data["BuildStamp"] = setting.BuildStamp
23-
data["BuildCommit"] = setting.BuildCommit
23+
data["EmailCodeValidHours"] = setting.EmailCodeValidMinutes / 60
2424
data["Subject"] = map[string]interface{}{}
2525
if u != nil {
26-
data["User"] = u
26+
data["Name"] = u.NameOrFallback()
2727
}
2828
return data
2929
}

pkg/services/notifications/notifications.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func Init() error {
3535
return errors.New("Invalid email address for smpt from_adress config")
3636
}
3737

38+
if setting.EmailCodeValidMinutes == 0 {
39+
setting.EmailCodeValidMinutes = 120
40+
}
41+
3842
return nil
3943
}
4044

@@ -50,7 +54,7 @@ func subjectTemplateFunc(obj map[string]interface{}, value string) string {
5054
func sendResetPasswordEmail(cmd *m.SendResetPasswordEmailCommand) error {
5155
var buffer bytes.Buffer
5256

53-
var data = getMailTmplData(nil)
57+
var data = getMailTmplData(cmd.User)
5458
code := CreateUserActiveCode(cmd.User, nil)
5559
data["Code"] = code
5660

@@ -75,3 +79,35 @@ func CreateUserActiveCode(u *m.User, startInf interface{}) string {
7579
code += hex.EncodeToString([]byte(u.Login))
7680
return code
7781
}
82+
83+
// // verify active code when active account
84+
// func VerifyUserActiveCode(code string) (user *User) {
85+
// minutes := setting.Service.ActiveCodeLives
86+
//
87+
// if user = getVerifyUser(code); user != nil {
88+
// // time limit code
89+
// prefix := code[:base.TimeLimitCodeLength]
90+
// data := com.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
91+
//
92+
// if base.VerifyTimeLimitCode(data, minutes, prefix) {
93+
// return user
94+
// }
95+
// }
96+
// return nil
97+
// }
98+
//
99+
// // verify active code when active account
100+
// func VerifyUserActiveCode(code string) (user *User) {
101+
// minutes := setting.Service.ActiveCodeLives
102+
//
103+
// if user = getVerifyUser(code); user != nil {
104+
// // time limit code
105+
// prefix := code[:base.TimeLimitCodeLength]
106+
// data := com.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
107+
//
108+
// if base.VerifyTimeLimitCode(data, minutes, prefix) {
109+
// return user
110+
// }
111+
// }
112+
// return nil
113+
// }

pkg/services/notifications/notifications_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func TestNotifications(t *testing.T) {
2828

2929
Convey("When sending reset email password", func() {
3030
sendResetPasswordEmail(&m.SendResetPasswordEmailCommand{User: &m.User{Email: "asd@asd.com"}})
31-
So(sentMail.Body, ShouldContainSubstring, "h2")
32-
So(sentMail.Subject, ShouldEqual, "Welcome to Grafana")
31+
So(sentMail.Body, ShouldContainSubstring, "body")
32+
So(sentMail.Subject, ShouldEqual, "Reset your Grafana password")
3333
So(sentMail.Body, ShouldNotContainSubstring, "Subject")
3434
})
3535
})

public/app/controllers/loginCtrl.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ function (angular, config) {
2121
$scope.disableUserSignUp = config.disableUserSignUp;
2222

2323
$scope.loginMode = true;
24-
$scope.submitBtnClass = 'btn-inverse';
2524
$scope.submitBtnText = 'Log in';
26-
$scope.strengthClass = '';
2725

2826
$scope.init = function() {
2927
$scope.$watch("loginMode", $scope.loginModeChanged);
30-
$scope.passwordChanged();
3128

3229
var params = $location.search();
3330
if (params.failedMsg) {
@@ -56,27 +53,6 @@ function (angular, config) {
5653
$scope.submitBtnText = newValue ? 'Log in' : 'Sign up';
5754
};
5855

59-
$scope.passwordChanged = function(newValue) {
60-
if (!newValue) {
61-
$scope.strengthText = "";
62-
$scope.strengthClass = "hidden";
63-
return;
64-
}
65-
if (newValue.length < 4) {
66-
$scope.strengthText = "strength: weak sauce.";
67-
$scope.strengthClass = "password-strength-bad";
68-
return;
69-
}
70-
if (newValue.length <= 8) {
71-
$scope.strengthText = "strength: you can do better.";
72-
$scope.strengthClass = "password-strength-ok";
73-
return;
74-
}
75-
76-
$scope.strengthText = "strength: strong like a bull.";
77-
$scope.strengthClass = "password-strength-good";
78-
};
79-
8056
$scope.signUp = function() {
8157
if (!$scope.loginForm.$valid) {
8258
return;

public/app/controllers/resetPasswordCtrl.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
define([
22
'angular',
3-
'app',
4-
'lodash'
53
],
6-
function (angular, app, _) {
4+
function (angular) {
75
'use strict';
86

97
var module = angular.module('grafana.controllers');
108

11-
module.controller('ResetPasswordCtrl', function($scope, contextSrv, backendSrv) {
9+
module.controller('ResetPasswordCtrl', function($scope, contextSrv, backendSrv, $location) {
1210

1311
contextSrv.sidemenu = false;
14-
$scope.sendMode = true;
1512
$scope.formModel = {};
13+
$scope.mode = 'send';
14+
15+
if ($location.search().code) {
16+
$scope.mode = 'reset';
17+
}
1618

1719
$scope.sendResetEmail = function() {
18-
if (!$scope.loginForm.$valid) {
20+
if (!$scope.sendResetForm.$valid) {
21+
return;
22+
}
23+
backendSrv.post('/api/user/password/send-reset-email', $scope.formModel).then(function() {
24+
$scope.mode = 'email-sent';
25+
});
26+
};
27+
28+
$scope.submitReset = function() {
29+
if (!$scope.resetForm.$valid) { return; }
30+
31+
if ($scope.formModel.newPassword !== $scope.formModel.confirmPassword) {
32+
$scope.appEvent('alert-warning', ['New passwords do not match', '']);
1933
return;
2034
}
2135

2236
backendSrv.post('/api/user/password/send-reset-email', $scope.formModel).then(function() {
37+
$location.path('login');
2338
});
2439
};
2540

public/app/directives/all.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ define([
1818
'./topnav',
1919
'./giveFocus',
2020
'./annotationTooltip',
21+
'./passwordStrenght',
2122
], function () {});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
define([
2+
'angular',
3+
],
4+
function (angular) {
5+
'use strict';
6+
7+
angular
8+
.module('grafana.directives')
9+
.directive('passwordStrength', function() {
10+
var template = '<div class="password-strength small" ng-if="!loginMode" ng-class="strengthClass">' +
11+
'<em>{{strengthText}}</em>' +
12+
'</div>';
13+
return {
14+
template: template,
15+
scope: {
16+
password: "=",
17+
},
18+
link: function($scope) {
19+
20+
$scope.strengthClass = '';
21+
22+
function passwordChanged(newValue) {
23+
if (!newValue) {
24+
$scope.strengthText = "";
25+
$scope.strengthClass = "hidden";
26+
return;
27+
}
28+
if (newValue.length < 4) {
29+
$scope.strengthText = "strength: weak sauce.";
30+
$scope.strengthClass = "password-strength-bad";
31+
return;
32+
}
33+
if (newValue.length <= 8) {
34+
$scope.strengthText = "strength: you can do better.";
35+
$scope.strengthClass = "password-strength-ok";
36+
return;
37+
}
38+
39+
$scope.strengthText = "strength: strong like a bull.";
40+
$scope.strengthClass = "password-strength-good";
41+
}
42+
43+
$scope.$watch("password", passwordChanged);
44+
}
45+
};
46+
});
47+
});

0 commit comments

Comments
 (0)