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