// 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, List, Result, Row, Select, Space, Spin, Switch, Tag} from "antd"; import {withRouter} from "react-router-dom"; import {TotpMfaType} from "./auth/MfaSetupPage"; import * as GroupBackend from "./backend/GroupBackend"; import * as UserBackend from "./backend/UserBackend"; import * as OrganizationBackend from "./backend/OrganizationBackend"; import EnableMfaModal from "./common/modal/EnableMfaModal"; import * as Setting from "./Setting"; import i18next from "i18next"; import CropperDivModal from "./common/modal/CropperDivModal.js"; import * as ApplicationBackend from "./backend/ApplicationBackend"; import PasswordModal from "./common/modal/PasswordModal"; import ResetModal from "./common/modal/ResetModal"; import AffiliationSelect from "./common/select/AffiliationSelect"; import OAuthWidget from "./common/OAuthWidget"; import SamlWidget from "./common/SamlWidget"; import RegionSelect from "./common/select/RegionSelect"; import WebAuthnCredentialTable from "./table/WebauthnCredentialTable"; import ManagedAccountTable from "./table/ManagedAccountTable"; import PropertyTable from "./table/propertyTable"; import {CountryCodeSelect} from "./common/select/CountryCodeSelect"; import PopconfirmModal from "./common/modal/PopconfirmModal"; import {DeleteMfa} from "./backend/MfaBackend"; import {CheckCircleOutlined, HolderOutlined, UsergroupAddOutlined} from "@ant-design/icons"; import * as MfaBackend from "./backend/MfaBackend"; import AccountAvatar from "./account/AccountAvatar"; 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, groups: null, organizations: [], applications: [], mode: props.location.mode !== undefined ? props.location.mode : "edit", loading: true, returnUrl: null, idCardInfo: ["ID card front", "ID card back", "ID card with person"], }; } UNSAFE_componentWillMount() { this.getUser(); this.getOrganizations(); this.getApplicationsByOrganization(this.state.organizationName); this.getUserApplication(); this.setReturnUrl(); } componentDidUpdate(prevProps, prevState, snapshot) { if (prevState.application !== this.state.application) { this.getGroups(this.state.organizationName); } } getUser() { UserBackend.getUser(this.state.organizationName, this.state.userName) .then((res) => { if (res.data === null) { this.props.history.push("/404"); return; } if (res.status === "error") { Setting.showMessage("error", res.msg); return; } this.setState({ user: res.data, multiFactorAuths: res.data?.multiFactorAuths ?? [], loading: false, }); }); } addUserKeys() { UserBackend.addUserKeys(this.state.user) .then((res) => { if (res.status === "ok") { this.getUser(); } else { Setting.showMessage("error", res.msg); } }); } getOrganizations() { OrganizationBackend.getOrganizations("admin") .then((res) => { this.setState({ organizations: res.data || [], }); }); } getApplicationsByOrganization(organizationName) { ApplicationBackend.getApplicationsByOrganization("admin", organizationName) .then((res) => { this.setState({ applications: res.data || [], }); }); } getUserApplication() { ApplicationBackend.getUserApplication(this.state.organizationName, this.state.userName) .then((res) => { if (res.status === "error") { Setting.showMessage("error", res.msg); return; } this.setState({ application: res.data, }); }); } getUserOrganization() { if (this.state.user === null || this.state.organizations.length === 0) { return null; } return this.state.organizations.filter(organization => organization.name === this.state.user.owner)[0]; } isGroupsVisible() { const organization = this.getUserOrganization(); if (!organization) { return false; } else { return organization.accountItems?.some((item) => item.name === "Groups" && item.visible); } } getGroups(organizationName) { if (this.isGroupsVisible()) { GroupBackend.getGroups(organizationName) .then((res) => { if (res.status === "ok") { this.setState({ groups: res.data, }); } }); } } setReturnUrl() { const searchParams = new URLSearchParams(this.props.location.search); const returnUrl = searchParams.get("returnUrl"); if (returnUrl !== null) { this.setState({ returnUrl: returnUrl, }); } } parseUserField(key, value) { if (["score", "karma", "ranking"].includes(key)) { value = Setting.myParseInt(value); } return value; } updateUserField(key, value) { value = this.parseUserField(key, value); const user = this.state.user; user[key] = value; this.setState({ user: user, }); } unlinked() { this.getUser(); } isSelf() { return (this.state.user.id === this.props.account?.id); } isSelfOrAdmin() { return this.isSelf() || Setting.isAdminUser(this.props.account); } getCountryCode() { return this.props.account.countryCode; } deleteMfa = () => { this.setState({ RemoveMfaLoading: true, }); DeleteMfa({ owner: this.state.user.owner, name: this.state.user.name, }).then((res) => { if (res.status === "ok") { Setting.showMessage("success", i18next.t("general:Successfully deleted")); this.setState({ multiFactorAuths: res.data, }); } else { Setting.showMessage("error", i18next.t("general:Failed to delete")); } }).finally(() => { this.setState({ RemoveMfaLoading: false, }); }); }; renderAccountItem(accountItem) { if (!accountItem.visible) { return null; } const isAdmin = Setting.isAdminUser(this.props.account); if (accountItem.viewRule === "Self") { if (!this.isSelfOrAdmin()) { return null; } } else if (accountItem.viewRule === "Admin") { if (!isAdmin) { return null; } } let disabled = false; if (accountItem.modifyRule === "Self") { if (!this.isSelfOrAdmin()) { disabled = true; } } else if (accountItem.modifyRule === "Admin") { if (!isAdmin) { disabled = true; } } else if (accountItem.modifyRule === "Immutable") { disabled = true; } if (accountItem.name === "Organization" || accountItem.name === "Name") { if (this.state.user.owner === "built-in" && this.state.user.name === "admin") { disabled = true; } } if (accountItem.name === "ID card info" || accountItem.name === "ID card") { if (this.state.user.properties?.isIdCardVerified === "true") { disabled = true; } } let isKeysGenerated = false; if (this.state.user.accessKey !== "" && this.state.user.accessKey !== "") { isKeysGenerated = true; } if (accountItem.name === "Organization") { return ( {Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} : ); } else if (accountItem.name === "Groups") { return ( {Setting.getLabel(i18next.t("general:Groups"), i18next.t("general:Groups - Tooltip"))} : ); } 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:Preview")}: {this.renderImage(this.state.user.avatar, i18next.t("user:Upload a photo"), i18next.t("user:Set new profile picture"), "avatar", false)} ); } else if (accountItem.name === "User type") { return ( {Setting.getLabel(i18next.t("general:User type"), i18next.t("general:User type - Tooltip"))} : { this.updateUserField("email", e.target.value); }} /> {/* backend auto get the current user, so admin can not edit. Just self can reset*/} {this.isSelf() ? : null} ); } else if (accountItem.name === "Phone") { return ( {Setting.getLabel(i18next.t("general:Phone"), i18next.t("general:Phone - Tooltip"))} : { this.updateUserField("countryCode", value); }} countryCodes={this.getUserOrganization()?.countryCodes} /> { this.updateUserField("phone", e.target.value); }} /> {this.isSelf() ? () : 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 === "Address") { return ( {Setting.getLabel(i18next.t("user:Address"), i18next.t("user:Address - Tooltip"))} : { this.updateUserField("address", 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 === "ID card type") { return ( {Setting.getLabel(i18next.t("user:ID card type"), i18next.t("user:ID card type - Tooltip"))} : { this.updateUserField("idCardType", e.target.value); }} /> ); } else if (accountItem.name === "ID card") { return ( {Setting.getLabel(i18next.t("user:ID card"), i18next.t("user:ID card - Tooltip"))} : { this.updateUserField("idCard", e.target.value); }} /> ); } else if (accountItem.name === "ID card info") { return ( {Setting.getLabel(i18next.t("user:ID card info"), i18next.t("user:ID card info - Tooltip"))} : {i18next.t("general:Preview")}: { [ {name: "ID card front", value: "idCardFront"}, {name: "ID card back", value: "idCardBack"}, {name: "ID card with person", value: "idCardWithPerson"}, ].map((entry) => { return this.renderImage(this.state.user.properties === null ? "" : (this.state.user.properties[entry.value] || ""), this.getIdCardType(entry.name), this.getIdCardText(entry.name), entry.value, disabled); }) } ); } 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.getUserOrganization()?.tags?.length > 0 ? ( { this.updateUserField("tag", e.target.value); }} /> ) } ); } else if (accountItem.name === "Language") { return ( {Setting.getLabel(i18next.t("user:Language"), i18next.t("user:Language - Tooltip"))} : { this.updateUserField("language", e.target.value); }} /> ); } else if (accountItem.name === "Gender") { return ( {Setting.getLabel(i18next.t("user:Gender"), i18next.t("user:Gender - Tooltip"))} : { this.updateUserField("gender", e.target.value); }} /> ); } else if (accountItem.name === "Birthday") { return ( {Setting.getLabel(i18next.t("user:Birthday"), i18next.t("user:Birthday - Tooltip"))} : { this.updateUserField("birthday", e.target.value); }} /> ); } else if (accountItem.name === "Education") { return ( {Setting.getLabel(i18next.t("user:Education"), i18next.t("user:Education - Tooltip"))} : { this.updateUserField("education", e.target.value); }} /> ); } else if (accountItem.name === "Score") { return ( {Setting.getLabel(i18next.t("user:Score"), i18next.t("user:Score - Tooltip"))} : { this.updateUserField("score", value); }} /> ); } else if (accountItem.name === "Karma") { return ( {Setting.getLabel(i18next.t("user:Karma"), i18next.t("user:Karma - Tooltip"))} : { this.updateUserField("karma", value); }} /> ); } else if (accountItem.name === "Ranking") { return ( {Setting.getLabel(i18next.t("user:Ranking"), i18next.t("user:Ranking - Tooltip"))} : { this.updateUserField("ranking", value); }} /> ); } else if (accountItem.name === "Signup application") { return ( {Setting.getLabel(i18next.t("general:Signup application"), i18next.t("general:Signup application - Tooltip"))} : {Setting.getLabel(i18next.t("general:Access secret"), i18next.t("general:Access secret - Tooltip"))} : ); } else if (accountItem.name === "Roles") { return ( {Setting.getLabel(i18next.t("general:Roles"), i18next.t("general:Roles - Tooltip"))} : { Setting.getTags(this.state.user.roles.map(role => role.name)) } ); } else if (accountItem.name === "Permissions") { return ( {Setting.getLabel(i18next.t("general:Permissions"), i18next.t("general:Permissions - Tooltip"))} : { Setting.getTags(this.state.user.permissions.map(permission => permission.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) => (providerItem.provider.category === "OAuth" || providerItem.provider.category === "Web3") ? ( {return this.unlinked();}} /> ) : ( {return this.unlinked();}} /> ) ) ) }
) ); } else if (accountItem.name === "Properties") { return ( {Setting.getLabel(i18next.t("user:Properties"), i18next.t("user:Properties - Tooltip"))} : {this.updateUserField("properties", value);}} /> ); } 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 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); }} /> ); } else if (accountItem.name === "Multi-factor authentication") { return ( !this.isSelfOrAdmin() ? null : ( {Setting.getLabel(i18next.t("mfa:Multi-factor authentication"), i18next.t("mfa:Multi-factor authentication - Tooltip "))} : mfaProps.enabled) ? this.deleteMfa()} /> : null }> ( {i18next.t("general:Type")}: {item.mfaType} {item.secret} {item.enabled ? ( {item.enabled ? } color="success"> {i18next.t("general:Enabled")} : null } {item.isPreferred ? } color="blue" style={{marginRight: 20}} > {i18next.t("mfa:preferred")} : } ) : {item.mfaType !== TotpMfaType && Setting.isAdminUser(this.props.account) && window.location.href.indexOf("/users") !== -1 ? { this.getUser(); }} /> : null} } )} /> ) ); } else if (accountItem.name === "WebAuthn credentials") { return ( {Setting.getLabel(i18next.t("user:WebAuthn credentials"), i18next.t("user:WebAuthn credentials"))} : {this.updateUserField("webauthnCredentials", table);}} refresh={this.getUser.bind(this)} /> ); } else if (accountItem.name === "Managed accounts") { return ( {Setting.getLabel(i18next.t("user:Managed accounts"), i18next.t("user:Managed accounts"))} : {this.updateUserField("managedAccounts", table);}} applications={this.state.applications} /> ); } } renderImage(imgUrl, title, set, tag, disabled) { return ( { imgUrl ?
:
+
{`Upload ${title}...`}
} organization.name === this.state.organizationName)} /> ); } renderUser() { return ( {this.state.mode === "add" ? i18next.t("user:New User") : i18next.t("user:Edit User")}     {this.state.mode === "add" ? : null} } style={(Setting.isMobile()) ? {margin: "5px"} : {}} type="inner"> { this.getUserOrganization()?.accountItems?.map(accountItem => { return ( { this.renderAccountItem(accountItem) } ); }) } ); } getIdCardType(key) { if (key === "ID card front") { return i18next.t("user:ID card front"); } else if (key === "ID card back") { return i18next.t("user:ID card back"); } else if (key === "ID card with person") { return i18next.t("user:ID card with person"); } else { return "Unknown Id card name: " + key; } } getIdCardText(key) { if (key === "ID card front") { return i18next.t("user:Upload ID card front picture"); } else if (key === "ID card back") { return i18next.t("user:Upload ID card back picture"); } else if (key === "ID card with person") { return i18next.t("user:Upload ID card with person picture"); } else { return "Unknown Id card name: " + key; } } submitUserEdit(needExit) { const user = Setting.deepCopy(this.state.user); UserBackend.updateUser(this.state.organizationName, this.state.userName, user) .then((res) => { if (res.status === "ok") { Setting.showMessage("success", i18next.t("general:Successfully saved")); this.setState({ organizationName: this.state.user.owner, userName: this.state.user.name, }); if (this.props.history !== undefined) { if (needExit) { const userListUrl = sessionStorage.getItem("userListUrl"); if (userListUrl !== null) { this.props.history.push(userListUrl); } else { this.props.history.push("/users"); } } else { this.props.history.push(`/users/${this.state.user.owner}/${this.state.user.name}`); } } else { if (needExit) { if (this.state.returnUrl) { window.location.href = this.state.returnUrl; } } } } else { Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`); this.updateUserField("owner", this.state.organizationName); this.updateUserField("name", this.state.userName); } }) .catch(error => { Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`); }); } deleteUser() { UserBackend.deleteUser(this.state.user) .then((res) => { if (res.status === "ok") { const userListUrl = sessionStorage.getItem("userListUrl"); if (userListUrl !== null) { this.props.history.push(userListUrl); } else { this.props.history.push("/users"); } } 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.loading ? : ( this.state.user !== null ? this.renderUser() : } /> ) } { this.state.user === null ? null :
{this.state.mode === "add" ? : null}
}
); } } export default withRouter(UserEditPage);