storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/numericgreaterfunc.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2020 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package condition 18 19 import ( 20 "fmt" 21 "net/http" 22 "strconv" 23 ) 24 25 func toNumericGreaterThanFuncString(n name, key Key, value int) string { 26 return fmt.Sprintf("%v:%v:%v", n, key, value) 27 } 28 29 // numericGreaterThanFunc - String equals function. It checks whether value by Key in given 30 // values map is in condition values. 31 // For example, 32 // - if values = ["mybucket/foo"], at evaluate() it returns whether string 33 // in value map for Key is in values. 34 type numericGreaterThanFunc struct { 35 k Key 36 value int 37 } 38 39 // evaluate() - evaluates to check whether value by Key in given values is in 40 // condition values. 41 func (f numericGreaterThanFunc) evaluate(values map[string][]string) bool { 42 requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] 43 if !ok { 44 requestValue = values[f.k.Name()] 45 } 46 47 if len(requestValue) == 0 { 48 return false 49 } 50 51 rvInt, err := strconv.Atoi(requestValue[0]) 52 if err != nil { 53 return false 54 } 55 56 return rvInt > f.value 57 } 58 59 // key() - returns condition key which is used by this condition function. 60 func (f numericGreaterThanFunc) key() Key { 61 return f.k 62 } 63 64 // name() - returns "NumericGreaterThan" condition name. 65 func (f numericGreaterThanFunc) name() name { 66 return numericGreaterThan 67 } 68 69 func (f numericGreaterThanFunc) String() string { 70 return toNumericGreaterThanFuncString(numericGreaterThan, f.k, f.value) 71 } 72 73 // toMap - returns map representation of this function. 74 func (f numericGreaterThanFunc) toMap() map[Key]ValueSet { 75 if !f.k.IsValid() { 76 return nil 77 } 78 79 values := NewValueSet() 80 values.Add(NewIntValue(f.value)) 81 82 return map[Key]ValueSet{ 83 f.k: values, 84 } 85 } 86 87 // numericGreaterThanEqualsFunc - String not equals function. It checks whether value by Key in 88 // given values is NOT in condition values. 89 // For example, 90 // - if values = ["mybucket/foo"], at evaluate() it returns whether string 91 // in value map for Key is NOT in values. 92 type numericGreaterThanEqualsFunc struct { 93 numericGreaterThanFunc 94 } 95 96 // evaluate() - evaluates to check whether value by Key in given values is NOT in 97 // condition values. 98 func (f numericGreaterThanEqualsFunc) evaluate(values map[string][]string) bool { 99 requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] 100 if !ok { 101 requestValue = values[f.k.Name()] 102 } 103 104 if len(requestValue) == 0 { 105 return false 106 } 107 108 rvInt, err := strconv.Atoi(requestValue[0]) 109 if err != nil { 110 return false 111 } 112 113 return rvInt >= f.value 114 } 115 116 // name() - returns "NumericGreaterThanEquals" condition name. 117 func (f numericGreaterThanEqualsFunc) name() name { 118 return numericGreaterThanEquals 119 } 120 121 func (f numericGreaterThanEqualsFunc) String() string { 122 return toNumericGreaterThanFuncString(numericGreaterThanEquals, f.numericGreaterThanFunc.k, f.numericGreaterThanFunc.value) 123 } 124 125 // newNumericGreaterThanFunc - returns new NumericGreaterThan function. 126 func newNumericGreaterThanFunc(key Key, values ValueSet) (Function, error) { 127 v, err := valueToInt(numericGreaterThan, values) 128 if err != nil { 129 return nil, err 130 } 131 132 return NewNumericGreaterThanFunc(key, v) 133 } 134 135 // NewNumericGreaterThanFunc - returns new NumericGreaterThan function. 136 func NewNumericGreaterThanFunc(key Key, value int) (Function, error) { 137 return &numericGreaterThanFunc{key, value}, nil 138 } 139 140 // newNumericGreaterThanEqualsFunc - returns new NumericGreaterThanEquals function. 141 func newNumericGreaterThanEqualsFunc(key Key, values ValueSet) (Function, error) { 142 v, err := valueToInt(numericGreaterThanEquals, values) 143 if err != nil { 144 return nil, err 145 } 146 147 return NewNumericGreaterThanEqualsFunc(key, v) 148 } 149 150 // NewNumericGreaterThanEqualsFunc - returns new NumericGreaterThanEquals function. 151 func NewNumericGreaterThanEqualsFunc(key Key, value int) (Function, error) { 152 return &numericGreaterThanEqualsFunc{numericGreaterThanFunc{key, value}}, nil 153 }