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