Chore: Avoid aliasing importing models in api package (#22492)

This commit is contained in:
Carl Bergquist
2020-03-04 12:57:20 +01:00
committed by GitHub
parent fcaff011b1
commit 3fdd2648b1
53 changed files with 910 additions and 912 deletions
+41 -41
View File
@@ -3,32 +3,32 @@ package api
import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
)
// GET /api/user (current authenticated user)
func GetSignedInUser(c *m.ReqContext) Response {
func GetSignedInUser(c *models.ReqContext) Response {
return getUserUserProfile(c.UserId)
}
// GET /api/users/:id
func GetUserByID(c *m.ReqContext) Response {
func GetUserByID(c *models.ReqContext) Response {
return getUserUserProfile(c.ParamsInt64(":id"))
}
func getUserUserProfile(userID int64) Response {
query := m.GetUserProfileQuery{UserId: userID}
query := models.GetUserProfileQuery{UserId: userID}
if err := bus.Dispatch(&query); err != nil {
if err == m.ErrUserNotFound {
return Error(404, m.ErrUserNotFound.Error(), nil)
if err == models.ErrUserNotFound {
return Error(404, models.ErrUserNotFound.Error(), nil)
}
return Error(500, "Failed to get user", err)
}
getAuthQuery := m.GetAuthInfoQuery{UserId: userID}
getAuthQuery := models.GetAuthInfoQuery{UserId: userID}
query.Result.AuthLabels = []string{}
if err := bus.Dispatch(&getAuthQuery); err == nil {
authLabel := GetAuthProviderLabel(getAuthQuery.Result.AuthModule)
@@ -42,16 +42,16 @@ func getUserUserProfile(userID int64) Response {
}
// GET /api/users/lookup
func GetUserByLoginOrEmail(c *m.ReqContext) Response {
query := m.GetUserByLoginQuery{LoginOrEmail: c.Query("loginOrEmail")}
func GetUserByLoginOrEmail(c *models.ReqContext) Response {
query := models.GetUserByLoginQuery{LoginOrEmail: c.Query("loginOrEmail")}
if err := bus.Dispatch(&query); err != nil {
if err == m.ErrUserNotFound {
return Error(404, m.ErrUserNotFound.Error(), nil)
if err == models.ErrUserNotFound {
return Error(404, models.ErrUserNotFound.Error(), nil)
}
return Error(500, "Failed to get user", err)
}
user := query.Result
result := m.UserProfileDTO{
result := models.UserProfileDTO{
Id: user.Id,
Name: user.Name,
Email: user.Email,
@@ -66,7 +66,7 @@ func GetUserByLoginOrEmail(c *m.ReqContext) Response {
}
// POST /api/user
func UpdateSignedInUser(c *m.ReqContext, cmd m.UpdateUserCommand) Response {
func UpdateSignedInUser(c *models.ReqContext, cmd models.UpdateUserCommand) 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)
@@ -80,13 +80,13 @@ func UpdateSignedInUser(c *m.ReqContext, cmd m.UpdateUserCommand) Response {
}
// POST /api/users/:id
func UpdateUser(c *m.ReqContext, cmd m.UpdateUserCommand) Response {
func UpdateUser(c *models.ReqContext, cmd models.UpdateUserCommand) Response {
cmd.UserId = c.ParamsInt64(":id")
return handleUpdateUser(cmd)
}
//POST /api/users/:id/using/:orgId
func UpdateUserActiveOrg(c *m.ReqContext) Response {
func UpdateUserActiveOrg(c *models.ReqContext) Response {
userID := c.ParamsInt64(":id")
orgID := c.ParamsInt64(":orgId")
@@ -94,7 +94,7 @@ func UpdateUserActiveOrg(c *m.ReqContext) Response {
return Error(401, "Not a valid organization", nil)
}
cmd := m.SetUsingOrgCommand{UserId: userID, OrgId: orgID}
cmd := models.SetUsingOrgCommand{UserId: userID, OrgId: orgID}
if err := bus.Dispatch(&cmd); err != nil {
return Error(500, "Failed to change active organization", err)
@@ -103,7 +103,7 @@ func UpdateUserActiveOrg(c *m.ReqContext) Response {
return Success("Active organization changed")
}
func handleUpdateUser(cmd m.UpdateUserCommand) Response {
func handleUpdateUser(cmd models.UpdateUserCommand) Response {
if len(cmd.Login) == 0 {
cmd.Login = cmd.Email
if len(cmd.Login) == 0 {
@@ -119,22 +119,22 @@ func handleUpdateUser(cmd m.UpdateUserCommand) Response {
}
// GET /api/user/orgs
func GetSignedInUserOrgList(c *m.ReqContext) Response {
func GetSignedInUserOrgList(c *models.ReqContext) Response {
return getUserOrgList(c.UserId)
}
// GET /api/user/teams
func GetSignedInUserTeamList(c *m.ReqContext) Response {
func GetSignedInUserTeamList(c *models.ReqContext) Response {
return getUserTeamList(c.OrgId, c.UserId)
}
// GET /api/users/:id/teams
func GetUserTeams(c *m.ReqContext) Response {
func GetUserTeams(c *models.ReqContext) Response {
return getUserTeamList(c.OrgId, c.ParamsInt64(":id"))
}
func getUserTeamList(orgID int64, userID int64) Response {
query := m.GetTeamsByUserQuery{OrgId: orgID, UserId: userID}
query := models.GetTeamsByUserQuery{OrgId: orgID, UserId: userID}
if err := bus.Dispatch(&query); err != nil {
return Error(500, "Failed to get user teams", err)
@@ -147,12 +147,12 @@ func getUserTeamList(orgID int64, userID int64) Response {
}
// GET /api/users/:id/orgs
func GetUserOrgList(c *m.ReqContext) Response {
func GetUserOrgList(c *models.ReqContext) Response {
return getUserOrgList(c.ParamsInt64(":id"))
}
func getUserOrgList(userID int64) Response {
query := m.GetUserOrgListQuery{UserId: userID}
query := models.GetUserOrgListQuery{UserId: userID}
if err := bus.Dispatch(&query); err != nil {
return Error(500, "Failed to get user organizations", err)
@@ -162,7 +162,7 @@ func getUserOrgList(userID int64) Response {
}
func validateUsingOrg(userID int64, orgID int64) bool {
query := m.GetUserOrgListQuery{UserId: userID}
query := models.GetUserOrgListQuery{UserId: userID}
if err := bus.Dispatch(&query); err != nil {
return false
@@ -180,14 +180,14 @@ func validateUsingOrg(userID int64, orgID int64) bool {
}
// POST /api/user/using/:id
func UserSetUsingOrg(c *m.ReqContext) Response {
func UserSetUsingOrg(c *models.ReqContext) Response {
orgID := c.ParamsInt64(":id")
if !validateUsingOrg(c.UserId, orgID) {
return Error(401, "Not a valid organization", nil)
}
cmd := m.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgID}
cmd := models.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgID}
if err := bus.Dispatch(&cmd); err != nil {
return Error(500, "Failed to change active organization", err)
@@ -197,14 +197,14 @@ func UserSetUsingOrg(c *m.ReqContext) Response {
}
// GET /profile/switch-org/:id
func (hs *HTTPServer) ChangeActiveOrgAndRedirectToHome(c *m.ReqContext) {
func (hs *HTTPServer) ChangeActiveOrgAndRedirectToHome(c *models.ReqContext) {
orgID := c.ParamsInt64(":id")
if !validateUsingOrg(c.UserId, orgID) {
hs.NotFoundHandler(c)
}
cmd := m.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgID}
cmd := models.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgID}
if err := bus.Dispatch(&cmd); err != nil {
hs.NotFoundHandler(c)
@@ -213,12 +213,12 @@ func (hs *HTTPServer) ChangeActiveOrgAndRedirectToHome(c *m.ReqContext) {
c.Redirect(setting.AppSubUrl + "/")
}
func ChangeUserPassword(c *m.ReqContext, cmd m.ChangeUserPasswordCommand) Response {
func ChangeUserPassword(c *models.ReqContext, cmd models.ChangeUserPasswordCommand) Response {
if setting.LDAPEnabled || setting.AuthProxyEnabled {
return Error(400, "Not allowed to change password when LDAP or Auth Proxy is enabled", nil)
}
userQuery := m.GetUserByIdQuery{Id: c.UserId}
userQuery := models.GetUserByIdQuery{Id: c.UserId}
if err := bus.Dispatch(&userQuery); err != nil {
return Error(500, "Could not read user from database", err)
@@ -232,7 +232,7 @@ func ChangeUserPassword(c *m.ReqContext, cmd m.ChangeUserPasswordCommand) Respon
return Error(401, "Invalid old password", nil)
}
password := m.Password(cmd.NewPassword)
password := models.Password(cmd.NewPassword)
if password.IsWeak() {
return Error(400, "New password is too short", nil)
}
@@ -251,7 +251,7 @@ func ChangeUserPassword(c *m.ReqContext, cmd m.ChangeUserPasswordCommand) Respon
}
// GET /api/users
func SearchUsers(c *m.ReqContext) Response {
func SearchUsers(c *models.ReqContext) Response {
query, err := searchUser(c)
if err != nil {
return Error(500, "Failed to fetch users", err)
@@ -261,7 +261,7 @@ func SearchUsers(c *m.ReqContext) Response {
}
// GET /api/users/search
func SearchUsersWithPaging(c *m.ReqContext) Response {
func SearchUsersWithPaging(c *models.ReqContext) Response {
query, err := searchUser(c)
if err != nil {
return Error(500, "Failed to fetch users", err)
@@ -270,7 +270,7 @@ func SearchUsersWithPaging(c *m.ReqContext) Response {
return JSON(200, query.Result)
}
func searchUser(c *m.ReqContext) (*m.SearchUsersQuery, error) {
func searchUser(c *models.ReqContext) (*models.SearchUsersQuery, error) {
perPage := c.QueryInt("perpage")
if perPage <= 0 {
perPage = 1000
@@ -283,7 +283,7 @@ func searchUser(c *m.ReqContext) (*m.SearchUsersQuery, error) {
searchQuery := c.Query("query")
query := &m.SearchUsersQuery{Query: searchQuery, Page: page, Limit: perPage}
query := &models.SearchUsersQuery{Query: searchQuery, Page: page, Limit: perPage}
if err := bus.Dispatch(query); err != nil {
return nil, err
}
@@ -304,13 +304,13 @@ func searchUser(c *m.ReqContext) (*m.SearchUsersQuery, error) {
return query, nil
}
func SetHelpFlag(c *m.ReqContext) Response {
func SetHelpFlag(c *models.ReqContext) Response {
flag := c.ParamsInt64(":id")
bitmask := &c.HelpFlags1
bitmask.AddFlag(m.HelpFlags1(flag))
bitmask.AddFlag(models.HelpFlags1(flag))
cmd := m.SetUserHelpFlagCommand{
cmd := models.SetUserHelpFlagCommand{
UserId: c.UserId,
HelpFlags1: *bitmask,
}
@@ -322,10 +322,10 @@ func SetHelpFlag(c *m.ReqContext) Response {
return JSON(200, &util.DynMap{"message": "Help flag set", "helpFlags1": cmd.HelpFlags1})
}
func ClearHelpFlags(c *m.ReqContext) Response {
cmd := m.SetUserHelpFlagCommand{
func ClearHelpFlags(c *models.ReqContext) Response {
cmd := models.SetUserHelpFlagCommand{
UserId: c.UserId,
HelpFlags1: m.HelpFlags1(0),
HelpFlags1: models.HelpFlags1(0),
}
if err := bus.Dispatch(&cmd); err != nil {