mirror of
https://github.com/stashapp/stash.git
synced 2026-02-08 00:12:55 +01:00
70 lines
1.1 KiB
Go
70 lines
1.1 KiB
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
)
|
|
|
|
type RoleEnum string
|
|
|
|
const (
|
|
RoleEnumAdmin RoleEnum = "ADMIN"
|
|
RoleEnumRead RoleEnum = "READ"
|
|
RoleEnumModify RoleEnum = "MODIFY"
|
|
)
|
|
|
|
func (e RoleEnum) Implies(other RoleEnum) bool {
|
|
// admin has all roles
|
|
if e == RoleEnumAdmin {
|
|
return true
|
|
}
|
|
|
|
// until we add a NONE value, all values imply read
|
|
if e.IsValid() && other == RoleEnumRead {
|
|
return true
|
|
}
|
|
|
|
// all others only imply themselves
|
|
return e == other
|
|
}
|
|
|
|
func (e RoleEnum) IsValid() bool {
|
|
switch e {
|
|
case RoleEnumRead, RoleEnumModify, RoleEnumAdmin:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (e RoleEnum) String() string {
|
|
return string(e)
|
|
}
|
|
|
|
func (e *RoleEnum) UnmarshalGQL(v interface{}) error {
|
|
str, ok := v.(string)
|
|
if !ok {
|
|
return fmt.Errorf("enums must be strings")
|
|
}
|
|
|
|
*e = RoleEnum(str)
|
|
if !e.IsValid() {
|
|
return fmt.Errorf("%s is not a valid RoleEnum", str)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e RoleEnum) MarshalGQL(w io.Writer) {
|
|
fmt.Fprint(w, strconv.Quote(e.String()))
|
|
}
|
|
|
|
type Roles []RoleEnum
|
|
|
|
func (r Roles) HasRole(role RoleEnum) bool {
|
|
for _, r := range r {
|
|
if r.Implies(role) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|