2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2022-01-01 15:11:16 +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.
|
|
|
|
|
|
|
|
import React from "react";
|
2022-07-10 15:45:55 +08:00
|
|
|
import {Button, Card, Col, Input, Row, Select, Switch} from "antd";
|
2022-01-01 15:11:16 +08:00
|
|
|
import * as RoleBackend from "./backend/RoleBackend";
|
|
|
|
import * as OrganizationBackend from "./backend/OrganizationBackend";
|
|
|
|
import * as Setting from "./Setting";
|
|
|
|
import i18next from "i18next";
|
2023-01-12 23:11:11 +08:00
|
|
|
import * as UserBackend from "./backend/UserBackend";
|
2022-01-01 15:11:16 +08:00
|
|
|
|
|
|
|
class RoleEditPage extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
classes: props,
|
|
|
|
organizationName: props.organizationName !== undefined ? props.organizationName : props.match.params.organizationName,
|
|
|
|
roleName: props.match.params.roleName,
|
|
|
|
role: null,
|
|
|
|
organizations: [],
|
|
|
|
users: [],
|
|
|
|
roles: [],
|
2022-02-25 18:16:02 +08:00
|
|
|
mode: props.location.mode !== undefined ? props.location.mode : "edit",
|
2022-01-01 15:11:16 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
UNSAFE_componentWillMount() {
|
|
|
|
this.getRole();
|
|
|
|
this.getOrganizations();
|
|
|
|
}
|
|
|
|
|
|
|
|
getRole() {
|
|
|
|
RoleBackend.getRole(this.state.organizationName, this.state.roleName)
|
|
|
|
.then((role) => {
|
|
|
|
this.setState({
|
|
|
|
role: role,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.getUsers(role.owner);
|
|
|
|
this.getRoles(role.owner);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getOrganizations() {
|
|
|
|
OrganizationBackend.getOrganizations("admin")
|
|
|
|
.then((res) => {
|
|
|
|
this.setState({
|
|
|
|
organizations: (res.msg === undefined) ? res : [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getUsers(organizationName) {
|
|
|
|
UserBackend.getUsers(organizationName)
|
|
|
|
.then((res) => {
|
|
|
|
this.setState({
|
|
|
|
users: res,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getRoles(organizationName) {
|
|
|
|
RoleBackend.getRoles(organizationName)
|
|
|
|
.then((res) => {
|
|
|
|
this.setState({
|
|
|
|
roles: res,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
parseRoleField(key, value) {
|
|
|
|
if ([""].includes(key)) {
|
|
|
|
value = Setting.myParseInt(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRoleField(key, value) {
|
|
|
|
value = this.parseRoleField(key, value);
|
|
|
|
|
2022-08-08 23:35:24 +08:00
|
|
|
const role = this.state.role;
|
2022-01-01 15:11:16 +08:00
|
|
|
role[key] = value;
|
|
|
|
this.setState({
|
|
|
|
role: role,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRole() {
|
|
|
|
return (
|
|
|
|
<Card size="small" title={
|
|
|
|
<div>
|
2022-02-25 18:16:02 +08:00
|
|
|
{this.state.mode === "add" ? i18next.t("role:New Role") : i18next.t("role:Edit Role")}
|
2022-01-01 15:11:16 +08:00
|
|
|
<Button onClick={() => this.submitRoleEdit(false)}>{i18next.t("general:Save")}</Button>
|
2022-07-10 15:45:55 +08:00
|
|
|
<Button style={{marginLeft: "20px"}} type="primary" onClick={() => this.submitRoleEdit(true)}>{i18next.t("general:Save & Exit")}</Button>
|
|
|
|
{this.state.mode === "add" ? <Button style={{marginLeft: "20px"}} onClick={() => this.deleteRole()}>{i18next.t("general:Cancel")}</Button> : null}
|
2022-01-01 15:11:16 +08:00
|
|
|
</div>
|
2022-08-06 23:43:09 +08:00
|
|
|
} style={(Setting.isMobile()) ? {margin: "5px"} : {}} type="inner">
|
2022-07-10 15:45:55 +08:00
|
|
|
<Row style={{marginTop: "10px"}} >
|
|
|
|
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
2022-01-01 15:11:16 +08:00
|
|
|
{Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
2023-01-12 23:11:11 +08:00
|
|
|
<Select virtual={false} style={{width: "100%"}} value={this.state.role.owner} onChange={(value => {this.updateRoleField("owner", value);})}
|
|
|
|
options={this.state.organizations.map((organization) => Setting.getOption(organization.name, organization.name))
|
|
|
|
} />
|
2022-01-01 15:11:16 +08:00
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-07-10 15:45:55 +08:00
|
|
|
<Row style={{marginTop: "20px"}} >
|
|
|
|
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
2022-01-01 15:11:16 +08:00
|
|
|
{Setting.getLabel(i18next.t("general:Name"), i18next.t("general:Name - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.role.name} onChange={e => {
|
2022-07-10 15:45:55 +08:00
|
|
|
this.updateRoleField("name", e.target.value);
|
2022-01-01 15:11:16 +08:00
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-07-10 15:45:55 +08:00
|
|
|
<Row style={{marginTop: "20px"}} >
|
|
|
|
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
2022-01-01 15:11:16 +08:00
|
|
|
{Setting.getLabel(i18next.t("general:Display name"), i18next.t("general:Display name - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.role.displayName} onChange={e => {
|
2022-07-10 15:45:55 +08:00
|
|
|
this.updateRoleField("displayName", e.target.value);
|
2022-01-01 15:11:16 +08:00
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-07-10 15:45:55 +08:00
|
|
|
<Row style={{marginTop: "20px"}} >
|
|
|
|
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
2022-01-01 15:11:16 +08:00
|
|
|
{Setting.getLabel(i18next.t("role:Sub users"), i18next.t("role:Sub users - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
2023-01-12 23:11:11 +08:00
|
|
|
<Select mode="tags" style={{width: "100%"}} value={this.state.role.users}
|
|
|
|
onChange={(value => {this.updateRoleField("users", value);})}
|
|
|
|
options={this.state.users.map((user) => Setting.getOption(`${user.owner}/${user.name}`, `${user.owner}/${user.name}`))}
|
|
|
|
/>
|
2022-01-01 15:11:16 +08:00
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-07-10 15:45:55 +08:00
|
|
|
<Row style={{marginTop: "20px"}} >
|
|
|
|
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
2022-01-01 15:11:16 +08:00
|
|
|
{Setting.getLabel(i18next.t("role:Sub roles"), i18next.t("role:Sub roles - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
2023-01-12 23:11:11 +08:00
|
|
|
<Select virtual={false} mode="tags" style={{width: "100%"}} value={this.state.role.roles} onChange={(value => {this.updateRoleField("roles", value);})}
|
|
|
|
options={this.state.roles.filter(role => (role.owner !== this.state.role.owner || role.name !== this.state.role.name)).map((role) => Setting.getOption(`${role.owner}/${role.name}`, `${role.owner}/${role.name}`))
|
|
|
|
} />
|
2022-01-01 15:11:16 +08:00
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-08-15 10:24:26 +08:00
|
|
|
<Row style={{marginTop: "20px"}} >
|
|
|
|
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("role:Sub domains"), i18next.t("role:Sub domains - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Select virtual={false} mode="tags" style={{width: "100%"}} value={this.state.role.domains} onChange={(value => {
|
|
|
|
this.updateRoleField("domains", value);
|
2023-01-12 23:11:11 +08:00
|
|
|
})}
|
|
|
|
options={this.state.role.domains?.map((domain) => Setting.getOption(domain, domain))
|
|
|
|
} />
|
2022-08-15 10:24:26 +08:00
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-07-10 15:45:55 +08:00
|
|
|
<Row style={{marginTop: "20px"}} >
|
|
|
|
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 19 : 2}>
|
2022-01-01 15:11:16 +08:00
|
|
|
{Setting.getLabel(i18next.t("general:Is enabled"), i18next.t("general:Is enabled - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={1} >
|
|
|
|
<Switch checked={this.state.role.isEnabled} onChange={checked => {
|
2022-07-10 15:45:55 +08:00
|
|
|
this.updateRoleField("isEnabled", checked);
|
2022-01-01 15:11:16 +08:00
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
</Card>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2022-01-01 15:11:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
submitRoleEdit(willExist) {
|
2022-08-08 23:35:24 +08:00
|
|
|
const role = Setting.deepCopy(this.state.role);
|
2022-01-01 15:11:16 +08:00
|
|
|
RoleBackend.updateRole(this.state.organizationName, this.state.roleName, role)
|
|
|
|
.then((res) => {
|
2022-12-02 00:06:28 +08:00
|
|
|
if (res.status === "ok") {
|
|
|
|
Setting.showMessage("success", i18next.t("general:Successfully saved"));
|
2022-01-01 15:11:16 +08:00
|
|
|
this.setState({
|
|
|
|
roleName: this.state.role.name,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (willExist) {
|
2022-07-10 15:45:55 +08:00
|
|
|
this.props.history.push("/roles");
|
2022-01-01 15:11:16 +08:00
|
|
|
} else {
|
|
|
|
this.props.history.push(`/roles/${this.state.role.owner}/${this.state.role.name}`);
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-02 00:06:28 +08:00
|
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`);
|
2022-07-10 15:45:55 +08:00
|
|
|
this.updateRoleField("name", this.state.roleName);
|
2022-01-01 15:11:16 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2022-12-02 00:06:28 +08:00
|
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
|
2022-01-01 15:11:16 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:16:02 +08:00
|
|
|
deleteRole() {
|
|
|
|
RoleBackend.deleteRole(this.state.role)
|
2022-12-02 00:06:28 +08:00
|
|
|
.then((res) => {
|
|
|
|
if (res.status === "ok") {
|
|
|
|
this.props.history.push("/roles");
|
|
|
|
} else {
|
|
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to delete")}: ${res.msg}`);
|
|
|
|
}
|
2022-02-25 18:16:02 +08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2022-12-02 00:06:28 +08:00
|
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
|
2022-02-25 18:16:02 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-01 15:11:16 +08:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{
|
|
|
|
this.state.role !== null ? this.renderRole() : null
|
|
|
|
}
|
2022-07-10 15:45:55 +08:00
|
|
|
<div style={{marginTop: "20px", marginLeft: "40px"}}>
|
2022-01-01 15:11:16 +08:00
|
|
|
<Button size="large" onClick={() => this.submitRoleEdit(false)}>{i18next.t("general:Save")}</Button>
|
2022-07-10 15:45:55 +08:00
|
|
|
<Button style={{marginLeft: "20px"}} type="primary" size="large" onClick={() => this.submitRoleEdit(true)}>{i18next.t("general:Save & Exit")}</Button>
|
|
|
|
{this.state.mode === "add" ? <Button style={{marginLeft: "20px"}} size="large" onClick={() => this.deleteRole()}>{i18next.t("general:Cancel")}</Button> : null}
|
2022-01-01 15:11:16 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RoleEditPage;
|