Chore: Moves common and response into separate packages (#30298)
* Chore: moves common and response into separate packages * Chore: moves common and response into separate packages * Update pkg/api/utils/common.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> * Chore: changes after PR comments * Chore: move wrap to routing package * Chore: move functions in common to response package * Chore: move functions in common to response package * Chore: formats imports Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
co-authored by
Arve Knudsen
parent
a94c256516
commit
3d41267fc4
+41
-40
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||
@@ -13,37 +14,37 @@ import (
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
func GetPendingOrgInvites(c *models.ReqContext) Response {
|
||||
func GetPendingOrgInvites(c *models.ReqContext) response.Response {
|
||||
query := models.GetTempUsersQuery{OrgId: c.OrgId, Status: models.TmpUserInvitePending}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return Error(500, "Failed to get invites from db", err)
|
||||
return response.Error(500, "Failed to get invites from db", err)
|
||||
}
|
||||
|
||||
for _, invite := range query.Result {
|
||||
invite.Url = setting.ToAbsUrl("invite/" + invite.Code)
|
||||
}
|
||||
|
||||
return JSON(200, query.Result)
|
||||
return response.JSON(200, query.Result)
|
||||
}
|
||||
|
||||
func AddOrgInvite(c *models.ReqContext, inviteDto dtos.AddInviteForm) Response {
|
||||
func AddOrgInvite(c *models.ReqContext, inviteDto dtos.AddInviteForm) response.Response {
|
||||
if !inviteDto.Role.IsValid() {
|
||||
return Error(400, "Invalid role specified", nil)
|
||||
return response.Error(400, "Invalid role specified", nil)
|
||||
}
|
||||
|
||||
// first try get existing user
|
||||
userQuery := models.GetUserByLoginQuery{LoginOrEmail: inviteDto.LoginOrEmail}
|
||||
if err := bus.Dispatch(&userQuery); err != nil {
|
||||
if !errors.Is(err, models.ErrUserNotFound) {
|
||||
return Error(500, "Failed to query db for existing user check", err)
|
||||
return response.Error(500, "Failed to query db for existing user check", err)
|
||||
}
|
||||
} else {
|
||||
return inviteExistingUserToOrg(c, userQuery.Result, &inviteDto)
|
||||
}
|
||||
|
||||
if setting.DisableLoginForm {
|
||||
return Error(400, "Cannot invite when login is disabled.", nil)
|
||||
return response.Error(400, "Cannot invite when login is disabled.", nil)
|
||||
}
|
||||
|
||||
cmd := models.CreateTempUserCommand{}
|
||||
@@ -55,13 +56,13 @@ func AddOrgInvite(c *models.ReqContext, inviteDto dtos.AddInviteForm) Response {
|
||||
var err error
|
||||
cmd.Code, err = util.GetRandomString(30)
|
||||
if err != nil {
|
||||
return Error(500, "Could not generate random string", err)
|
||||
return response.Error(500, "Could not generate random string", err)
|
||||
}
|
||||
cmd.Role = inviteDto.Role
|
||||
cmd.RemoteAddr = c.Req.RemoteAddr
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
return Error(500, "Failed to save invite to database", err)
|
||||
return response.Error(500, "Failed to save invite to database", err)
|
||||
}
|
||||
|
||||
// send invite email
|
||||
@@ -80,31 +81,31 @@ func AddOrgInvite(c *models.ReqContext, inviteDto dtos.AddInviteForm) Response {
|
||||
|
||||
if err := bus.Dispatch(&emailCmd); err != nil {
|
||||
if errors.Is(err, models.ErrSmtpNotEnabled) {
|
||||
return Error(412, err.Error(), err)
|
||||
return response.Error(412, err.Error(), err)
|
||||
}
|
||||
|
||||
return Error(500, "Failed to send email invite", err)
|
||||
return response.Error(500, "Failed to send email invite", err)
|
||||
}
|
||||
|
||||
emailSentCmd := models.UpdateTempUserWithEmailSentCommand{Code: cmd.Result.Code}
|
||||
if err := bus.Dispatch(&emailSentCmd); err != nil {
|
||||
return Error(500, "Failed to update invite with email sent info", err)
|
||||
return response.Error(500, "Failed to update invite with email sent info", err)
|
||||
}
|
||||
|
||||
return Success(fmt.Sprintf("Sent invite to %s", inviteDto.LoginOrEmail))
|
||||
return response.Success(fmt.Sprintf("Sent invite to %s", inviteDto.LoginOrEmail))
|
||||
}
|
||||
|
||||
return Success(fmt.Sprintf("Created invite for %s", inviteDto.LoginOrEmail))
|
||||
return response.Success(fmt.Sprintf("Created invite for %s", inviteDto.LoginOrEmail))
|
||||
}
|
||||
|
||||
func inviteExistingUserToOrg(c *models.ReqContext, user *models.User, inviteDto *dtos.AddInviteForm) Response {
|
||||
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 errors.Is(err, models.ErrOrgUserAlreadyAdded) {
|
||||
return Error(412, fmt.Sprintf("User %s is already added to organization", inviteDto.LoginOrEmail), err)
|
||||
return response.Error(412, fmt.Sprintf("User %s is already added to organization", inviteDto.LoginOrEmail), err)
|
||||
}
|
||||
return Error(500, "Error while trying to create org user", err)
|
||||
return response.Error(500, "Error while trying to create org user", err)
|
||||
}
|
||||
|
||||
if inviteDto.SendEmail && util.IsEmail(user.Email) {
|
||||
@@ -119,42 +120,42 @@ func inviteExistingUserToOrg(c *models.ReqContext, user *models.User, inviteDto
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&emailCmd); err != nil {
|
||||
return Error(500, "Failed to send email invited_to_org", err)
|
||||
return response.Error(500, "Failed to send email invited_to_org", err)
|
||||
}
|
||||
}
|
||||
|
||||
return JSON(200, util.DynMap{
|
||||
return response.JSON(200, util.DynMap{
|
||||
"message": fmt.Sprintf("Existing Grafana user %s added to org %s", user.NameOrFallback(), c.OrgName),
|
||||
"userId": user.Id,
|
||||
})
|
||||
}
|
||||
|
||||
func RevokeInvite(c *models.ReqContext) Response {
|
||||
func RevokeInvite(c *models.ReqContext) response.Response {
|
||||
if ok, rsp := updateTempUserStatus(c.Params(":code"), models.TmpUserRevoked); !ok {
|
||||
return rsp
|
||||
}
|
||||
|
||||
return Success("Invite revoked")
|
||||
return response.Success("Invite revoked")
|
||||
}
|
||||
|
||||
// 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.
|
||||
func GetInviteInfoByCode(c *models.ReqContext) Response {
|
||||
func GetInviteInfoByCode(c *models.ReqContext) response.Response {
|
||||
query := models.GetTempUserByCodeQuery{Code: c.Params(":code")}
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
if errors.Is(err, models.ErrTempUserNotFound) {
|
||||
return Error(404, "Invite not found", nil)
|
||||
return response.Error(404, "Invite not found", nil)
|
||||
}
|
||||
return Error(500, "Failed to get invite", err)
|
||||
return response.Error(500, "Failed to get invite", err)
|
||||
}
|
||||
|
||||
invite := query.Result
|
||||
if invite.Status != models.TmpUserInvitePending {
|
||||
return Error(404, "Invite not found", nil)
|
||||
return response.Error(404, "Invite not found", nil)
|
||||
}
|
||||
|
||||
return JSON(200, dtos.InviteInfo{
|
||||
return response.JSON(200, dtos.InviteInfo{
|
||||
Email: invite.Email,
|
||||
Name: invite.Name,
|
||||
Username: invite.Email,
|
||||
@@ -162,19 +163,19 @@ func GetInviteInfoByCode(c *models.ReqContext) Response {
|
||||
})
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) CompleteInvite(c *models.ReqContext, completeInvite dtos.CompleteInviteForm) 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 errors.Is(err, models.ErrTempUserNotFound) {
|
||||
return Error(404, "Invite not found", nil)
|
||||
return response.Error(404, "Invite not found", nil)
|
||||
}
|
||||
return Error(500, "Failed to get invite", err)
|
||||
return response.Error(500, "Failed to get invite", err)
|
||||
}
|
||||
|
||||
invite := query.Result
|
||||
if invite.Status != models.TmpUserInvitePending {
|
||||
return Error(412, fmt.Sprintf("Invite cannot be used in status %s", invite.Status), nil)
|
||||
return response.Error(412, fmt.Sprintf("Invite cannot be used in status %s", invite.Status), nil)
|
||||
}
|
||||
|
||||
cmd := models.CreateUserCommand{
|
||||
@@ -187,10 +188,10 @@ func (hs *HTTPServer) CompleteInvite(c *models.ReqContext, completeInvite dtos.C
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
if errors.Is(err, models.ErrUserAlreadyExists) {
|
||||
return Error(412, fmt.Sprintf("User with email '%s' or username '%s' already exists", completeInvite.Email, completeInvite.Username), err)
|
||||
return response.Error(412, fmt.Sprintf("User with email '%s' or username '%s' already exists", completeInvite.Email, completeInvite.Username), err)
|
||||
}
|
||||
|
||||
return Error(500, "failed to create user", err)
|
||||
return response.Error(500, "failed to create user", err)
|
||||
}
|
||||
|
||||
user := &cmd.Result
|
||||
@@ -199,7 +200,7 @@ func (hs *HTTPServer) CompleteInvite(c *models.ReqContext, completeInvite dtos.C
|
||||
Name: user.NameOrFallback(),
|
||||
Email: user.Email,
|
||||
}); err != nil {
|
||||
return Error(500, "failed to publish event", err)
|
||||
return response.Error(500, "failed to publish event", err)
|
||||
}
|
||||
|
||||
if ok, rsp := applyUserInvite(user, invite, true); !ok {
|
||||
@@ -208,34 +209,34 @@ func (hs *HTTPServer) CompleteInvite(c *models.ReqContext, completeInvite dtos.C
|
||||
|
||||
err := hs.loginUserWithUser(user, c)
|
||||
if err != nil {
|
||||
return Error(500, "failed to accept invite", err)
|
||||
return response.Error(500, "failed to accept invite", err)
|
||||
}
|
||||
|
||||
metrics.MApiUserSignUpCompleted.Inc()
|
||||
metrics.MApiUserSignUpInvite.Inc()
|
||||
|
||||
return JSON(200, util.DynMap{
|
||||
return response.JSON(200, util.DynMap{
|
||||
"message": "User created and logged in",
|
||||
"id": user.Id,
|
||||
})
|
||||
}
|
||||
|
||||
func updateTempUserStatus(code string, status models.TempUserStatus) (bool, Response) {
|
||||
func updateTempUserStatus(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 {
|
||||
return false, Error(500, "Failed to update invite status", err)
|
||||
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) {
|
||||
func applyUserInvite(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 !errors.Is(err, models.ErrOrgUserAlreadyAdded) {
|
||||
return false, Error(500, "Error while trying to create org user", err)
|
||||
return false, response.Error(500, "Error while trying to create org user", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +248,7 @@ func applyUserInvite(user *models.User, invite *models.TempUserDTO, setActive bo
|
||||
if setActive {
|
||||
// set org to active
|
||||
if err := bus.Dispatch(&models.SetUsingOrgCommand{OrgId: invite.OrgId, UserId: user.Id}); err != nil {
|
||||
return false, Error(500, "Failed to set org as active", err)
|
||||
return false, response.Error(500, "Failed to set org as active", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user