|
|
|
@ -1,6 +1,7 @@ |
|
|
|
|
package api |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
@ -18,7 +19,7 @@ import ( |
|
|
|
|
func GetPendingOrgInvites(c *models.ReqContext) response.Response { |
|
|
|
|
query := models.GetTempUsersQuery{OrgId: c.OrgId, Status: models.TmpUserInvitePending} |
|
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil { |
|
|
|
|
if err := bus.DispatchCtx(c.Req.Context(), &query); err != nil { |
|
|
|
|
return response.Error(500, "Failed to get invites from db", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -62,7 +63,7 @@ func AddOrgInvite(c *models.ReqContext, inviteDto dtos.AddInviteForm) response.R |
|
|
|
|
cmd.Role = inviteDto.Role |
|
|
|
|
cmd.RemoteAddr = c.Req.RemoteAddr |
|
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil { |
|
|
|
|
if err := bus.DispatchCtx(c.Req.Context(), &cmd); err != nil { |
|
|
|
|
return response.Error(500, "Failed to save invite to database", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -102,7 +103,7 @@ func AddOrgInvite(c *models.ReqContext, inviteDto dtos.AddInviteForm) response.R |
|
|
|
|
func inviteExistingUserToOrg(c *models.ReqContext, user *models.User, inviteDto *dtos.AddInviteForm) response.Response { |
|
|
|
|
// user exists, add org role
|
|
|
|
|
createOrgUserCmd := models.AddOrgUserCommand{OrgId: c.OrgId, UserId: user.Id, Role: inviteDto.Role} |
|
|
|
|
if err := bus.Dispatch(&createOrgUserCmd); err != nil { |
|
|
|
|
if err := bus.DispatchCtx(c.Req.Context(), &createOrgUserCmd); err != nil { |
|
|
|
|
if errors.Is(err, models.ErrOrgUserAlreadyAdded) { |
|
|
|
|
return response.Error(412, fmt.Sprintf("User %s is already added to organization", inviteDto.LoginOrEmail), err) |
|
|
|
|
} |
|
|
|
@ -132,7 +133,7 @@ func inviteExistingUserToOrg(c *models.ReqContext, user *models.User, inviteDto |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func RevokeInvite(c *models.ReqContext) response.Response { |
|
|
|
|
if ok, rsp := updateTempUserStatus(web.Params(c.Req)[":code"], models.TmpUserRevoked); !ok { |
|
|
|
|
if ok, rsp := updateTempUserStatus(c.Req.Context(), web.Params(c.Req)[":code"], models.TmpUserRevoked); !ok { |
|
|
|
|
return rsp |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -144,7 +145,7 @@ func RevokeInvite(c *models.ReqContext) response.Response { |
|
|
|
|
// If a (pending) invite is not found, 404 is returned.
|
|
|
|
|
func GetInviteInfoByCode(c *models.ReqContext) response.Response { |
|
|
|
|
query := models.GetTempUserByCodeQuery{Code: web.Params(c.Req)[":code"]} |
|
|
|
|
if err := bus.Dispatch(&query); err != nil { |
|
|
|
|
if err := bus.DispatchCtx(c.Req.Context(), &query); err != nil { |
|
|
|
|
if errors.Is(err, models.ErrTempUserNotFound) { |
|
|
|
|
return response.Error(404, "Invite not found", nil) |
|
|
|
|
} |
|
|
|
@ -167,7 +168,7 @@ func GetInviteInfoByCode(c *models.ReqContext) response.Response { |
|
|
|
|
func (hs *HTTPServer) CompleteInvite(c *models.ReqContext, completeInvite dtos.CompleteInviteForm) response.Response { |
|
|
|
|
query := models.GetTempUserByCodeQuery{Code: completeInvite.InviteCode} |
|
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil { |
|
|
|
|
if err := bus.DispatchCtx(c.Req.Context(), &query); err != nil { |
|
|
|
|
if errors.Is(err, models.ErrTempUserNotFound) { |
|
|
|
|
return response.Error(404, "Invite not found", nil) |
|
|
|
|
} |
|
|
|
@ -203,7 +204,7 @@ func (hs *HTTPServer) CompleteInvite(c *models.ReqContext, completeInvite dtos.C |
|
|
|
|
return response.Error(500, "failed to publish event", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ok, rsp := applyUserInvite(user, invite, true); !ok { |
|
|
|
|
if ok, rsp := applyUserInvite(c.Req.Context(), user, invite, true); !ok { |
|
|
|
|
return rsp |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -221,33 +222,33 @@ func (hs *HTTPServer) CompleteInvite(c *models.ReqContext, completeInvite dtos.C |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func updateTempUserStatus(code string, status models.TempUserStatus) (bool, response.Response) { |
|
|
|
|
func updateTempUserStatus(ctx context.Context, code string, status models.TempUserStatus) (bool, response.Response) { |
|
|
|
|
// update temp user status
|
|
|
|
|
updateTmpUserCmd := models.UpdateTempUserStatusCommand{Code: code, Status: status} |
|
|
|
|
if err := bus.Dispatch(&updateTmpUserCmd); err != nil { |
|
|
|
|
if err := bus.DispatchCtx(ctx, &updateTmpUserCmd); err != nil { |
|
|
|
|
return false, response.Error(500, "Failed to update invite status", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func applyUserInvite(user *models.User, invite *models.TempUserDTO, setActive bool) (bool, response.Response) { |
|
|
|
|
func applyUserInvite(ctx context.Context, user *models.User, invite *models.TempUserDTO, setActive bool) (bool, response.Response) { |
|
|
|
|
// add to org
|
|
|
|
|
addOrgUserCmd := models.AddOrgUserCommand{OrgId: invite.OrgId, UserId: user.Id, Role: invite.Role} |
|
|
|
|
if err := bus.Dispatch(&addOrgUserCmd); err != nil { |
|
|
|
|
if err := bus.DispatchCtx(ctx, &addOrgUserCmd); err != nil { |
|
|
|
|
if !errors.Is(err, models.ErrOrgUserAlreadyAdded) { |
|
|
|
|
return false, response.Error(500, "Error while trying to create org user", err) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// update temp user status
|
|
|
|
|
if ok, rsp := updateTempUserStatus(invite.Code, models.TmpUserCompleted); !ok { |
|
|
|
|
if ok, rsp := updateTempUserStatus(ctx, invite.Code, models.TmpUserCompleted); !ok { |
|
|
|
|
return false, rsp |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if setActive { |
|
|
|
|
// set org to active
|
|
|
|
|
if err := bus.Dispatch(&models.SetUsingOrgCommand{OrgId: invite.OrgId, UserId: user.Id}); err != nil { |
|
|
|
|
if err := bus.DispatchCtx(ctx, &models.SetUsingOrgCommand{OrgId: invite.OrgId, UserId: user.Id}); err != nil { |
|
|
|
|
return false, response.Error(500, "Failed to set org as active", err) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|