// 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, Card, Col, Input, InputNumber, Radio, Row, Select, Switch} from "antd";
import * as OrganizationBackend from "./backend/OrganizationBackend";
import * as ApplicationBackend from "./backend/ApplicationBackend";
import * as LdapBackend from "./backend/LdapBackend";
import * as Setting from "./Setting";
import * as Conf from "./Conf";
import * as Obfuscator from "./auth/Obfuscator";
import i18next from "i18next";
import {LinkOutlined} from "@ant-design/icons";
import LdapTable from "./table/LdapTable";
import AccountTable from "./table/AccountTable";
import ThemeEditor from "./common/theme/ThemeEditor";
import MfaTable from "./table/MfaTable";
import {NavItemTree} from "./common/NavItemTree";
const {Option} = Select;
class OrganizationEditPage extends React.Component {
constructor(props) {
super(props);
this.state = {
classes: props,
organizationName: props.match.params.organizationName,
organization: null,
applications: [],
ldaps: null,
mode: props.location.mode !== undefined ? props.location.mode : "edit",
};
}
UNSAFE_componentWillMount() {
this.getOrganization();
this.getApplications();
this.getLdaps();
}
getOrganization() {
OrganizationBackend.getOrganization("admin", this.state.organizationName)
.then((res) => {
if (res.status === "ok") {
const organization = res.data;
if (organization === null) {
this.props.history.push("/404");
return;
}
organization["enableDarkLogo"] = !!organization["logoDark"];
this.setState({
organization: organization,
});
} else {
Setting.showMessage("error", res.msg);
}
});
}
getApplications() {
ApplicationBackend.getApplicationsByOrganization("admin", this.state.organizationName)
.then((res) => {
if (res.status === "error") {
Setting.showMessage("error", res.msg);
return;
}
this.setState({
applications: res.data || [],
});
});
}
getLdaps() {
LdapBackend.getLdaps(this.state.organizationName)
.then(res => {
let resdata = [];
if (res.status === "ok") {
if (res.data !== null) {
resdata = res.data;
}
}
this.setState({
ldaps: resdata,
});
});
}
parseOrganizationField(key, value) {
// if ([].includes(key)) {
// value = Setting.myParseInt(value);
// }
return value;
}
updateOrganizationField(key, value) {
value = this.parseOrganizationField(key, value);
const organization = this.state.organization;
organization[key] = value;
this.setState({
organization: organization,
});
}
updatePasswordObfuscator(key, value) {
const organization = this.state.organization;
if (organization.passwordObfuscatorType === "") {
organization.passwordObfuscatorType = "Plain";
}
if (key === "type") {
organization.passwordObfuscatorType = value;
organization.passwordObfuscatorKey = Obfuscator.getRandomKeyForObfuscator(value);
} else if (key === "key") {
organization.passwordObfuscatorKey = value;
}
this.setState({
organization: organization,
});
}
renderOrganization() {
return (
{this.state.mode === "add" ? i18next.t("organization:New Organization") : i18next.t("organization:Edit Organization")}
{this.state.mode === "add" ? : null}
} style={(Setting.isMobile()) ? {margin: "5px"} : {}} type="inner">
{Setting.getLabel(i18next.t("general:Name"), i18next.t("general:Name - Tooltip"))} :
{
this.updateOrganizationField("name", e.target.value);
}} />
{Setting.getLabel(i18next.t("general:Display name"), i18next.t("general:Display name - Tooltip"))} :
{
this.updateOrganizationField("displayName", e.target.value);
}} />
{Setting.getLabel(i18next.t("general:Enable dark logo"), i18next.t("general:Enable dark logo - Tooltip"))} :
{
this.updateOrganizationField("enableDarkLogo", e);
if (!e) {
this.updateOrganizationField("logoDark", "");
}
}} />
{Setting.getLabel(i18next.t("general:Logo"), i18next.t("general:Logo - Tooltip"))} :
{Setting.getLabel(i18next.t("general:URL"), i18next.t("general:URL - Tooltip"))} :
} value={this.state.organization.logo} onChange={e => {
this.updateOrganizationField("logo", e.target.value);
}} />
{i18next.t("general:Preview")}:
{
!this.state.organization.enableDarkLogo ? null : (
{Setting.getLabel(i18next.t("general:Logo dark"), i18next.t("general:Logo dark - Tooltip"))} :
{Setting.getLabel(i18next.t("general:URL"), i18next.t("general:URL - Tooltip"))} :
} value={this.state.organization.logoDark} onChange={e => {
this.updateOrganizationField("logoDark", e.target.value);
}} />
{i18next.t("general:Preview")}:
)
}
{Setting.getLabel(i18next.t("general:Favicon"), i18next.t("general:Favicon - Tooltip"))} :
{Setting.getLabel(i18next.t("general:URL"), i18next.t("general:URL - Tooltip"))} :
} value={this.state.organization.favicon} onChange={e => {
this.updateOrganizationField("favicon", e.target.value);
}} />
{i18next.t("general:Preview")}:
{Setting.getLabel(i18next.t("organization:Website URL"), i18next.t("organization:Website URL - Tooltip"))} :
} value={this.state.organization.websiteUrl} onChange={e => {
this.updateOrganizationField("websiteUrl", e.target.value);
}} />
{Setting.getLabel(i18next.t("general:Password type"), i18next.t("general:Password type - Tooltip"))} :
{Setting.getLabel(i18next.t("general:Password salt"), i18next.t("general:Password salt - Tooltip"))} :
{
this.updateOrganizationField("passwordSalt", e.target.value);
}} />
{Setting.getLabel(i18next.t("general:Password complexity options"), i18next.t("general:Password complexity options - Tooltip"))} :
{Setting.getLabel(i18next.t("general:Password obfuscator"), i18next.t("general:Password obfuscator - Tooltip"))} :
{
(this.state.organization.passwordObfuscatorType === "Plain" || this.state.organization.passwordObfuscatorType === "") ? null : (
{Setting.getLabel(i18next.t("general:Password obf key"), i18next.t("general:Password obf key - Tooltip"))} :
{this.updatePasswordObfuscator("key", e.target.value);}} />
)
}
{Setting.getLabel(i18next.t("organization:Password expire days"), i18next.t("organization:Password expire days - Tooltip"))} :
{
this.updateOrganizationField("passwordExpireDays", value);
}} />
{Setting.getLabel(i18next.t("general:Supported country codes"), i18next.t("general:Supported country codes - Tooltip"))} :
{Setting.getLabel(i18next.t("general:Languages"), i18next.t("general:Languages - Tooltip"))} :
{Setting.getLabel(i18next.t("general:Default avatar"), i18next.t("general:Default avatar - Tooltip"))} :
{Setting.getLabel(i18next.t("general:URL"), i18next.t("general:URL - Tooltip"))} :
} value={this.state.organization.defaultAvatar} onChange={e => {
this.updateOrganizationField("defaultAvatar", e.target.value);
}} />
{i18next.t("general:Preview")}:
{Setting.getLabel(i18next.t("general:Default application"), i18next.t("general:Default application - Tooltip"))} :
{Setting.getLabel(i18next.t("organization:Tags"), i18next.t("organization:Tags - Tooltip"))} :
{Setting.getLabel(i18next.t("general:Master password"), i18next.t("general:Master password - Tooltip"))} :
{
this.updateOrganizationField("masterPassword", e.target.value);
}} />
{Setting.getLabel(i18next.t("general:Default password"), i18next.t("general:Default password - Tooltip"))} :
{
this.updateOrganizationField("defaultPassword", e.target.value);
}} />
{Setting.getLabel(i18next.t("general:Master verification code"), i18next.t("general:Master verification code - Tooltip"))} :
{
this.updateOrganizationField("masterVerificationCode", e.target.value);
}} />
{Setting.getLabel(i18next.t("general:IP whitelist"), i18next.t("general:IP whitelist - Tooltip"))} :
{
this.updateOrganizationField("ipWhitelist", e.target.value);
}} />
{Setting.getLabel(i18next.t("organization:Init score"), i18next.t("organization:Init score - Tooltip"))} :
{
this.updateOrganizationField("initScore", value);
}} />
{Setting.getLabel(i18next.t("organization:Soft deletion"), i18next.t("organization:Soft deletion - Tooltip"))} :
{
this.updateOrganizationField("enableSoftDeletion", checked);
}} />
{Setting.getLabel(i18next.t("organization:Is profile public"), i18next.t("organization:Is profile public - Tooltip"))} :
{
this.updateOrganizationField("isProfilePublic", checked);
}} />
{Setting.getLabel(i18next.t("organization:Use Email as username"), i18next.t("organization:Use Email as username - Tooltip"))} :
{
this.updateOrganizationField("useEmailAsUsername", checked);
}} />
{Setting.getLabel(i18next.t("general:Enable tour"), i18next.t("general:Enable tour - Tooltip"))} :
{
this.updateOrganizationField("enableTour", checked);
}} />
{Setting.getLabel(i18next.t("general:Navbar items"), i18next.t("general:Navbar items - Tooltip"))} :
{
this.updateOrganizationField("navItems", checked);
}}
/>
{Setting.getLabel(i18next.t("organization:Account items"), i18next.t("organization:Account items - Tooltip"))} :
{this.updateOrganizationField("accountItems", value);}}
/>
{Setting.getLabel(i18next.t("general:MFA items"), i18next.t("general:MFA items - Tooltip"))} :
{this.updateOrganizationField("mfaItems", value);}}
/>
{Setting.getLabel(i18next.t("theme:Theme"), i18next.t("theme:Theme - Tooltip"))} :
{
const {_, ...theme} = this.state.organization.themeData ?? {...Conf.ThemeDefault, isEnabled: false};
this.updateOrganizationField("themeData", {...theme, isEnabled: e.target.value});
}} >
{i18next.t("organization:Follow global theme")}
{i18next.t("theme:Customize theme")}
{
this.state.organization.themeData?.isEnabled ?
{
const {isEnabled} = this.state.organization.themeData ?? {...Conf.ThemeDefault, isEnabled: false};
this.updateOrganizationField("themeData", {...nextThemeData, isEnabled});
}} />
: null
}
{Setting.getLabel(i18next.t("general:LDAPs"), i18next.t("general:LDAPs - Tooltip"))} :
{
this.setState({ldaps: value});
}}
/>
);
}
submitOrganizationEdit(exitAfterSave) {
const organization = Setting.deepCopy(this.state.organization);
organization.accountItems = organization.accountItems?.filter(accountItem => accountItem.name !== "Please select an account item");
const passwordObfuscatorErrorMessage = Obfuscator.checkPasswordObfuscator(organization.passwordObfuscatorType, organization.passwordObfuscatorKey);
if (passwordObfuscatorErrorMessage.length > 0) {
Setting.showMessage("error", passwordObfuscatorErrorMessage);
return;
}
OrganizationBackend.updateOrganization(this.state.organization.owner, this.state.organizationName, organization)
.then((res) => {
if (res.status === "ok") {
Setting.showMessage("success", i18next.t("general:Successfully saved"));
if (this.props.account.organization.name === this.state.organizationName) {
this.props.onChangeTheme(Setting.getThemeData(this.state.organization));
}
this.setState({
organizationName: this.state.organization.name,
});
window.dispatchEvent(new Event("storageOrganizationsChanged"));
if (exitAfterSave) {
this.props.history.push("/organizations");
} else {
this.props.history.push(`/organizations/${this.state.organization.name}`);
}
} else {
Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`);
this.updateOrganizationField("name", this.state.organizationName);
}
})
.catch(error => {
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
});
}
deleteOrganization() {
OrganizationBackend.deleteOrganization(this.state.organization)
.then((res) => {
if (res.status === "ok") {
this.props.history.push("/organizations");
window.dispatchEvent(new Event("storageOrganizationsChanged"));
} 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.organization !== null ? this.renderOrganization() : null
}
{this.state.mode === "add" ? : null}
);
}
}
export default OrganizationEditPage;