storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/browser/app/js/buckets/PolicyInput.js (about)

     1  /*
     2   * MinIO Cloud Storage (C) 2018 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  import { READ_ONLY, WRITE_ONLY, READ_WRITE } from '../constants'
    18  
    19  import React from "react"
    20  import { connect } from "react-redux"
    21  import classnames from "classnames"
    22  import * as actionsBuckets from "./actions"
    23  import * as actionsAlert from "../alert/actions"
    24  import web from "../web"
    25  
    26  export class PolicyInput extends React.Component {
    27    componentDidMount() {
    28      const { currentBucket, fetchPolicies } = this.props
    29      fetchPolicies(currentBucket)
    30    }
    31  
    32    componentWillUnmount() {
    33      const { setPolicies } = this.props
    34      setPolicies([])
    35    }
    36  
    37    handlePolicySubmit(e) {
    38      e.preventDefault()
    39      const { currentBucket, fetchPolicies, showAlert } = this.props
    40      
    41      if (this.prefix.value === "*")
    42        this.prefix.value = ""
    43      
    44      let policyAlreadyExists = this.props.policies.some(
    45        elem => this.prefix.value === elem.prefix && this.policy.value === elem.policy
    46      )
    47      if (policyAlreadyExists) {
    48        showAlert("danger", "Policy for this prefix already exists.")
    49        return
    50      }
    51      
    52      web.
    53        SetBucketPolicy({
    54          bucketName: currentBucket,
    55          prefix: this.prefix.value,
    56          policy: this.policy.value
    57        })
    58        .then(() => {
    59          fetchPolicies(currentBucket)
    60          this.prefix.value = ''
    61        })
    62        .catch(e => showAlert("danger", e.message))
    63    }
    64  
    65    render() {
    66      return (
    67        <header className="pmb-list">
    68          <div className="pmbl-item">
    69            <input 
    70              type="text"
    71              ref={ prefix => this.prefix = prefix }
    72              className="form-control"
    73              placeholder="Prefix"
    74            />
    75          </div>
    76          <div className="pmbl-item">
    77            <select ref={ policy => this.policy = policy } className="form-control">
    78              <option value={ READ_ONLY }>
    79                Read Only
    80              </option>
    81              <option value={ WRITE_ONLY }>
    82                Write Only
    83              </option>
    84              <option value={ READ_WRITE }>
    85                Read and Write
    86              </option>
    87            </select>
    88          </div>
    89          <div className="pmbl-item">
    90            <button className="btn btn-block btn-primary" onClick={ this.handlePolicySubmit.bind(this) }>
    91              Add
    92            </button>
    93          </div>
    94        </header>
    95      )
    96    }
    97  }
    98  
    99  const mapStateToProps = state => {
   100    return {
   101      currentBucket: state.buckets.currentBucket,
   102      policies: state.buckets.policies
   103    }
   104  }
   105  
   106  const mapDispatchToProps = dispatch => {
   107    return {
   108      fetchPolicies: bucket => dispatch(actionsBuckets.fetchPolicies(bucket)),
   109      setPolicies: policies => dispatch(actionsBuckets.setPolicies(policies)),
   110      showAlert: (type, message) =>
   111        dispatch(actionsAlert.set({ type: type, message: message }))
   112    }
   113  }
   114  
   115  export default connect(mapStateToProps, mapDispatchToProps)(PolicyInput)