forked from hyperledger-archives/burrow
/
snatives.go
121 lines (108 loc) · 3.21 KB
/
snatives.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package permission
import (
"fmt"
"strings"
"github.com/hyperledger/burrow/crypto"
)
//---------------------------------------------------------------------------------------------------
// PermissionsTx.PermArgs interface and argument encoding
func (pa PermArgs) String() string {
body := make([]string, 0, 5)
body = append(body, fmt.Sprintf("PermFlag: %v", String(pa.Action)))
if pa.Target != nil {
body = append(body, fmt.Sprintf("Address: %s", *pa.Target))
}
if pa.Permission != nil {
body = append(body, fmt.Sprintf("Permission: %v", String(*pa.Permission)))
}
if pa.Role != nil {
body = append(body, fmt.Sprintf("Role: %s", *pa.Role))
}
if pa.Value != nil {
body = append(body, fmt.Sprintf("Value: %v", *pa.Value))
}
return fmt.Sprintf("PermArgs{%s}", strings.Join(body, ", "))
}
func (pa PermArgs) EnsureValid() error {
pf := pa.Action
// Address
if pa.Target == nil && pf != SetGlobal {
return fmt.Errorf("PermArgs for PermFlag %v requires Address to be provided but was nil", pf)
}
if pf == HasRole || pf == AddRole || pf == RemoveRole {
// Role
if pa.Role == nil {
return fmt.Errorf("PermArgs for PermFlag %v requires Role to be provided but was nil", pf)
}
// Permission
} else if pa.Permission == nil {
return fmt.Errorf("PermArgs for PermFlag %v requires Permission to be provided but was nil", pf)
// Value
} else if (pf == SetBase || pf == SetGlobal) && pa.Value == nil {
return fmt.Errorf("PermArgs for PermFlag %v requires Value to be provided but was nil", pf)
}
return nil
}
func HasBaseArgs(address crypto.Address, permFlag PermFlag) PermArgs {
return PermArgs{
Action: HasBase,
Target: &address,
Permission: &permFlag,
}
}
func SetBaseArgs(address crypto.Address, permFlag PermFlag, value bool) PermArgs {
return PermArgs{
Action: SetBase,
Target: &address,
Permission: &permFlag,
Value: &value,
}
}
func UnsetBaseArgs(address crypto.Address, permFlag PermFlag) PermArgs {
return PermArgs{
Action: UnsetBase,
Target: &address,
Permission: &permFlag,
}
}
func SetGlobalArgs(permFlag PermFlag, value bool) PermArgs {
return PermArgs{
Action: SetGlobal,
Permission: &permFlag,
Value: &value,
}
}
func HasRoleArgs(address crypto.Address, role string) PermArgs {
return PermArgs{
Action: HasRole,
Target: &address,
Role: &role,
}
}
func AddRoleArgs(address crypto.Address, role string) PermArgs {
return PermArgs{
Action: AddRole,
Target: &address,
Role: &role,
}
}
func RemoveRoleArgs(address crypto.Address, role string) PermArgs {
return PermArgs{
Action: RemoveRole,
Target: &address,
Role: &role,
}
}