2015-07-17 02:51:34 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-11-04 05:17:07 -05:00
|
|
|
"context"
|
2020-08-13 07:38:54 -05:00
|
|
|
"errors"
|
2015-07-21 05:18:11 -05:00
|
|
|
"fmt"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2022-07-08 06:07:00 -05:00
|
|
|
"strconv"
|
2015-07-21 05:18:11 -05:00
|
|
|
|
2015-07-17 02:51:34 -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-07-20 10:46:48 -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-07-08 06:07:00 -05:00
|
|
|
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
2022-06-28 07:32:25 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2015-07-18 10:39:12 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-07-17 02:51:34 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-07-17 02:51:34 -05:00
|
|
|
)
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route GET /org/invites org_invites getPendingOrgInvites
|
|
|
|
//
|
|
|
|
// Get pending invites.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: getPendingOrgInvitesResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 500: internalServerError
|
2022-01-31 10:24:52 -06:00
|
|
|
func (hs *HTTPServer) GetPendingOrgInvites(c *models.ReqContext) response.Response {
|
2020-03-04 05:57:20 -06:00
|
|
|
query := models.GetTempUsersQuery{OrgId: c.OrgId, Status: models.TmpUserInvitePending}
|
2015-07-17 02:51:34 -05:00
|
|
|
|
2022-01-31 10:24:52 -06:00
|
|
|
if err := hs.SQLStore.GetTempUsersQuery(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get invites from db", err)
|
2015-07-17 02:51:34 -05:00
|
|
|
}
|
|
|
|
|
2015-07-20 07:26:49 -05:00
|
|
|
for _, invite := range query.Result {
|
|
|
|
invite.Url = setting.ToAbsUrl("invite/" + invite.Code)
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, query.Result)
|
2015-07-17 02:51:34 -05:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route POST /org/invites org_invites addOrgInvite
|
|
|
|
//
|
|
|
|
// Add invite.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 400: badRequestError
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 412: SMTPNotEnabledError
|
|
|
|
// 500: internalServerError
|
2022-01-31 10:24:52 -06:00
|
|
|
func (hs *HTTPServer) AddOrgInvite(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06: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 02:51:34 -05:00
|
|
|
if !inviteDto.Role.IsValid() {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Invalid role specified", nil)
|
2015-07-17 02:51:34 -05:00
|
|
|
}
|
2022-06-27 11:40:12 -05:00
|
|
|
if !c.OrgRole.Includes(inviteDto.Role) && !c.IsGrafanaAdmin {
|
|
|
|
return response.Error(http.StatusForbidden, "Cannot assign a role higher than user's role", nil)
|
|
|
|
}
|
2015-07-17 02:51:34 -05:00
|
|
|
|
2015-07-18 04:43:34 -05:00
|
|
|
// first try get existing user
|
2020-03-04 05:57:20 -06:00
|
|
|
userQuery := models.GetUserByLoginQuery{LoginOrEmail: inviteDto.LoginOrEmail}
|
2022-01-31 10:24:52 -06:00
|
|
|
if err := hs.SQLStore.GetUserByLogin(c.Req.Context(), &userQuery); err != nil {
|
2022-07-20 07:50:06 -05:00
|
|
|
if !errors.Is(err, user.ErrUserNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to query db for existing user check", err)
|
2015-07-18 04:43:34 -05:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-08 06:07:00 -05:00
|
|
|
// Evaluate permissions for adding an existing user to the organization
|
|
|
|
userIDScope := ac.Scope("users", "id", strconv.Itoa(int(userQuery.Result.ID)))
|
|
|
|
hasAccess, err := hs.AccessControl.Evaluate(c.Req.Context(), c.SignedInUser, ac.EvalPermission(ac.ActionOrgUsersAdd, userIDScope))
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to evaluate permissions", err)
|
|
|
|
}
|
|
|
|
if !hasAccess {
|
|
|
|
return response.Error(http.StatusForbidden, "Permission denied: not permitted to add an existing user to this organisation", err)
|
|
|
|
}
|
2022-01-31 10:24:52 -06:00
|
|
|
return hs.inviteExistingUserToOrg(c, userQuery.Result, &inviteDto)
|
2015-07-18 04:43:34 -05:00
|
|
|
}
|
|
|
|
|
2019-09-12 11:45:50 -05:00
|
|
|
if setting.DisableLoginForm {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Cannot invite when login is disabled.", nil)
|
2019-09-12 11:45:50 -05:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
cmd := models.CreateTempUserCommand{}
|
2015-07-17 02:51:34 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
2015-08-10 07:03:08 -05:00
|
|
|
cmd.Email = inviteDto.LoginOrEmail
|
2015-07-17 02:51:34 -05:00
|
|
|
cmd.Name = inviteDto.Name
|
2020-03-04 05:57:20 -06:00
|
|
|
cmd.Status = models.TmpUserInvitePending
|
2015-07-17 02:51:34 -05:00
|
|
|
cmd.InvitedByUserId = c.UserId
|
2022-07-27 11:37:27 -05:00
|
|
|
var err error
|
2019-10-23 03:40:12 -05:00
|
|
|
cmd.Code, err = util.GetRandomString(30)
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Could not generate random string", err)
|
2019-10-23 03:40:12 -05:00
|
|
|
}
|
2015-07-17 07:42:49 -05:00
|
|
|
cmd.Role = inviteDto.Role
|
2015-07-20 03:57:39 -05:00
|
|
|
cmd.RemoteAddr = c.Req.RemoteAddr
|
2015-07-17 02:51:34 -05:00
|
|
|
|
2022-01-31 10:24:52 -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 save invite to database", err)
|
2015-07-17 02:51:34 -05:00
|
|
|
}
|
|
|
|
|
2015-07-18 10:39:12 -05:00
|
|
|
// send invite email
|
2017-12-13 06:16:44 -06:00
|
|
|
if inviteDto.SendEmail && util.IsEmail(inviteDto.LoginOrEmail) {
|
2020-03-04 05:57:20 -06:00
|
|
|
emailCmd := models.SendEmailCommand{
|
2015-08-10 07:03:08 -05:00
|
|
|
To: []string{inviteDto.LoginOrEmail},
|
2021-07-19 05:31:51 -05:00
|
|
|
Template: "new_user_invite",
|
2015-07-18 10:39:12 -05:00
|
|
|
Data: map[string]interface{}{
|
2015-08-11 03:35:10 -05: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 10:39:12 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:24:52 -06:00
|
|
|
if err := hs.AlertNG.NotificationService.SendEmailCommandHandler(c.Req.Context(), &emailCmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrSmtpNotEnabled) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(412, err.Error(), err)
|
2018-05-28 12:49:31 -05:00
|
|
|
}
|
2020-08-13 07:38:54 -05:00
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to send email invite", err)
|
2015-07-18 10:39:12 -05:00
|
|
|
}
|
2015-08-10 07:03:08 -05:00
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
emailSentCmd := models.UpdateTempUserWithEmailSentCommand{Code: cmd.Result.Code}
|
2022-01-31 10:24:52 -06:00
|
|
|
if err := hs.SQLStore.UpdateTempUserWithEmailSent(c.Req.Context(), &emailSentCmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update invite with email sent info", err)
|
2017-06-30 13:21:05 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success(fmt.Sprintf("Sent invite to %s", inviteDto.LoginOrEmail))
|
2015-07-18 10:39:12 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success(fmt.Sprintf("Created invite for %s", inviteDto.LoginOrEmail))
|
2015-07-17 02:51:34 -05:00
|
|
|
}
|
2015-07-20 03:57:39 -05:00
|
|
|
|
2022-06-28 07:32:25 -05:00
|
|
|
func (hs *HTTPServer) inviteExistingUserToOrg(c *models.ReqContext, user *user.User, inviteDto *dtos.AddInviteForm) response.Response {
|
2015-08-11 03:35:10 -05:00
|
|
|
// user exists, add org role
|
2022-06-28 07:32:25 -05:00
|
|
|
createOrgUserCmd := models.AddOrgUserCommand{OrgId: c.OrgId, UserId: user.ID, Role: inviteDto.Role}
|
2022-01-31 10:24:52 -06:00
|
|
|
if err := hs.SQLStore.AddOrgUser(c.Req.Context(), &createOrgUserCmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrOrgUserAlreadyAdded) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(412, fmt.Sprintf("User %s is already added to organization", inviteDto.LoginOrEmail), err)
|
2015-08-11 03:35:10 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Error while trying to create org user", err)
|
2018-03-22 06:37:35 -05:00
|
|
|
}
|
2015-08-11 03:35:10 -05:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
if inviteDto.SendEmail && util.IsEmail(user.Email) {
|
2020-03-04 05:57:20 -06:00
|
|
|
emailCmd := models.SendEmailCommand{
|
2018-03-22 06:37:35 -05:00
|
|
|
To: []string{user.Email},
|
2021-07-19 05:31:51 -05:00
|
|
|
Template: "invited_to_org",
|
2018-03-22 06:37:35 -05:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"Name": user.NameOrFallback(),
|
|
|
|
"OrgName": c.OrgName,
|
|
|
|
"InvitedBy": util.StringsFallback3(c.Name, c.Email, c.Login),
|
|
|
|
},
|
2015-08-11 03:35:10 -05:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:24:52 -06:00
|
|
|
if err := hs.AlertNG.NotificationService.SendEmailCommandHandler(c.Req.Context(), &emailCmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to send email invited_to_org", err)
|
2018-03-22 06:37:35 -05:00
|
|
|
}
|
2015-08-11 03:35:10 -05:00
|
|
|
}
|
2018-03-22 06:37:35 -05:00
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, util.DynMap{
|
2020-09-14 01:58:23 -05:00
|
|
|
"message": fmt.Sprintf("Existing Grafana user %s added to org %s", user.NameOrFallback(), c.OrgName),
|
2022-06-28 07:32:25 -05:00
|
|
|
"userId": user.ID,
|
2020-09-14 01:58:23 -05:00
|
|
|
})
|
2015-08-11 03:35:10 -05:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route DELETE /org/invites/{invitation_code}/revoke org_invites revokeInvite
|
|
|
|
//
|
|
|
|
// Revoke invite.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
2022-01-31 10:24:52 -06: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-30 11:56:53 -05:00
|
|
|
return rsp
|
2015-07-20 03:57:39 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Invite revoked")
|
2015-07-20 03:57:39 -05:00
|
|
|
}
|
2015-07-20 08:52:49 -05:00
|
|
|
|
2019-12-04 06:01:37 -06: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-01-31 10:24:52 -06:00
|
|
|
func (hs *HTTPServer) GetInviteInfoByCode(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
query := models.GetTempUserByCodeQuery{Code: web.Params(c.Req)[":code"]}
|
2022-01-31 10:24:52 -06:00
|
|
|
if err := hs.SQLStore.GetTempUserByCode(c.Req.Context(), &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 response.Error(404, "Invite not found", nil)
|
2015-07-20 08:52:49 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get invite", err)
|
2015-07-20 08:52:49 -05:00
|
|
|
}
|
|
|
|
|
2015-08-10 06:46:59 -05:00
|
|
|
invite := query.Result
|
2020-03-04 05:57:20 -06:00
|
|
|
if invite.Status != models.TmpUserInvitePending {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Invite not found", nil)
|
2019-12-04 06:01:37 -06:00
|
|
|
}
|
2015-07-20 08:52:49 -05:00
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, dtos.InviteInfo{
|
2015-08-10 06:46:59 -05:00
|
|
|
Email: invite.Email,
|
|
|
|
Name: invite.Name,
|
|
|
|
Username: invite.Email,
|
|
|
|
InvitedBy: util.StringsFallback3(invite.InvitedByName, invite.InvitedByLogin, invite.InvitedByEmail),
|
|
|
|
})
|
2015-07-20 08:52:49 -05:00
|
|
|
}
|
2015-07-20 10:46:48 -05:00
|
|
|
|
2021-11-29 03:18:01 -06: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 05:57:20 -06:00
|
|
|
query := models.GetTempUserByCodeQuery{Code: completeInvite.InviteCode}
|
2015-07-20 10:46:48 -05:00
|
|
|
|
2022-01-31 10:24:52 -06:00
|
|
|
if err := hs.SQLStore.GetTempUserByCode(c.Req.Context(), &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 response.Error(404, "Invite not found", nil)
|
2015-07-20 10:46:48 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get invite", err)
|
2015-07-20 10:46:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
invite := query.Result
|
2020-03-04 05:57:20 -06:00
|
|
|
if invite.Status != models.TmpUserInvitePending {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(412, fmt.Sprintf("Invite cannot be used in status %s", invite.Status), nil)
|
2015-07-29 02:30:23 -05:00
|
|
|
}
|
2015-07-20 10:46:48 -05:00
|
|
|
|
2022-06-28 07:32:25 -05:00
|
|
|
cmd := user.CreateUserCommand{
|
2015-09-01 05:35:06 -05:00
|
|
|
Email: completeInvite.Email,
|
|
|
|
Name: completeInvite.Name,
|
|
|
|
Login: completeInvite.Username,
|
|
|
|
Password: completeInvite.Password,
|
|
|
|
SkipOrgSetup: true,
|
2015-07-20 10:46:48 -05:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:32:25 -05:00
|
|
|
usr, err := hs.Login.CreateUser(cmd)
|
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(412, fmt.Sprintf("User with email '%s' or username '%s' already exists", completeInvite.Email, completeInvite.Username), err)
|
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-07-20 10:46:48 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
Name: usr.NameOrFallback(),
|
|
|
|
Email: usr.Email,
|
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-07-20 10:46:48 -05:00
|
|
|
|
2022-06-28 07:32:25 -05:00
|
|
|
if ok, rsp := hs.applyUserInvite(c.Req.Context(), usr, invite, true); !ok {
|
2015-08-30 11:56:53 -05:00
|
|
|
return rsp
|
|
|
|
}
|
|
|
|
|
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 accept invite", err)
|
2020-03-23 07:37:53 -05:00
|
|
|
}
|
2015-08-30 11:56:53 -05:00
|
|
|
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MApiUserSignUpCompleted.Inc()
|
|
|
|
metrics.MApiUserSignUpInvite.Inc()
|
2015-08-30 11:56:53 -05:00
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, util.DynMap{
|
2020-09-07 10:06:11 -05:00
|
|
|
"message": "User created and logged in",
|
2022-06-28 07:32:25 -05:00
|
|
|
"id": usr.ID,
|
2020-09-07 10:06:11 -05:00
|
|
|
})
|
2015-08-30 11:56:53 -05:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:24:52 -06:00
|
|
|
func (hs *HTTPServer) updateTempUserStatus(ctx context.Context, code string, status models.TempUserStatus) (bool, response.Response) {
|
2015-08-30 11:56:53 -05:00
|
|
|
// update temp user status
|
2020-03-04 05:57:20 -06:00
|
|
|
updateTmpUserCmd := models.UpdateTempUserStatusCommand{Code: code, Status: status}
|
2022-01-31 10:24:52 -06:00
|
|
|
if err := hs.SQLStore.UpdateTempUserStatus(ctx, &updateTmpUserCmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return false, response.Error(500, "Failed to update invite status", err)
|
2015-08-30 11:56:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:32:25 -05:00
|
|
|
func (hs *HTTPServer) applyUserInvite(ctx context.Context, user *user.User, invite *models.TempUserDTO, setActive bool) (bool, response.Response) {
|
2015-08-11 03:45:03 -05:00
|
|
|
// add to org
|
2022-06-28 07:32:25 -05:00
|
|
|
addOrgUserCmd := models.AddOrgUserCommand{OrgId: invite.OrgId, UserId: user.ID, Role: invite.Role}
|
2022-01-31 10:24:52 -06:00
|
|
|
if err := hs.SQLStore.AddOrgUser(ctx, &addOrgUserCmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if !errors.Is(err, models.ErrOrgUserAlreadyAdded) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return false, response.Error(500, "Error while trying to create org user", err)
|
2015-08-17 03:55:52 -05:00
|
|
|
}
|
2015-08-11 03:45:03 -05:00
|
|
|
}
|
|
|
|
|
2015-07-20 10:46:48 -05:00
|
|
|
// update temp user status
|
2022-01-31 10:24:52 -06:00
|
|
|
if ok, rsp := hs.updateTempUserStatus(ctx, invite.Code, models.TmpUserCompleted); !ok {
|
2015-08-30 11:56:53 -05:00
|
|
|
return false, rsp
|
2015-07-20 10:46:48 -05:00
|
|
|
}
|
|
|
|
|
2015-08-30 11:56:53 -05:00
|
|
|
if setActive {
|
|
|
|
// set org to active
|
2022-06-28 07:32:25 -05:00
|
|
|
if err := hs.SQLStore.SetUsingOrg(ctx, &models.SetUsingOrgCommand{OrgId: invite.OrgId, UserId: user.ID}); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return false, response.Error(500, "Failed to set org as active", err)
|
2015-08-30 11:56:53 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-20 10:46:48 -05:00
|
|
|
|
2015-08-30 11:56:53 -05:00
|
|
|
return true, nil
|
2015-07-20 10:46:48 -05:00
|
|
|
}
|
2022-07-27 08:54:37 -05:00
|
|
|
|
|
|
|
// swagger:parameters addOrgInvite
|
|
|
|
type AddInviteParams struct {
|
|
|
|
// in:body
|
|
|
|
// required:true
|
|
|
|
Body dtos.AddInviteForm `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters revokeInvite
|
|
|
|
type RevokeInviteParams struct {
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
Code string `json:"invitation_code"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response getPendingOrgInvitesResponse
|
|
|
|
type GetPendingOrgInvitesResponse struct {
|
|
|
|
// The response message
|
|
|
|
// in: body
|
|
|
|
Body []*models.TempUserDTO `json:"body"`
|
|
|
|
}
|