2018-08-06 01:18:54 -07:00
|
|
|
package types
|
|
|
|
|
2018-11-07 04:36:42 -08:00
|
|
|
import (
|
2018-08-06 01:18:54 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-11-07 04:36:42 -08:00
|
|
|
"github.com/hashicorp/golang-lru"
|
2018-08-06 01:18:54 -07:00
|
|
|
)
|
|
|
|
|
2018-08-12 19:00:35 -07:00
|
|
|
type AccessType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
FullAccess AccessType = iota
|
|
|
|
ReadOnly
|
|
|
|
Transact
|
|
|
|
ContractDeploy
|
|
|
|
)
|
|
|
|
|
2018-08-06 01:18:54 -07:00
|
|
|
type PermStruct struct {
|
2018-11-07 04:36:42 -08:00
|
|
|
AcctId common.Address
|
2018-08-12 19:00:35 -07:00
|
|
|
AcctAccess AccessType
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
2018-09-27 04:24:38 -07:00
|
|
|
type OrgStruct struct {
|
|
|
|
OrgId string
|
2018-11-07 04:36:42 -08:00
|
|
|
Keys []string
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
2018-08-06 01:18:54 -07:00
|
|
|
|
2018-11-07 04:36:42 -08:00
|
|
|
var AcctMap, AcctMapErr = lru.NewARC(100)
|
2018-09-27 04:24:38 -07:00
|
|
|
|
2018-11-07 04:36:42 -08:00
|
|
|
var OrgKeyMap, OrgKeyMapErr = lru.NewARC(100)
|
2018-08-06 01:18:54 -07:00
|
|
|
|
2018-11-07 04:36:42 -08:00
|
|
|
func AddAccountAccess(acctId common.Address, access uint8) {
|
|
|
|
AcctMap.Add(acctId, &PermStruct{AcctId: acctId, AcctAccess: AccessType(access)})
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
|
|
|
|
2018-08-12 19:00:35 -07:00
|
|
|
func GetAcctAccess(acctId common.Address) AccessType {
|
2018-11-07 04:36:42 -08:00
|
|
|
if AcctMap.Len() != 0 {
|
|
|
|
if val, ok := AcctMap.Get(acctId); ok {
|
|
|
|
vo := val.(*PermStruct)
|
|
|
|
return vo.AcctAccess
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-07 04:50:49 -08:00
|
|
|
if AcctMap.Len() == 0 {
|
2018-11-02 03:42:14 -07:00
|
|
|
return FullAccess
|
|
|
|
} else {
|
|
|
|
return ReadOnly
|
|
|
|
}
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
2018-09-27 04:24:38 -07:00
|
|
|
|
2018-11-07 04:36:42 -08:00
|
|
|
func AddOrgKey(orgId string, key string) {
|
|
|
|
if OrgKeyMap.Len() != 0 {
|
|
|
|
if val, ok := OrgKeyMap.Get(orgId); ok {
|
2018-10-02 01:28:55 -07:00
|
|
|
// Org record exists. Append the key only
|
2018-11-07 04:36:42 -08:00
|
|
|
vo := val.(*OrgStruct)
|
|
|
|
vo.Keys = append(vo.Keys, key)
|
2018-10-02 01:28:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 04:36:42 -08:00
|
|
|
OrgKeyMap.Add(orgId, &OrgStruct{OrgId: orgId, Keys: []string{key}})
|
2018-10-02 01:28:55 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 04:36:42 -08:00
|
|
|
func DeleteOrgKey(orgId string, key string) {
|
|
|
|
if val, ok := OrgKeyMap.Get(orgId); ok {
|
|
|
|
vo := val.(*OrgStruct)
|
|
|
|
for i, keyVal := range vo.Keys {
|
|
|
|
if keyVal == key {
|
|
|
|
vo.Keys = append(vo.Keys[:i], vo.Keys[i+1:]...)
|
|
|
|
break
|
2018-10-02 01:28:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 04:36:42 -08:00
|
|
|
func ResolvePrivateForKeys(orgId string) []string {
|
2018-09-27 04:24:38 -07:00
|
|
|
var keys []string
|
2018-11-07 04:36:42 -08:00
|
|
|
if val, ok := OrgKeyMap.Get(orgId); ok {
|
|
|
|
vo := val.(*OrgStruct)
|
|
|
|
if len(vo.Keys) > 0 {
|
|
|
|
keys = vo.Keys
|
|
|
|
} else {
|
|
|
|
keys = append(keys, orgId)
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
2018-11-07 04:36:42 -08:00
|
|
|
return keys
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
|
|
|
keys = append(keys, orgId)
|
|
|
|
return keys
|
|
|
|
}
|