storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/dategreaterthanfunc.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 "time" 23 ) 24 25 func toDateGreaterThanFuncString(n name, key Key, value time.Time) string { 26 return fmt.Sprintf("%v:%v:%v", n, key, value.Format(time.RFC3339)) 27 } 28 29 // dateGreaterThanFunc - 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 dateGreaterThanFunc struct { 35 k Key 36 value time.Time 37 } 38 39 // evaluate() - evaluates to check whether value by Key in given values is in 40 // condition values. 41 func (f dateGreaterThanFunc) 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 t, err := time.Parse(time.RFC3339, requestValue[0]) 52 if err != nil { 53 return false 54 } 55 56 return t.After(f.value) 57 } 58 59 // key() - returns condition key which is used by this condition function. 60 func (f dateGreaterThanFunc) key() Key { 61 return f.k 62 } 63 64 // name() - returns "DateGreaterThan" condition name. 65 func (f dateGreaterThanFunc) name() name { 66 return dateGreaterThan 67 } 68 69 func (f dateGreaterThanFunc) String() string { 70 return toDateGreaterThanFuncString(dateGreaterThan, f.k, f.value) 71 } 72 73 // toMap - returns map representation of this function. 74 func (f dateGreaterThanFunc) toMap() map[Key]ValueSet { 75 if !f.k.IsValid() { 76 return nil 77 } 78 79 values := NewValueSet() 80 values.Add(NewStringValue(f.value.Format(time.RFC3339))) 81 82 return map[Key]ValueSet{ 83 f.k: values, 84 } 85 } 86 87 // dateNotEqualsFunc - 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 dateGreaterThanEqualsFunc struct { 93 dateGreaterThanFunc 94 } 95 96 // evaluate() - evaluates to check whether value by Key in given values is NOT in 97 // condition values. 98 func (f dateGreaterThanEqualsFunc) 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 t, err := time.Parse(time.RFC3339, requestValue[0]) 109 if err != nil { 110 return false 111 } 112 113 return t.After(f.value) || t.Equal(f.value) 114 } 115 116 // name() - returns "DateNotEquals" condition name. 117 func (f dateGreaterThanEqualsFunc) name() name { 118 return dateGreaterThanEquals 119 } 120 121 func (f dateGreaterThanEqualsFunc) String() string { 122 return toDateGreaterThanFuncString(dateNotEquals, f.dateGreaterThanFunc.k, f.dateGreaterThanFunc.value) 123 } 124 125 // newDateGreaterThanFunc - returns new DateGreaterThan function. 126 func newDateGreaterThanFunc(key Key, values ValueSet) (Function, error) { 127 v, err := valueToTime(dateGreaterThan, values) 128 if err != nil { 129 return nil, err 130 } 131 132 return NewDateGreaterThanFunc(key, v) 133 } 134 135 // NewDateGreaterThanFunc - returns new DateGreaterThan function. 136 func NewDateGreaterThanFunc(key Key, value time.Time) (Function, error) { 137 return &dateGreaterThanFunc{key, value}, nil 138 } 139 140 // newDateNotEqualsFunc - returns new DateNotEquals function. 141 func newDateGreaterThanEqualsFunc(key Key, values ValueSet) (Function, error) { 142 v, err := valueToTime(dateNotEquals, values) 143 if err != nil { 144 return nil, err 145 } 146 147 return NewDateGreaterThanEqualsFunc(key, v) 148 } 149 150 // NewDateGreaterThanEqualsFunc - returns new DateNotEquals function. 151 func NewDateGreaterThanEqualsFunc(key Key, value time.Time) (Function, error) { 152 return &dateGreaterThanEqualsFunc{dateGreaterThanFunc{key, value}}, nil 153 }