// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import React from "react"; import {Button, Popconfirm, Table} from "antd"; import * as Setting from "./Setting"; import * as LdapBackend from "./backend/LdapBackend"; import i18next from "i18next"; import {Link} from "react-router-dom"; class LdapSyncPage extends React.Component { constructor(props) { super(props); this.state = { ldapId: props.match.params.ldapId, organizationName: props.match.params.organizationName, ldap: null, users: [], existUuids: [], selectedUsers: [], }; } UNSAFE_componentWillMount() { this.getLdap(); } syncUsers() { const selectedUsers = this.state.selectedUsers; if (selectedUsers === null || selectedUsers.length === 0) { Setting.showMessage("error", "Please select al least 1 user first"); return; } LdapBackend.syncUsers(this.state.ldap.owner, this.state.ldap.id, selectedUsers) .then((res => { if (res.status === "ok") { const exist = res.data.exist; const failed = res.data.failed; const existUser = []; const failedUser = []; if ((!exist || exist.length === 0) && (!failed || failed.length === 0)) { Setting.goToLink(`/organizations/${this.state.ldap.owner}/users`); } else { if (exist && exist.length > 0) { exist.forEach(elem => { existUser.push(elem.cn); }); Setting.showMessage("error", `User [${existUser}] is already exist`); } if (failed && failed.length > 0) { failed.forEach(elem => { failedUser.push(elem.cn); }); Setting.showMessage("error", `Sync [${failedUser}] failed`); } } } else { Setting.showMessage("error", res.msg); } })); } getLdap() { LdapBackend.getLdap(this.state.organizationName, this.state.ldapId) .then((res) => { if (res.status === "ok") { this.setState({ ldap: res.data, }); this.getLdapUser(); } else { Setting.showMessage("error", res.msg); } }); } getLdapUser() { LdapBackend.getLdapUser(this.state.organizationName, this.state.ldapId) .then((res) => { if (res.status === "ok") { this.setState((prevState) => { prevState.users = res.data.users; prevState.existUuids = res.data2?.length > 0 ? res.data2.filter(uuid => uuid !== "") : []; return prevState; }); } else { Setting.showMessage("error", res.msg); } }); } buildValArray(data, key) { const valTypesArray = []; if (data !== null && data.length > 0) { data.forEach(elem => { const val = elem[key]; if (!valTypesArray.includes(val)) { valTypesArray.push(val); } }); } return valTypesArray; } buildFilter(data, key) { const filterArray = []; if (data !== null && data.length > 0) { const valArray = this.buildValArray(data, key); valArray.forEach(elem => { filterArray.push({ text: elem, value: elem, }); }); } return filterArray; } renderTable(users) { const columns = [ { title: i18next.t("ldap:CN"), dataIndex: "cn", key: "cn", sorter: (a, b) => a.cn.localeCompare(b.cn), render: (text, record, index) => { return (