mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Move role types to a separate package (#55665)
* Move role types to a separate package * Make role type singular and remove _ from directory name
This commit is contained in:
parent
3eaeffda07
commit
7f98f4b411
69
pkg/models/roletype/role_type.go
Normal file
69
pkg/models/roletype/role_type.go
Normal file
@ -0,0 +1,69 @@
|
||||
package roletype
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// swagger:enum RoleType
|
||||
type RoleType string
|
||||
|
||||
const (
|
||||
RoleViewer RoleType = "Viewer"
|
||||
RoleEditor RoleType = "Editor"
|
||||
RoleAdmin RoleType = "Admin"
|
||||
)
|
||||
|
||||
func (r RoleType) IsValid() bool {
|
||||
return r == RoleViewer || r == RoleAdmin || r == RoleEditor
|
||||
}
|
||||
|
||||
func (r RoleType) Includes(other RoleType) bool {
|
||||
if r == RoleAdmin {
|
||||
return true
|
||||
}
|
||||
|
||||
if r == RoleEditor {
|
||||
return other != RoleAdmin
|
||||
}
|
||||
|
||||
return r == other
|
||||
}
|
||||
|
||||
func (r RoleType) Children() []RoleType {
|
||||
switch r {
|
||||
case RoleAdmin:
|
||||
return []RoleType{RoleEditor, RoleViewer}
|
||||
case RoleEditor:
|
||||
return []RoleType{RoleViewer}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r RoleType) Parents() []RoleType {
|
||||
switch r {
|
||||
case RoleEditor:
|
||||
return []RoleType{RoleAdmin}
|
||||
case RoleViewer:
|
||||
return []RoleType{RoleEditor, RoleAdmin}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RoleType) UnmarshalText(data []byte) error {
|
||||
// make sure "viewer" and "Viewer" are both correct
|
||||
str := strings.Title(string(data))
|
||||
|
||||
*r = RoleType(str)
|
||||
if !r.IsValid() {
|
||||
if (*r) != "" {
|
||||
return fmt.Errorf("invalid role value: %s", *r)
|
||||
}
|
||||
|
||||
*r = RoleViewer
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -2,9 +2,10 @@ package org
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models/roletype"
|
||||
)
|
||||
|
||||
// Typed errors
|
||||
@ -37,8 +38,7 @@ type OrgUser struct {
|
||||
Updated time.Time
|
||||
}
|
||||
|
||||
// swagger:enum RoleType
|
||||
type RoleType string
|
||||
type RoleType = roletype.RoleType
|
||||
|
||||
const (
|
||||
RoleViewer RoleType = "Viewer"
|
||||
@ -156,60 +156,6 @@ type RemoveOrgUserCommand struct {
|
||||
UserWasDeleted bool
|
||||
}
|
||||
|
||||
func (r RoleType) IsValid() bool {
|
||||
return r == RoleViewer || r == RoleAdmin || r == RoleEditor
|
||||
}
|
||||
|
||||
func (r RoleType) Includes(other RoleType) bool {
|
||||
if r == RoleAdmin {
|
||||
return true
|
||||
}
|
||||
|
||||
if r == RoleEditor {
|
||||
return other != RoleAdmin
|
||||
}
|
||||
|
||||
return r == other
|
||||
}
|
||||
|
||||
func (r RoleType) Children() []RoleType {
|
||||
switch r {
|
||||
case RoleAdmin:
|
||||
return []RoleType{RoleEditor, RoleViewer}
|
||||
case RoleEditor:
|
||||
return []RoleType{RoleViewer}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r RoleType) Parents() []RoleType {
|
||||
switch r {
|
||||
case RoleEditor:
|
||||
return []RoleType{RoleAdmin}
|
||||
case RoleViewer:
|
||||
return []RoleType{RoleEditor, RoleAdmin}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RoleType) UnmarshalText(data []byte) error {
|
||||
// make sure "viewer" and "Viewer" are both correct
|
||||
str := strings.Title(string(data))
|
||||
|
||||
*r = RoleType(str)
|
||||
if !r.IsValid() {
|
||||
if (*r) != "" {
|
||||
return fmt.Errorf("invalid role value: %s", *r)
|
||||
}
|
||||
|
||||
*r = RoleViewer
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type ByOrgName []*UserOrgDTO
|
||||
|
||||
// Len returns the length of an array of organisations.
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/models/roletype"
|
||||
)
|
||||
|
||||
type HelpFlags1 uint64
|
||||
@ -196,7 +196,7 @@ type SignedInUser struct {
|
||||
UserID int64 `xorm:"user_id"`
|
||||
OrgID int64 `xorm:"org_id"`
|
||||
OrgName string
|
||||
OrgRole org.RoleType
|
||||
OrgRole roletype.RoleType
|
||||
ExternalAuthModule string
|
||||
ExternalAuthID string
|
||||
Login string
|
||||
@ -268,7 +268,7 @@ func (u *SignedInUser) ToUserDisplayDTO() *UserDisplayDTO {
|
||||
}
|
||||
}
|
||||
|
||||
func (u *SignedInUser) HasRole(role org.RoleType) bool {
|
||||
func (u *SignedInUser) HasRole(role roletype.RoleType) bool {
|
||||
if u.IsGrafanaAdmin {
|
||||
return true
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user