<commit_before>import React, { Component } from 'react'
import { connect } from 'react-redux'
import {
  Button,
  Card,
  CardHeader,
  CardBody,
  CardFooter,
  InputGroup,
  InputGroupAddon,
  Input,
  Form,
  Col,
  Label,
  FormGroup,
  FormText,
  Table,
  Row
} from 'reactstrap'
import EnableSecurity from './EnableSecurity'

export function fetchSecurityConfig () {
  fetch(`/security/config`, {
    credentials: 'include'
  })
    .then(response => response.json())
    .then(data => {
      this.setState(data)
    })
}

export function fetchSecurityUsers () {
  fetch(`/security/users`, {
    credentials: 'include'
  })
    .then(response => response.json())
    .then(data => {
      this.setState({ users: data })
    })
}

class Security extends Component {
  constructor (props) {
    super(props)
    this.state = {
      users: [],
      allow_readonly: false,
      expiration: ''
    }

    this.handleChange = this.handleChange.bind(this)
    this.handleAddUser = this.handleAddUser.bind(this)
    this.handleSaveConfig = this.handleSaveConfig.bind(this)
    this.fetchSecurityUsers = fetchSecurityUsers.bind(this)
    this.fetchSecurityConfig = fetchSecurityConfig.bind(this)
    this.handleCancel = this.handleCancel.bind(this)
    this.handleApply = this.handleApply.bind(this)
    this.handleUserChange = this.handleUserChange.bind(this)
    this.deleteUser = this.deleteUser.bind(this)
  }

  componentDidMount () {
    if (this.props.loginStatus.authenticationRequired) {
      this.fetchSecurityConfig()
      this.fetchSecurityUsers()
    }
  }

  handleChange (event) {
    const value =
      event.target.type === 'checkbox'
        ? event.target.checked
        : event.target.value
    this.setState({ [event.target.name]: value })
  }

  handleUserChange (event) {
    const value =
      event.target.type === 'checkbox'
        ? event.target.checked
        : event.target.value
    this.state.selectedUser[event.target.name] = value
    this.setState({
      selectedUser: this.state.selectedUser
    })
  }

  handleAddUser (event) {
    var newUser = {
      type: 'readonly',
      isNew: true
    }
    this.setState(
      {
        selectedUser: newUser,
        selectedIndex: this.state.users.length - 1
      },
      () => {
        this.refs['selectedUser'].scrollIntoView()
      }
    )
  }

  handleApply (event) {
    event.preventDefault()

    if (
      !this.state.selectedUser.userId ||
      this.state.selectedUser.userId.length == 0
    ) {
      alert('Please specify a User Id')
      return
    }

    if (this.state.selectedUser.password) {
      if (
        this.state.selectedUser.password !=
        this.<PASSWORD>.selectedUser.confirmPassword
      ) {
        alert('Passwords do not match')
        return
      }
    }

    var isNew = this.state.selectedUser.isNew

    var payload = {
      password: <PASSWORD>,
      type: this.state.selectedUser.type || 'readonly'
    }

    fetch(`/security/users/${this.state.selectedUser.userId}`, {
      method: isNew ? 'POST' : 'PUT',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(payload),
      credentials: 'include'
    })
      .then(response => response.text())
      .then(response => {
        this.setState({
          selectedUser: null,
          selectedIndex: -1
        })
        alert(response)
        this.fetchSecurityUsers()
      })
  }

  deleteUser (event) {
    fetch(`/security/users/${this.state.selectedUser.userId}`, {
      method: 'DELETE',
      headers: {
        'Content-Type': 'application/json'
      },
      credentials: 'include'
    })
      .then(response => response.text())
      .then(response => {
        this.setState({
          selectedUser: null,
          selectedIndex: -1
        })
        alert(response)
        this.fetchSecurityUsers()
      })
  }

  changePassword (userId) {
    console.log(userId)
  }

  handleSaveConfig () {
    var payload = {
      allow_readonly: this.state.allow_readonly,
      expiration: this.state.expiration
    }<commit_msg>chore: remove unused code<commit_after>import React, { Component } from 'react'
import { connect } from 'react-redux'
import {
  Button,
  Card,
  CardHeader,
  CardBody,
  CardFooter,
  InputGroup,
  InputGroupAddon,
  Input,
  Form,
  Col,
  Label,
  FormGroup,
  FormText,
  Table,
  Row
} from 'reactstrap'
import EnableSecurity from './EnableSecurity'

export function fetchSecurityConfig () {
  fetch(`/security/config`, {
    credentials: 'include'
  })
    .then(response => response.json())
    .then(data => {
      this.setState(data)
    })
}

export function fetchSecurityUsers () {
  fetch(`/security/users`, {
    credentials: 'include'
  })
    .then(response => response.json())
    .then(data => {
      this.setState({ users: data })
    })
}

class Security extends Component {
  constructor (props) {
    super(props)
    this.state = {
      users: [],
      allow_readonly: false,
      expiration: ''
    }

    this.handleChange = this.handleChange.bind(this)
    this.handleAddUser = this.handleAddUser.bind(this)
    this.handleSaveConfig = this.handleSaveConfig.bind(this)
    this.fetchSecurityUsers = fetchSecurityUsers.bind(this)
    this.fetchSecurityConfig = fetchSecurityConfig.bind(this)
    this.handleCancel = this.handleCancel.bind(this)
    this.handleApply = this.handleApply.bind(this)
    this.handleUserChange = this.handleUserChange.bind(this)
    this.deleteUser = this.deleteUser.bind(this)
  }

  componentDidMount () {
    if (this.props.loginStatus.authenticationRequired) {
      this.fetchSecurityConfig()
      this.fetchSecurityUsers()
    }
  }

  handleChange (event) {
    const value =
      event.target.type === 'checkbox'
        ? event.target.checked
        : event.target.value
    this.setState({ [event.target.name]: value })
  }

  handleUserChange (event) {
    const value =
      event.target.type === 'checkbox'
        ? event.target.checked
        : event.target.value
    this.state.selectedUser[event.target.name] = value
    this.setState({
      selectedUser: this.state.selectedUser
    })
  }

  handleAddUser (event) {
    var newUser = {
      type: 'readonly',
      isNew: true
    }
    this.setState(
      {
        selectedUser: newUser,
        selectedIndex: this.state.users.length - 1
      },
      () => {
        this.refs['selectedUser'].scrollIntoView()
      }
    )
  }

  handleApply (event) {
    event.preventDefault()

    if (
      !this.state.selectedUser.userId ||
      this.state.selectedUser.userId.length == 0
    ) {
      alert('Please specify a User Id')
      return
    }

    if (this.state.selectedUser.password) {
      if (
        this.state.selectedUser.password !=
        <PASSWORD>.selectedUser.confirmPassword
      ) {
        alert('Passwords do not match')
        return
      }
    }

    var isNew = this.state.selectedUser.isNew

    var payload = {
      password: <PASSWORD>,
      type: this.state.selectedUser.type || 'readonly'
    }

    fetch(`/security/users/${this.state.selectedUser.userId}`, {
      method: isNew ? 'POST' : 'PUT',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(payload),
      credentials: 'include'
    })
      .then(response => response.text())
      .then(response => {
        this.setState({
          selectedUser: null,
          selectedIndex: -1
        })
        alert(response)
        this.fetchSecurityUsers()
      })
  }

  deleteUser (event) {
    fetch(`/security/users/${this.state.selectedUser.userId}`, {
      method: 'DELETE',
      headers: {
        'Content-Type': 'application/json'
      },
      credentials: 'include'
    })
      .then(response => response.text())
      .then(response => {
        this.setState({
          selectedUser: null,
          selectedIndex: -1
        })
        alert(response)
        this.fetchSecurityUsers()
      })
  }

  handleSaveConfig () {
    var payload = {
      allow_readonly: this.state.allow_readonly,
      expiration: this.state.expiration
    }
{'fraction_non_alphanumeric': 0.12838503200393894, 'fraction_numerical': 0.0009847365829640572, 'mean_word_length': 2.379783693843594, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '641815', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2462, 'n_words': 424}