// Copyright 2021 The casbin 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 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 AuthBackend from "./auth/AuthBackend"; import * as ApplicationBackend from "./backend/ApplicationBackend"; import * as Provider from "./auth/Provider"; import PasswordModal from "./PasswordModal"; import ResetModal from "./ResetModal"; 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: [], }; } UNSAFE_componentWillMount() { this.getUser(); this.getOrganizations(); this.getDefaultApplication(); } getUser() { UserBackend.getUser(this.state.organizationName, this.state.userName) .then((user) => { this.setState({ user: user, }); }); } getOrganizations() { OrganizationBackend.getOrganizations("admin") .then((res) => { this.setState({ organizations: (res.msg === undefined) ? res : [], }); }); } getDefaultApplication() { ApplicationBackend.getDefaultApplication("admin") .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, }); } unlinkUser(providerType) { const body = { providerType: providerType, }; AuthBackend.unlink(body) .then((res) => { if (res.status === 'ok') { Setting.showMessage("success", `Linked successfully`); this.getUser(); } else { Setting.showMessage("error", `Failed to unlink: ${res.msg}`); } }); } getProviderLink(provider, linkedValue) { if (provider.type === "GitHub") { return `https://github.com/${linkedValue}`; } else if (provider.type === "Google") { return "https://mail.google.com"; } else { return ""; } } renderIdp(provider) { const linkedValue = this.state.user[provider.type.toLowerCase()]; return ( { Setting.getProviderLogo(provider) } { `${provider.type}:` } { linkedValue === "" ? ( "(empty)" ) : ( { linkedValue } ) } { linkedValue === "" ? ( ) : ( ) } ) } renderUser() { return ( {i18next.t("user:Edit User")}     } style={{marginLeft: '5px'}} type="inner"> {i18next.t("general:Organization")}: ID: {i18next.t("general:Name")}: { this.updateUserField('name', e.target.value); }} /> {i18next.t("general:Display name")}: { this.updateUserField('displayName', e.target.value); }} /> {i18next.t("general:Avatar")}: {i18next.t("general:URL")}: } value={this.state.user.avatar} onChange={e => { this.updateUserField('avatar', e.target.value); }} /> {i18next.t("general:Preview")}: {this.state.user.avatar} {i18next.t("general:User type")}: {i18next.t("general:Password")}: {i18next.t("general:Email")}: { this.state.user.id === this.props.account.id ? () : null} {i18next.t("general:Phone")}: { this.state.user.id === this.props.account.id ? () : null} {i18next.t("user:Affiliation")}: { this.updateUserField('affiliation', e.target.value); }} /> {i18next.t("user:Tag")}: { this.updateUserField('tag', e.target.value); }} /> {i18next.t("user:Third-party logins")}:
{ this.state.application?.providerObjs.filter(provider => Setting.isProviderVisible(provider)).map((provider, index) => this.renderIdp(provider)) }
{ !Setting.isAdminUser(this.props.account) ? null : ( {i18next.t("user:Is admin")}: { this.updateUserField('isAdmin', checked); }} /> {i18next.t("user:Is global admin")}: { this.updateUserField('isGlobalAdmin', checked); }} /> {i18next.t("user:Is forbidden")}: { this.updateUserField('isForbidden', checked); }} /> ) }
) } submitUserEdit() { 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) { 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}`); }); } render() { return (
{ this.state.user !== null ? this.renderUser() : null }
); } } export default UserEditPage;