2015-01-19 09:28:45 -06:00
|
|
|
package models
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Typed errors
|
|
|
|
var (
|
2018-12-01 11:28:10 -06:00
|
|
|
ErrUserNotFound = errors.New("User not found")
|
|
|
|
ErrLastGrafanaAdmin = errors.New("Cannot remove last grafana admin")
|
2015-01-19 11:01:04 -06:00
|
|
|
)
|
2015-01-19 09:28:45 -06:00
|
|
|
|
2016-12-09 08:25:02 -06:00
|
|
|
type Password string
|
|
|
|
|
|
|
|
func (p Password) IsWeak() bool {
|
|
|
|
return len(p) <= 4
|
|
|
|
}
|
|
|
|
|
2015-01-19 09:28:45 -06:00
|
|
|
type User struct {
|
2015-02-12 03:32:22 -06:00
|
|
|
Id int64
|
|
|
|
Version int
|
|
|
|
Email string
|
|
|
|
Name string
|
|
|
|
Login string
|
|
|
|
Password string
|
|
|
|
Salt string
|
|
|
|
Rands string
|
|
|
|
Company string
|
|
|
|
EmailVerified bool
|
|
|
|
Theme string
|
2016-11-09 03:41:39 -06:00
|
|
|
HelpFlags1 HelpFlags1
|
2019-05-21 06:52:49 -05:00
|
|
|
IsDisabled bool
|
2015-01-19 09:28:45 -06:00
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
IsAdmin bool
|
|
|
|
OrgId int64
|
2015-01-19 09:28:45 -06:00
|
|
|
|
2017-08-09 03:36:41 -05:00
|
|
|
Created time.Time
|
|
|
|
Updated time.Time
|
|
|
|
LastSeenAt time.Time
|
2015-01-19 09:28:45 -06:00
|
|
|
}
|
|
|
|
|
2015-06-08 03:57:01 -05:00
|
|
|
func (u *User) NameOrFallback() string {
|
|
|
|
if u.Name != "" {
|
|
|
|
return u.Name
|
|
|
|
} else if u.Login != "" {
|
|
|
|
return u.Login
|
|
|
|
} else {
|
|
|
|
return u.Email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 09:28:45 -06:00
|
|
|
// ---------------------
|
|
|
|
// COMMANDS
|
|
|
|
|
|
|
|
type CreateUserCommand struct {
|
2016-09-20 11:36:36 -05:00
|
|
|
Email string
|
|
|
|
Login string
|
|
|
|
Name string
|
|
|
|
Company string
|
|
|
|
OrgName string
|
|
|
|
Password string
|
|
|
|
EmailVerified bool
|
|
|
|
IsAdmin bool
|
2019-07-15 01:14:32 -05:00
|
|
|
IsDisabled bool
|
2016-09-20 11:36:36 -05:00
|
|
|
SkipOrgSetup bool
|
|
|
|
DefaultOrgRole string
|
2015-08-31 04:35:07 -05:00
|
|
|
|
|
|
|
Result User
|
2015-01-19 09:28:45 -06:00
|
|
|
}
|
2015-01-19 11:01:04 -06:00
|
|
|
|
|
|
|
type UpdateUserCommand struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Login string `json:"login"`
|
2015-02-28 07:30:08 -06:00
|
|
|
Theme string `json:"theme"`
|
2015-01-19 11:01:04 -06:00
|
|
|
|
|
|
|
UserId int64 `json:"-"`
|
|
|
|
}
|
|
|
|
|
2015-02-19 09:09:49 -06:00
|
|
|
type ChangeUserPasswordCommand struct {
|
|
|
|
OldPassword string `json:"oldPassword"`
|
|
|
|
NewPassword string `json:"newPassword"`
|
|
|
|
|
|
|
|
UserId int64 `json:"-"`
|
|
|
|
}
|
|
|
|
|
2015-02-26 08:43:48 -06:00
|
|
|
type UpdateUserPermissionsCommand struct {
|
|
|
|
IsGrafanaAdmin bool
|
|
|
|
UserId int64 `json:"-"`
|
|
|
|
}
|
|
|
|
|
2019-05-21 06:52:49 -05:00
|
|
|
type DisableUserCommand struct {
|
|
|
|
UserId int64
|
|
|
|
IsDisabled bool
|
|
|
|
}
|
|
|
|
|
2019-05-31 05:22:22 -05:00
|
|
|
type BatchDisableUsersCommand struct {
|
|
|
|
UserIds []int64
|
|
|
|
IsDisabled bool
|
|
|
|
}
|
|
|
|
|
2015-02-11 09:47:22 -06:00
|
|
|
type DeleteUserCommand struct {
|
|
|
|
UserId int64
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
type SetUsingOrgCommand struct {
|
|
|
|
UserId int64
|
|
|
|
OrgId int64
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------
|
|
|
|
// QUERIES
|
|
|
|
|
|
|
|
type GetUserByLoginQuery struct {
|
|
|
|
LoginOrEmail string
|
|
|
|
Result *User
|
|
|
|
}
|
|
|
|
|
2016-10-19 23:45:10 -05:00
|
|
|
type GetUserByEmailQuery struct {
|
|
|
|
Email string
|
|
|
|
Result *User
|
|
|
|
}
|
|
|
|
|
2015-02-10 08:36:51 -06:00
|
|
|
type GetUserByIdQuery struct {
|
|
|
|
Id int64
|
|
|
|
Result *User
|
|
|
|
}
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
type GetSignedInUserQuery struct {
|
|
|
|
UserId int64
|
2015-05-02 05:06:58 -05:00
|
|
|
Login string
|
|
|
|
Email string
|
2017-04-14 08:47:39 -05:00
|
|
|
OrgId int64
|
2015-01-19 11:01:04 -06:00
|
|
|
Result *SignedInUser
|
|
|
|
}
|
|
|
|
|
2015-02-28 07:30:08 -06:00
|
|
|
type GetUserProfileQuery struct {
|
2015-01-19 11:01:04 -06:00
|
|
|
UserId int64
|
2015-02-28 07:30:08 -06:00
|
|
|
Result UserProfileDTO
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type SearchUsersQuery struct {
|
2019-06-14 03:50:38 -05:00
|
|
|
OrgId int64
|
|
|
|
Query string
|
|
|
|
Page int
|
|
|
|
Limit int
|
|
|
|
AuthModule string
|
2015-01-19 11:01:04 -06:00
|
|
|
|
2019-07-16 22:24:56 -05:00
|
|
|
IsDisabled *bool
|
2019-07-15 01:14:32 -05:00
|
|
|
|
2017-02-08 07:20:07 -06:00
|
|
|
Result SearchUserQueryResult
|
|
|
|
}
|
|
|
|
|
|
|
|
type SearchUserQueryResult struct {
|
|
|
|
TotalCount int64 `json:"totalCount"`
|
|
|
|
Users []*UserSearchHitDTO `json:"users"`
|
|
|
|
Page int `json:"page"`
|
|
|
|
PerPage int `json:"perPage"`
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
type GetUserOrgListQuery struct {
|
|
|
|
UserId int64
|
|
|
|
Result []*UserOrgDTO
|
|
|
|
}
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
// ------------------------
|
|
|
|
// DTO & Projections
|
|
|
|
|
|
|
|
type SignedInUser struct {
|
|
|
|
UserId int64
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId int64
|
|
|
|
OrgName string
|
|
|
|
OrgRole RoleType
|
2015-01-19 11:01:04 -06:00
|
|
|
Login string
|
|
|
|
Name string
|
|
|
|
Email string
|
|
|
|
ApiKeyId int64
|
2017-08-16 08:03:49 -05:00
|
|
|
OrgCount int
|
2015-01-19 11:01:04 -06:00
|
|
|
IsGrafanaAdmin bool
|
2017-12-15 07:19:49 -06:00
|
|
|
IsAnonymous bool
|
2016-11-09 03:41:39 -06:00
|
|
|
HelpFlags1 HelpFlags1
|
2017-08-09 03:36:41 -05:00
|
|
|
LastSeenAt time.Time
|
2018-10-25 08:20:01 -05:00
|
|
|
Teams []int64
|
2017-08-09 03:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SignedInUser) ShouldUpdateLastSeenAt() bool {
|
|
|
|
return u.UserId > 0 && time.Since(u.LastSeenAt) > time.Minute*5
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:06:11 -06:00
|
|
|
func (u *SignedInUser) NameOrFallback() string {
|
|
|
|
if u.Name != "" {
|
|
|
|
return u.Name
|
|
|
|
} else if u.Login != "" {
|
|
|
|
return u.Login
|
|
|
|
} else {
|
|
|
|
return u.Email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 03:36:41 -05:00
|
|
|
type UpdateUserLastSeenAtCommand struct {
|
|
|
|
UserId int64
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2017-06-16 20:25:24 -05:00
|
|
|
func (user *SignedInUser) HasRole(role RoleType) bool {
|
|
|
|
if user.IsGrafanaAdmin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.OrgRole.Includes(role)
|
|
|
|
}
|
|
|
|
|
2019-08-08 03:27:47 -05:00
|
|
|
func (user *SignedInUser) IsRealUser() bool {
|
|
|
|
return user.UserId != 0
|
|
|
|
}
|
|
|
|
|
2015-02-28 07:30:08 -06:00
|
|
|
type UserProfileDTO struct {
|
2019-09-11 07:43:05 -05:00
|
|
|
Id int64 `json:"id"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Login string `json:"login"`
|
|
|
|
Theme string `json:"theme"`
|
|
|
|
OrgId int64 `json:"orgId"`
|
|
|
|
IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
|
|
|
|
IsDisabled bool `json:"isDisabled"`
|
|
|
|
IsExternal bool `json:"isExternal"`
|
|
|
|
AuthLabels []string `json:"authLabels"`
|
|
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
2019-09-28 06:12:33 -05:00
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
2020-01-13 10:10:19 -06:00
|
|
|
AvatarUrl string `json:"avatarUrl"`
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type UserSearchHitDTO struct {
|
2019-06-25 10:29:07 -05:00
|
|
|
Id int64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Login string `json:"login"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
AvatarUrl string `json:"avatarUrl"`
|
|
|
|
IsAdmin bool `json:"isAdmin"`
|
|
|
|
IsDisabled bool `json:"isDisabled"`
|
|
|
|
LastSeenAt time.Time `json:"lastSeenAt"`
|
|
|
|
LastSeenAtAge string `json:"lastSeenAtAge"`
|
2019-07-10 04:06:51 -05:00
|
|
|
AuthLabels []string `json:"authLabels"`
|
|
|
|
AuthModule AuthModuleConversion `json:"-"`
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
2015-11-16 08:55:02 -06:00
|
|
|
|
|
|
|
type UserIdDTO struct {
|
2015-11-16 09:28:38 -06:00
|
|
|
Id int64 `json:"id"`
|
|
|
|
Message string `json:"message"`
|
2015-11-16 08:55:02 -06:00
|
|
|
}
|
2019-06-25 10:29:07 -05:00
|
|
|
|
|
|
|
// 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 wanna write to database
|
|
|
|
func (auth *AuthModuleConversion) ToDB() ([]byte, error) {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|