2015-02-23 13:07:49 -06:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2016-04-25 07:00:49 -05:00
|
|
|
"encoding/json"
|
2015-02-23 13:07:49 -06:00
|
|
|
"errors"
|
2016-04-25 07:00:49 -05:00
|
|
|
"fmt"
|
2015-02-23 13:07:49 -06:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Typed errors
|
|
|
|
var (
|
2015-07-21 05:18:11 -05:00
|
|
|
ErrInvalidRoleType = errors.New("Invalid role type")
|
|
|
|
ErrLastOrgAdmin = errors.New("Cannot remove last organization admin")
|
|
|
|
ErrOrgUserNotFound = errors.New("Cannot find the organization user")
|
|
|
|
ErrOrgUserAlreadyAdded = errors.New("User is already added to organization")
|
2015-02-23 13:07:49 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type RoleType string
|
|
|
|
|
|
|
|
const (
|
2017-12-13 11:53:42 -06:00
|
|
|
ROLE_VIEWER RoleType = "Viewer"
|
|
|
|
ROLE_EDITOR RoleType = "Editor"
|
|
|
|
ROLE_ADMIN RoleType = "Admin"
|
2015-02-23 13:07:49 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r RoleType) IsValid() bool {
|
2017-12-13 11:53:42 -06:00
|
|
|
return r == ROLE_VIEWER || r == ROLE_ADMIN || r == ROLE_EDITOR
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2015-12-21 16:09:27 -06:00
|
|
|
func (r RoleType) Includes(other RoleType) bool {
|
|
|
|
if r == ROLE_ADMIN {
|
|
|
|
return true
|
|
|
|
}
|
2017-06-17 17:24:38 -05:00
|
|
|
|
2017-12-13 11:53:42 -06:00
|
|
|
if r == ROLE_EDITOR {
|
2015-12-21 16:09:27 -06:00
|
|
|
return other != ROLE_ADMIN
|
|
|
|
}
|
2019-03-20 04:58:20 -05:00
|
|
|
|
2019-09-19 01:19:11 -05:00
|
|
|
return r == other
|
2015-12-21 16:09:27 -06:00
|
|
|
}
|
|
|
|
|
2016-04-25 07:00:49 -05:00
|
|
|
func (r *RoleType) UnmarshalJSON(data []byte) error {
|
|
|
|
var str string
|
|
|
|
err := json.Unmarshal(data, &str)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*r = RoleType(str)
|
|
|
|
|
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 !(*r).IsValid() {
|
2016-04-25 07:00:49 -05:00
|
|
|
if (*r) != "" {
|
2018-04-16 13:25:48 -05:00
|
|
|
return fmt.Errorf("JSON validation error: invalid role value: %s", *r)
|
2016-04-25 07:00:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
*r = ROLE_VIEWER
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
type OrgUser struct {
|
2015-05-01 02:48:07 -05:00
|
|
|
Id int64
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId int64
|
|
|
|
UserId int64
|
|
|
|
Role RoleType
|
|
|
|
Created time.Time
|
|
|
|
Updated time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------
|
|
|
|
// COMMANDS
|
|
|
|
|
|
|
|
type RemoveOrgUserCommand struct {
|
2018-10-11 00:48:35 -05:00
|
|
|
UserId int64
|
|
|
|
OrgId int64
|
|
|
|
ShouldDeleteOrphanedUser bool
|
2018-10-11 14:20:53 -05:00
|
|
|
UserWasDeleted bool
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type AddOrgUserCommand struct {
|
|
|
|
LoginOrEmail string `json:"loginOrEmail" binding:"Required"`
|
|
|
|
Role RoleType `json:"role" binding:"Required"`
|
|
|
|
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
UserId int64 `json:"-"`
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:48:07 -05:00
|
|
|
type UpdateOrgUserCommand struct {
|
|
|
|
Role RoleType `json:"role" binding:"Required"`
|
|
|
|
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
UserId int64 `json:"-"`
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
// ----------------------
|
|
|
|
// QUERIES
|
|
|
|
|
|
|
|
type GetOrgUsersQuery struct {
|
2018-02-09 03:42:37 -06:00
|
|
|
OrgId int64
|
|
|
|
Query string
|
|
|
|
Limit int
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
Result []*OrgUserDTO
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------
|
|
|
|
// Projections and DTOs
|
|
|
|
|
|
|
|
type OrgUserDTO struct {
|
2017-08-09 03:36:41 -05:00
|
|
|
OrgId int64 `json:"orgId"`
|
|
|
|
UserId int64 `json:"userId"`
|
|
|
|
Email string `json:"email"`
|
2017-08-18 01:17:35 -05:00
|
|
|
AvatarUrl string `json:"avatarUrl"`
|
2017-08-09 03:36:41 -05:00
|
|
|
Login string `json:"login"`
|
|
|
|
Role string `json:"role"`
|
|
|
|
LastSeenAt time.Time `json:"lastSeenAt"`
|
|
|
|
LastSeenAtAge string `json:"lastSeenAtAge"`
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|