Files
casdoor/web/src/UserListPage.js

412 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";
2021-12-31 12:56:19 +08:00
import {Button, Popconfirm, Switch, Table, Upload} from 'antd';
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();
2021-02-13 12:15:19 +08:00
return {
2021-05-30 15:13:43 +08:00
owner: "built-in", // this.props.account.username,
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}`,
2021-02-13 14:49:31 +08:00
avatar: "https://casbin.org/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: "",
2021-02-15 10:05:14 +08:00
isAdmin: false,
2021-05-05 23:40:18 +08:00
isGlobalAdmin: false,
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) => {
Setting.showMessage("success", `User added successfully`);
2021-12-12 18:51:12 +08:00
this.props.history.push(`/users/${newUser.owner}/${newUser.name}`);
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},
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`);
const { pagination } = this.state;
this.fetch({ pagination });
} else {
Setting.showMessage("error", `Users failed to upload: ${res.msg}`);
}
} else if (status === 'error') {
Setting.showMessage("error", `File failed to upload`);
}
}
renderUpload() {
const props = {
name: 'file',
accept: '.xlsx',
method: 'post',
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-02-13 12:15:19 +08:00
renderTable(users) {
// transfer country code to name based on selected language
var countries = require("i18n-iso-countries");
countries.registerLocale(require("i18n-iso-countries/langs/" + i18next.language + ".json"));
for (var 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"),
2021-02-13 12:15:19 +08:00
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>
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',
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>
)
}
},
2021-02-13 12:15:19 +08:00
{
title: i18next.t("general:Name"),
2021-02-13 12:15:19 +08:00
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>
)
}
},
{
2021-04-28 00:13:50 +08:00
title: i18next.t("general:Created time"),
2021-02-13 12:15:19 +08:00
dataIndex: 'createdTime',
key: 'createdTime',
2021-11-06 15:52:03 +08:00
width: '160px',
sorter: true,
2021-02-13 12:15:19 +08:00
render: (text, record, index) => {
return Setting.getFormattedDate(text);
}
},
{
2021-04-28 00:13:50 +08:00
title: i18next.t("general:Display name"),
2021-02-13 12:15:19 +08:00
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"),
2021-02-13 14:49:31 +08:00
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>
)
}
},
2021-02-13 12:15:19 +08:00
{
title: i18next.t("general:Email"),
2021-02-13 12:15:19 +08:00
dataIndex: 'email',
key: 'email',
2021-02-14 21:21:42 +08:00
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>
)
}
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"),
2021-02-14 13:43:55 +08:00
dataIndex: 'affiliation',
key: 'affiliation',
2021-12-29 20:50:49 +08:00
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',
2021-12-29 20:50:49 +08:00
width: '140px',
sorter: true,
...this.getColumnSearchProps('region'),
},
2021-02-14 21:21:42 +08:00
{
title: i18next.t("user:Tag"),
2021-02-14 21:21:42 +08:00
dataIndex: 'tag',
key: 'tag',
2021-12-29 20:50:49 +08:00
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"),
2021-02-14 13:43:55 +08:00
dataIndex: 'isAdmin',
key: 'isAdmin',
2021-11-06 15:52:03 +08:00
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} />
)
}
},
2021-02-15 22:14:19 +08:00
{
2021-04-28 00:13:50 +08:00
title: i18next.t("user:Is global admin"),
2021-02-15 22:14:19 +08:00
dataIndex: 'isGlobalAdmin',
key: 'isGlobalAdmin',
2021-12-29 20:50:49 +08:00
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} />
)
}
},
{
title: i18next.t("user:Is forbidden"),
dataIndex: 'isForbidden',
key: 'isForbidden',
2021-11-06 15:52:03 +08:00
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} />
)
}
},
{
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} />
)
}
},
2021-02-13 12:15:19 +08:00
{
title: i18next.t("general:Action"),
2021-02-13 12:15:19 +08:00
dataIndex: '',
key: 'op',
2021-03-27 11:38:15 +08:00
width: '190px',
fixed: (Setting.isMobile()) ? "false" : "right",
2021-02-13 12:15:19 +08:00
render: (text, record, index) => {
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)}
>
<Button style={{marginBottom: '10px'}} type="danger">{i18next.t("general:Delete")}</Button>
2021-02-13 12:15:19 +08:00
</Popconfirm>
</div>
)
}
},
];
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}
2021-02-13 12:15:19 +08:00
title={() => (
<div>
2021-12-31 12:56:19 +08:00
{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()
}
2021-02-13 12:15:19 +08:00
</div>
)}
loading={this.state.loading}
onChange={this.handleTableChange}
2021-02-13 12:15:19 +08:00
/>
</div>
);
}
fetch = (params = {}) => {
let field = params.searchedColumn, value = params.searchText;
let 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;