| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-11-04 18:17:07 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2020-08-13 20:38:54 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2020-08-13 20:38:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-27 19:59:58 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2015-06-08 23:56:56 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/events" | 
					
						
							| 
									
										
										
										
											2019-02-24 06:35:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/metrics" | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2015-08-27 19:59:58 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | // GET /api/user/signup/options
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func GetSignUpOptions(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | 		"verifyEmailEnabled": setting.VerifyEmailEnabled, | 
					
						
							|  |  |  | 		"autoAssignOrg":      setting.AutoAssignOrg, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-27 22:14:53 +08:00
										 |  |  | // POST /api/user/signup
 | 
					
						
							| 
									
										
										
										
											2022-01-27 17:33:02 +08:00
										 |  |  | func (hs *HTTPServer) SignUp(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	form := dtos.SignUpForm{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &form); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-11 23:19:29 +08:00
										 |  |  | 	if !setting.AllowUserSignUp { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(401, "User signup is disabled", nil) | 
					
						
							| 
									
										
										
										
											2015-01-29 22:46:54 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	existing := models.GetUserByLoginQuery{LoginOrEmail: form.Email} | 
					
						
							| 
									
										
										
										
											2022-01-27 17:33:02 +08:00
										 |  |  | 	if err := hs.SQLStore.GetUserByLogin(c.Req.Context(), &existing); err == nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(422, "User with same email address already exists", nil) | 
					
						
							| 
									
										
										
										
											2015-08-27 19:59:58 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	cmd := models.CreateTempUserCommand{} | 
					
						
							| 
									
										
										
										
											2015-08-27 19:59:58 +08:00
										 |  |  | 	cmd.OrgId = -1 | 
					
						
							|  |  |  | 	cmd.Email = form.Email | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	cmd.Status = models.TmpUserSignUpStarted | 
					
						
							| 
									
										
										
										
											2015-08-27 19:59:58 +08:00
										 |  |  | 	cmd.InvitedByUserId = c.UserId | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 	cmd.Code, err = util.GetRandomString(20) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to generate random string", err) | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-27 19:59:58 +08:00
										 |  |  | 	cmd.RemoteAddr = c.Req.RemoteAddr | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 17:33:02 +08:00
										 |  |  | 	if err := hs.SQLStore.CreateTempUser(c.Req.Context(), &cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to create signup", err) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 16:36:01 +08:00
										 |  |  | 	if err := bus.Publish(c.Req.Context(), &events.SignUpStarted{ | 
					
						
							| 
									
										
										
										
											2015-08-28 15:24:30 +08:00
										 |  |  | 		Email: form.Email, | 
					
						
							|  |  |  | 		Code:  cmd.Code, | 
					
						
							| 
									
										
										
										
											2019-10-09 00:57:53 +08:00
										 |  |  | 	}); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to publish event", err) | 
					
						
							| 
									
										
										
										
											2019-10-09 00:57:53 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-21 16:52:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 22:58:46 +08:00
										 |  |  | 	metrics.MApiUserSignUpStarted.Inc() | 
					
						
							| 
									
										
										
										
											2015-08-28 15:24:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{"status": "SignUpCreated"}) | 
					
						
							| 
									
										
										
										
											2015-08-28 15:24:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func (hs *HTTPServer) SignUpStep2(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	form := dtos.SignUpStep2Form{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &form); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-28 15:24:30 +08:00
										 |  |  | 	if !setting.AllowUserSignUp { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(401, "User signup is disabled", nil) | 
					
						
							| 
									
										
										
										
											2015-08-28 15:24:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	createUserCmd := models.CreateUserCommand{ | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | 		Email:    form.Email, | 
					
						
							|  |  |  | 		Login:    form.Username, | 
					
						
							|  |  |  | 		Name:     form.Name, | 
					
						
							|  |  |  | 		Password: form.Password, | 
					
						
							|  |  |  | 		OrgName:  form.OrgName, | 
					
						
							| 
									
										
										
										
											2015-08-28 15:24:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-01 18:35:06 +08:00
										 |  |  | 	// verify email
 | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | 	if setting.VerifyEmailEnabled { | 
					
						
							| 
									
										
										
										
											2022-01-27 17:33:02 +08:00
										 |  |  | 		if ok, rsp := hs.verifyUserSignUpEmail(c.Req.Context(), form.Email, form.Code); !ok { | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | 			return rsp | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		createUserCmd.EmailVerified = true | 
					
						
							| 
									
										
										
										
											2015-08-28 15:24:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-19 00:16:56 +08:00
										 |  |  | 	user, err := hs.Login.CreateUser(createUserCmd) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-08-13 20:38:54 +08:00
										 |  |  | 		if errors.Is(err, models.ErrUserAlreadyExists) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(401, "User with same email address already exists", nil) | 
					
						
							| 
									
										
										
										
											2020-08-13 20:38:54 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to create user", err) | 
					
						
							| 
									
										
										
										
											2015-08-28 21:14:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 	// publish signup event
 | 
					
						
							| 
									
										
										
										
											2022-01-04 16:36:01 +08:00
										 |  |  | 	if err := bus.Publish(c.Req.Context(), &events.SignUpCompleted{ | 
					
						
							| 
									
										
										
										
											2015-08-28 21:14:24 +08:00
										 |  |  | 		Email: user.Email, | 
					
						
							|  |  |  | 		Name:  user.NameOrFallback(), | 
					
						
							| 
									
										
										
										
											2019-10-09 00:57:53 +08:00
										 |  |  | 	}); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to publish event", err) | 
					
						
							| 
									
										
										
										
											2019-10-09 00:57:53 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-28 21:14:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | 	// mark temp user as completed
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 	if ok, rsp := hs.updateTempUserStatus(c.Req.Context(), form.Code, models.TmpUserCompleted); !ok { | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | 		return rsp | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-28 21:14:24 +08:00
										 |  |  | 	// check for pending invites
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	invitesQuery := models.GetTempUsersQuery{Email: form.Email, Status: models.TmpUserInvitePending} | 
					
						
							| 
									
										
										
										
											2022-01-27 17:33:02 +08:00
										 |  |  | 	if err := hs.SQLStore.GetTempUsersQuery(c.Req.Context(), &invitesQuery); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to query database for invites", err) | 
					
						
							| 
									
										
										
										
											2015-08-28 21:14:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-13 08:01:03 +08:00
										 |  |  | 	apiResponse := util.DynMap{"message": "User sign up completed successfully", "code": "redirect-to-landing-page"} | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 	for _, invite := range invitesQuery.Result { | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 		if ok, rsp := hs.applyUserInvite(c.Req.Context(), user, invite, false); !ok { | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 			return rsp | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		apiResponse["code"] = "redirect-to-select-org" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-19 00:16:56 +08:00
										 |  |  | 	err = hs.loginUserWithUser(user, c) | 
					
						
							| 
									
										
										
										
											2020-03-23 20:37:53 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "failed to login user", err) | 
					
						
							| 
									
										
										
										
											2020-03-23 20:37:53 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 22:58:46 +08:00
										 |  |  | 	metrics.MApiUserSignUpCompleted.Inc() | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, apiResponse) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 17:33:02 +08:00
										 |  |  | func (hs *HTTPServer) verifyUserSignUpEmail(ctx context.Context, email string, code string) (bool, response.Response) { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := models.GetTempUserByCodeQuery{Code: code} | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 17:33:02 +08:00
										 |  |  | 	if err := hs.SQLStore.GetTempUserByCode(ctx, &query); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if errors.Is(err, models.ErrTempUserNotFound) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return false, response.Error(404, "Invalid email verification code", nil) | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return false, response.Error(500, "Failed to read temp user", err) | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tempUser := query.Result | 
					
						
							|  |  |  | 	if tempUser.Email != email { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return false, response.Error(404, "Email verification code does not match email", nil) | 
					
						
							| 
									
										
										
										
											2015-08-31 17:35:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true, nil | 
					
						
							|  |  |  | } |