feat(invite): more progress on completing invite form and actually creating a real user, #2353
This commit is contained in:
+2
-1
@@ -45,6 +45,7 @@ func Register(r *macaron.Macaron) {
|
||||
|
||||
// invited
|
||||
r.Get("/api/user/invite/:code", wrap(GetInviteInfoByCode))
|
||||
r.Post("/api/user/invite/complete", bind(dtos.CompleteInviteForm{}), wrap(CompleteInvite))
|
||||
|
||||
// reset password
|
||||
r.Get("/user/password/send-reset-email", Index)
|
||||
@@ -97,7 +98,7 @@ func Register(r *macaron.Macaron) {
|
||||
// invites
|
||||
r.Get("/invites", wrap(GetPendingOrgInvites))
|
||||
r.Post("/invites", bind(dtos.AddInviteForm{}), wrap(AddOrgInvite))
|
||||
r.Patch("/invites/:id/revoke", wrap(RevokeInvite))
|
||||
r.Patch("/invites/:code/revoke", wrap(RevokeInvite))
|
||||
}, regOrgAdmin)
|
||||
|
||||
// create new org
|
||||
|
||||
@@ -14,3 +14,12 @@ type InviteInfo struct {
|
||||
Name string `json:"name"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
type CompleteInviteForm struct {
|
||||
InviteCode string `json:"inviteCode"`
|
||||
Email string `json:"email" binding:"Required"`
|
||||
Name string `json:"name"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
ConfirmPassword string `json:"confirmPassword"`
|
||||
}
|
||||
|
||||
+49
-3
@@ -3,6 +3,8 @@ package api
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
"github.com/grafana/grafana/pkg/metrics"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
@@ -85,8 +87,7 @@ func AddOrgInvite(c *middleware.Context, inviteDto dtos.AddInviteForm) Response
|
||||
|
||||
func RevokeInvite(c *middleware.Context) Response {
|
||||
cmd := m.UpdateTempUserStatusCommand{
|
||||
Id: c.ParamsInt64(":id"),
|
||||
OrgId: c.OrgId,
|
||||
Code: c.Params(":code"),
|
||||
Status: m.TmpUserRevoked,
|
||||
}
|
||||
|
||||
@@ -98,7 +99,7 @@ func RevokeInvite(c *middleware.Context) Response {
|
||||
}
|
||||
|
||||
func GetInviteInfoByCode(c *middleware.Context) Response {
|
||||
query := m.GetTempUsersByCodeQuery{Code: c.Params(":code")}
|
||||
query := m.GetTempUserByCodeQuery{Code: c.Params(":code")}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
if err == m.ErrTempUserNotFound {
|
||||
@@ -115,3 +116,48 @@ func GetInviteInfoByCode(c *middleware.Context) Response {
|
||||
|
||||
return Json(200, &info)
|
||||
}
|
||||
|
||||
func CompleteInvite(c *middleware.Context, completeInvite dtos.CompleteInviteForm) Response {
|
||||
query := m.GetTempUserByCodeQuery{Code: completeInvite.InviteCode}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
if err == m.ErrTempUserNotFound {
|
||||
return ApiError(404, "Invite not found", nil)
|
||||
}
|
||||
return ApiError(500, "Failed to get invite", err)
|
||||
}
|
||||
|
||||
invite := query.Result
|
||||
|
||||
cmd := m.CreateUserCommand{
|
||||
Email: completeInvite.Email,
|
||||
Name: completeInvite.Name,
|
||||
Login: completeInvite.Username,
|
||||
Password: completeInvite.Password,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
return ApiError(500, "failed to create user", err)
|
||||
}
|
||||
|
||||
user := cmd.Result
|
||||
|
||||
bus.Publish(&events.UserSignedUp{
|
||||
Id: user.Id,
|
||||
Name: user.Name,
|
||||
Email: user.Email,
|
||||
Login: user.Login,
|
||||
})
|
||||
|
||||
// update temp user status
|
||||
updateTmpUserCmd := m.UpdateTempUserStatusCommand{Code: invite.Code, Status: m.TmpUserCompleted}
|
||||
if err := bus.Dispatch(&updateTmpUserCmd); err != nil {
|
||||
return ApiError(500, "Failed to update invite status", err)
|
||||
}
|
||||
|
||||
loginUserWithUser(&user, c)
|
||||
|
||||
metrics.M_Api_User_SignUp.Inc(1)
|
||||
|
||||
return ApiSuccess("User created and logged in")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user