2021-05-09 19:54:20 +08:00
|
|
|
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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 {Button, Col, Modal, Row, Input,} from "antd";
|
|
|
|
import i18next from "i18next";
|
|
|
|
import React from "react";
|
|
|
|
import * as UserBackend from "./backend/UserBackend";
|
|
|
|
import * as Setting from "./Setting";
|
|
|
|
|
|
|
|
export const PasswordModal = (props) => {
|
|
|
|
const [visible, setVisible] = React.useState(false);
|
|
|
|
const [confirmLoading, setConfirmLoading] = React.useState(false);
|
2021-05-15 12:35:49 +08:00
|
|
|
const [oldPassword, setOldPassword] = React.useState("");
|
|
|
|
const [newPassword, setNewPassword] = React.useState("");
|
|
|
|
const [rePassword, setRePassword] = React.useState("");
|
2021-05-09 19:54:20 +08:00
|
|
|
const {user} = props;
|
2021-12-12 19:15:24 +08:00
|
|
|
const {account} = props;
|
2021-05-09 19:54:20 +08:00
|
|
|
|
|
|
|
const showModal = () => {
|
|
|
|
setVisible(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
|
|
|
setVisible(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleOk = () => {
|
|
|
|
if (newPassword === "" || rePassword === "") {
|
|
|
|
Setting.showMessage("error", i18next.t("user:Empty input!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (newPassword !== rePassword) {
|
|
|
|
Setting.showMessage("error", i18next.t("user:Two passwords you typed do not match."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setConfirmLoading(true);
|
|
|
|
UserBackend.setPassword(user.owner, user.name, oldPassword, newPassword).then((res) => {
|
|
|
|
setConfirmLoading(false);
|
|
|
|
if (res.status === "ok") {
|
|
|
|
Setting.showMessage("success", i18next.t("user:Password Set"));
|
|
|
|
setVisible(false);
|
|
|
|
}
|
|
|
|
else Setting.showMessage("error", i18next.t(`user:${res.msg}`));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let hasOldPassword = user.password !== "";
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Row>
|
|
|
|
<Button type="default" onClick={showModal}>
|
|
|
|
{ hasOldPassword ? i18next.t("user:Modify password...") : i18next.t("user:Set password...")}
|
|
|
|
</Button>
|
|
|
|
<Modal
|
2021-06-10 20:04:39 +08:00
|
|
|
maskClosable={false}
|
2021-05-09 19:54:20 +08:00
|
|
|
title={i18next.t("user:Password")}
|
|
|
|
visible={visible}
|
|
|
|
okText={i18next.t("user:Set Password")}
|
|
|
|
cancelText={i18next.t("user:Cancel")}
|
|
|
|
confirmLoading={confirmLoading}
|
|
|
|
onCancel={handleCancel}
|
|
|
|
onOk={handleOk}
|
|
|
|
width={600}
|
|
|
|
>
|
|
|
|
<Col style={{margin: "0px auto 40px auto", width: 1000, height: 300}}>
|
2021-12-12 19:15:24 +08:00
|
|
|
{ (hasOldPassword && !Setting.isAdminUser(account)) ? (
|
2021-05-09 19:54:20 +08:00
|
|
|
<Row style={{width: "100%", marginBottom: "20px"}}>
|
2021-05-15 12:35:49 +08:00
|
|
|
<Input.Password addonBefore={i18next.t("user:Old Password")} placeholder={i18next.t("user:input password")} onChange={(e) => setOldPassword(e.target.value)}/>
|
2021-05-09 19:54:20 +08:00
|
|
|
</Row>
|
|
|
|
) : null}
|
|
|
|
<Row style={{width: "100%", marginBottom: "20px"}}>
|
2021-05-15 12:35:49 +08:00
|
|
|
<Input.Password addonBefore={i18next.t("user:New Password")} placeholder={i18next.t("user:input password")} onChange={(e) => setNewPassword(e.target.value)}/>
|
2021-05-09 19:54:20 +08:00
|
|
|
</Row>
|
|
|
|
<Row style={{width: "100%", marginBottom: "20px"}}>
|
2021-05-15 12:35:49 +08:00
|
|
|
<Input.Password addonBefore={i18next.t("user:Re-enter New")} placeholder={i18next.t("user:input password")} onChange={(e) => setRePassword(e.target.value)}/>
|
2021-05-09 19:54:20 +08:00
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
</Modal>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PasswordModal;
|