// Copyright 2023 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, Card, Col, Input, Row, Select, Switch} from "antd"; import * as GroupBackend from "./backend/GroupBackend"; import * as OrganizationBackend from "./backend/OrganizationBackend"; import * as Setting from "./Setting"; import i18next from "i18next"; class GroupEditPage extends React.Component { constructor(props) { super(props); this.state = { classes: props, groupName: props.match.params.groupName, organizationName: props.organizationName !== undefined ? props.organizationName : props.match.params.organizationName, group: null, users: [], groups: [], organizations: [], mode: props.location.mode !== undefined ? props.location.mode : "edit", }; } UNSAFE_componentWillMount() { this.getGroup(); this.getGroups(this.state.organizationName); this.getOrganizations(); } getGroup() { GroupBackend.getGroup(this.state.organizationName, this.state.groupName) .then((res) => { if (res.status === "ok") { this.setState({ group: res.data, }); } }); } getGroups(organizationName) { GroupBackend.getGroups(organizationName) .then((res) => { if (res.status === "ok") { this.setState({ groups: res.data, }); } }); } getOrganizations() { OrganizationBackend.getOrganizationNames("admin") .then((res) => { if (res.status === "ok") { this.setState({ organizations: res.data || [], }); } }); } parseGroupField(key, value) { if ([""].includes(key)) { value = Setting.myParseInt(value); } return value; } updateGroupField(key, value) { value = this.parseGroupField(key, value); const group = this.state.group; group[key] = value; this.setState({ group: group, }); } getParentIdOptions() { const groups = this.state.groups.filter((group) => group.name !== this.state.group.name); const organization = this.state.organizations.find((organization) => organization.name === this.state.group.owner); if (organization !== undefined) { groups.push({name: organization.name, displayName: organization.displayName}); } return groups.map((group) => ({label: group.displayName, value: group.name})); } renderGroup() { return ( {this.state.mode === "add" ? i18next.t("group:New Group") : i18next.t("group:Edit Group")}     {this.state.mode === "add" ? : null} } style={(Setting.isMobile()) ? {margin: "5px"} : {}} type="inner" > {Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} : { this.updateGroupField("name", e.target.value); }} /> {Setting.getLabel(i18next.t("general:Display name"), i18next.t("general:Display name - Tooltip"))} : { this.updateGroupField("displayName", e.target.value); }} /> {Setting.getLabel(i18next.t("general:Type"), i18next.t("general:Type - Tooltip"))} : { this.updateGroupField("parentId", value); } )} /> {Setting.getLabel(i18next.t("general:Users"), i18next.t("general:Users - Tooltip"))} : { Setting.getTags(this.state.group.users, "users") } {Setting.getLabel(i18next.t("general:Is enabled"), i18next.t("general:Is enabled - Tooltip"))} : { this.updateGroupField("isEnabled", checked); }} /> ); } submitGroupEdit(exitAfterSave) { const group = Setting.deepCopy(this.state.group); group["isTopGroup"] = this.state.organizations.some((organization) => organization.name === group.parentId); GroupBackend.updateGroup(this.state.organizationName, this.state.groupName, group) .then((res) => { if (res.status === "ok") { Setting.showMessage("success", i18next.t("general:Successfully saved")); this.setState({ groupName: this.state.group.name, }); if (exitAfterSave) { const groupTreeUrl = sessionStorage.getItem("groupTreeUrl"); if (groupTreeUrl !== null) { sessionStorage.removeItem("groupTreeUrl"); this.props.history.push(groupTreeUrl); } else { this.props.history.push("/groups"); } } else { this.props.history.push(`/groups/${this.state.group.owner}/${this.state.group.name}`); } } else { Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`); this.updateGroupField("name", this.state.groupName); } }) .catch(error => { Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`); }); } deleteGroup() { GroupBackend.deleteGroup(this.state.group) .then((res) => { if (res.status === "ok") { const groupTreeUrl = sessionStorage.getItem("groupTreeUrl"); if (groupTreeUrl !== null) { sessionStorage.removeItem("groupTreeUrl"); this.props.history.push(groupTreeUrl); } else { this.props.history.push("/groups"); } } else { Setting.showMessage("error", `${i18next.t("general:Failed to delete")}: ${res.msg}`); } }) .catch(error => { Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`); }); } render() { return (
{ this.state.group !== null ? this.renderGroup() : null }
{this.state.mode === "add" ? : null}
); } } export default GroupEditPage;