Skip to content

Commit

Permalink
feat(signup): almost done with new sign up flow, grafana#2353
Browse files Browse the repository at this point in the history
  • Loading branch information
torkelo committed Aug 31, 2015
1 parent 13c70ac commit d19e101
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 64 deletions.
3 changes: 3 additions & 0 deletions conf/defaults.ini
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ auto_assign_org = true
# Default role new users will be automatically assigned (if auto_assign_org above is set to true)
auto_assign_org_role = Viewer

# Require email validation before sign up completes
verify_email_enabled = false

#################################### Anonymous Auth ##########################
[auth.anonymous]
# enable anonymous access
Expand Down
Binary file removed grafana
Binary file not shown.
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Register(r *macaron.Macaron) {

// sign up
r.Get("/signup", Index)
r.Get("/api/user/signup/options", wrap(GetSignUpOptions))
r.Post("/api/user/signup", bind(dtos.SignUpForm{}), wrap(SignUp))
r.Post("/api/user/signup/step2", bind(dtos.SignUpStep2Form{}), wrap(SignUpStep2))

Expand Down
75 changes: 43 additions & 32 deletions pkg/api/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import (
"github.com/grafana/grafana/pkg/util"
)

// GET /api/user/signup/options
func GetSignUpOptions(c *middleware.Context) Response {
return Json(200, util.DynMap{
"verifyEmailEnabled": setting.VerifyEmailEnabled,
"autoAssignOrg": setting.AutoAssignOrg,
})
}

// POST /api/user/signup
func SignUp(c *middleware.Context, form dtos.SignUpForm) Response {
if !setting.AllowUserSignUp {
Expand All @@ -19,7 +27,7 @@ func SignUp(c *middleware.Context, form dtos.SignUpForm) Response {

existing := m.GetUserByLoginQuery{LoginOrEmail: form.Email}
if err := bus.Dispatch(&existing); err == nil {
return ApiError(401, "User with same email address already exists", nil)
return ApiError(422, "User with same email address already exists", nil)
}

cmd := m.CreateTempUserCommand{}
Expand Down Expand Up @@ -49,64 +57,49 @@ func SignUpStep2(c *middleware.Context, form dtos.SignUpStep2Form) Response {
return ApiError(401, "User signup is disabled", nil)
}

query := m.GetTempUserByCodeQuery{Code: form.Code}

if err := bus.Dispatch(&query); err != nil {
if err == m.ErrTempUserNotFound {
return ApiError(404, "Invalid email verification code", nil)
}
return ApiError(500, "Failed to read temp user", err)
createUserCmd := m.CreateUserCommand{
Email: form.Email,
Login: form.Username,
Name: form.Name,
Password: form.Password,
OrgName: form.OrgName,
}

tempUser := query.Result
if tempUser.Email != form.Email {
return ApiError(404, "Email verification code does not match email", nil)
if setting.VerifyEmailEnabled {
if ok, rsp := verifyUserSignUpEmail(form.Email, form.Code); !ok {
return rsp
}
createUserCmd.EmailVerified = true
}

existing := m.GetUserByLoginQuery{LoginOrEmail: tempUser.Email}
existing := m.GetUserByLoginQuery{LoginOrEmail: form.Email}
if err := bus.Dispatch(&existing); err == nil {
return ApiError(401, "User with same email address already exists", nil)
}

// create user
createUserCmd := m.CreateUserCommand{
Email: tempUser.Email,
Login: form.Username,
Name: form.Name,
Password: form.Password,
OrgName: form.OrgName,
}

if err := bus.Dispatch(&createUserCmd); err != nil {
return ApiError(500, "Failed to create user", err)
}

// publish signup event
user := &createUserCmd.Result

bus.Publish(&events.SignUpCompleted{
Email: user.Email,
Name: user.NameOrFallback(),
})

// update tempuser
updateTempUserCmd := m.UpdateTempUserStatusCommand{
Code: tempUser.Code,
Status: m.TmpUserCompleted,
}

if err := bus.Dispatch(&updateTempUserCmd); err != nil {
return ApiError(500, "Failed to update temp user", err)
// mark temp user as completed
if ok, rsp := updateTempUserStatus(form.Code, m.TmpUserCompleted); !ok {
return rsp
}

// check for pending invites
invitesQuery := m.GetTempUsersQuery{Email: tempUser.Email, Status: m.TmpUserInvitePending}
invitesQuery := m.GetTempUsersQuery{Email: form.Email, Status: m.TmpUserInvitePending}
if err := bus.Dispatch(&invitesQuery); err != nil {
return ApiError(500, "Failed to query database for invites", err)
}

apiResponse := util.DynMap{"message": "User sign up completed succesfully", "code": "redirect-to-landing-page"}

for _, invite := range invitesQuery.Result {
if ok, rsp := applyUserInvite(user, invite, false); !ok {
return rsp
Expand All @@ -119,3 +112,21 @@ func SignUpStep2(c *middleware.Context, form dtos.SignUpStep2Form) Response {

return Json(200, apiResponse)
}

func verifyUserSignUpEmail(email string, code string) (bool, Response) {
query := m.GetTempUserByCodeQuery{Code: code}

if err := bus.Dispatch(&query); err != nil {
if err == m.ErrTempUserNotFound {
return false, ApiError(404, "Invalid email verification code", nil)
}
return false, ApiError(500, "Failed to read temp user", err)
}

tempUser := query.Result
if tempUser.Email != email {
return false, ApiError(404, "Email verification code does not match email", nil)
}

return true, nil
}
19 changes: 10 additions & 9 deletions pkg/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ func (u *User) NameOrFallback() string {
// COMMANDS

type CreateUserCommand struct {
Email string `json:"email" binding:"Required"`
Login string `json:"login"`
Name string `json:"name"`
Company string `json:"compay"`
OrgName string `json:"orgName"`
Password string `json:"password" binding:"Required"`
IsAdmin bool `json:"-"`

Result User `json:"-"`
Email string
Login string
Name string
Company string
OrgName string
Password string
EmailVerified bool
IsAdmin bool

Result User
}

type UpdateUserCommand struct {
Expand Down
4 changes: 4 additions & 0 deletions pkg/services/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func validateResetPasswordCode(query *m.ValidateResetPasswordCodeQuery) error {
}

func signUpStartedHandler(evt *events.SignUpStarted) error {
if !setting.VerifyEmailEnabled {
return nil
}

log.Info("User signup started: %s", evt.Email)

if evt.Email == "" {
Expand Down
17 changes: 9 additions & 8 deletions pkg/services/sqlstore/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ func CreateUser(cmd *m.CreateUserCommand) error {

// create user
user := m.User{
Email: cmd.Email,
Name: cmd.Name,
Login: cmd.Login,
Company: cmd.Company,
IsAdmin: cmd.IsAdmin,
OrgId: orgId,
Created: time.Now(),
Updated: time.Now(),
Email: cmd.Email,
Name: cmd.Name,
Login: cmd.Login,
Company: cmd.Company,
IsAdmin: cmd.IsAdmin,
OrgId: orgId,
EmailVerified: cmd.EmailVerified,
Created: time.Now(),
Updated: time.Now(),
}

if len(cmd.Password) > 0 {
Expand Down
15 changes: 10 additions & 5 deletions pkg/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ var (
EmailCodeValidMinutes int

// User settings
AllowUserSignUp bool
AllowUserOrgCreate bool
AutoAssignOrg bool
AutoAssignOrgRole string
RequireEmailValidation bool
AllowUserSignUp bool
AllowUserOrgCreate bool
AutoAssignOrg bool
AutoAssignOrgRole string
VerifyEmailEnabled bool

// Http auth
AdminUser string
Expand Down Expand Up @@ -394,6 +394,7 @@ func NewConfigContext(args *CommandLineArgs) {
AllowUserOrgCreate = users.Key("allow_org_create").MustBool(true)
AutoAssignOrg = users.Key("auto_assign_org").MustBool(true)
AutoAssignOrgRole = users.Key("auto_assign_org_role").In("Editor", []string{"Editor", "Admin", "Read Only Editor", "Viewer"})
VerifyEmailEnabled = users.Key("verify_email_enabled").MustBool(false)

// anonymous access
AnonymousEnabled = Cfg.Section("auth.anonymous").Key("enabled").MustBool(false)
Expand Down Expand Up @@ -425,6 +426,10 @@ func NewConfigContext(args *CommandLineArgs) {

readSessionConfig()
readSmtpSettings()

if VerifyEmailEnabled && !Smtp.Enabled {
log.Warn("require_email_validation is enabled but smpt is disabled")
}
}

func readSessionConfig() {
Expand Down
10 changes: 9 additions & 1 deletion public/app/controllers/signupCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ function (angular, config) {
$scope.formModel.email = params.email;
$scope.formModel.username = params.email;
$scope.formModel.code = params.code;

$scope.verifyEmailEnabled = false;
$scope.autoAssignOrg = false;

backendSrv.get('/api/user/signup/options').then(function(options) {
$scope.verifyEmailEnabled = options.verifyEmailEnabled;
$scope.autoAssignOrg = options.autoAssignOrg;
});
};

$scope.submit = function() {
if (!$scope.signupForm.$valid) {
if (!$scope.signUpForm.$valid) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions public/app/partials/signup_step2.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ <h3>

<br>

<form name="signupForm" class="login-form">
<form name="signUpForm" class="login-form">

<div style="display: inline-block; margin-bottom: 25px; width: 300px">
<div style="display: inline-block; margin-bottom: 25px; width: 300px" ng-if="verifyEmailEnabled">
<div class="editor-option">
<label class="small">Email verification code: (sent to your email)</label>
<input type="text" class="input input-xlarge text-center" ng-model="formModel.code" required></input>
</div>
</div>

<div class="tight-from-container">
<div class="tight-form">
<div class="tight-form" ng-if="!autoAssignOrg">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 128px">
Organization name
Expand Down
11 changes: 5 additions & 6 deletions public/app/services/backendSrv.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,16 @@ function (angular, _, config) {
return;
}

if (err.status === 422) {
alertSrv.set("Validation failed", "", "warning", 4000);
throw err.data;
}

var data = err.data || { message: 'Unexpected error' };

if (_.isString(data)) {
data = { message: data };
}

if (err.status === 422) {
alertSrv.set("Validation failed", data.message, "warning", 4000);
throw data;
}

data.severity = 'error';

if (err.status < 500) {
Expand Down

0 comments on commit d19e101

Please sign in to comment.