2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2022-01-01 16:28:06 +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 16:28:06 +08:00
|
|
|
import * as PermissionBackend from "./backend/PermissionBackend";
|
|
|
|
import * as OrganizationBackend from "./backend/OrganizationBackend";
|
|
|
|
import * as UserBackend from "./backend/UserBackend";
|
|
|
|
import * as Setting from "./Setting";
|
|
|
|
import i18next from "i18next";
|
|
|
|
import * as RoleBackend from "./backend/RoleBackend";
|
2022-05-24 18:27:47 +08:00
|
|
|
import * as ModelBackend from "./backend/ModelBackend";
|
2022-07-13 00:34:35 +08:00
|
|
|
import * as ApplicationBackend from "./backend/ApplicationBackend";
|
2022-01-01 16:28:06 +08:00
|
|
|
|
2022-07-10 15:45:55 +08:00
|
|
|
const {Option} = Select;
|
2022-01-01 16:28:06 +08:00
|
|
|
|
|
|
|
class PermissionEditPage extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
classes: props,
|
|
|
|
organizationName: props.organizationName !== undefined ? props.organizationName : props.match.params.organizationName,
|
|
|
|
permissionName: props.match.params.permissionName,
|
|
|
|
permission: null,
|
|
|
|
organizations: [],
|
|
|
|
users: [],
|
|
|
|
roles: [],
|
2022-05-24 18:27:47 +08:00
|
|
|
models: [],
|
2022-07-13 00:34:35 +08:00
|
|
|
resources: [],
|
2022-02-25 18:16:02 +08:00
|
|
|
mode: props.location.mode !== undefined ? props.location.mode : "edit",
|
2022-01-01 16:28:06 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
UNSAFE_componentWillMount() {
|
|
|
|
this.getPermission();
|
|
|
|
this.getOrganizations();
|
|
|
|
}
|
|
|
|
|
|
|
|
getPermission() {
|
|
|
|
PermissionBackend.getPermission(this.state.organizationName, this.state.permissionName)
|
|
|
|
.then((permission) => {
|
|
|
|
this.setState({
|
|
|
|
permission: permission,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.getUsers(permission.owner);
|
|
|
|
this.getRoles(permission.owner);
|
2022-05-24 18:27:47 +08:00
|
|
|
this.getModels(permission.owner);
|
2022-07-13 00:34:35 +08:00
|
|
|
this.getResources(permission.owner);
|
2022-01-01 16:28:06 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-24 18:27:47 +08:00
|
|
|
getModels(organizationName) {
|
|
|
|
ModelBackend.getModels(organizationName)
|
|
|
|
.then((res) => {
|
|
|
|
this.setState({
|
|
|
|
models: res,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-13 00:34:35 +08:00
|
|
|
getResources(organizationName) {
|
|
|
|
ApplicationBackend.getApplicationsByOrganization("admin", organizationName)
|
|
|
|
.then((res) => {
|
|
|
|
this.setState({
|
|
|
|
resources: (res.msg === undefined) ? res : [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-01 16:28:06 +08:00
|
|
|
parsePermissionField(key, value) {
|
|
|
|
if ([""].includes(key)) {
|
|
|
|
value = Setting.myParseInt(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePermissionField(key, value) {
|
|
|
|
value = this.parsePermissionField(key, value);
|
|
|
|
|
2022-08-08 23:35:24 +08:00
|
|
|
const permission = this.state.permission;
|
2022-01-01 16:28:06 +08:00
|
|
|
permission[key] = value;
|
|
|
|
this.setState({
|
|
|
|
permission: permission,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPermission() {
|
|
|
|
return (
|
|
|
|
<Card size="small" title={
|
|
|
|
<div>
|
2022-02-25 18:16:02 +08:00
|
|
|
{this.state.mode === "add" ? i18next.t("permission:New Permission") : i18next.t("permission:Edit Permission")}
|
2022-01-01 16:28:06 +08:00
|
|
|
<Button onClick={() => this.submitPermissionEdit(false)}>{i18next.t("general:Save")}</Button>
|
2022-07-10 15:45:55 +08:00
|
|
|
<Button style={{marginLeft: "20px"}} type="primary" onClick={() => this.submitPermissionEdit(true)}>{i18next.t("general:Save & Exit")}</Button>
|
|
|
|
{this.state.mode === "add" ? <Button style={{marginLeft: "20px"}} onClick={() => this.deletePermission()}>{i18next.t("general:Cancel")}</Button> : null}
|
2022-01-01 16:28:06 +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 16:28:06 +08:00
|
|
|
{Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
2022-07-10 15:45:55 +08:00
|
|
|
<Select virtual={false} style={{width: "100%"}} value={this.state.permission.owner} onChange={(owner => {
|
|
|
|
this.updatePermissionField("owner", owner);
|
2022-01-15 21:11:47 +08:00
|
|
|
|
|
|
|
this.getUsers(owner);
|
|
|
|
this.getRoles(owner);
|
2022-07-16 15:00:42 +08:00
|
|
|
this.getModels(owner);
|
|
|
|
this.getResources(owner);
|
2022-01-15 21:11:47 +08:00
|
|
|
})}>
|
2022-01-01 16:28:06 +08:00
|
|
|
{
|
|
|
|
this.state.organizations.map((organization, index) => <Option key={index} value={organization.name}>{organization.name}</Option>)
|
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</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 16:28:06 +08:00
|
|
|
{Setting.getLabel(i18next.t("general:Name"), i18next.t("general:Name - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.permission.name} onChange={e => {
|
2022-07-10 15:45:55 +08:00
|
|
|
this.updatePermissionField("name", e.target.value);
|
2022-01-01 16:28:06 +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 16:28:06 +08:00
|
|
|
{Setting.getLabel(i18next.t("general:Display name"), i18next.t("general:Display name - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.permission.displayName} onChange={e => {
|
2022-07-10 15:45:55 +08:00
|
|
|
this.updatePermissionField("displayName", e.target.value);
|
2022-01-01 16:28:06 +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-05-24 18:27:47 +08:00
|
|
|
{Setting.getLabel(i18next.t("general:Model"), i18next.t("general:Model - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
2022-07-10 15:45:55 +08:00
|
|
|
<Select virtual={false} style={{width: "100%"}} value={this.state.permission.model} onChange={(model => {
|
|
|
|
this.updatePermissionField("model", model);
|
2022-05-24 18:27:47 +08:00
|
|
|
})}>
|
|
|
|
{
|
|
|
|
this.state.models.map((model, index) => <Option key={index} value={model.name}>{model.name}</Option>)
|
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</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 16:28:06 +08:00
|
|
|
{Setting.getLabel(i18next.t("role:Sub users"), i18next.t("role:Sub users - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
2022-07-10 15:45:55 +08:00
|
|
|
<Select virtual={false} mode="tags" style={{width: "100%"}} value={this.state.permission.users} onChange={(value => {this.updatePermissionField("users", value);})}>
|
2022-01-01 16:28:06 +08:00
|
|
|
{
|
|
|
|
this.state.users.map((user, index) => <Option key={index} value={`${user.owner}/${user.name}`}>{`${user.owner}/${user.name}`}</Option>)
|
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</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 16:28:06 +08:00
|
|
|
{Setting.getLabel(i18next.t("role:Sub roles"), i18next.t("role:Sub roles - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
2022-07-10 15:45:55 +08:00
|
|
|
<Select virtual={false} mode="tags" style={{width: "100%"}} value={this.state.permission.roles} onChange={(value => {this.updatePermissionField("roles", value);})}>
|
2022-01-01 16:28:06 +08:00
|
|
|
{
|
|
|
|
this.state.roles.filter(roles => (roles.owner !== this.state.roles.owner || roles.name !== this.state.roles.name)).map((permission, index) => <Option key={index} value={`${permission.owner}/${permission.name}`}>{`${permission.owner}/${permission.name}`}</Option>)
|
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</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.permission.domains} onChange={(value => {
|
|
|
|
this.updateRoleField("domains", value);
|
|
|
|
})}>
|
|
|
|
{
|
|
|
|
this.state.permission.domains.map((domain, index) => <Option key={index} value={domain}>{domain}</Option>)
|
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</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 16:28:06 +08:00
|
|
|
{Setting.getLabel(i18next.t("permission:Resource type"), i18next.t("permission:Resource type - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
2022-07-10 15:45:55 +08:00
|
|
|
<Select virtual={false} style={{width: "100%"}} value={this.state.permission.resourceType} onChange={(value => {
|
|
|
|
this.updatePermissionField("resourceType", value);
|
2022-01-01 16:28:06 +08:00
|
|
|
})}>
|
|
|
|
{
|
|
|
|
[
|
2022-07-10 15:45:55 +08:00
|
|
|
{id: "Application", name: "Application"},
|
2022-01-01 16:28:06 +08:00
|
|
|
].map((item, index) => <Option key={index} value={item.id}>{item.name}</Option>)
|
2022-07-13 00:34:35 +08:00
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row style={{marginTop: "20px"}} >
|
|
|
|
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("permission:Resources"), i18next.t("permission:Resources - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Select virtual={false} mode="tags" style={{width: "100%"}} value={this.state.permission.resources} onChange={(value => {this.updatePermissionField("resources", value);})}>
|
|
|
|
{
|
|
|
|
this.state.resources.map((resource, index) => <Option key={index} value={`${resource.name}`}>{`${resource.name}`}</Option>)
|
2022-01-01 16:28:06 +08:00
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</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 16:28:06 +08:00
|
|
|
{Setting.getLabel(i18next.t("permission:Actions"), i18next.t("permission:Actions - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
2022-07-10 15:45:55 +08:00
|
|
|
<Select virtual={false} mode="tags" style={{width: "100%"}} value={this.state.permission.actions} onChange={(value => {
|
|
|
|
this.updatePermissionField("actions", value);
|
2022-01-01 16:28:06 +08:00
|
|
|
})}>
|
|
|
|
{
|
|
|
|
[
|
2022-07-10 15:45:55 +08:00
|
|
|
{id: "Read", name: "Read"},
|
|
|
|
{id: "Write", name: "Write"},
|
|
|
|
{id: "Admin", name: "Admin"},
|
2022-01-01 16:28:06 +08:00
|
|
|
].map((item, index) => <Option key={index} value={item.id}>{item.name}</Option>)
|
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</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 16:28:06 +08:00
|
|
|
{Setting.getLabel(i18next.t("permission:Effect"), i18next.t("permission:Effect - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
2022-07-10 15:45:55 +08:00
|
|
|
<Select virtual={false} style={{width: "100%"}} value={this.state.permission.effect} onChange={(value => {
|
|
|
|
this.updatePermissionField("effect", value);
|
2022-01-01 16:28:06 +08:00
|
|
|
})}>
|
|
|
|
{
|
|
|
|
[
|
2022-07-10 15:45:55 +08:00
|
|
|
{id: "Allow", name: "Allow"},
|
|
|
|
{id: "Deny", name: "Deny"},
|
2022-01-01 16:28:06 +08:00
|
|
|
].map((item, index) => <Option key={index} value={item.id}>{item.name}</Option>)
|
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</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 16:28:06 +08:00
|
|
|
{Setting.getLabel(i18next.t("general:Is enabled"), i18next.t("general:Is enabled - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={1} >
|
|
|
|
<Switch checked={this.state.permission.isEnabled} onChange={checked => {
|
2022-07-10 15:45:55 +08:00
|
|
|
this.updatePermissionField("isEnabled", checked);
|
2022-01-01 16:28:06 +08:00
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
</Card>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2022-01-01 16:28:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
submitPermissionEdit(willExist) {
|
2022-08-08 23:35:24 +08:00
|
|
|
const permission = Setting.deepCopy(this.state.permission);
|
2022-01-01 16:28:06 +08:00
|
|
|
PermissionBackend.updatePermission(this.state.organizationName, this.state.permissionName, permission)
|
|
|
|
.then((res) => {
|
|
|
|
if (res.msg === "") {
|
2022-07-10 15:45:55 +08:00
|
|
|
Setting.showMessage("success", "Successfully saved");
|
2022-01-01 16:28:06 +08:00
|
|
|
this.setState({
|
|
|
|
permissionName: this.state.permission.name,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (willExist) {
|
2022-07-10 15:45:55 +08:00
|
|
|
this.props.history.push("/permissions");
|
2022-01-01 16:28:06 +08:00
|
|
|
} else {
|
|
|
|
this.props.history.push(`/permissions/${this.state.permission.owner}/${this.state.permission.name}`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Setting.showMessage("error", res.msg);
|
2022-07-10 15:45:55 +08:00
|
|
|
this.updatePermissionField("name", this.state.permissionName);
|
2022-01-01 16:28:06 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
Setting.showMessage("error", `Failed to connect to server: ${error}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:16:02 +08:00
|
|
|
deletePermission() {
|
|
|
|
PermissionBackend.deletePermission(this.state.permission)
|
|
|
|
.then(() => {
|
2022-07-10 15:45:55 +08:00
|
|
|
this.props.history.push("/permissions");
|
2022-02-25 18:16:02 +08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
Setting.showMessage("error", `Permission failed to delete: ${error}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-01 16:28:06 +08:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{
|
|
|
|
this.state.permission !== null ? this.renderPermission() : null
|
|
|
|
}
|
2022-07-10 15:45:55 +08:00
|
|
|
<div style={{marginTop: "20px", marginLeft: "40px"}}>
|
2022-01-01 16:28:06 +08:00
|
|
|
<Button size="large" onClick={() => this.submitPermissionEdit(false)}>{i18next.t("general:Save")}</Button>
|
2022-07-10 15:45:55 +08:00
|
|
|
<Button style={{marginLeft: "20px"}} type="primary" size="large" onClick={() => this.submitPermissionEdit(true)}>{i18next.t("general:Save & Exit")}</Button>
|
|
|
|
{this.state.mode === "add" ? <Button style={{marginLeft: "20px"}} size="large" onClick={() => this.deletePermission()}>{i18next.t("general:Cancel")}</Button> : null}
|
2022-01-01 16:28:06 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PermissionEditPage;
|