Files
casdoor/web/src/UserListPage.js

413 lines
12 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, Popconfirm, 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";
import * as Setting from "./Setting";
import * as UserBackend from "./backend/UserBackend";
import i18next from "i18next";
import BaseListPage from "./BaseListPage";
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 = {
classes: props,
organizationName: props.match.params.organizationName,
data: [],
pagination: {
current: 1,
pageSize: 10,
},
loading: false,
searchText: "",
searchedColumn: "",
2021-02-13 12:15:19 +08:00
};
}
newUser() {
2021-12-12 18:51:12 +08:00
const randomName = Setting.getRandomName();
2022-09-13 21:32:18 +08:00
const owner = (this.state.organizationName !== undefined) ? this.state.organizationName : this.props.account.owner;
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(),
2021-06-04 20:47:27 +08:00
address: [],
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"),
isGlobalAdmin: (owner === "built-in"),
2021-05-05 23:40:18 +08:00
IsForbidden: false,
2021-11-06 15:52:03 +08:00
isDeleted: false,
2021-05-30 15:13:43 +08:00
properties: {},
2021-12-12 18:51:12 +08:00
signupApplication: "app-built-in",
};
2021-02-13 12:15:19 +08:00
}
addUser() {
const newUser = this.newUser();
UserBackend.addUser(newUser)
.then((res) => {
this.props.history.push({pathname: `/users/${newUser.owner}/${newUser.name}`, mode: "add"});
2022-08-07 00:17:27 +08:00
}
2021-02-13 12:15:19 +08:00
)
.catch(error => {
Setting.showMessage("error", `User failed to add: ${error}`);
});
}
deleteUser(i) {
UserBackend.deleteUser(this.state.data[i])
2021-02-13 12:15:19 +08:00
.then((res) => {
Setting.showMessage("success", "User deleted successfully");
this.setState({
data: Setting.deleteRow(this.state.data, i),
pagination: {total: this.state.pagination.total - 1},
});
2022-08-07 00:17:27 +08:00
}
2021-02-13 12:15:19 +08:00
)
.catch(error => {
Setting.showMessage("error", `User failed to delete: ${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
}
}
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}>
<Button type="primary" size="small">
<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) {
// transfer country code to name based on selected language
const countries = require("i18n-iso-countries");
countries.registerLocale(require("i18n-iso-countries/langs/" + i18next.language + ".json"));
for (const index in users) {
users[index].region = countries.getName(users[index].region, i18next.language, {select: "official"});
}
2021-02-13 12:15:19 +08:00
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/${text}`}>
{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}>
2021-02-13 14:49:31 +08:00
<img src={text} alt={text} width={50} />
</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 21:21:42 +08:00
// {
// title: 'Phone',
// dataIndex: 'phone',
// key: 'phone',
// width: '120px',
// sorter: (a, b) => a.phone.localeCompare(b.phone),
// },
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"),
},
2021-02-14 21:21:42 +08:00
{
title: i18next.t("user:Tag"),
dataIndex: "tag",
key: "tag",
width: "110px",
sorter: true,
...this.getColumnSearchProps("tag"),
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-02-15 22:14:19 +08:00
{
2021-04-28 00:13:50 +08:00
title: i18next.t("user:Is global admin"),
dataIndex: "isGlobalAdmin",
key: "isGlobalAdmin",
width: "140px",
sorter: true,
2021-05-02 12:18:28 +08:00
render: (text, record, index) => {
return (
<Switch disabled checkedChildren="ON" unCheckedChildren="OFF" checked={text} />
);
2022-08-06 23:54:56 +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) => {
2022-09-13 21:32:18 +08:00
const disabled = (record.owner === this.props.account.owner && record.name === this.props.account.name);
2021-02-13 12:15:19 +08:00
return (
<div>
<Button style={{marginTop: "10px", marginBottom: "10px", marginRight: "10px"}} type="primary" onClick={() => this.props.history.push(`/users/${record.owner}/${record.name}`)}>{i18next.t("general:Edit")}</Button>
2021-02-13 12:15:19 +08:00
<Popconfirm
title={`Sure to delete user: ${record.name} ?`}
onConfirm={() => this.deleteUser(index)}
>
2022-09-13 21:32:18 +08:00
<Button disabled={disabled} style={{marginBottom: "10px"}} type="danger">{i18next.t("general:Delete")}</Button>
2021-02-13 12:15:19 +08:00
</Popconfirm>
</div>
);
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="name" size="middle" bordered pagination={paginationProps}
title={() => (
<div>
{i18next.t("general:Users")}&nbsp;&nbsp;&nbsp;&nbsp;
<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.state.organizationName === undefined) {
UserBackend.getGlobalUsers(params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder)
.then((res) => {
if (res.status === "ok") {
this.setState({
loading: false,
data: res.data,
pagination: {
...params.pagination,
total: res.data2,
},
searchText: params.searchText,
searchedColumn: params.searchedColumn,
});
}
});
} else {
UserBackend.getUsers(this.state.organizationName, params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder)
.then((res) => {
if (res.status === "ok") {
this.setState({
loading: false,
data: res.data,
pagination: {
...params.pagination,
total: res.data2,
},
searchText: params.searchText,
searchedColumn: params.searchedColumn,
});
}
});
}
};
2021-02-13 12:15:19 +08:00
}
export default UserListPage;