| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-11-04 18:17:07 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2020-08-13 20:38:54 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2015-07-21 18:18:11 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2015-07-21 18:18:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +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-07-17 15:51:34 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +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-07-18 23:39:12 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | func (hs *HTTPServer) GetPendingOrgInvites(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := models.GetTempUsersQuery{OrgId: c.OrgId, Status: models.TmpUserInvitePending} | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 	if err := hs.SQLStore.GetTempUsersQuery(c.Req.Context(), &query); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get invites from db", err) | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 20:26:49 +08:00
										 |  |  | 	for _, invite := range query.Result { | 
					
						
							|  |  |  | 		invite.Url = setting.ToAbsUrl("invite/" + invite.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, query.Result) | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | func (hs *HTTPServer) AddOrgInvite(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	inviteDto := dtos.AddInviteForm{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &inviteDto); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | 	if !inviteDto.Role.IsValid() { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(400, "Invalid role specified", nil) | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-18 17:43:34 +08:00
										 |  |  | 	// first try get existing user
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	userQuery := models.GetUserByLoginQuery{LoginOrEmail: inviteDto.LoginOrEmail} | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 	if err := hs.SQLStore.GetUserByLogin(c.Req.Context(), &userQuery); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if !errors.Is(err, models.ErrUserNotFound) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(500, "Failed to query db for existing user check", err) | 
					
						
							| 
									
										
										
										
											2015-07-18 17:43:34 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 		return hs.inviteExistingUserToOrg(c, userQuery.Result, &inviteDto) | 
					
						
							| 
									
										
										
										
											2015-07-18 17:43:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 00:45:50 +08:00
										 |  |  | 	if setting.DisableLoginForm { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(400, "Cannot invite when login is disabled.", nil) | 
					
						
							| 
									
										
										
										
											2019-09-13 00:45:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	cmd := models.CreateTempUserCommand{} | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							| 
									
										
										
										
											2015-08-10 20:03:08 +08:00
										 |  |  | 	cmd.Email = inviteDto.LoginOrEmail | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | 	cmd.Name = inviteDto.Name | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	cmd.Status = models.TmpUserInvitePending | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | 	cmd.InvitedByUserId = c.UserId | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 	cmd.Code, err = util.GetRandomString(30) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Could not generate random string", err) | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-17 20:42:49 +08:00
										 |  |  | 	cmd.Role = inviteDto.Role | 
					
						
							| 
									
										
										
										
											2015-07-20 16:57:39 +08:00
										 |  |  | 	cmd.RemoteAddr = c.Req.RemoteAddr | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +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 save invite to database", err) | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-18 23:39:12 +08:00
										 |  |  | 	// send invite email
 | 
					
						
							| 
									
										
										
										
											2017-12-13 20:16:44 +08:00
										 |  |  | 	if inviteDto.SendEmail && util.IsEmail(inviteDto.LoginOrEmail) { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		emailCmd := models.SendEmailCommand{ | 
					
						
							| 
									
										
										
										
											2015-08-10 20:03:08 +08:00
										 |  |  | 			To:       []string{inviteDto.LoginOrEmail}, | 
					
						
							| 
									
										
										
										
											2021-07-19 18:31:51 +08:00
										 |  |  | 			Template: "new_user_invite", | 
					
						
							| 
									
										
										
										
											2015-07-18 23:39:12 +08:00
										 |  |  | 			Data: map[string]interface{}{ | 
					
						
							| 
									
										
										
										
											2015-08-11 16:35:10 +08:00
										 |  |  | 				"Name":      util.StringsFallback2(cmd.Name, cmd.Email), | 
					
						
							|  |  |  | 				"OrgName":   c.OrgName, | 
					
						
							|  |  |  | 				"Email":     c.Email, | 
					
						
							|  |  |  | 				"LinkUrl":   setting.ToAbsUrl("invite/" + cmd.Code), | 
					
						
							|  |  |  | 				"InvitedBy": util.StringsFallback3(c.Name, c.Email, c.Login), | 
					
						
							| 
									
										
										
										
											2015-07-18 23:39:12 +08:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 		if err := hs.AlertNG.NotificationService.SendEmailCommandHandler(c.Req.Context(), &emailCmd); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 			if errors.Is(err, models.ErrSmtpNotEnabled) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 				return response.Error(412, err.Error(), err) | 
					
						
							| 
									
										
										
										
											2018-05-29 01:49:31 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-08-13 20:38:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(500, "Failed to send email invite", err) | 
					
						
							| 
									
										
										
										
											2015-07-18 23:39:12 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-08-10 20:03:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		emailSentCmd := models.UpdateTempUserWithEmailSentCommand{Code: cmd.Result.Code} | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 		if err := hs.SQLStore.UpdateTempUserWithEmailSent(c.Req.Context(), &emailSentCmd); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(500, "Failed to update invite with email sent info", err) | 
					
						
							| 
									
										
										
										
											2017-07-01 02:21:05 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Success(fmt.Sprintf("Sent invite to %s", inviteDto.LoginOrEmail)) | 
					
						
							| 
									
										
										
										
											2015-07-18 23:39:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Success(fmt.Sprintf("Created invite for %s", inviteDto.LoginOrEmail)) | 
					
						
							| 
									
										
										
										
											2015-07-17 15:51:34 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-20 16:57:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | func (hs *HTTPServer) inviteExistingUserToOrg(c *models.ReqContext, user *models.User, inviteDto *dtos.AddInviteForm) response.Response { | 
					
						
							| 
									
										
										
										
											2015-08-11 16:35:10 +08:00
										 |  |  | 	// user exists, add org role
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	createOrgUserCmd := models.AddOrgUserCommand{OrgId: c.OrgId, UserId: user.Id, Role: inviteDto.Role} | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 	if err := hs.SQLStore.AddOrgUser(c.Req.Context(), &createOrgUserCmd); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if errors.Is(err, models.ErrOrgUserAlreadyAdded) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(412, fmt.Sprintf("User %s is already added to organization", inviteDto.LoginOrEmail), err) | 
					
						
							| 
									
										
										
										
											2015-08-11 16:35:10 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Error while trying to create org user", err) | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-11 16:35:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	if inviteDto.SendEmail && util.IsEmail(user.Email) { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		emailCmd := models.SendEmailCommand{ | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 			To:       []string{user.Email}, | 
					
						
							| 
									
										
										
										
											2021-07-19 18:31:51 +08:00
										 |  |  | 			Template: "invited_to_org", | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 			Data: map[string]interface{}{ | 
					
						
							|  |  |  | 				"Name":      user.NameOrFallback(), | 
					
						
							|  |  |  | 				"OrgName":   c.OrgName, | 
					
						
							|  |  |  | 				"InvitedBy": util.StringsFallback3(c.Name, c.Email, c.Login), | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-08-11 16:35:10 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 		if err := hs.AlertNG.NotificationService.SendEmailCommandHandler(c.Req.Context(), &emailCmd); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(500, "Failed to send email invited_to_org", err) | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-08-11 16:35:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2020-09-14 14:58:23 +08:00
										 |  |  | 		"message": fmt.Sprintf("Existing Grafana user %s added to org %s", user.NameOrFallback(), c.OrgName), | 
					
						
							|  |  |  | 		"userId":  user.Id, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-08-11 16:35:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | func (hs *HTTPServer) RevokeInvite(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	if ok, rsp := hs.updateTempUserStatus(c.Req.Context(), web.Params(c.Req)[":code"], models.TmpUserRevoked); !ok { | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 		return rsp | 
					
						
							| 
									
										
										
										
											2015-07-20 16:57:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Success("Invite revoked") | 
					
						
							| 
									
										
										
										
											2015-07-20 16:57:39 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-20 21:52:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-04 20:01:37 +08:00
										 |  |  | // GetInviteInfoByCode gets a pending user invite corresponding to a certain code.
 | 
					
						
							|  |  |  | // A response containing an InviteInfo object is returned if the invite is found.
 | 
					
						
							|  |  |  | // If a (pending) invite is not found, 404 is returned.
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | func (hs *HTTPServer) GetInviteInfoByCode(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	query := models.GetTempUserByCodeQuery{Code: web.Params(c.Req)[":code"]} | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 	if err := hs.SQLStore.GetTempUserByCode(c.Req.Context(), &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 response.Error(404, "Invite not found", nil) | 
					
						
							| 
									
										
										
										
											2015-07-20 21:52:49 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get invite", err) | 
					
						
							| 
									
										
										
										
											2015-07-20 21:52:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-10 19:46:59 +08:00
										 |  |  | 	invite := query.Result | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	if invite.Status != models.TmpUserInvitePending { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Invite not found", nil) | 
					
						
							| 
									
										
										
										
											2019-12-04 20:01:37 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-20 21:52:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, dtos.InviteInfo{ | 
					
						
							| 
									
										
										
										
											2015-08-10 19:46:59 +08:00
										 |  |  | 		Email:     invite.Email, | 
					
						
							|  |  |  | 		Name:      invite.Name, | 
					
						
							|  |  |  | 		Username:  invite.Email, | 
					
						
							|  |  |  | 		InvitedBy: util.StringsFallback3(invite.InvitedByName, invite.InvitedByLogin, invite.InvitedByEmail), | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-07-20 21:52:49 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func (hs *HTTPServer) CompleteInvite(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	completeInvite := dtos.CompleteInviteForm{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &completeInvite); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := models.GetTempUserByCodeQuery{Code: completeInvite.InviteCode} | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 	if err := hs.SQLStore.GetTempUserByCode(c.Req.Context(), &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 response.Error(404, "Invite not found", nil) | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get invite", err) | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	invite := query.Result | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	if invite.Status != models.TmpUserInvitePending { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(412, fmt.Sprintf("Invite cannot be used in status %s", invite.Status), nil) | 
					
						
							| 
									
										
										
										
											2015-07-29 15:30:23 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	cmd := models.CreateUserCommand{ | 
					
						
							| 
									
										
										
										
											2015-09-01 18:35:06 +08:00
										 |  |  | 		Email:        completeInvite.Email, | 
					
						
							|  |  |  | 		Name:         completeInvite.Name, | 
					
						
							|  |  |  | 		Login:        completeInvite.Username, | 
					
						
							|  |  |  | 		Password:     completeInvite.Password, | 
					
						
							|  |  |  | 		SkipOrgSetup: true, | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-19 00:16:56 +08:00
										 |  |  | 	user, err := hs.Login.CreateUser(cmd) | 
					
						
							|  |  |  | 	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(412, fmt.Sprintf("User with email '%s' or username '%s' already exists", completeInvite.Email, completeInvite.Username), err) | 
					
						
							| 
									
										
										
										
											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-07-20 23:46:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 		Name:  user.NameOrFallback(), | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +08:00
										 |  |  | 		Email: user.Email, | 
					
						
							| 
									
										
										
										
											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-07-20 23:46:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 	if ok, rsp := hs.applyUserInvite(c.Req.Context(), user, invite, true); !ok { | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 		return rsp | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 accept invite", err) | 
					
						
							| 
									
										
										
										
											2020-03-23 20:37:53 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 22:58:46 +08:00
										 |  |  | 	metrics.MApiUserSignUpCompleted.Inc() | 
					
						
							|  |  |  | 	metrics.MApiUserSignUpInvite.Inc() | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2020-09-07 23:06:11 +08:00
										 |  |  | 		"message": "User created and logged in", | 
					
						
							|  |  |  | 		"id":      user.Id, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | func (hs *HTTPServer) updateTempUserStatus(ctx context.Context, code string, status models.TempUserStatus) (bool, response.Response) { | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 	// update temp user status
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	updateTmpUserCmd := models.UpdateTempUserStatusCommand{Code: code, Status: status} | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 	if err := hs.SQLStore.UpdateTempUserStatus(ctx, &updateTmpUserCmd); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return false, response.Error(500, "Failed to update invite status", err) | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | func (hs *HTTPServer) applyUserInvite(ctx context.Context, user *models.User, invite *models.TempUserDTO, setActive bool) (bool, response.Response) { | 
					
						
							| 
									
										
										
										
											2015-08-11 16:45:03 +08:00
										 |  |  | 	// add to org
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	addOrgUserCmd := models.AddOrgUserCommand{OrgId: invite.OrgId, UserId: user.Id, Role: invite.Role} | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 	if err := hs.SQLStore.AddOrgUser(ctx, &addOrgUserCmd); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if !errors.Is(err, models.ErrOrgUserAlreadyAdded) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return false, response.Error(500, "Error while trying to create org user", err) | 
					
						
							| 
									
										
										
										
											2015-08-17 16:55:52 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-08-11 16:45:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +08:00
										 |  |  | 	// update temp user status
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 	if ok, rsp := hs.updateTempUserStatus(ctx, invite.Code, models.TmpUserCompleted); !ok { | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 		return false, rsp | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 	if setActive { | 
					
						
							|  |  |  | 		// set org to active
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:24:52 +08:00
										 |  |  | 		if err := hs.SQLStore.SetUsingOrg(ctx, &models.SetUsingOrgCommand{OrgId: invite.OrgId, UserId: user.Id}); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return false, response.Error(500, "Failed to set org as active", err) | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 00:56:53 +08:00
										 |  |  | 	return true, nil | 
					
						
							| 
									
										
										
										
											2015-07-20 23:46:48 +08:00
										 |  |  | } |