Chore: Delete password and search from models package (#62482)

* Chore: Delete password and search from models package

* Rename model to AdminCreateUserResponse
This commit is contained in:
idafurjes
2023-01-31 11:04:55 +01:00
committed by GitHub
parent 60575041a5
commit cbc10f9c5d
9 changed files with 29 additions and 110 deletions

View File

@@ -12,7 +12,6 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/auth"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
@@ -83,9 +82,9 @@ func (hs *HTTPServer) AdminCreateUser(c *contextmodel.ReqContext) response.Respo
metrics.MApiAdminUserCreate.Inc()
result := models.UserIdDTO{
result := user.AdminCreateUserResponse{
Message: "User created",
Id: usr.ID,
ID: usr.ID,
}
return response.JSON(http.StatusOK, result)
@@ -506,7 +505,7 @@ type AdminUpdateUserPermissionsParams struct {
// swagger:response adminCreateUserResponse
type AdminCreateUserResponseResponse struct {
// in:body
Body models.UserIdDTO `json:"body"`
Body user.AdminCreateUserResponse `json:"body"`
}
// swagger:response adminGetUserAuthTokensResponse

View File

@@ -7,7 +7,6 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/services/notifications"
@@ -83,7 +82,7 @@ func (hs *HTTPServer) ResetPassword(c *contextmodel.ReqContext) response.Respons
return response.Error(400, "Passwords do not match", nil)
}
password := models.Password(form.NewPassword)
password := user.Password(form.NewPassword)
if password.IsWeak() {
return response.Error(400, "New password is too short", nil)
}

View File

@@ -9,7 +9,6 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/services/org"
@@ -72,7 +71,7 @@ func (hs *HTTPServer) getUserUserProfile(c *contextmodel.ReqContext, userID int6
}
userProfile.AccessControl = hs.getAccessControlMetadata(c, c.OrgID, "global.users:id:", strconv.FormatInt(userID, 10))
userProfile.AvatarUrl = dtos.GetGravatarUrl(userProfile.Email)
userProfile.AvatarURL = dtos.GetGravatarUrl(userProfile.Email)
return response.JSON(http.StatusOK, userProfile)
}
@@ -429,12 +428,12 @@ func (hs *HTTPServer) ChangeUserPassword(c *contextmodel.ReqContext) response.Re
userQuery := user.GetUserByIDQuery{ID: c.UserID}
user, err := hs.userService.GetByID(c.Req.Context(), &userQuery)
usr, err := hs.userService.GetByID(c.Req.Context(), &userQuery)
if err != nil {
return response.Error(500, "Could not read user from database", err)
}
getAuthQuery := login.GetAuthInfoQuery{UserId: user.ID}
getAuthQuery := login.GetAuthInfoQuery{UserId: usr.ID}
if err := hs.authInfoService.GetAuthInfo(c.Req.Context(), &getAuthQuery); err == nil {
authModule := getAuthQuery.Result.AuthModule
if authModule == login.LDAPAuthModule || authModule == login.AuthProxyAuthModule {
@@ -442,21 +441,21 @@ func (hs *HTTPServer) ChangeUserPassword(c *contextmodel.ReqContext) response.Re
}
}
passwordHashed, err := util.EncodePassword(cmd.OldPassword, user.Salt)
passwordHashed, err := util.EncodePassword(cmd.OldPassword, usr.Salt)
if err != nil {
return response.Error(500, "Failed to encode password", err)
}
if passwordHashed != user.Password {
if passwordHashed != usr.Password {
return response.Error(401, "Invalid old password", nil)
}
password := models.Password(cmd.NewPassword)
password := user.Password(cmd.NewPassword)
if password.IsWeak() {
return response.Error(400, "New password is too short", nil)
}
cmd.UserID = c.UserID
cmd.NewPassword, err = util.EncodePassword(cmd.NewPassword, user.Salt)
cmd.NewPassword, err = util.EncodePassword(cmd.NewPassword, usr.Salt)
if err != nil {
return response.Error(500, "Failed to encode password", err)
}

View File

@@ -113,7 +113,7 @@ func TestUserAPIEndpoint_userLoggedIn(t *testing.T) {
AuthLabels: []string{},
CreatedAt: fakeNow,
UpdatedAt: fakeNow,
AvatarUrl: avatarUrl,
AvatarURL: avatarUrl,
}
var resp user.UserProfileDTO
@@ -122,7 +122,7 @@ func TestUserAPIEndpoint_userLoggedIn(t *testing.T) {
require.NoError(t, err)
resp.CreatedAt = fakeNow
resp.UpdatedAt = fakeNow
resp.AvatarUrl = avatarUrl
resp.AvatarURL = avatarUrl
require.EqualValues(t, expected, resp)
}, mock)

View File

@@ -11,7 +11,6 @@ import (
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/runner"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/util"
)
@@ -46,7 +45,7 @@ func resetPasswordCommand(c utils.CommandLine, runner runner.Runner) error {
}
func resetPassword(adminId int64, newPassword string, userSvc user.Service) error {
password := models.Password(newPassword)
password := user.Password(newPassword)
if password.IsWeak() {
return fmt.Errorf("new password is too short")
}

View File

@@ -1,62 +0,0 @@
package models
import (
"strings"
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
)
type SortOption struct {
Name string
DisplayName string
Description string
Index int
MetaName string
Filter []SortOptionFilter
}
type SortOptionFilter interface {
searchstore.FilterOrderBy
}
type HitType string
const (
DashHitDB HitType = "dash-db"
DashHitHome HitType = "dash-home"
DashHitFolder HitType = "dash-folder"
)
type Hit struct {
ID int64 `json:"id"`
UID string `json:"uid"`
Title string `json:"title"`
URI string `json:"uri"`
URL string `json:"url"`
Slug string `json:"slug"`
Type HitType `json:"type"`
Tags []string `json:"tags"`
IsStarred bool `json:"isStarred"`
FolderID int64 `json:"folderId,omitempty"`
FolderUID string `json:"folderUid,omitempty"`
FolderTitle string `json:"folderTitle,omitempty"`
FolderURL string `json:"folderUrl,omitempty"`
SortMeta int64 `json:"sortMeta"`
SortMetaName string `json:"sortMetaName,omitempty"`
}
type HitList []*Hit
func (s HitList) Len() int { return len(s) }
func (s HitList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s HitList) Less(i, j int) bool {
if s[i].Type == "dash-folder" && s[j].Type == "dash-db" {
return true
}
if s[i].Type == "dash-db" && s[j].Type == "dash-folder" {
return false
}
return strings.ToLower(s[i].Title) < strings.ToLower(s[j].Title)
}

View File

@@ -1,26 +0,0 @@
package models
type Password string
func (p Password) IsWeak() bool {
return len(p) <= 4
}
type UserIdDTO struct {
Id int64 `json:"id"`
Message string `json:"message"`
}
// implement Conversion interface to define custom field mapping (xorm feature)
type AuthModuleConversion []string
func (auth *AuthModuleConversion) FromDB(data []byte) error {
auth_module := string(data)
*auth = []string{auth_module}
return nil
}
// Just a stub, we don't want to write to database
func (auth *AuthModuleConversion) ToDB() ([]byte, error) {
return []byte{}, nil
}

View File

@@ -152,7 +152,7 @@ type UserProfileDTO struct {
AuthLabels []string `json:"authLabels"`
UpdatedAt time.Time `json:"updatedAt"`
CreatedAt time.Time `json:"createdAt"`
AvatarUrl string `json:"avatarUrl"`
AvatarURL string `json:"avatarUrl"`
AccessControl map[string]bool `json:"accessControl,omitempty"`
}
@@ -241,7 +241,7 @@ type UserDisplayDTO struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Login string `json:"login,omitempty"`
AvatarUrl string `json:"avatarUrl"`
AvatarURL string `json:"avatarUrl"`
}
// ------------------------
@@ -362,3 +362,14 @@ const (
QuotaTargetSrv string = "user"
QuotaTarget string = "user"
)
type AdminCreateUserResponse struct {
ID int64 `json:"id"`
Message string `json:"message"`
}
type Password string
func (p Password) IsWeak() bool {
return len(p) <= 4
}

View File

@@ -16,13 +16,13 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboardimport"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/org/orgimpl"
"github.com/grafana/grafana/pkg/services/plugindashboards"
"github.com/grafana/grafana/pkg/services/quota/quotaimpl"
"github.com/grafana/grafana/pkg/services/search/model"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/services/user/userimpl"
@@ -171,7 +171,7 @@ providers:
})
b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
dashboardList := &models.HitList{}
dashboardList := &model.HitList{}
err = json.Unmarshal(b, dashboardList)
require.NoError(t, err)
assert.Equal(t, 1, dashboardList.Len())