2015-01-19 09:28:45 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2018-06-07 11:17:01 -05:00
|
|
|
"context"
|
2019-04-23 03:24:47 -05:00
|
|
|
"fmt"
|
2020-04-06 06:27:11 -05:00
|
|
|
"sort"
|
2017-04-14 08:47:39 -05:00
|
|
|
"strconv"
|
2015-01-19 11:01:04 -06:00
|
|
|
"strings"
|
2015-01-19 09:28:45 -06:00
|
|
|
"time"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/events"
|
2019-06-14 03:50:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2015-01-19 09:28:45 -06:00
|
|
|
)
|
|
|
|
|
2020-11-10 23:21:08 -06:00
|
|
|
func (ss *SQLStore) addUserQueryAndCommandHandlers() {
|
2021-05-18 11:24:42 -05:00
|
|
|
ss.Bus.AddHandlerCtx(ss.GetSignedInUserWithCacheCtx)
|
2018-10-31 08:47:14 -05:00
|
|
|
|
2021-06-21 00:51:33 -05:00
|
|
|
bus.AddHandlerCtx("sql", GetUserById)
|
2015-01-19 11:57:51 -06:00
|
|
|
bus.AddHandler("sql", UpdateUser)
|
2015-02-19 09:09:49 -06:00
|
|
|
bus.AddHandler("sql", ChangeUserPassword)
|
2015-01-19 11:01:04 -06:00
|
|
|
bus.AddHandler("sql", GetUserByLogin)
|
2016-10-19 23:45:10 -05:00
|
|
|
bus.AddHandler("sql", GetUserByEmail)
|
2015-02-23 13:07:49 -06:00
|
|
|
bus.AddHandler("sql", SetUsingOrg)
|
2017-08-09 03:36:41 -05:00
|
|
|
bus.AddHandler("sql", UpdateUserLastSeenAt)
|
2015-02-28 07:30:08 -06:00
|
|
|
bus.AddHandler("sql", GetUserProfile)
|
2015-01-19 11:01:04 -06:00
|
|
|
bus.AddHandler("sql", SearchUsers)
|
2015-02-23 13:07:49 -06:00
|
|
|
bus.AddHandler("sql", GetUserOrgList)
|
2019-05-21 06:52:49 -05:00
|
|
|
bus.AddHandler("sql", DisableUser)
|
2019-05-31 05:22:22 -05:00
|
|
|
bus.AddHandler("sql", BatchDisableUsers)
|
2015-02-11 09:47:22 -06:00
|
|
|
bus.AddHandler("sql", DeleteUser)
|
2016-11-09 03:41:39 -06:00
|
|
|
bus.AddHandler("sql", SetUserHelpFlag)
|
2015-01-19 09:28:45 -06:00
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
func getOrgIdForNewUser(sess *DBSession, cmd models.CreateUserCommand) (int64, error) {
|
2015-09-01 05:35:06 -05:00
|
|
|
if cmd.SkipOrgSetup {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
2020-04-15 04:11:45 -05:00
|
|
|
if setting.AutoAssignOrg && cmd.OrgId != 0 {
|
|
|
|
err := verifyExistingOrg(sess, cmd.OrgId)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return cmd.OrgId, nil
|
|
|
|
}
|
|
|
|
|
2019-11-08 04:11:03 -06:00
|
|
|
orgName := cmd.OrgName
|
|
|
|
if len(orgName) == 0 {
|
|
|
|
orgName = util.StringsFallback2(cmd.Email, cmd.Login)
|
2015-01-27 08:14:53 -06:00
|
|
|
}
|
|
|
|
|
2019-11-08 04:11:03 -06:00
|
|
|
return getOrCreateOrg(sess, orgName)
|
2015-01-27 08:14:53 -06:00
|
|
|
}
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
type userCreationArgs struct {
|
|
|
|
Login string
|
|
|
|
Email string
|
|
|
|
Name string
|
|
|
|
Company string
|
|
|
|
Password string
|
|
|
|
IsAdmin bool
|
|
|
|
IsDisabled bool
|
|
|
|
EmailVerified bool
|
|
|
|
OrgID int64
|
|
|
|
OrgName string
|
|
|
|
DefaultOrgRole string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *SQLStore) getOrgIDForNewUser(sess *DBSession, args userCreationArgs) (int64, error) {
|
|
|
|
if ss.Cfg.AutoAssignOrg && args.OrgID != 0 {
|
|
|
|
if err := verifyExistingOrg(sess, args.OrgID); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return args.OrgID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
orgName := args.OrgName
|
|
|
|
if orgName == "" {
|
|
|
|
orgName = util.StringsFallback2(args.Email, args.Login)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss.getOrCreateOrg(sess, orgName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// createUser creates a user in the database.
|
2021-03-18 08:27:59 -05:00
|
|
|
func (ss *SQLStore) createUser(ctx context.Context, sess *DBSession, args userCreationArgs, skipOrgSetup bool) (models.User, error) {
|
2021-01-07 04:36:13 -06:00
|
|
|
var user models.User
|
2021-03-18 08:27:59 -05:00
|
|
|
var orgID int64 = -1
|
|
|
|
if !skipOrgSetup {
|
|
|
|
var err error
|
|
|
|
orgID, err = ss.getOrgIDForNewUser(sess, args)
|
2021-01-07 04:36:13 -06:00
|
|
|
if err != nil {
|
2021-03-18 08:27:59 -05:00
|
|
|
return user, err
|
2021-01-07 04:36:13 -06:00
|
|
|
}
|
2021-03-18 08:27:59 -05:00
|
|
|
}
|
2021-01-07 04:36:13 -06:00
|
|
|
|
2021-03-18 08:27:59 -05:00
|
|
|
if args.Email == "" {
|
|
|
|
args.Email = args.Login
|
|
|
|
}
|
2021-01-07 04:36:13 -06:00
|
|
|
|
2021-03-18 08:27:59 -05:00
|
|
|
exists, err := sess.Where("email=? OR login=?", args.Email, args.Login).Get(&models.User{})
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return user, models.ErrUserAlreadyExists
|
|
|
|
}
|
2021-01-07 04:36:13 -06:00
|
|
|
|
2021-03-18 08:27:59 -05:00
|
|
|
// create user
|
|
|
|
user = models.User{
|
|
|
|
Email: args.Email,
|
|
|
|
Name: args.Name,
|
|
|
|
Login: args.Login,
|
|
|
|
Company: args.Company,
|
|
|
|
IsAdmin: args.IsAdmin,
|
|
|
|
IsDisabled: args.IsDisabled,
|
|
|
|
OrgId: orgID,
|
|
|
|
EmailVerified: args.EmailVerified,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
LastSeenAt: time.Now().AddDate(-10, 0, 0),
|
|
|
|
}
|
2021-01-07 04:36:13 -06:00
|
|
|
|
2021-03-18 08:27:59 -05:00
|
|
|
salt, err := util.GetRandomString(10)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
user.Salt = salt
|
|
|
|
rands, err := util.GetRandomString(10)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
user.Rands = rands
|
2021-01-07 04:36:13 -06:00
|
|
|
|
2021-03-18 08:27:59 -05:00
|
|
|
if len(args.Password) > 0 {
|
|
|
|
encodedPassword, err := util.EncodePassword(args.Password, user.Salt)
|
|
|
|
if err != nil {
|
|
|
|
return user, err
|
2021-01-07 04:36:13 -06:00
|
|
|
}
|
2021-03-18 08:27:59 -05:00
|
|
|
user.Password = encodedPassword
|
|
|
|
}
|
2021-01-07 04:36:13 -06:00
|
|
|
|
2021-03-18 08:27:59 -05:00
|
|
|
sess.UseBool("is_admin")
|
2021-01-07 04:36:13 -06:00
|
|
|
|
2021-03-18 08:27:59 -05:00
|
|
|
if _, err := sess.Insert(&user); err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2021-01-07 04:36:13 -06:00
|
|
|
|
2021-03-18 08:27:59 -05:00
|
|
|
sess.publishAfterCommit(&events.UserCreated{
|
|
|
|
Timestamp: user.Created,
|
|
|
|
Id: user.Id,
|
|
|
|
Name: user.Name,
|
|
|
|
Login: user.Login,
|
|
|
|
Email: user.Email,
|
|
|
|
})
|
|
|
|
|
|
|
|
// create org user link
|
|
|
|
if !skipOrgSetup {
|
|
|
|
orgUser := models.OrgUser{
|
|
|
|
OrgId: orgID,
|
|
|
|
UserId: user.Id,
|
|
|
|
Role: models.ROLE_ADMIN,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if ss.Cfg.AutoAssignOrg && !user.IsAdmin {
|
|
|
|
if len(args.DefaultOrgRole) > 0 {
|
|
|
|
orgUser.Role = models.RoleType(args.DefaultOrgRole)
|
|
|
|
} else {
|
|
|
|
orgUser.Role = models.RoleType(ss.Cfg.AutoAssignOrgRole)
|
2021-01-07 04:36:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 08:27:59 -05:00
|
|
|
if _, err = sess.Insert(&orgUser); err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2021-01-07 04:36:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
func (ss *SQLStore) CreateUser(ctx context.Context, cmd models.CreateUserCommand) (*models.User, error) {
|
|
|
|
var user *models.User
|
|
|
|
err := ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
2019-11-08 04:11:03 -06:00
|
|
|
orgId, err := getOrgIdForNewUser(sess, cmd)
|
2018-06-15 14:23:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2018-06-15 14:23:57 -05:00
|
|
|
if cmd.Email == "" {
|
|
|
|
cmd.Email = cmd.Login
|
|
|
|
}
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
exists, err := sess.Where("email=? OR login=?", cmd.Email, cmd.Login).Get(&models.User{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-13 07:38:54 -05:00
|
|
|
if exists {
|
|
|
|
return models.ErrUserAlreadyExists
|
|
|
|
}
|
|
|
|
|
2018-06-15 14:23:57 -05:00
|
|
|
// create user
|
2021-03-17 10:06:10 -05:00
|
|
|
user = &models.User{
|
2018-06-15 14:23:57 -05:00
|
|
|
Email: cmd.Email,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Login: cmd.Login,
|
|
|
|
Company: cmd.Company,
|
|
|
|
IsAdmin: cmd.IsAdmin,
|
2019-07-15 01:14:32 -05:00
|
|
|
IsDisabled: cmd.IsDisabled,
|
2018-06-15 14:23:57 -05:00
|
|
|
OrgId: orgId,
|
|
|
|
EmailVerified: cmd.EmailVerified,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
LastSeenAt: time.Now().AddDate(-10, 0, 0),
|
|
|
|
}
|
2015-01-19 09:28:45 -06:00
|
|
|
|
2019-10-23 03:40:12 -05:00
|
|
|
salt, err := util.GetRandomString(10)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.Salt = salt
|
|
|
|
rands, err := util.GetRandomString(10)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.Rands = rands
|
2018-08-21 06:30:39 -05:00
|
|
|
|
2018-06-15 14:23:57 -05:00
|
|
|
if len(cmd.Password) > 0 {
|
2019-10-23 03:40:12 -05:00
|
|
|
encodedPassword, err := util.EncodePassword(cmd.Password, user.Salt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.Password = encodedPassword
|
2018-06-15 14:23:57 -05:00
|
|
|
}
|
2015-09-01 05:35:06 -05:00
|
|
|
|
2018-06-15 14:23:57 -05:00
|
|
|
sess.UseBool("is_admin")
|
2015-09-01 05:35:06 -05:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
if _, err := sess.Insert(user); err != nil {
|
2018-06-15 14:23:57 -05:00
|
|
|
return err
|
2018-06-07 11:17:01 -05:00
|
|
|
}
|
|
|
|
|
2018-06-15 14:23:57 -05:00
|
|
|
sess.publishAfterCommit(&events.UserCreated{
|
|
|
|
Timestamp: user.Created,
|
|
|
|
Id: user.Id,
|
|
|
|
Name: user.Name,
|
|
|
|
Login: user.Login,
|
|
|
|
Email: user.Email,
|
|
|
|
})
|
|
|
|
|
|
|
|
// create org user link
|
|
|
|
if !cmd.SkipOrgSetup {
|
2019-06-14 03:50:38 -05:00
|
|
|
orgUser := models.OrgUser{
|
2018-06-15 14:23:57 -05:00
|
|
|
OrgId: orgId,
|
|
|
|
UserId: user.Id,
|
2019-06-14 03:50:38 -05:00
|
|
|
Role: models.ROLE_ADMIN,
|
2018-06-15 14:23:57 -05:00
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
2015-09-01 05:35:06 -05:00
|
|
|
}
|
|
|
|
|
2018-06-15 14:23:57 -05:00
|
|
|
if setting.AutoAssignOrg && !user.IsAdmin {
|
|
|
|
if len(cmd.DefaultOrgRole) > 0 {
|
2019-06-14 03:50:38 -05:00
|
|
|
orgUser.Role = models.RoleType(cmd.DefaultOrgRole)
|
2018-06-15 14:23:57 -05:00
|
|
|
} else {
|
2019-06-14 03:50:38 -05:00
|
|
|
orgUser.Role = models.RoleType(setting.AutoAssignOrgRole)
|
2018-06-15 14:23:57 -05:00
|
|
|
}
|
|
|
|
}
|
2018-06-07 11:17:01 -05:00
|
|
|
|
2018-06-15 14:23:57 -05:00
|
|
|
if _, err = sess.Insert(&orgUser); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-06-07 11:17:01 -05:00
|
|
|
|
2018-06-15 14:23:57 -05:00
|
|
|
return nil
|
2018-06-15 14:57:13 -05:00
|
|
|
})
|
2021-03-17 10:06:10 -05:00
|
|
|
|
|
|
|
return user, err
|
2018-06-07 11:17:01 -05:00
|
|
|
}
|
|
|
|
|
2021-06-21 00:51:33 -05:00
|
|
|
func GetUserById(ctx context.Context, query *models.GetUserByIdQuery) error {
|
|
|
|
return withDbSession(ctx, x, func(sess *DBSession) error {
|
|
|
|
user := new(models.User)
|
|
|
|
has, err := sess.ID(query.Id).Get(user)
|
2015-02-10 08:36:51 -06:00
|
|
|
|
2021-06-21 00:51:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
|
|
|
return models.ErrUserNotFound
|
|
|
|
}
|
2015-02-10 08:36:51 -06:00
|
|
|
|
2021-06-21 00:51:33 -05:00
|
|
|
query.Result = user
|
2015-02-10 08:36:51 -06:00
|
|
|
|
2021-06-21 00:51:33 -05:00
|
|
|
return nil
|
|
|
|
})
|
2015-02-10 08:36:51 -06:00
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func GetUserByLogin(query *models.GetUserByLoginQuery) error {
|
2015-01-19 11:01:04 -06:00
|
|
|
if query.LoginOrEmail == "" {
|
2019-06-14 03:50:38 -05:00
|
|
|
return models.ErrUserNotFound
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2016-06-06 00:15:37 -05:00
|
|
|
// Try and find the user by login first.
|
|
|
|
// It's not sufficient to assume that a LoginOrEmail with an "@" is an email.
|
2019-06-14 03:50:38 -05:00
|
|
|
user := &models.User{Login: query.LoginOrEmail}
|
2015-01-19 11:01:04 -06:00
|
|
|
has, err := x.Get(user)
|
|
|
|
|
2016-06-06 00:15:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
|
|
|
if !has && strings.Contains(query.LoginOrEmail, "@") {
|
2016-06-06 00:15:37 -05:00
|
|
|
// If the user wasn't found, and it contains an "@" fallback to finding the
|
|
|
|
// user by email.
|
2019-06-14 03:50:38 -05:00
|
|
|
user = &models.User{Email: query.LoginOrEmail}
|
2016-06-06 00:15:37 -05:00
|
|
|
has, err = x.Get(user)
|
|
|
|
}
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
|
|
|
} else if !has {
|
2019-06-14 03:50:38 -05:00
|
|
|
return models.ErrUserNotFound
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = user
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func GetUserByEmail(query *models.GetUserByEmailQuery) error {
|
2016-10-19 23:45:10 -05:00
|
|
|
if query.Email == "" {
|
2019-06-14 03:50:38 -05:00
|
|
|
return models.ErrUserNotFound
|
2016-10-19 23:45:10 -05:00
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
user := &models.User{Email: query.Email}
|
2016-10-19 23:45:10 -05:00
|
|
|
has, err := x.Get(user)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
|
|
|
} else if !has {
|
2019-06-14 03:50:38 -05:00
|
|
|
return models.ErrUserNotFound
|
2016-10-19 23:45:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = user
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func UpdateUser(cmd *models.UpdateUserCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2019-06-14 03:50:38 -05:00
|
|
|
user := models.User{
|
2015-01-19 11:57:51 -06:00
|
|
|
Name: cmd.Name,
|
|
|
|
Email: cmd.Email,
|
|
|
|
Login: cmd.Login,
|
2015-02-28 07:30:08 -06:00
|
|
|
Theme: cmd.Theme,
|
2015-01-19 11:57:51 -06:00
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
2018-09-16 05:37:08 -05:00
|
|
|
if _, err := sess.ID(cmd.UserId).Update(&user); err != nil {
|
2015-02-04 10:15:05 -06:00
|
|
|
return err
|
2015-02-03 09:57:42 -06:00
|
|
|
}
|
2015-02-04 10:15:05 -06:00
|
|
|
|
|
|
|
sess.publishAfterCommit(&events.UserUpdated{
|
|
|
|
Timestamp: user.Created,
|
|
|
|
Id: user.Id,
|
|
|
|
Name: user.Name,
|
|
|
|
Login: user.Login,
|
|
|
|
Email: user.Email,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2015-01-19 11:57:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func ChangeUserPassword(cmd *models.ChangeUserPasswordCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2019-06-14 03:50:38 -05:00
|
|
|
user := models.User{
|
2015-02-19 09:09:49 -06:00
|
|
|
Password: cmd.NewPassword,
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
2018-09-16 05:37:08 -05:00
|
|
|
_, err := sess.ID(cmd.UserId).Update(&user)
|
2018-04-16 12:54:23 -05:00
|
|
|
return err
|
2015-02-19 09:09:49 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func UpdateUserLastSeenAt(cmd *models.UpdateUserLastSeenAtCommand) error {
|
2017-08-09 03:36:41 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2019-06-14 03:50:38 -05:00
|
|
|
user := models.User{
|
2017-08-09 03:36:41 -05:00
|
|
|
Id: cmd.UserId,
|
|
|
|
LastSeenAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
2018-09-16 05:37:08 -05:00
|
|
|
_, err := sess.ID(cmd.UserId).Update(&user)
|
2018-04-16 12:54:23 -05:00
|
|
|
return err
|
2017-08-09 03:36:41 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func SetUsingOrg(cmd *models.SetUsingOrgCommand) error {
|
|
|
|
getOrgsForUserCmd := &models.GetUserOrgListQuery{UserId: cmd.UserId}
|
2019-10-22 07:08:18 -05:00
|
|
|
if err := GetUserOrgList(getOrgsForUserCmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-17 08:02:14 -06:00
|
|
|
|
|
|
|
valid := false
|
|
|
|
for _, other := range getOrgsForUserCmd.Result {
|
|
|
|
if other.OrgId == cmd.OrgId {
|
|
|
|
valid = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !valid {
|
2017-04-14 08:47:39 -05:00
|
|
|
return fmt.Errorf("user does not belong to org")
|
2017-02-17 08:02:14 -06:00
|
|
|
}
|
|
|
|
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2018-06-13 11:01:50 -05:00
|
|
|
return setUsingOrgInTransaction(sess, cmd.UserId, cmd.OrgId)
|
2015-01-19 11:01:04 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-13 11:01:50 -05:00
|
|
|
func setUsingOrgInTransaction(sess *DBSession, userID int64, orgID int64) error {
|
2019-06-14 03:50:38 -05:00
|
|
|
user := models.User{
|
2018-06-13 11:01:50 -05:00
|
|
|
Id: userID,
|
|
|
|
OrgId: orgID,
|
|
|
|
}
|
|
|
|
|
2018-09-16 05:37:08 -05:00
|
|
|
_, err := sess.ID(userID).Update(&user)
|
2018-06-13 11:01:50 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func GetUserProfile(query *models.GetUserProfileQuery) error {
|
|
|
|
var user models.User
|
2015-01-19 11:01:04 -06:00
|
|
|
has, err := x.Id(query.UserId).Get(&user)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
|
|
|
} else if !has {
|
2019-06-14 03:50:38 -05:00
|
|
|
return models.ErrUserNotFound
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
query.Result = models.UserProfileDTO{
|
2018-03-22 10:27:24 -05:00
|
|
|
Id: user.Id,
|
2015-05-18 12:06:19 -05:00
|
|
|
Name: user.Name,
|
|
|
|
Email: user.Email,
|
|
|
|
Login: user.Login,
|
|
|
|
Theme: user.Theme,
|
|
|
|
IsGrafanaAdmin: user.IsAdmin,
|
2019-05-21 06:52:49 -05:00
|
|
|
IsDisabled: user.IsDisabled,
|
2015-05-19 03:16:32 -05:00
|
|
|
OrgId: user.OrgId,
|
2019-09-11 07:43:05 -05:00
|
|
|
UpdatedAt: user.Updated,
|
2019-09-28 06:12:33 -05:00
|
|
|
CreatedAt: user.Created,
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 06:27:11 -05:00
|
|
|
type byOrgName []*models.UserOrgDTO
|
|
|
|
|
|
|
|
// Len returns the length of an array of organisations.
|
|
|
|
func (o byOrgName) Len() int {
|
|
|
|
return len(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap swaps two indices of an array of organizations.
|
|
|
|
func (o byOrgName) Swap(i, j int) {
|
|
|
|
o[i], o[j] = o[j], o[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less returns whether element i of an array of organizations is less than element j.
|
|
|
|
func (o byOrgName) Less(i, j int) bool {
|
|
|
|
if strings.ToLower(o[i].Name) < strings.ToLower(o[j].Name) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return o[i].Name < o[j].Name
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func GetUserOrgList(query *models.GetUserOrgListQuery) error {
|
|
|
|
query.Result = make([]*models.UserOrgDTO, 0)
|
2015-02-23 13:07:49 -06:00
|
|
|
sess := x.Table("org_user")
|
|
|
|
sess.Join("INNER", "org", "org_user.org_id=org.id")
|
|
|
|
sess.Where("org_user.user_id=?", query.UserId)
|
2015-02-24 11:32:29 -06:00
|
|
|
sess.Cols("org.name", "org_user.role", "org_user.org_id")
|
2018-04-13 07:53:36 -05:00
|
|
|
sess.OrderBy("org.name")
|
2015-01-19 11:01:04 -06:00
|
|
|
err := sess.Find(&query.Result)
|
2020-04-06 06:27:11 -05:00
|
|
|
sort.Sort(byOrgName(query.Result))
|
2015-01-19 11:01:04 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-18 12:44:17 -06:00
|
|
|
func newSignedInUserCacheKey(orgID, userID int64) string {
|
|
|
|
return fmt.Sprintf("signed-in-user-%d-%d", userID, orgID)
|
|
|
|
}
|
|
|
|
|
2021-05-18 11:24:42 -05:00
|
|
|
func (ss *SQLStore) GetSignedInUserWithCacheCtx(ctx context.Context, query *models.GetSignedInUserQuery) error {
|
2018-12-18 12:44:17 -06:00
|
|
|
cacheKey := newSignedInUserCacheKey(query.OrgId, query.UserId)
|
2018-10-31 08:47:14 -05:00
|
|
|
if cached, found := ss.CacheService.Get(cacheKey); found {
|
2019-06-14 03:50:38 -05:00
|
|
|
query.Result = cached.(*models.SignedInUser)
|
2018-10-31 08:47:14 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-18 11:24:42 -05:00
|
|
|
err := GetSignedInUser(ctx, query)
|
2018-11-05 02:58:13 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-18 12:44:17 -06:00
|
|
|
cacheKey = newSignedInUserCacheKey(query.Result.OrgId, query.UserId)
|
2018-11-02 10:03:12 -05:00
|
|
|
ss.CacheService.Set(cacheKey, query.Result, time.Second*5)
|
2018-11-05 02:58:13 -06:00
|
|
|
return nil
|
2018-11-02 10:03:12 -05:00
|
|
|
}
|
|
|
|
|
2021-05-18 11:24:42 -05:00
|
|
|
func GetSignedInUser(ctx context.Context, query *models.GetSignedInUserQuery) error {
|
2018-11-02 10:03:12 -05:00
|
|
|
orgId := "u.org_id"
|
|
|
|
if query.OrgId > 0 {
|
|
|
|
orgId = strconv.FormatInt(query.OrgId, 10)
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:21:08 -06:00
|
|
|
var rawSQL = `SELECT
|
2017-08-09 03:36:41 -05:00
|
|
|
u.id as user_id,
|
|
|
|
u.is_admin as is_grafana_admin,
|
|
|
|
u.email as email,
|
|
|
|
u.login as login,
|
|
|
|
u.name as name,
|
|
|
|
u.help_flags1 as help_flags1,
|
|
|
|
u.last_seen_at as last_seen_at,
|
2017-08-16 08:03:49 -05:00
|
|
|
(SELECT COUNT(*) FROM org_user where org_user.user_id = u.id) as org_count,
|
2017-08-09 03:36:41 -05:00
|
|
|
org.name as org_name,
|
|
|
|
org_user.role as org_role,
|
|
|
|
org.id as org_id
|
2017-04-14 08:47:39 -05:00
|
|
|
FROM ` + dialect.Quote("user") + ` as u
|
|
|
|
LEFT OUTER JOIN org_user on org_user.org_id = ` + orgId + ` and org_user.user_id = u.id
|
|
|
|
LEFT OUTER JOIN org on org.id = org_user.org_id `
|
2015-05-02 05:06:58 -05:00
|
|
|
|
2015-05-02 05:30:53 -05:00
|
|
|
sess := x.Table("user")
|
2021-05-27 06:55:33 -05:00
|
|
|
sess = sess.Context(ctx)
|
2020-07-16 07:39:01 -05:00
|
|
|
switch {
|
|
|
|
case query.UserId > 0:
|
2020-11-10 23:21:08 -06:00
|
|
|
sess.SQL(rawSQL+"WHERE u.id=?", query.UserId)
|
2020-07-16 07:39:01 -05:00
|
|
|
case query.Login != "":
|
2020-11-10 23:21:08 -06:00
|
|
|
sess.SQL(rawSQL+"WHERE u.login=?", query.Login)
|
2020-07-16 07:39:01 -05:00
|
|
|
case query.Email != "":
|
2020-11-10 23:21:08 -06:00
|
|
|
sess.SQL(rawSQL+"WHERE u.email=?", query.Email)
|
2015-05-02 05:06:58 -05:00
|
|
|
}
|
2015-01-19 11:01:04 -06:00
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
var user models.SignedInUser
|
2015-05-02 05:30:53 -05:00
|
|
|
has, err := sess.Get(&user)
|
2015-01-19 11:01:04 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
2019-06-14 03:50:38 -05:00
|
|
|
return models.ErrUserNotFound
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 02:02:37 -05:00
|
|
|
if user.OrgRole == "" {
|
|
|
|
user.OrgId = -1
|
|
|
|
user.OrgName = "Org missing"
|
2018-11-05 03:49:56 -06:00
|
|
|
}
|
2018-10-25 08:20:01 -05:00
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
getTeamsByUserQuery := &models.GetTeamsByUserQuery{OrgId: user.OrgId, UserId: user.UserId}
|
2018-11-05 03:49:56 -06:00
|
|
|
err = GetTeamsByUser(getTeamsByUserQuery)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Teams = make([]int64, len(getTeamsByUserQuery.Result))
|
|
|
|
for i, t := range getTeamsByUserQuery.Result {
|
|
|
|
user.Teams[i] = t.Id
|
2015-05-19 02:02:37 -05:00
|
|
|
}
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
query.Result = &user
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func SearchUsers(query *models.SearchUsersQuery) error {
|
|
|
|
query.Result = models.SearchUserQueryResult{
|
|
|
|
Users: make([]*models.UserSearchHitDTO, 0),
|
2017-02-08 07:20:07 -06:00
|
|
|
}
|
2017-08-09 03:36:41 -05:00
|
|
|
|
2017-03-07 09:03:54 -06:00
|
|
|
queryWithWildcards := "%" + query.Query + "%"
|
|
|
|
|
2017-08-09 03:36:41 -05:00
|
|
|
whereConditions := make([]string, 0)
|
|
|
|
whereParams := make([]interface{}, 0)
|
2019-06-25 10:29:07 -05:00
|
|
|
sess := x.Table("user").Alias("u")
|
|
|
|
|
|
|
|
// Join with only most recent auth module
|
|
|
|
joinCondition := `(
|
|
|
|
SELECT id from user_auth
|
|
|
|
WHERE user_auth.user_id = u.id
|
|
|
|
ORDER BY user_auth.created DESC `
|
|
|
|
joinCondition = "user_auth.id=" + joinCondition + dialect.Limit(1) + ")"
|
|
|
|
sess.Join("LEFT", "user_auth", joinCondition)
|
2017-08-09 03:36:41 -05:00
|
|
|
if query.OrgId > 0 {
|
|
|
|
whereConditions = append(whereConditions, "org_id = ?")
|
|
|
|
whereParams = append(whereParams, query.OrgId)
|
|
|
|
}
|
|
|
|
|
2017-03-07 09:03:54 -06:00
|
|
|
if query.Query != "" {
|
2017-11-29 08:17:31 -06:00
|
|
|
whereConditions = append(whereConditions, "(email "+dialect.LikeStr()+" ? OR name "+dialect.LikeStr()+" ? OR login "+dialect.LikeStr()+" ?)")
|
2017-08-09 03:36:41 -05:00
|
|
|
whereParams = append(whereParams, queryWithWildcards, queryWithWildcards, queryWithWildcards)
|
2017-03-07 09:03:54 -06:00
|
|
|
}
|
2017-08-09 03:36:41 -05:00
|
|
|
|
2019-07-16 22:24:56 -05:00
|
|
|
if query.IsDisabled != nil {
|
2019-07-15 01:14:32 -05:00
|
|
|
whereConditions = append(whereConditions, "is_disabled = ?")
|
2019-07-16 22:24:56 -05:00
|
|
|
whereParams = append(whereParams, query.IsDisabled)
|
2019-07-15 01:14:32 -05:00
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
if query.AuthModule != "" {
|
2020-06-03 08:28:13 -05:00
|
|
|
whereConditions = append(whereConditions, `auth_module=?`)
|
2019-06-14 03:50:38 -05:00
|
|
|
whereParams = append(whereParams, query.AuthModule)
|
|
|
|
}
|
|
|
|
|
2021-08-31 04:35:16 -05:00
|
|
|
if query.Filter == models.ActiveLast30Days {
|
|
|
|
activeUserDeadlineDate := time.Now().Add(-activeUserTimeLimit)
|
|
|
|
whereConditions = append(whereConditions, `last_seen_at > ?`)
|
|
|
|
whereParams = append(whereParams, activeUserDeadlineDate)
|
|
|
|
}
|
|
|
|
|
2017-08-09 03:36:41 -05:00
|
|
|
if len(whereConditions) > 0 {
|
|
|
|
sess.Where(strings.Join(whereConditions, " AND "), whereParams...)
|
|
|
|
}
|
|
|
|
|
2021-02-15 02:59:39 -06:00
|
|
|
if query.Limit > 0 {
|
|
|
|
offset := query.Limit * (query.Page - 1)
|
|
|
|
sess.Limit(query.Limit, offset)
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:29:07 -05:00
|
|
|
sess.Cols("u.id", "u.email", "u.name", "u.login", "u.is_admin", "u.is_disabled", "u.last_seen_at", "user_auth.auth_module")
|
2020-04-03 12:15:10 -05:00
|
|
|
sess.Asc("u.login", "u.email")
|
2017-02-08 07:20:07 -06:00
|
|
|
if err := sess.Find(&query.Result.Users); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-09 03:36:41 -05:00
|
|
|
// get total
|
2019-06-14 03:50:38 -05:00
|
|
|
user := models.User{}
|
2019-06-25 10:29:07 -05:00
|
|
|
countSess := x.Table("user").Alias("u")
|
2017-08-09 03:36:41 -05:00
|
|
|
|
2020-06-03 08:28:13 -05:00
|
|
|
// Join with user_auth table if users filtered by auth_module
|
|
|
|
if query.AuthModule != "" {
|
|
|
|
countSess.Join("LEFT", "user_auth", joinCondition)
|
|
|
|
}
|
|
|
|
|
2017-08-09 03:36:41 -05:00
|
|
|
if len(whereConditions) > 0 {
|
|
|
|
countSess.Where(strings.Join(whereConditions, " AND "), whereParams...)
|
2017-03-07 09:03:54 -06:00
|
|
|
}
|
2017-08-09 03:36:41 -05:00
|
|
|
|
2017-03-07 09:03:54 -06:00
|
|
|
count, err := countSess.Count(&user)
|
2017-02-08 07:20:07 -06:00
|
|
|
query.Result.TotalCount = count
|
2017-08-09 03:36:41 -05:00
|
|
|
|
|
|
|
for _, user := range query.Result.Users {
|
|
|
|
user.LastSeenAtAge = util.GetAgeString(user.LastSeenAt)
|
|
|
|
}
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
return err
|
|
|
|
}
|
2015-02-11 09:47:22 -06:00
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func DisableUser(cmd *models.DisableUserCommand) error {
|
|
|
|
user := models.User{}
|
2019-05-21 06:52:49 -05:00
|
|
|
sess := x.Table("user")
|
2020-01-10 04:43:44 -06:00
|
|
|
|
|
|
|
if has, err := sess.ID(cmd.UserId).Get(&user); err != nil {
|
2019-10-22 07:08:18 -05:00
|
|
|
return err
|
2020-01-10 04:43:44 -06:00
|
|
|
} else if !has {
|
|
|
|
return models.ErrUserNotFound
|
2019-10-22 07:08:18 -05:00
|
|
|
}
|
2019-05-21 06:52:49 -05:00
|
|
|
|
|
|
|
user.IsDisabled = cmd.IsDisabled
|
|
|
|
sess.UseBool("is_disabled")
|
|
|
|
|
|
|
|
_, err := sess.ID(cmd.UserId).Update(&user)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func BatchDisableUsers(cmd *models.BatchDisableUsersCommand) error {
|
2019-05-31 05:22:22 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
|
|
|
userIds := cmd.UserIds
|
|
|
|
|
|
|
|
if len(userIds) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
user_id_params := strings.Repeat(",?", len(userIds)-1)
|
|
|
|
disableSQL := "UPDATE " + dialect.Quote("user") + " SET is_disabled=? WHERE Id IN (?" + user_id_params + ")"
|
|
|
|
|
|
|
|
disableParams := []interface{}{disableSQL, cmd.IsDisabled}
|
|
|
|
for _, v := range userIds {
|
|
|
|
disableParams = append(disableParams, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := sess.Exec(disableParams...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func DeleteUser(cmd *models.DeleteUserCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2018-10-11 00:48:35 -05:00
|
|
|
return deleteUserInTransaction(sess, cmd)
|
|
|
|
})
|
|
|
|
}
|
2015-08-12 02:23:46 -05:00
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func deleteUserInTransaction(sess *DBSession, cmd *models.DeleteUserCommand) error {
|
2020-09-22 09:22:19 -05:00
|
|
|
// Check if user exists
|
2020-01-10 04:43:44 -06:00
|
|
|
user := models.User{Id: cmd.UserId}
|
|
|
|
has, err := sess.Get(&user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return models.ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:48:35 -05:00
|
|
|
deletes := []string{
|
|
|
|
"DELETE FROM star WHERE user_id = ?",
|
|
|
|
"DELETE FROM " + dialect.Quote("user") + " WHERE id = ?",
|
|
|
|
"DELETE FROM org_user WHERE user_id = ?",
|
|
|
|
"DELETE FROM dashboard_acl WHERE user_id = ?",
|
|
|
|
"DELETE FROM preferences WHERE user_id = ?",
|
|
|
|
"DELETE FROM team_member WHERE user_id = ?",
|
|
|
|
"DELETE FROM user_auth WHERE user_id = ?",
|
2019-06-10 06:35:47 -05:00
|
|
|
"DELETE FROM user_auth_token WHERE user_id = ?",
|
|
|
|
"DELETE FROM quota WHERE user_id = ?",
|
2018-10-11 00:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, sql := range deletes {
|
|
|
|
_, err := sess.Exec(sql, cmd.UserId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-08-12 02:23:46 -05:00
|
|
|
}
|
2018-10-11 00:48:35 -05:00
|
|
|
}
|
2015-08-12 02:23:46 -05:00
|
|
|
|
2018-10-11 00:48:35 -05:00
|
|
|
return nil
|
2015-02-11 09:47:22 -06:00
|
|
|
}
|
2015-02-26 08:43:48 -06:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
func (ss *SQLStore) UpdateUserPermissions(userID int64, isAdmin bool) error {
|
|
|
|
return ss.WithTransactionalDbSession(context.Background(), func(sess *DBSession) error {
|
|
|
|
var user models.User
|
|
|
|
if _, err := sess.ID(userID).Get(&user); err != nil {
|
2019-10-22 07:08:18 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-02-26 08:43:48 -06:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
user.IsAdmin = isAdmin
|
2015-02-26 08:43:48 -06:00
|
|
|
sess.UseBool("is_admin")
|
2018-12-01 11:28:10 -06:00
|
|
|
|
2018-09-16 05:37:08 -05:00
|
|
|
_, err := sess.ID(user.Id).Update(&user)
|
2018-12-01 11:28:10 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate that after update there is at least one server admin
|
|
|
|
if err := validateOneAdminLeft(sess); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-02-26 08:43:48 -06:00
|
|
|
})
|
|
|
|
}
|
2016-11-09 03:41:39 -06:00
|
|
|
|
2019-06-14 03:50:38 -05:00
|
|
|
func SetUserHelpFlag(cmd *models.SetUserHelpFlagCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2019-06-14 03:50:38 -05:00
|
|
|
user := models.User{
|
2016-11-09 03:41:39 -06:00
|
|
|
Id: cmd.UserId,
|
|
|
|
HelpFlags1: cmd.HelpFlags1,
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
2018-09-16 05:37:08 -05:00
|
|
|
_, err := sess.ID(cmd.UserId).Cols("help_flags1").Update(&user)
|
2018-04-16 12:54:23 -05:00
|
|
|
return err
|
2016-11-09 03:41:39 -06:00
|
|
|
})
|
|
|
|
}
|
2018-12-01 11:28:10 -06:00
|
|
|
|
|
|
|
func validateOneAdminLeft(sess *DBSession) error {
|
|
|
|
// validate that there is an admin user left
|
2019-06-14 03:50:38 -05:00
|
|
|
count, err := sess.Where("is_admin=?", true).Count(&models.User{})
|
2018-12-01 11:28:10 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
2019-06-14 03:50:38 -05:00
|
|
|
return models.ErrLastGrafanaAdmin
|
2018-12-01 11:28:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|