// 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, Result, Row, Select, Spin, Switch} from 'antd'; import * as UserBackend from "./backend/UserBackend"; import * as OrganizationBackend from "./backend/OrganizationBackend"; import * as Setting from "./Setting"; import {LinkOutlined} from "@ant-design/icons"; import i18next from "i18next"; import CropperDiv from "./CropperDiv.js"; import * as ApplicationBackend from "./backend/ApplicationBackend"; import PasswordModal from "./PasswordModal"; import ResetModal from "./ResetModal"; import AffiliationSelect from "./common/AffiliationSelect"; import OAuthWidget from "./common/OAuthWidget"; import SamlWidget from "./common/SamlWidget"; import SelectRegionBox from "./SelectRegionBox"; import {Controlled as CodeMirror} from 'react-codemirror2'; import "codemirror/lib/codemirror.css"; require('codemirror/theme/material-darker.css'); require("codemirror/mode/javascript/javascript"); const { Option } = Select; class UserEditPage extends React.Component { constructor(props) { super(props); this.state = { classes: props, organizationName: props.organizationName !== undefined ? props.organizationName : props.match.params.organizationName, userName: props.userName !== undefined ? props.userName : props.match.params.userName, user: null, application: null, organizations: [], applications: [], mode: props.location.mode !== undefined ? props.location.mode : "edit", loading: true, }; } UNSAFE_componentWillMount() { this.getUser(); this.getOrganizations(); this.getApplicationsByOrganization(this.state.organizationName); this.getUserApplication(); } getUser() { UserBackend.getUser(this.state.organizationName, this.state.userName) .then((data) => { if (data.status === null || data.status !== "error") { this.setState({ user: data, }); } this.setState({ loading: false, }); }); } getOrganizations() { OrganizationBackend.getOrganizations("admin") .then((res) => { this.setState({ organizations: (res.msg === undefined) ? res : [], }); }); } getApplicationsByOrganization(organizationName) { ApplicationBackend.getApplicationsByOrganization("admin", organizationName) .then((res) => { this.setState({ applications: (res.msg === undefined) ? res : [], }); }); } getUserApplication() { ApplicationBackend.getUserApplication(this.state.organizationName, this.state.userName) .then((application) => { this.setState({ application: application, }); }); } parseUserField(key, value) { // if ([].includes(key)) { // value = Setting.myParseInt(value); // } return value; } updateUserField(key, value) { value = this.parseUserField(key, value); let user = this.state.user; user[key] = value; this.setState({ user: user, }); } unlinked() { this.getUser(); } isSelfOrAdmin() { return (this.state.user.id === this.props.account?.id) || Setting.isAdminUser(this.props.account); } renderAccountItem(accountItem) { if (!accountItem.visible) { return null; } const isSelf = this.state.user.id === this.props.account; const isAdmin = Setting.isAdminUser(this.props.account); if (accountItem.viewRule === "Self") { if (!isSelf && !isAdmin) { return null; } } else if (accountItem.viewRule === "Admin") { if (!isAdmin) { return null; } } let disabled = false; if (accountItem.modifyRule === "Self") { if (!isSelf && !isAdmin) { disabled = true; } } else if (accountItem.modifyRule === "Admin") { if (!isAdmin) { disabled = true; } } else if (accountItem.modifyRule === "Immutable") { disabled = true; } if (accountItem.name === "Organization") { return ( {Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} : {this.updateUserField('owner', value);})}> { this.state.organizations.map((organization, index) => {organization.name}) } ) } else if (accountItem.name === "ID") { return ( {Setting.getLabel("ID", i18next.t("general:ID - Tooltip"))} : ) } else if (accountItem.name === "Name") { return ( {Setting.getLabel(i18next.t("general:Name"), i18next.t("general:Name - Tooltip"))} : { this.updateUserField('name', e.target.value); }} /> ) } else if (accountItem.name === "Display name") { return ( {Setting.getLabel(i18next.t("general:Display name"), i18next.t("general:Display name - Tooltip"))} : { this.updateUserField('displayName', e.target.value); }} /> ) } else if (accountItem.name === "Avatar") { return ( {Setting.getLabel(i18next.t("general:Avatar"), i18next.t("general:Avatar - Tooltip"))} : {i18next.t("general:URL")}: } value={this.state.user.avatar} onChange={e => { this.updateUserField('avatar', e.target.value); }} /> {i18next.t("general:Preview")}: ) } else if (accountItem.name === "User type") { return ( {Setting.getLabel(i18next.t("general:User type"), i18next.t("general:User type - Tooltip"))} : {this.updateUserField('type', value);})}> { ['normal-user'] .map((item, index) => {item}) } ) } else if (accountItem.name === "Password") { return ( {Setting.getLabel(i18next.t("general:Password"), i18next.t("general:Password - Tooltip"))} : ) } else if (accountItem.name === "Email") { return ( {Setting.getLabel(i18next.t("general:Email"), i18next.t("general:Email - Tooltip"))} : { this.updateUserField('email', e.target.value); }} /> { this.state.user.id === this.props.account?.id ? () : null} ) } else if (accountItem.name === "Phone") { return ( {Setting.getLabel(i18next.t("general:Phone"), i18next.t("general:Phone - Tooltip"))} : { this.updateUserField('phone', e.target.value); }}/> { this.state.user.id === this.props.account?.id ? () : null} ) } else if (accountItem.name === "Country/Region") { return ( {Setting.getLabel(i18next.t("user:Country/Region"), i18next.t("user:Country/Region - Tooltip"))} : { this.updateUserField("region", value); }} /> ) } else if (accountItem.name === "Location") { return ( {Setting.getLabel(i18next.t("user:Location"), i18next.t("user:Location - Tooltip"))} : { this.updateUserField('location', e.target.value); }} /> ) } else if (accountItem.name === "Affiliation") { return ( (this.state.application === null || this.state.user === null) ? null : ( { return this.updateUserField(key, value)}} /> ) ) } else if (accountItem.name === "Title") { return ( {Setting.getLabel(i18next.t("user:Title"), i18next.t("user:Title - Tooltip"))} : { this.updateUserField('title', e.target.value); }} /> ) } else if (accountItem.name === "Homepage") { return ( {Setting.getLabel(i18next.t("user:Homepage"), i18next.t("user:Homepage - Tooltip"))} : { this.updateUserField('homepage', e.target.value); }} /> ) } else if (accountItem.name === "Bio") { return ( {Setting.getLabel(i18next.t("user:Bio"), i18next.t("user:Bio - Tooltip"))} : { this.updateUserField('bio', e.target.value); }} /> ) } else if (accountItem.name === "Tag") { return ( {Setting.getLabel(i18next.t("user:Tag"), i18next.t("user:Tag - Tooltip"))} : { this.state.application?.organizationObj.tags?.length > 0 ? ( {this.updateUserField('tag', value);})}> { this.state.application.organizationObj.tags?.map((tag, index) => { const tokens = tag.split("|"); const value = tokens[0]; const displayValue = Setting.getLanguage() !== "zh" ? tokens[0] : tokens[1]; return {displayValue} }) } ) : ( { this.updateUserField('tag', e.target.value); }} /> ) } ) } else if (accountItem.name === "Signup application") { return ( {Setting.getLabel(i18next.t("general:Signup application"), i18next.t("general:Signup application - Tooltip"))} : {this.updateUserField('signupApplication', value);})}> { this.state.applications.map((application, index) => {application.name}) } ) } else if (accountItem.name === "3rd-party logins") { return ( !this.isSelfOrAdmin() ? null : ( {Setting.getLabel(i18next.t("user:3rd-party logins"), i18next.t("user:3rd-party logins - Tooltip"))} : { (this.state.application === null || this.state.user === null) ? null : ( this.state.application?.providers.filter(providerItem => Setting.isProviderVisible(providerItem)).map((providerItem, index) => (providerItem.provider.category === "OAuth") ? ( { return this.unlinked()}} /> ) : ( { return this.unlinked()}} /> ) ) ) } ) ) } else if (accountItem.name === "Properties") { return ( {i18next.t("user:Properties")}: ) } else if (accountItem.name === "Is admin") { return ( {Setting.getLabel(i18next.t("user:Is admin"), i18next.t("user:Is admin - Tooltip"))} : { this.updateUserField('isAdmin', checked); }} /> ) } else if (accountItem.name === "Is global admin") { return ( {Setting.getLabel(i18next.t("user:Is global admin"), i18next.t("user:Is global admin - Tooltip"))} : { this.updateUserField('isGlobalAdmin', checked); }} /> ) } else if (accountItem.name === "Is forbidden") { return ( {Setting.getLabel(i18next.t("user:Is forbidden"), i18next.t("user:Is forbidden - Tooltip"))} : { this.updateUserField('isForbidden', checked); }} /> ) } else if (accountItem.name === "Is deleted") { return ( {Setting.getLabel(i18next.t("user:Is deleted"), i18next.t("user:Is deleted - Tooltip"))} : { this.updateUserField('isDeleted', checked); }} /> ) } } renderUser() { return ( {this.state.mode === "add" ? i18next.t("user:New User") : i18next.t("user:Edit User")} this.submitUserEdit(false)}>{i18next.t("general:Save")} this.submitUserEdit(true)}>{i18next.t("general:Save & Exit")} {this.state.mode === "add" ? this.deleteUser()}>{i18next.t("general:Cancel")} : null} } style={(Setting.isMobile())? {margin: '5px'}:{}} type="inner"> { this.state.application?.organizationObj.accountItems?.map(accountItem => this.renderAccountItem(accountItem)) } ) } submitUserEdit(willExist) { let user = Setting.deepCopy(this.state.user); UserBackend.updateUser(this.state.organizationName, this.state.userName, user) .then((res) => { if (res.msg === "") { Setting.showMessage("success", `Successfully saved`); this.setState({ organizationName: this.state.user.owner, userName: this.state.user.name, }); if (this.props.history !== undefined) { if (willExist) { this.props.history.push(`/users`); } else { this.props.history.push(`/users/${this.state.user.owner}/${this.state.user.name}`); } } } else { Setting.showMessage("error", res.msg); this.updateUserField('owner', this.state.organizationName); this.updateUserField('name', this.state.userName); } }) .catch(error => { Setting.showMessage("error", `Failed to connect to server: ${error}`); }); } deleteUser() { UserBackend.deleteUser(this.state.user) .then(() => { this.props.history.push(`/users`); }) .catch(error => { Setting.showMessage("error", `User failed to delete: ${error}`); }); } render() { return ( { this.state.loading ? : ( this.state.user !== null ? this.renderUser() : {i18next.t("general:Back Home")}} /> ) } { this.state.user === null ? null : this.submitUserEdit(false)}>{i18next.t("general:Save")} this.submitUserEdit(true)}>{i18next.t("general:Save & Exit")} {this.state.mode === "add" ? this.deleteUser()}>{i18next.t("general:Cancel")} : null} } ); } } export default UserEditPage;