casdoor/web/src/UserEditPage.js

362 lines
13 KiB
JavaScript
Raw Normal View History

2021-02-13 13:30:51 +08:00
// 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.
2021-02-13 12:15:19 +08:00
import React from "react";
2021-02-14 13:43:55 +08:00
import {Button, Card, Col, Input, Row, Select, Switch} from 'antd';
2021-02-13 12:15:19 +08:00
import * as UserBackend from "./backend/UserBackend";
import * as OrganizationBackend from "./backend/OrganizationBackend";
import * as Setting from "./Setting";
2021-02-13 14:49:31 +08:00
import {LinkOutlined} from "@ant-design/icons";
import i18next from "i18next";
feat: added avatar tailoring and uploading Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed type errors Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed the wrong folder Signed-off-by: Kininaru <shiftregister233@outlook.com> rewrite login check logic, added unix time to avatar url Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed a bug about strconv Signed-off-by: Kininaru <shiftregister233@outlook.com> supported oss Signed-off-by: Kininaru <shiftregister233@outlook.com> disabled oss provide qiniu Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar url error Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar length bug Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar length bug Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed oss.conf Signed-off-by: Kininaru <shiftregister233@outlook.com> Put uploading avatar into UserEditPage Signed-off-by: Kininaru <shiftregister233@outlook.com> removed avatar dir Signed-off-by: Kininaru <shiftregister233@outlook.com> removed avatar in main.go Signed-off-by: Kininaru <shiftregister233@outlook.com> Made CropperDiv a reusable component, and updated README for OSS config Signed-off-by: Kininaru <shiftregister233@outlook.com> Convert ts to js Signed-off-by: Kininaru <shiftregister233@outlook.com> removed ts Signed-off-by: Kininaru <shiftregister233@outlook.com> fix: set avatar link to string 255 Signed-off-by: Kininaru <shiftregister233@outlook.com> fix: updated yarn lock Signed-off-by: Kininaru <shiftregister233@outlook.com> add: Casbin license Signed-off-by: Kininaru <shiftregister233@outlook.com>
2021-03-14 15:50:36 +08:00
import CropperDiv from "./CropperDiv.js";
2021-04-19 01:14:41 +08:00
import * as ApplicationBackend from "./backend/ApplicationBackend";
import PasswordModal from "./PasswordModal";
import ResetModal from "./ResetModal";
2021-06-04 20:47:27 +08:00
import AffiliationSelect from "./common/AffiliationSelect";
2021-06-20 00:42:32 +08:00
import OAuthWidget from "./common/OAuthWidget";
2021-02-13 12:15:19 +08:00
2021-05-30 18:35:05 +08:00
import {Controlled as CodeMirror} from 'react-codemirror2'
import "codemirror/lib/codemirror.css"
require('codemirror/theme/material-darker.css');
require("codemirror/mode/javascript/javascript");
2021-02-13 12:15:19 +08:00
const { Option } = Select;
class UserEditPage extends React.Component {
constructor(props) {
super(props);
this.state = {
classes: props,
2021-02-13 17:56:58 +08:00
organizationName: props.organizationName !== undefined ? props.organizationName : props.match.params.organizationName,
userName: props.userName !== undefined ? props.userName : props.match.params.userName,
2021-02-13 12:15:19 +08:00
user: null,
2021-04-19 01:14:41 +08:00
application: null,
2021-02-13 12:15:19 +08:00
organizations: [],
};
}
2021-03-27 11:38:15 +08:00
UNSAFE_componentWillMount() {
2021-02-13 12:15:19 +08:00
this.getUser();
this.getOrganizations();
2021-04-19 01:14:41 +08:00
this.getDefaultApplication();
2021-02-13 12:15:19 +08:00
}
getUser() {
UserBackend.getUser(this.state.organizationName, this.state.userName)
.then((user) => {
this.setState({
user: user,
});
});
}
getOrganizations() {
OrganizationBackend.getOrganizations("admin")
.then((res) => {
this.setState({
2021-03-28 00:48:34 +08:00
organizations: (res.msg === undefined) ? res : [],
2021-02-13 12:15:19 +08:00
});
});
}
2021-04-19 01:14:41 +08:00
getDefaultApplication() {
ApplicationBackend.getDefaultApplication("admin")
.then((application) => {
this.setState({
application: application,
});
});
}
2021-02-13 12:15:19 +08:00
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,
});
}
2021-06-20 00:42:32 +08:00
unlinked() {
this.getUser();
2021-04-18 23:14:46 +08:00
}
2021-05-31 01:42:03 +08:00
isSelfOrAdmin() {
return (this.state.user.id === this.props.account?.id) || Setting.isAdminUser(this.props.account);
}
2021-02-13 12:15:19 +08:00
renderUser() {
return (
<Card size="small" title={
<div>
{i18next.t("user:Edit User")}&nbsp;&nbsp;&nbsp;&nbsp;
<Button type="primary" onClick={this.submitUserEdit.bind(this)}>{i18next.t("general:Save")}</Button>
2021-02-13 12:15:19 +08:00
</div>
} style={{marginLeft: '5px'}} type="inner">
<Row style={{marginTop: '10px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("general:Organization")}:
2021-02-13 12:15:19 +08:00
</Col>
<Col span={22} >
2021-05-16 21:52:50 +08:00
<Select virtual={false} style={{width: '100%'}} disabled={!Setting.isAdminUser(this.props.account)} value={this.state.user.owner} onChange={(value => {this.updateUserField('owner', value);})}>
2021-02-13 12:15:19 +08:00
{
this.state.organizations.map((organization, index) => <Option key={index} value={organization.name}>{organization.name}</Option>)
}
</Select>
</Col>
</Row>
2021-02-13 20:10:41 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
ID:
</Col>
<Col span={22} >
<Input value={this.state.user.id} disabled={true} />
</Col>
</Row>
2021-02-13 12:15:19 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("general:Name")}:
2021-02-13 12:15:19 +08:00
</Col>
<Col span={22} >
2021-03-06 00:40:11 +08:00
<Input value={this.state.user.name} disabled={true} onChange={e => {
2021-02-13 12:15:19 +08:00
this.updateUserField('name', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
2021-04-28 00:13:50 +08:00
{i18next.t("general:Display name")}:
2021-02-13 12:15:19 +08:00
</Col>
<Col span={22} >
2021-02-13 14:49:31 +08:00
<Input value={this.state.user.displayName} onChange={e => {
this.updateUserField('displayName', e.target.value);
2021-02-13 12:15:19 +08:00
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("general:Avatar")}:
2021-02-13 12:15:19 +08:00
</Col>
<Col span={22} >
2021-02-13 14:49:31 +08:00
<Row style={{marginTop: '20px'}} >
2021-04-26 18:21:28 +08:00
<Col style={{marginTop: '5px'}} span={2}>
2021-04-27 20:59:50 +08:00
{i18next.t("general:URL")}:
2021-02-13 14:49:31 +08:00
</Col>
2021-04-26 18:21:28 +08:00
<Col span={22} >
2021-02-13 14:49:31 +08:00
<Input prefix={<LinkOutlined/>} value={this.state.user.avatar} onChange={e => {
this.updateUserField('avatar', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
2021-04-26 18:21:28 +08:00
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("general:Preview")}:
2021-02-13 14:49:31 +08:00
</Col>
2021-04-26 18:21:28 +08:00
<Col span={22} >
2021-03-27 11:38:15 +08:00
<a target="_blank" rel="noreferrer" href={this.state.user.avatar}>
2021-02-13 14:49:31 +08:00
<img src={this.state.user.avatar} alt={this.state.user.avatar} height={90} style={{marginBottom: '20px'}}/>
</a>
</Col>
</Row>
feat: added avatar tailoring and uploading Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed type errors Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed the wrong folder Signed-off-by: Kininaru <shiftregister233@outlook.com> rewrite login check logic, added unix time to avatar url Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed a bug about strconv Signed-off-by: Kininaru <shiftregister233@outlook.com> supported oss Signed-off-by: Kininaru <shiftregister233@outlook.com> disabled oss provide qiniu Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar url error Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar length bug Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar length bug Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed oss.conf Signed-off-by: Kininaru <shiftregister233@outlook.com> Put uploading avatar into UserEditPage Signed-off-by: Kininaru <shiftregister233@outlook.com> removed avatar dir Signed-off-by: Kininaru <shiftregister233@outlook.com> removed avatar in main.go Signed-off-by: Kininaru <shiftregister233@outlook.com> Made CropperDiv a reusable component, and updated README for OSS config Signed-off-by: Kininaru <shiftregister233@outlook.com> Convert ts to js Signed-off-by: Kininaru <shiftregister233@outlook.com> removed ts Signed-off-by: Kininaru <shiftregister233@outlook.com> fix: set avatar link to string 255 Signed-off-by: Kininaru <shiftregister233@outlook.com> fix: updated yarn lock Signed-off-by: Kininaru <shiftregister233@outlook.com> add: Casbin license Signed-off-by: Kininaru <shiftregister233@outlook.com>
2021-03-14 15:50:36 +08:00
<Row style={{marginTop: '20px'}}>
2021-05-24 20:48:04 +08:00
<CropperDiv buttonText={`${i18next.t("user:Upload a photo")}...`} title={i18next.t("user:Upload a photo")} targetFunction={UserBackend.uploadAvatar} />
feat: added avatar tailoring and uploading Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed type errors Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed the wrong folder Signed-off-by: Kininaru <shiftregister233@outlook.com> rewrite login check logic, added unix time to avatar url Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed a bug about strconv Signed-off-by: Kininaru <shiftregister233@outlook.com> supported oss Signed-off-by: Kininaru <shiftregister233@outlook.com> disabled oss provide qiniu Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar url error Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar length bug Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar length bug Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed oss.conf Signed-off-by: Kininaru <shiftregister233@outlook.com> Put uploading avatar into UserEditPage Signed-off-by: Kininaru <shiftregister233@outlook.com> removed avatar dir Signed-off-by: Kininaru <shiftregister233@outlook.com> removed avatar in main.go Signed-off-by: Kininaru <shiftregister233@outlook.com> Made CropperDiv a reusable component, and updated README for OSS config Signed-off-by: Kininaru <shiftregister233@outlook.com> Convert ts to js Signed-off-by: Kininaru <shiftregister233@outlook.com> removed ts Signed-off-by: Kininaru <shiftregister233@outlook.com> fix: set avatar link to string 255 Signed-off-by: Kininaru <shiftregister233@outlook.com> fix: updated yarn lock Signed-off-by: Kininaru <shiftregister233@outlook.com> add: Casbin license Signed-off-by: Kininaru <shiftregister233@outlook.com>
2021-03-14 15:50:36 +08:00
</Row>
2021-02-13 14:49:31 +08:00
</Col>
</Row>
2021-04-26 19:00:23 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
2021-04-28 00:13:50 +08:00
{i18next.t("general:User type")}:
2021-04-26 19:00:23 +08:00
</Col>
<Col span={22} >
<Select virtual={false} style={{width: '100%'}} value={this.state.user.type} onChange={(value => {this.updateUserField('type', value);})}>
{
['normal-user']
.map((item, index) => <Option key={index} value={item}>{item}</Option>)
}
</Select>
</Col>
</Row>
2021-02-13 12:15:19 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("general:Password")}:
2021-02-13 12:15:19 +08:00
</Col>
<Col span={22} >
<PasswordModal user={this.state.user} />
2021-02-13 12:15:19 +08:00
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("general:Email")}:
2021-02-13 12:15:19 +08:00
</Col>
2021-05-15 20:44:40 +08:00
<Col style={{paddingRight: '20px'}} span={11} >
<Input value={this.state.user.email} disabled />
2021-05-15 20:44:40 +08:00
</Col>
<Col span={11} >
2021-05-31 01:42:03 +08:00
{ this.state.user.id === this.props.account?.id ? (<ResetModal org={this.state.application?.organizationObj} buttonText={i18next.t("user:Reset Email...")} destType={"email"} coolDownTime={60}/>) : null}
2021-02-13 12:15:19 +08:00
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("general:Phone")}:
2021-02-13 12:15:19 +08:00
</Col>
2021-05-15 20:44:40 +08:00
<Col style={{paddingRight: '20px'}} span={11} >
2021-05-14 17:39:53 +08:00
<Input value={this.state.user.phone} addonBefore={`+${this.state.application?.organizationObj.phonePrefix}`} disabled />
2021-05-15 20:44:40 +08:00
</Col>
<Col span={11} >
2021-05-31 01:42:03 +08:00
{ this.state.user.id === this.props.account?.id ? (<ResetModal org={this.state.application?.organizationObj} buttonText={i18next.t("user:Reset Phone...")} destType={"phone"} coolDownTime={60}/>) : null}
2021-02-13 12:15:19 +08:00
</Col>
</Row>
2021-06-04 20:47:27 +08:00
{
(this.state.application === null || this.state.user === null) ? null : (
2021-06-18 02:05:19 +08:00
<AffiliationSelect labelSpan={2} application={this.state.application} user={this.state.user} onUpdateUserField={(key, value) => { return this.updateUserField(key, value)}} />
2021-06-04 20:47:27 +08:00
)
}
2021-02-14 21:21:42 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("user:Tag")}:
2021-02-14 21:21:42 +08:00
</Col>
<Col span={22} >
<Input value={this.state.user.tag} onChange={e => {
this.updateUserField('tag', e.target.value);
}} />
</Col>
</Row>
2021-05-30 18:35:05 +08:00
{
2021-05-31 01:42:03 +08:00
!this.isSelfOrAdmin() ? null : (
2021-05-30 18:35:05 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
2021-05-31 01:42:03 +08:00
{i18next.t("user:Third-party logins")}:
2021-05-30 18:35:05 +08:00
</Col>
<Col span={22} >
2021-05-31 01:42:03 +08:00
<div style={{marginBottom: 20}}>
{
2021-06-20 00:42:32 +08:00
this.state.application?.providers.filter(providerItem => Setting.isProviderVisible(providerItem)).map((providerItem, index) => <OAuthWidget user={this.state.user} application={this.state.application} providerItem={providerItem} onUnlinked={() => { return this.unlinked()}} />)
2021-05-31 01:42:03 +08:00
}
</div>
2021-05-30 18:35:05 +08:00
</Col>
</Row>
)
}
2021-02-15 22:14:19 +08:00
{
!Setting.isAdminUser(this.props.account) ? null : (
<React.Fragment>
2021-05-31 01:42:03 +08:00
{/*<Row style={{marginTop: '20px'}} >*/}
{/* <Col style={{marginTop: '5px'}} span={2}>*/}
{/* {i18next.t("user:Properties")}:*/}
{/* </Col>*/}
{/* <Col span={22} >*/}
{/* <CodeMirror*/}
{/* value={JSON.stringify(this.state.user.properties, null, 4)}*/}
{/* options={{mode: 'javascript', theme: "material-darker"}}*/}
{/* />*/}
{/* </Col>*/}
{/*</Row>*/}
2021-02-15 22:14:19 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
2021-04-28 00:13:50 +08:00
{i18next.t("user:Is admin")}:
2021-02-15 22:14:19 +08:00
</Col>
<Col span={1} >
<Switch checked={this.state.user.isAdmin} onChange={checked => {
this.updateUserField('isAdmin', checked);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
2021-04-28 00:13:50 +08:00
{i18next.t("user:Is global admin")}:
2021-02-15 22:14:19 +08:00
</Col>
<Col span={1} >
<Switch checked={this.state.user.isGlobalAdmin} onChange={checked => {
this.updateUserField('isGlobalAdmin', checked);
}} />
</Col>
</Row>
2021-05-02 12:18:28 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("user:Is forbidden")}:
</Col>
<Col span={1} >
<Switch checked={this.state.user.isForbidden} onChange={checked => {
this.updateUserField('isForbidden', checked);
}} />
</Col>
</Row>
2021-02-15 22:14:19 +08:00
</React.Fragment>
)
}
2021-02-13 12:15:19 +08:00
</Card>
)
}
submitUserEdit() {
let user = Setting.deepCopy(this.state.user);
UserBackend.updateUser(this.state.organizationName, this.state.userName, user)
.then((res) => {
2021-03-28 00:48:34 +08:00
if (res.msg === "") {
2021-02-13 12:15:19 +08:00
Setting.showMessage("success", `Successfully saved`);
this.setState({
organizationName: this.state.user.owner,
userName: this.state.user.name,
});
2021-02-13 17:56:58 +08:00
if (this.props.history !== undefined) {
this.props.history.push(`/users/${this.state.user.owner}/${this.state.user.name}`);
}
2021-02-13 12:15:19 +08:00
} else {
2021-03-28 00:48:34 +08:00
Setting.showMessage("error", res.msg);
2021-02-13 12:15:19 +08:00
this.updateUserField('owner', this.state.organizationName);
this.updateUserField('name', this.state.userName);
}
})
.catch(error => {
2021-03-28 08:59:12 +08:00
Setting.showMessage("error", `Failed to connect to server: ${error}`);
2021-02-13 12:15:19 +08:00
});
}
render() {
return (
<div>
<Row style={{width: "100%"}}>
<Col span={1}>
</Col>
<Col span={22}>
{
this.state.user !== null ? this.renderUser() : null
}
</Col>
<Col span={1}>
</Col>
</Row>
<Row style={{margin: 10}}>
<Col span={2}>
</Col>
<Col span={18}>
<Button type="primary" size="large" onClick={this.submitUserEdit.bind(this)}>{i18next.t("general:Save")}</Button>
2021-02-13 12:15:19 +08:00
</Col>
</Row>
</div>
);
}
}
export default UserEditPage;