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