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
+58
-57
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"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/models"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
@@ -11,23 +12,23 @@ import (
|
||||
)
|
||||
|
||||
// GET /api/user (current authenticated user)
|
||||
func GetSignedInUser(c *models.ReqContext) Response {
|
||||
func GetSignedInUser(c *models.ReqContext) response.Response {
|
||||
return getUserUserProfile(c.UserId)
|
||||
}
|
||||
|
||||
// GET /api/users/:id
|
||||
func GetUserByID(c *models.ReqContext) Response {
|
||||
func GetUserByID(c *models.ReqContext) response.Response {
|
||||
return getUserUserProfile(c.ParamsInt64(":id"))
|
||||
}
|
||||
|
||||
func getUserUserProfile(userID int64) Response {
|
||||
func getUserUserProfile(userID int64) response.Response {
|
||||
query := models.GetUserProfileQuery{UserId: userID}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
if errors.Is(err, models.ErrUserNotFound) {
|
||||
return Error(404, models.ErrUserNotFound.Error(), nil)
|
||||
return response.Error(404, models.ErrUserNotFound.Error(), nil)
|
||||
}
|
||||
return Error(500, "Failed to get user", err)
|
||||
return response.Error(500, "Failed to get user", err)
|
||||
}
|
||||
|
||||
getAuthQuery := models.GetAuthInfoQuery{UserId: userID}
|
||||
@@ -40,17 +41,17 @@ func getUserUserProfile(userID int64) Response {
|
||||
|
||||
query.Result.AvatarUrl = dtos.GetGravatarUrl(query.Result.Email)
|
||||
|
||||
return JSON(200, query.Result)
|
||||
return response.JSON(200, query.Result)
|
||||
}
|
||||
|
||||
// GET /api/users/lookup
|
||||
func GetUserByLoginOrEmail(c *models.ReqContext) Response {
|
||||
func GetUserByLoginOrEmail(c *models.ReqContext) response.Response {
|
||||
query := models.GetUserByLoginQuery{LoginOrEmail: c.Query("loginOrEmail")}
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
if errors.Is(err, models.ErrUserNotFound) {
|
||||
return Error(404, models.ErrUserNotFound.Error(), nil)
|
||||
return response.Error(404, models.ErrUserNotFound.Error(), nil)
|
||||
}
|
||||
return Error(500, "Failed to get user", err)
|
||||
return response.Error(500, "Failed to get user", err)
|
||||
}
|
||||
user := query.Result
|
||||
result := models.UserProfileDTO{
|
||||
@@ -64,17 +65,17 @@ func GetUserByLoginOrEmail(c *models.ReqContext) Response {
|
||||
UpdatedAt: user.Updated,
|
||||
CreatedAt: user.Created,
|
||||
}
|
||||
return JSON(200, &result)
|
||||
return response.JSON(200, &result)
|
||||
}
|
||||
|
||||
// POST /api/user
|
||||
func UpdateSignedInUser(c *models.ReqContext, cmd models.UpdateUserCommand) Response {
|
||||
func UpdateSignedInUser(c *models.ReqContext, cmd models.UpdateUserCommand) response.Response {
|
||||
if setting.AuthProxyEnabled {
|
||||
if setting.AuthProxyHeaderProperty == "email" && cmd.Email != c.Email {
|
||||
return Error(400, "Not allowed to change email when auth proxy is using email property", nil)
|
||||
return response.Error(400, "Not allowed to change email when auth proxy is using email property", nil)
|
||||
}
|
||||
if setting.AuthProxyHeaderProperty == "username" && cmd.Login != c.Login {
|
||||
return Error(400, "Not allowed to change username when auth proxy is using username property", nil)
|
||||
return response.Error(400, "Not allowed to change username when auth proxy is using username property", nil)
|
||||
}
|
||||
}
|
||||
cmd.UserId = c.UserId
|
||||
@@ -82,85 +83,85 @@ func UpdateSignedInUser(c *models.ReqContext, cmd models.UpdateUserCommand) Resp
|
||||
}
|
||||
|
||||
// POST /api/users/:id
|
||||
func UpdateUser(c *models.ReqContext, cmd models.UpdateUserCommand) Response {
|
||||
func UpdateUser(c *models.ReqContext, cmd models.UpdateUserCommand) response.Response {
|
||||
cmd.UserId = c.ParamsInt64(":id")
|
||||
return handleUpdateUser(cmd)
|
||||
}
|
||||
|
||||
// POST /api/users/:id/using/:orgId
|
||||
func UpdateUserActiveOrg(c *models.ReqContext) Response {
|
||||
func UpdateUserActiveOrg(c *models.ReqContext) response.Response {
|
||||
userID := c.ParamsInt64(":id")
|
||||
orgID := c.ParamsInt64(":orgId")
|
||||
|
||||
if !validateUsingOrg(userID, orgID) {
|
||||
return Error(401, "Not a valid organization", nil)
|
||||
return response.Error(401, "Not a valid organization", nil)
|
||||
}
|
||||
|
||||
cmd := models.SetUsingOrgCommand{UserId: userID, OrgId: orgID}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
return Error(500, "Failed to change active organization", err)
|
||||
return response.Error(500, "Failed to change active organization", err)
|
||||
}
|
||||
|
||||
return Success("Active organization changed")
|
||||
return response.Success("Active organization changed")
|
||||
}
|
||||
|
||||
func handleUpdateUser(cmd models.UpdateUserCommand) Response {
|
||||
func handleUpdateUser(cmd models.UpdateUserCommand) response.Response {
|
||||
if len(cmd.Login) == 0 {
|
||||
cmd.Login = cmd.Email
|
||||
if len(cmd.Login) == 0 {
|
||||
return Error(400, "Validation error, need to specify either username or email", nil)
|
||||
return response.Error(400, "Validation error, need to specify either username or email", nil)
|
||||
}
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
return Error(500, "Failed to update user", err)
|
||||
return response.Error(500, "Failed to update user", err)
|
||||
}
|
||||
|
||||
return Success("User updated")
|
||||
return response.Success("User updated")
|
||||
}
|
||||
|
||||
// GET /api/user/orgs
|
||||
func GetSignedInUserOrgList(c *models.ReqContext) Response {
|
||||
func GetSignedInUserOrgList(c *models.ReqContext) response.Response {
|
||||
return getUserOrgList(c.UserId)
|
||||
}
|
||||
|
||||
// GET /api/user/teams
|
||||
func GetSignedInUserTeamList(c *models.ReqContext) Response {
|
||||
func GetSignedInUserTeamList(c *models.ReqContext) response.Response {
|
||||
return getUserTeamList(c.OrgId, c.UserId)
|
||||
}
|
||||
|
||||
// GET /api/users/:id/teams
|
||||
func GetUserTeams(c *models.ReqContext) Response {
|
||||
func GetUserTeams(c *models.ReqContext) response.Response {
|
||||
return getUserTeamList(c.OrgId, c.ParamsInt64(":id"))
|
||||
}
|
||||
|
||||
func getUserTeamList(orgID int64, userID int64) Response {
|
||||
func getUserTeamList(orgID int64, userID int64) response.Response {
|
||||
query := models.GetTeamsByUserQuery{OrgId: orgID, UserId: userID}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return Error(500, "Failed to get user teams", err)
|
||||
return response.Error(500, "Failed to get user teams", err)
|
||||
}
|
||||
|
||||
for _, team := range query.Result {
|
||||
team.AvatarUrl = dtos.GetGravatarUrlWithDefault(team.Email, team.Name)
|
||||
}
|
||||
return JSON(200, query.Result)
|
||||
return response.JSON(200, query.Result)
|
||||
}
|
||||
|
||||
// GET /api/users/:id/orgs
|
||||
func GetUserOrgList(c *models.ReqContext) Response {
|
||||
func GetUserOrgList(c *models.ReqContext) response.Response {
|
||||
return getUserOrgList(c.ParamsInt64(":id"))
|
||||
}
|
||||
|
||||
func getUserOrgList(userID int64) Response {
|
||||
func getUserOrgList(userID int64) response.Response {
|
||||
query := models.GetUserOrgListQuery{UserId: userID}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return Error(500, "Failed to get user organizations", err)
|
||||
return response.Error(500, "Failed to get user organizations", err)
|
||||
}
|
||||
|
||||
return JSON(200, query.Result)
|
||||
return response.JSON(200, query.Result)
|
||||
}
|
||||
|
||||
func validateUsingOrg(userID int64, orgID int64) bool {
|
||||
@@ -182,20 +183,20 @@ func validateUsingOrg(userID int64, orgID int64) bool {
|
||||
}
|
||||
|
||||
// POST /api/user/using/:id
|
||||
func UserSetUsingOrg(c *models.ReqContext) Response {
|
||||
func UserSetUsingOrg(c *models.ReqContext) response.Response {
|
||||
orgID := c.ParamsInt64(":id")
|
||||
|
||||
if !validateUsingOrg(c.UserId, orgID) {
|
||||
return Error(401, "Not a valid organization", nil)
|
||||
return response.Error(401, "Not a valid organization", nil)
|
||||
}
|
||||
|
||||
cmd := models.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgID}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
return Error(500, "Failed to change active organization", err)
|
||||
return response.Error(500, "Failed to change active organization", err)
|
||||
}
|
||||
|
||||
return Success("Active organization changed")
|
||||
return response.Success("Active organization changed")
|
||||
}
|
||||
|
||||
// GET /profile/switch-org/:id
|
||||
@@ -215,41 +216,41 @@ func (hs *HTTPServer) ChangeActiveOrgAndRedirectToHome(c *models.ReqContext) {
|
||||
c.Redirect(setting.AppSubUrl + "/")
|
||||
}
|
||||
|
||||
func ChangeUserPassword(c *models.ReqContext, cmd models.ChangeUserPasswordCommand) Response {
|
||||
func ChangeUserPassword(c *models.ReqContext, cmd models.ChangeUserPasswordCommand) response.Response {
|
||||
if setting.LDAPEnabled || setting.AuthProxyEnabled {
|
||||
return Error(400, "Not allowed to change password when LDAP or Auth Proxy is enabled", nil)
|
||||
return response.Error(400, "Not allowed to change password when LDAP or Auth Proxy is enabled", nil)
|
||||
}
|
||||
|
||||
userQuery := models.GetUserByIdQuery{Id: c.UserId}
|
||||
|
||||
if err := bus.Dispatch(&userQuery); err != nil {
|
||||
return Error(500, "Could not read user from database", err)
|
||||
return response.Error(500, "Could not read user from database", err)
|
||||
}
|
||||
|
||||
passwordHashed, err := util.EncodePassword(cmd.OldPassword, userQuery.Result.Salt)
|
||||
if err != nil {
|
||||
return Error(500, "Failed to encode password", err)
|
||||
return response.Error(500, "Failed to encode password", err)
|
||||
}
|
||||
if passwordHashed != userQuery.Result.Password {
|
||||
return Error(401, "Invalid old password", nil)
|
||||
return response.Error(401, "Invalid old password", nil)
|
||||
}
|
||||
|
||||
password := models.Password(cmd.NewPassword)
|
||||
if password.IsWeak() {
|
||||
return Error(400, "New password is too short", nil)
|
||||
return response.Error(400, "New password is too short", nil)
|
||||
}
|
||||
|
||||
cmd.UserId = c.UserId
|
||||
cmd.NewPassword, err = util.EncodePassword(cmd.NewPassword, userQuery.Result.Salt)
|
||||
if err != nil {
|
||||
return Error(500, "Failed to encode password", err)
|
||||
return response.Error(500, "Failed to encode password", err)
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
return Error(500, "Failed to change user password", err)
|
||||
return response.Error(500, "Failed to change user password", err)
|
||||
}
|
||||
|
||||
return Success("User password changed")
|
||||
return response.Success("User password changed")
|
||||
}
|
||||
|
||||
// redirectToChangePassword handles GET /.well-known/change-password.
|
||||
@@ -258,23 +259,23 @@ func redirectToChangePassword(c *models.ReqContext) {
|
||||
}
|
||||
|
||||
// GET /api/users
|
||||
func SearchUsers(c *models.ReqContext) Response {
|
||||
func SearchUsers(c *models.ReqContext) response.Response {
|
||||
query, err := searchUser(c)
|
||||
if err != nil {
|
||||
return Error(500, "Failed to fetch users", err)
|
||||
return response.Error(500, "Failed to fetch users", err)
|
||||
}
|
||||
|
||||
return JSON(200, query.Result.Users)
|
||||
return response.JSON(200, query.Result.Users)
|
||||
}
|
||||
|
||||
// GET /api/users/search
|
||||
func SearchUsersWithPaging(c *models.ReqContext) Response {
|
||||
func SearchUsersWithPaging(c *models.ReqContext) response.Response {
|
||||
query, err := searchUser(c)
|
||||
if err != nil {
|
||||
return Error(500, "Failed to fetch users", err)
|
||||
return response.Error(500, "Failed to fetch users", err)
|
||||
}
|
||||
|
||||
return JSON(200, query.Result)
|
||||
return response.JSON(200, query.Result)
|
||||
}
|
||||
|
||||
func searchUser(c *models.ReqContext) (*models.SearchUsersQuery, error) {
|
||||
@@ -311,7 +312,7 @@ func searchUser(c *models.ReqContext) (*models.SearchUsersQuery, error) {
|
||||
return query, nil
|
||||
}
|
||||
|
||||
func SetHelpFlag(c *models.ReqContext) Response {
|
||||
func SetHelpFlag(c *models.ReqContext) response.Response {
|
||||
flag := c.ParamsInt64(":id")
|
||||
|
||||
bitmask := &c.HelpFlags1
|
||||
@@ -323,23 +324,23 @@ func SetHelpFlag(c *models.ReqContext) Response {
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
return Error(500, "Failed to update help flag", err)
|
||||
return response.Error(500, "Failed to update help flag", err)
|
||||
}
|
||||
|
||||
return JSON(200, &util.DynMap{"message": "Help flag set", "helpFlags1": cmd.HelpFlags1})
|
||||
return response.JSON(200, &util.DynMap{"message": "Help flag set", "helpFlags1": cmd.HelpFlags1})
|
||||
}
|
||||
|
||||
func ClearHelpFlags(c *models.ReqContext) Response {
|
||||
func ClearHelpFlags(c *models.ReqContext) response.Response {
|
||||
cmd := models.SetUserHelpFlagCommand{
|
||||
UserId: c.UserId,
|
||||
HelpFlags1: models.HelpFlags1(0),
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
return Error(500, "Failed to update help flag", err)
|
||||
return response.Error(500, "Failed to update help flag", err)
|
||||
}
|
||||
|
||||
return JSON(200, &util.DynMap{"message": "Help flag set", "helpFlags1": cmd.HelpFlags1})
|
||||
return response.JSON(200, &util.DynMap{"message": "Help flag set", "helpFlags1": cmd.HelpFlags1})
|
||||
}
|
||||
|
||||
func GetAuthProviderLabel(authModule string) string {
|
||||
|
||||
Reference in New Issue
Block a user