Files
casdoor/web/src/UserListPage.js

501 lines
16 KiB
JavaScript
Raw Normal View History

2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-02-13 13:30:51 +08:00
//
// 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";
import {Link} from "react-router-dom";
import {Button, Space, Switch, Table, Upload} from "antd";
2021-12-31 12:56:19 +08:00
import {UploadOutlined} from "@ant-design/icons";
2021-02-13 12:15:19 +08:00
import moment from "moment";
2022-11-06 20:19:31 +08:00
import * as OrganizationBackend from "./backend/OrganizationBackend";
2021-02-13 12:15:19 +08:00
import * as Setting from "./Setting";
import * as UserBackend from "./backend/UserBackend";
import i18next from "i18next";
import BaseListPage from "./BaseListPage";
import PopconfirmModal from "./common/modal/PopconfirmModal";
import AccountAvatar from "./account/AccountAvatar";
2021-02-13 12:15:19 +08:00
class UserListPage extends BaseListPage {
2021-02-13 12:15:19 +08:00
constructor(props) {
super(props);
this.state = {
...this.state,
organization: null,
};
}
UNSAFE_componentWillMount() {
super.UNSAFE_componentWillMount();
this.getOrganization(this.state.organizationName);
}
componentDidUpdate(prevProps, prevState) {
if (this.props.match.path !== prevProps.match.path || this.props.organizationName !== prevProps.organizationName) {
this.setState({
organizationName: this.props.organizationName ?? this.props.match?.params.organizationName,
});
}
if (this.state.organizationName !== prevState.organizationName) {
this.getOrganization(this.state.organizationName);
}
if (prevProps.groupName !== this.props.groupName || this.state.organizationName !== prevState.organizationName) {
this.fetch({
pagination: this.state.pagination,
searchText: this.state.searchText,
searchedColumn: this.state.searchedColumn,
});
}
2021-02-13 12:15:19 +08:00
}
newUser() {
2021-12-12 18:51:12 +08:00
const randomName = Setting.getRandomName();
const owner = (Setting.isDefaultOrganizationSelected(this.props.account) || this.props.groupName) ? this.state.organizationName : Setting.getRequestOrganization(this.props.account);
2021-02-13 12:15:19 +08:00
return {
2022-09-13 21:32:18 +08:00
owner: owner,
name: `user_${randomName}`,
2021-02-13 12:15:19 +08:00
createdTime: moment().format(),
2021-04-26 19:00:23 +08:00
type: "normal-user",
2021-02-15 10:05:14 +08:00
password: "123",
2021-11-06 15:52:03 +08:00
passwordSalt: "",
displayName: `New User - ${randomName}`,
avatar: `${Setting.StaticBaseUrl}/img/casbin.svg`,
2021-12-23 21:28:40 +08:00
email: `${randomName}@example.com`,
phone: Setting.getRandomNumber(),
countryCode: this.state.organization.countryCodes?.length > 0 ? this.state.organization.countryCodes[0] : "",
2021-06-04 20:47:27 +08:00
address: [],
groups: this.props.groupName ? [`${owner}/${this.props.groupName}`] : [],
2021-02-15 10:05:14 +08:00
affiliation: "Example Inc.",
tag: "staff",
region: "",
2022-09-13 21:32:18 +08:00
isAdmin: (owner === "built-in"),
2021-05-05 23:40:18 +08:00
IsForbidden: false,
score: this.state.organization.initScore,
2021-11-06 15:52:03 +08:00
isDeleted: false,
2021-05-30 15:13:43 +08:00
properties: {},
signupApplication: this.state.organization.defaultApplication,
};
2021-02-13 12:15:19 +08:00
}
addUser() {
const newUser = this.newUser();
UserBackend.addUser(newUser)
.then((res) => {
if (res.status === "ok") {
2023-03-31 18:35:57 +08:00
sessionStorage.setItem("userListUrl", window.location.pathname);
this.props.history.push({pathname: `/users/${newUser.owner}/${newUser.name}`, mode: "add"});
Setting.showMessage("success", i18next.t("general:Successfully added"));
} else {
Setting.showMessage("error", `${i18next.t("general:Failed to add")}: ${res.msg}`);
}
})
2021-02-13 12:15:19 +08:00
.catch(error => {
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
2021-02-13 12:15:19 +08:00
});
}
deleteUser(i) {
UserBackend.deleteUser(this.state.data[i])
2021-02-13 12:15:19 +08:00
.then((res) => {
if (res.status === "ok") {
Setting.showMessage("success", i18next.t("general:Successfully deleted"));
this.setState({
data: Setting.deleteRow(this.state.data, i),
pagination: {total: this.state.pagination.total - 1},
});
} else {
Setting.showMessage("error", `${i18next.t("general:Failed to delete")}: ${res.msg}`);
}
})
2021-02-13 12:15:19 +08:00
.catch(error => {
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
2021-02-13 12:15:19 +08:00
});
}
removeUserFromGroup(i) {
const user = this.state.data[i];
const group = this.props.groupName;
UserBackend.removeUserFromGroup({groupName: group, owner: user.owner, name: user.name})
.then((res) => {
if (res.status === "ok") {
Setting.showMessage("success", i18next.t("general:Successfully removed"));
this.setState({
data: Setting.deleteRow(this.state.data, i),
pagination: {total: this.state.pagination.total - 1},
});
} else {
Setting.showMessage("error", `${i18next.t("general:Failed to remove")}: ${res.msg}`);
}
})
.catch(error => {
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
});
}
2021-12-31 12:56:19 +08:00
uploadFile(info) {
const {status, response: res} = info.file;
if (status === "done") {
if (res.status === "ok") {
Setting.showMessage("success", "Users uploaded successfully, refreshing the page");
2021-12-31 12:56:19 +08:00
const {pagination} = this.state;
this.fetch({pagination});
2021-12-31 12:56:19 +08:00
} else {
Setting.showMessage("error", `Users failed to upload: ${res.msg}`);
}
} else if (status === "error") {
Setting.showMessage("error", "File failed to upload");
2021-12-31 12:56:19 +08:00
}
}
getOrganization(organizationName) {
OrganizationBackend.getOrganization("admin", organizationName)
.then((res) => {
if (res.status === "ok") {
this.setState({
organization: res.data,
});
} else {
Setting.showMessage("error", `Failed to get organization: ${res.msg}`);
}
});
}
2021-12-31 12:56:19 +08:00
renderUpload() {
const props = {
name: "file",
accept: ".xlsx",
method: "post",
2021-12-31 12:56:19 +08:00
action: `${Setting.ServerUrl}/api/upload-users`,
withCredentials: true,
onChange: (info) => {
this.uploadFile(info);
},
};
return (
<Upload {...props}>
2023-08-27 16:28:37 +08:00
<Button id="upload-button" type="primary" size="small">
2021-12-31 12:56:19 +08:00
<UploadOutlined /> {i18next.t("user:Upload (.xlsx)")}
</Button>
</Upload>
);
2021-12-31 12:56:19 +08:00
}
2021-02-13 12:15:19 +08:00
renderTable(users) {
const columns = [
{
title: i18next.t("general:Organization"),
dataIndex: "owner",
key: "owner",
width: (Setting.isMobile()) ? "100px" : "120px",
fixed: "left",
sorter: true,
...this.getColumnSearchProps("owner"),
2021-02-13 12:15:19 +08:00
render: (text, record, index) => {
return (
2021-03-26 21:58:10 +08:00
<Link to={`/organizations/${text}`}>
2021-02-13 12:15:19 +08:00
{text}
2021-03-26 21:58:10 +08:00
</Link>
);
2022-08-06 23:54:56 +08:00
},
2021-02-13 12:15:19 +08:00
},
2021-08-07 19:52:01 +08:00
{
title: i18next.t("general:Application"),
dataIndex: "signupApplication",
key: "signupApplication",
2021-08-07 19:52:01 +08:00
width: (Setting.isMobile()) ? "100px" : "120px",
fixed: "left",
sorter: true,
...this.getColumnSearchProps("signupApplication"),
2021-08-07 19:52:01 +08:00
render: (text, record, index) => {
return (
<Link to={`/applications/${record.owner}/${text}`}>
2021-08-07 19:52:01 +08:00
{text}
</Link>
);
2022-08-06 23:54:56 +08:00
},
2021-08-07 19:52:01 +08:00
},
2021-02-13 12:15:19 +08:00
{
title: i18next.t("general:Name"),
dataIndex: "name",
key: "name",
2021-12-31 09:36:48 +08:00
width: (Setting.isMobile()) ? "80px" : "110px",
fixed: "left",
sorter: true,
...this.getColumnSearchProps("name"),
2021-02-13 12:15:19 +08:00
render: (text, record, index) => {
return (
<Link to={`/users/${record.owner}/${text}`}>
{text}
</Link>
);
2022-08-06 23:54:56 +08:00
},
2021-02-13 12:15:19 +08:00
},
{
2021-04-28 00:13:50 +08:00
title: i18next.t("general:Created time"),
dataIndex: "createdTime",
key: "createdTime",
width: "160px",
sorter: true,
2021-02-13 12:15:19 +08:00
render: (text, record, index) => {
return Setting.getFormattedDate(text);
2022-08-06 23:54:56 +08:00
},
2021-02-13 12:15:19 +08:00
},
{
2021-04-28 00:13:50 +08:00
title: i18next.t("general:Display name"),
dataIndex: "displayName",
key: "displayName",
2021-12-29 20:50:49 +08:00
// width: '100px',
sorter: true,
...this.getColumnSearchProps("displayName"),
2021-02-13 12:15:19 +08:00
},
2021-02-13 14:49:31 +08:00
{
title: i18next.t("general:Avatar"),
dataIndex: "avatar",
key: "avatar",
width: "80px",
2021-02-13 14:49:31 +08:00
render: (text, record, index) => {
return (
2021-03-27 11:38:15 +08:00
<a target="_blank" rel="noreferrer" href={text}>
<AccountAvatar referrerPolicy="no-referrer" src={text} alt={text} size={50} />
2021-02-13 14:49:31 +08:00
</a>
);
2022-08-06 23:54:56 +08:00
},
2021-02-13 14:49:31 +08:00
},
2021-02-13 12:15:19 +08:00
{
title: i18next.t("general:Email"),
dataIndex: "email",
key: "email",
width: "160px",
sorter: true,
...this.getColumnSearchProps("email"),
2021-02-13 20:13:32 +08:00
render: (text, record, index) => {
return (
<a href={`mailto:${text}`}>
{text}
</a>
);
2022-08-06 23:54:56 +08:00
},
2021-02-13 12:15:19 +08:00
},
2021-05-01 16:50:47 +08:00
{
title: i18next.t("general:Phone"),
dataIndex: "phone",
key: "phone",
width: "120px",
sorter: true,
...this.getColumnSearchProps("phone"),
2021-05-01 16:50:47 +08:00
},
2021-02-14 13:43:55 +08:00
{
title: i18next.t("user:Affiliation"),
dataIndex: "affiliation",
key: "affiliation",
width: "140px",
sorter: true,
...this.getColumnSearchProps("affiliation"),
2021-02-14 13:43:55 +08:00
},
{
title: i18next.t("user:Country/Region"),
dataIndex: "region",
key: "region",
width: "140px",
sorter: true,
...this.getColumnSearchProps("region"),
render: (text, record, index) => {
return Setting.initCountries().getName(record.region, Setting.getLanguage(), {select: "official"});
},
},
2021-02-14 21:21:42 +08:00
{
title: i18next.t("user:Tag"),
dataIndex: "tag",
key: "tag",
width: "110px",
sorter: true,
...this.getColumnSearchProps("tag"),
2022-11-06 20:19:31 +08:00
render: (text, record, index) => {
2023-12-19 21:07:44 +08:00
if (this.state.organization?.tags?.length === 0) {
return text;
}
2022-11-06 20:19:31 +08:00
const tagMap = {};
this.state.organization?.tags?.map((tag, index) => {
const tokens = tag.split("|");
const displayValue = Setting.getLanguage() !== "zh" ? tokens[0] : tokens[1];
tagMap[tokens[0]] = displayValue;
});
return tagMap[text];
},
2021-02-14 21:21:42 +08:00
},
2021-02-14 13:43:55 +08:00
{
2021-04-28 00:13:50 +08:00
title: i18next.t("user:Is admin"),
dataIndex: "isAdmin",
key: "isAdmin",
width: "110px",
sorter: true,
2021-02-14 13:43:55 +08:00
render: (text, record, index) => {
return (
<Switch disabled checkedChildren="ON" unCheckedChildren="OFF" checked={text} />
);
2022-08-06 23:54:56 +08:00
},
2021-02-14 13:43:55 +08:00
},
2021-05-02 12:18:28 +08:00
{
title: i18next.t("user:Is forbidden"),
dataIndex: "isForbidden",
key: "isForbidden",
width: "110px",
sorter: true,
2021-02-15 22:14:19 +08:00
render: (text, record, index) => {
2021-11-06 15:52:03 +08:00
return (
<Switch disabled checkedChildren="ON" unCheckedChildren="OFF" checked={text} />
);
2022-08-06 23:54:56 +08:00
},
2021-11-06 15:52:03 +08:00
},
{
title: i18next.t("user:Is deleted"),
dataIndex: "isDeleted",
key: "isDeleted",
width: "110px",
sorter: true,
2021-11-06 15:52:03 +08:00
render: (text, record, index) => {
2021-02-15 22:14:19 +08:00
return (
<Switch disabled checkedChildren="ON" unCheckedChildren="OFF" checked={text} />
);
2022-08-06 23:54:56 +08:00
},
2021-02-15 22:14:19 +08:00
},
2021-02-13 12:15:19 +08:00
{
title: i18next.t("general:Action"),
dataIndex: "",
key: "op",
width: "190px",
fixed: (Setting.isMobile()) ? "false" : "right",
2021-02-13 12:15:19 +08:00
render: (text, record, index) => {
const isTreePage = this.props.groupName !== undefined;
const disabled = (record.owner === this.props.account.owner && record.name === this.props.account.name) || (record.owner === "built-in" && record.name === "admin");
2021-02-13 12:15:19 +08:00
return (
<Space>
<Button size={isTreePage ? "small" : "middle"} type="primary" onClick={() => {
2023-03-31 18:35:57 +08:00
sessionStorage.setItem("userListUrl", window.location.pathname);
this.props.history.push(`/users/${record.owner}/${record.name}`);
}}>{i18next.t("general:Edit")}
</Button>
{isTreePage ?
<PopconfirmModal
text={i18next.t("general:remove")}
title={i18next.t("general:Sure to remove") + `: ${record.name} ?`}
onConfirm={() => this.removeUserFromGroup(index)}
disabled={disabled}
size="small"
/> : null}
<PopconfirmModal
title={i18next.t("general:Sure to delete") + `: ${record.name} ?`}
2021-02-13 12:15:19 +08:00
onConfirm={() => this.deleteUser(index)}
disabled={disabled}
size={isTreePage ? "small" : "default"}
/>
</Space>
);
2022-08-06 23:54:56 +08:00
},
2021-02-13 12:15:19 +08:00
},
];
const paginationProps = {
total: this.state.pagination.total,
showQuickJumper: true,
showSizeChanger: true,
showTotal: () => i18next.t("general:{total} in total").replace("{total}", this.state.pagination.total),
};
2021-02-13 12:15:19 +08:00
return (
<div>
<Table scroll={{x: "max-content"}} columns={columns} dataSource={users} rowKey={(record) => `${record.owner}/${record.name}`} size="middle" bordered pagination={paginationProps}
title={() => (
<div>
{i18next.t("general:Users")}&nbsp;&nbsp;&nbsp;&nbsp;
2023-08-27 16:28:37 +08:00
<Button style={{marginRight: "5px"}} type="primary" size="small" onClick={this.addUser.bind(this)}>{i18next.t("general:Add")} </Button>
{
this.renderUpload()
}
</div>
)}
loading={this.state.loading}
onChange={this.handleTableChange}
2021-02-13 12:15:19 +08:00
/>
</div>
);
}
fetch = (params = {}) => {
const field = params.searchedColumn, value = params.searchText;
const sortField = params.sortField, sortOrder = params.sortOrder;
this.setState({loading: true});
if (this.props.match?.path === "/users") {
(Setting.isDefaultOrganizationSelected(this.props.account) ? UserBackend.getGlobalUsers(params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder) : UserBackend.getUsers(Setting.getRequestOrganization(this.props.account), params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder))
.then((res) => {
this.setState({
loading: false,
});
if (res.status === "ok") {
this.setState({
data: res.data,
pagination: {
...params.pagination,
total: res.data2,
},
searchText: params.searchText,
searchedColumn: params.searchedColumn,
});
} else {
2023-02-18 16:21:12 +08:00
if (Setting.isResponseDenied(res)) {
this.setState({
isAuthorized: false,
});
} else {
Setting.showMessage("error", res.msg);
}
}
});
} else {
(this.props.groupName ?
UserBackend.getUsers(this.state.organizationName, params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder, this.props.groupName) :
UserBackend.getUsers(this.state.organizationName, params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder))
.then((res) => {
this.setState({
loading: false,
});
if (res.status === "ok") {
this.setState({
data: res.data,
pagination: {
...params.pagination,
total: res.data2,
},
searchText: params.searchText,
searchedColumn: params.searchedColumn,
});
} else {
2023-02-18 16:21:12 +08:00
if (Setting.isResponseDenied(res)) {
this.setState({
isAuthorized: false,
});
} else {
Setting.showMessage("error", res.msg);
}
}
});
}
};
2021-02-13 12:15:19 +08:00
}
export default UserListPage;