storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/numericlessfunc.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 toNumericLessThanFuncString(n name, key Key, value int) string {
    26  	return fmt.Sprintf("%v:%v:%v", n, key, value)
    27  }
    28  
    29  // numericLessThanFunc - 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 numericLessThanFunc 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 numericLessThanFunc) 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 numericLessThanFunc) key() Key {
    61  	return f.k
    62  }
    63  
    64  // name() - returns "NumericLessThan" condition name.
    65  func (f numericLessThanFunc) name() name {
    66  	return numericLessThan
    67  }
    68  
    69  func (f numericLessThanFunc) String() string {
    70  	return toNumericLessThanFuncString(numericLessThan, f.k, f.value)
    71  }
    72  
    73  // toMap - returns map representation of this function.
    74  func (f numericLessThanFunc) 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  // numericLessThanEqualsFunc - 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 numericLessThanEqualsFunc struct {
    93  	numericLessThanFunc
    94  }
    95  
    96  // evaluate() - evaluates to check whether value by Key in given values is NOT in
    97  // condition values.
    98  func (f numericLessThanEqualsFunc) 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 "NumericLessThanEquals" condition name.
   117  func (f numericLessThanEqualsFunc) name() name {
   118  	return numericLessThanEquals
   119  }
   120  
   121  func (f numericLessThanEqualsFunc) String() string {
   122  	return toNumericLessThanFuncString(numericLessThanEquals, f.numericLessThanFunc.k, f.numericLessThanFunc.value)
   123  }
   124  
   125  // newNumericLessThanFunc - returns new NumericLessThan function.
   126  func newNumericLessThanFunc(key Key, values ValueSet) (Function, error) {
   127  	v, err := valueToInt(numericLessThan, values)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	return NewNumericLessThanFunc(key, v)
   133  }
   134  
   135  // NewNumericLessThanFunc - returns new NumericLessThan function.
   136  func NewNumericLessThanFunc(key Key, value int) (Function, error) {
   137  	return &numericLessThanFunc{key, value}, nil
   138  }
   139  
   140  // newNumericLessThanEqualsFunc - returns new NumericLessThanEquals function.
   141  func newNumericLessThanEqualsFunc(key Key, values ValueSet) (Function, error) {
   142  	v, err := valueToInt(numericLessThanEquals, values)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	return NewNumericLessThanEqualsFunc(key, v)
   148  }
   149  
   150  // NewNumericLessThanEqualsFunc - returns new NumericLessThanEquals function.
   151  func NewNumericLessThanEqualsFunc(key Key, value int) (Function, error) {
   152  	return &numericLessThanEqualsFunc{numericLessThanFunc{key, value}}, nil
   153  }