tendermint/permission/types/permissions.go

291 lines
6.9 KiB
Go
Raw Normal View History

2015-05-19 21:40:02 -07:00
package types
import (
"fmt"
. "github.com/tendermint/tendermint/common"
2015-06-14 15:18:17 -07:00
"reflect"
2015-05-19 21:40:02 -07:00
)
//------------------------------------------------------------------------------------------------
var (
GlobalPermissionsAddress = Zero256[:20]
GlobalPermissionsAddress256 = Zero256
DougAddress = append([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, []byte("THISISDOUG")...)
DougAddress256 = LeftPadWord256(DougAddress)
)
// A particular permission
2015-05-20 16:20:06 -07:00
type PermFlag uint64
2015-05-19 21:40:02 -07:00
// Base permission references are like unix (the index is already bit shifted)
const (
2015-05-20 16:20:06 -07:00
Root PermFlag = 1 << iota // 1
Send // 2
Call // 4
CreateContract // 8
CreateAccount // 16
Bond // 32
2015-06-09 15:50:42 -07:00
Name // 64
2015-05-19 21:40:02 -07:00
2015-06-09 15:50:42 -07:00
DefaultBBPB = Send | Call | CreateContract | CreateAccount | Bond | Name
2015-05-19 21:40:02 -07:00
2015-06-19 23:53:21 -07:00
// XXX: must be adjusted if base perms added/removed
2015-06-09 15:50:42 -07:00
NumBasePermissions uint = 7
2015-05-19 21:40:02 -07:00
TopBasePermission PermFlag = 1 << (NumBasePermissions - 1)
2015-06-19 23:53:21 -07:00
AllBasePermissions PermFlag = TopBasePermission | (TopBasePermission - 1)
AllSet PermFlag = AllBasePermissions | AllSNativePermissions
2015-05-19 21:40:02 -07:00
)
2015-06-14 15:18:17 -07:00
// should have same ordering as above
type BasePermissionsString struct {
Root bool `json:"root,omitempty"`
Send bool `json:"send,omitempty"`
Call bool `json:"call,omitempty"`
CreateContract bool `json:"create_contract,omitempty"`
CreateAccount bool `json:"create_account,omitempty"`
Bond bool `json:"bond,omitempty"`
Name bool `json:"name,omitempty"`
}
2015-05-19 21:40:02 -07:00
//---------------------------------------------------------------------------------------------
// Base chain permissions struct
type BasePermissions struct {
// bit array with "has"/"doesn't have" for each permission
2015-06-14 15:18:17 -07:00
Perms PermFlag `json:"perms"`
2015-05-19 21:40:02 -07:00
// bit array with "set"/"not set" for each permission (not-set should fall back to global)
2015-06-14 15:18:17 -07:00
SetBit PermFlag `json:"set"`
2015-05-19 21:40:02 -07:00
}
func NewBasePermissions() *BasePermissions {
return &BasePermissions{0, 0}
}
// Get a permission value. ty should be a power of 2.
// ErrValueNotSet is returned if the permission's set bit is off,
// and should be caught by caller so the global permission can be fetched
func (p *BasePermissions) Get(ty PermFlag) (bool, error) {
2015-05-21 12:51:57 -07:00
if ty == 0 {
return false, ErrInvalidPermission(ty)
}
2015-05-19 21:40:02 -07:00
if p.SetBit&ty == 0 {
return false, ErrValueNotSet(ty)
}
return p.Perms&ty > 0, nil
}
// Set a permission bit. Will set the permission's set bit to true.
func (p *BasePermissions) Set(ty PermFlag, value bool) error {
2015-05-21 16:56:40 -07:00
if ty == 0 {
return ErrInvalidPermission(ty)
}
2015-05-19 21:40:02 -07:00
p.SetBit |= ty
if value {
p.Perms |= ty
} else {
p.Perms &= ^ty
}
return nil
}
// Set the permission's set bit to false
func (p *BasePermissions) Unset(ty PermFlag) error {
2015-05-21 16:56:40 -07:00
if ty == 0 {
return ErrInvalidPermission(ty)
}
2015-05-19 21:40:02 -07:00
p.SetBit &= ^ty
return nil
}
2015-06-14 15:18:17 -07:00
// Check if the permission is set
func (p *BasePermissions) IsSet(ty PermFlag) bool {
if ty == 0 {
return false
}
return p.SetBit&ty > 0
}
2015-05-19 21:40:02 -07:00
func (p *BasePermissions) Copy() *BasePermissions {
if p == nil {
return nil
}
2015-05-19 21:40:02 -07:00
return &BasePermissions{
Perms: p.Perms,
SetBit: p.SetBit,
}
}
func (p *BasePermissions) String() string {
return fmt.Sprintf("Base: %b; Set: %b", p.Perms, p.SetBit)
}
//---------------------------------------------------------------------------------------------
type AccountPermissions struct {
2015-06-14 15:18:17 -07:00
Base *BasePermissions `json:"base"`
Roles []string `json:"roles"`
2015-05-19 21:40:02 -07:00
}
func NewAccountPermissions() *AccountPermissions {
return &AccountPermissions{
Base: NewBasePermissions(),
Roles: []string{},
}
}
// Returns true if the role is found
func (aP *AccountPermissions) HasRole(role string) bool {
2015-05-21 16:50:09 -07:00
role = string(LeftPadBytes([]byte(role), 32))
2015-05-19 21:40:02 -07:00
for _, r := range aP.Roles {
if r == role {
return true
}
}
return false
}
// Returns true if the role is added, and false if it already exists
func (aP *AccountPermissions) AddRole(role string) bool {
2015-05-21 16:50:09 -07:00
role = string(LeftPadBytes([]byte(role), 32))
2015-05-19 21:40:02 -07:00
for _, r := range aP.Roles {
if r == role {
return false
}
}
aP.Roles = append(aP.Roles, role)
return true
}
// Returns true if the role is removed, and false if it is not found
func (aP *AccountPermissions) RmRole(role string) bool {
2015-05-21 16:50:09 -07:00
role = string(LeftPadBytes([]byte(role), 32))
2015-05-19 21:40:02 -07:00
for i, r := range aP.Roles {
if r == role {
post := []string{}
if len(aP.Roles) > i+1 {
post = aP.Roles[i+1:]
}
aP.Roles = append(aP.Roles[:i], post...)
return true
}
}
return false
}
func (aP *AccountPermissions) Copy() *AccountPermissions {
if aP == nil {
return nil
}
2015-05-19 21:40:02 -07:00
r := make([]string, len(aP.Roles))
copy(r, aP.Roles)
return &AccountPermissions{
Base: aP.Base.Copy(),
Roles: r,
}
}
func NewDefaultAccountPermissions() *AccountPermissions {
return &AccountPermissions{
Base: &BasePermissions{
Perms: DefaultBBPB,
SetBit: AllSet,
},
Roles: []string{},
}
}
2015-06-14 15:18:17 -07:00
//---------------------------------------------------------------------------------------------
// Utilities to make bitmasks human readable
func NewDefaultAccountPermissionsString() BasePermissionsString {
return BasePermissionsString{
Root: false,
Bond: true,
Send: true,
Call: true,
Name: true,
CreateAccount: true,
CreateContract: true,
}
}
func AccountPermissionsFromStrings(perms *BasePermissionsString, roles []string) (*AccountPermissions, error) {
base := NewBasePermissions()
permRv := reflect.ValueOf(perms)
for i := uint(0); i < uint(permRv.NumField()); i++ {
v := permRv.Field(int(i)).Bool()
base.Set(1<<i, v)
}
aP := &AccountPermissions{
Base: base,
Roles: make([]string, len(roles)),
}
copy(aP.Roles, roles)
return aP, nil
}
func AccountPermissionsToStrings(aP *AccountPermissions) (*BasePermissionsString, []string, error) {
perms := new(BasePermissionsString)
permsRv := reflect.ValueOf(perms).Elem()
for i := uint(0); i < NumBasePermissions; i++ {
pf := PermFlag(1 << i)
if aP.Base.IsSet(pf) {
// won't err if the bit is set
v, _ := aP.Base.Get(pf)
f := permsRv.Field(int(i))
f.SetBool(v)
}
}
roles := make([]string, len(aP.Roles))
copy(roles, aP.Roles)
return perms, roles, nil
}
func PermFlagToString(pf PermFlag) (perm string, err error) {
switch pf {
case Root:
perm = "root"
case Send:
perm = "send"
case Call:
perm = "call"
case CreateContract:
perm = "create_contract"
case CreateAccount:
perm = "create_account"
case Bond:
perm = "bond"
case Name:
perm = "name"
default:
err = fmt.Errorf("Unknown permission flag %b", pf)
}
return
}
func PermStringToFlag(perm string) (pf PermFlag, err error) {
switch perm {
case "root":
pf = Root
case "send":
pf = Send
case "call":
pf = Call
case "create_contract":
pf = CreateContract
case "create_account":
pf = CreateAccount
case "bond":
pf = Bond
case "name":
pf = Name
default:
err = fmt.Errorf("Unknown permission %s", perm)
}
return
}