import React from 'react'; import { FormBuilder, Icon, Input, Alert } from "../../components/"; import { Backend, Config } from "../../model/"; import { FormObjToJSON, notify, format, createFormBackend } from "../../helpers/"; import { t } from '../../locales/'; import "./backend.scss"; export class BackendPage extends React.Component { constructor(props){ super(props); this.state = { backend_enabled: [], backend_available: [], auth_available: ["LDAP", "SAML", "OpenID", "External"], auth_enabled: null, config: null }; } componentDidMount(){ Promise.all([ Backend.all(), Config.all() ]).then((data) => { let [backend, config] = data; this.setState({ backend_available: backend, backend_enabled: window.CONFIG["connections"].map((conn) => { return createFormBackend(backend, conn); }), config: config }); }); } onChange(e){ this.setState({refresh: Math.random()}); // refresh the screen to refresh the mutation // that have happenned down the stack let json = FormObjToJSON(this.state.config); json.connections = this.state.backend_enabled.map((backend) => { let data = FormObjToJSON(backend, (obj, key) => { if(obj[key].enabled === true){ obj[key] = obj[key].value || obj[key].default; } else { delete obj[key]; } }); const key = Object.keys(data)[0]; return data[key]; }); // persist config object in the backend this.props.isSaving(true); return Config.save(json, true, () => { this.props.isSaving(false); }, (err) => { notify.send(err && err.message || t('Oops'), 'error'); this.props.isSaving(false); }); } addBackend(backend_id){ this.setState({ backend_enabled: this.state.backend_enabled.concat( createFormBackend(this.state.backend_available, { type: backend_id, label: backend_id.toUpperCase() }) ) }, this.onChange.bind(this)); } removeBackend(n){ this.setState({ backend_enabled: this.state.backend_enabled.filter((_, i) => i !== n) }, this.onChange.bind(this)); } onClickAuthAvailable(auth){ this.setState({ auth_enabled: this.state.auth_enabled === auth ? null : auth }); } render(){ const update = (value, struct) => { struct.enabled = value; this.setState({refresh: Math.random()}); if(value === false){ struct.value = null; } return; }; const enable = (struct) => { if(typeof struct.value === "string"){ struct.enabled = true; return true; } return !!struct.enabled; }; const isActiveBackend = (backend_key) => { return this.state.backend_enabled .map((b) => Object.keys(b)[0]) .indexOf(backend_key) !== -1; }; const isActiveAuth = (auth_key) => { return auth_key === this.state.auth_enabled; }; return (