Skip to content
Snippets Groups Projects
capability.go 1.34 KiB
Newer Older
Gabriel Zachmann's avatar
Gabriel Zachmann committed
package capabilities

import (
	"database/sql/driver"
	"encoding/json"
)

// Constants for capabilities
const (
	CapabilityAT               Capability = "AT"
	CapabilityCreateST                    = "create_super_token"
	CapabilitySettings                    = "settings"
	CapabilityTokeninfoHistory            = "tokeninfo_history"
	CapabilityTokeninfoTree               = "tokeninfo_tree"
	CapabilityListST                      = "list_super_tokens"
)

Gabriel Zachmann's avatar
Gabriel Zachmann committed
// Capabilities is a slice of Capability
type Capabilities []Capability

// Capability is a capability string
type Capability string

// Scan implements the sql.Scanner interface.
func (c *Capabilities) Scan(src interface{}) error {
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	if src == nil {
		return nil
	}
	val := src.([]uint8)
	err := json.Unmarshal(val, &c)
	return err
}

// Value implements the driver.Valuer interface
func (c Capabilities) Value() (driver.Value, error) {
	if len(c) == 0 {
		return nil, nil
	}
	return json.Marshal(c)
}

// Tighten tightens two set of Capabilities into one new
Gabriel Zachmann's avatar
Gabriel Zachmann committed
func Tighten(a, b Capabilities) (res Capabilities) {
	if b == nil {
		return a
	}
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	for _, bb := range b {
		if a.Has(bb) {
			res = append(res, bb)
Gabriel Zachmann's avatar
Gabriel Zachmann committed
		}
	}
	return
}
// Has checks if Capabilities slice contains the passed Capability
func (c Capabilities) Has(a Capability) bool {
	for _, cc := range c {
		if cc == a {
			return true
		}
	}
	return false
}