|
|
@ -3,7 +3,6 @@ package types |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
. "github.com/tendermint/tendermint/common" |
|
|
|
"reflect" |
|
|
|
) |
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
@ -20,34 +19,34 @@ type PermFlag uint64 |
|
|
|
|
|
|
|
// Base permission references are like unix (the index is already bit shifted)
|
|
|
|
const ( |
|
|
|
Root PermFlag = 1 << iota // 1
|
|
|
|
Send // 2
|
|
|
|
Call // 4
|
|
|
|
CreateContract // 8
|
|
|
|
CreateAccount // 16
|
|
|
|
Bond // 32
|
|
|
|
Name // 64
|
|
|
|
|
|
|
|
DefaultBBPB = Send | Call | CreateContract | CreateAccount | Bond | Name |
|
|
|
|
|
|
|
// XXX: must be adjusted if base perms added/removed
|
|
|
|
NumBasePermissions uint = 7 |
|
|
|
TopBasePermission PermFlag = 1 << (NumBasePermissions - 1) |
|
|
|
AllBasePermissions PermFlag = TopBasePermission | (TopBasePermission - 1) |
|
|
|
|
|
|
|
AllSet PermFlag = AllBasePermissions | AllSNativePermissions |
|
|
|
Root PermFlag = 1 << iota // 1
|
|
|
|
Send // 2
|
|
|
|
Call // 4
|
|
|
|
CreateContract // 8
|
|
|
|
CreateAccount // 16
|
|
|
|
Bond // 32
|
|
|
|
Name // 64
|
|
|
|
NumBasePermissions uint = 7 // NOTE Adjust this too.
|
|
|
|
|
|
|
|
TopBasePermFlag PermFlag = 1 << (NumBasePermissions - 1) |
|
|
|
AllBasePermFlags PermFlag = TopBasePermFlag | (TopBasePermFlag - 1) |
|
|
|
AllPermFlags PermFlag = AllBasePermFlags | AllSNativePermFlags |
|
|
|
DefaultBasePermFlags PermFlag = Send | Call | CreateContract | CreateAccount | Bond | Name |
|
|
|
) |
|
|
|
|
|
|
|
// 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"` |
|
|
|
} |
|
|
|
var ( |
|
|
|
ZeroBasePermissions = BasePermissions{0, 0} |
|
|
|
ZeroAccountPermissions = AccountPermissions{ |
|
|
|
Base: ZeroBasePermissions, |
|
|
|
} |
|
|
|
DefaultAccountPermissions = AccountPermissions{ |
|
|
|
Base: BasePermissions{ |
|
|
|
Perms: DefaultBasePermFlags, |
|
|
|
SetBit: AllPermFlags, |
|
|
|
}, |
|
|
|
Roles: []string{}, |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
@ -60,10 +59,6 @@ type BasePermissions struct { |
|
|
|
SetBit PermFlag `json:"set"` |
|
|
|
} |
|
|
|
|
|
|
|
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
|
|
|
@ -108,32 +103,15 @@ func (p *BasePermissions) IsSet(ty PermFlag) bool { |
|
|
|
return p.SetBit&ty > 0 |
|
|
|
} |
|
|
|
|
|
|
|
func (p *BasePermissions) Copy() *BasePermissions { |
|
|
|
if p == nil { |
|
|
|
return nil |
|
|
|
} |
|
|
|
return &BasePermissions{ |
|
|
|
Perms: p.Perms, |
|
|
|
SetBit: p.SetBit, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (p *BasePermissions) String() string { |
|
|
|
func (p BasePermissions) String() string { |
|
|
|
return fmt.Sprintf("Base: %b; Set: %b", p.Perms, p.SetBit) |
|
|
|
} |
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type AccountPermissions struct { |
|
|
|
Base *BasePermissions `json:"base"` |
|
|
|
Roles []string `json:"roles"` |
|
|
|
} |
|
|
|
|
|
|
|
func NewAccountPermissions() *AccountPermissions { |
|
|
|
return &AccountPermissions{ |
|
|
|
Base: NewBasePermissions(), |
|
|
|
Roles: []string{}, |
|
|
|
} |
|
|
|
Base BasePermissions `json:"base"` |
|
|
|
Roles []string `json:"roles"` |
|
|
|
} |
|
|
|
|
|
|
|
// Returns true if the role is found
|
|
|
@ -175,75 +153,7 @@ func (aP *AccountPermissions) RmRole(role string) bool { |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
func (aP *AccountPermissions) Copy() *AccountPermissions { |
|
|
|
if aP == nil { |
|
|
|
return nil |
|
|
|
} |
|
|
|
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{}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------
|
|
|
|
// 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 { |
|
|
|