feat: set password for users

Signed-off-by: Kininaru <shiftregister233@outlook.com>

removed useless blank

Signed-off-by: Kininaru <shiftregister233@outlook.com>

add license header

Signed-off-by: Kininaru <shiftregister233@outlook.com>

i18n

Signed-off-by: Kininaru <shiftregister233@outlook.com>

database logic

Signed-off-by: Kininaru <shiftregister233@outlook.com>

i18n

Signed-off-by: Kininaru <shiftregister233@outlook.com>

i18n

Signed-off-by: Kininaru <shiftregister233@outlook.com>
This commit is contained in:
Kininaru
2021-05-09 19:54:20 +08:00
parent 19c1ef1b68
commit 33e61cc5cf
8 changed files with 220 additions and 7 deletions

93
web/src/PasswordModal.js Normal file
View File

@ -0,0 +1,93 @@
// 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);
const {user} = props;
const showModal = () => {
setVisible(true);
};
const handleCancel = () => {
setVisible(false);
};
const handleOk = () => {
let oldPassword = document.getElementById("old-password")?.value;
let newPassword = document.getElementById("new-password").value;
let rePassword = document.getElementById("re-new-password").value;
if (oldPassword === null || oldPassword === undefined) oldPassword = "";
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
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}}>
{ hasOldPassword ? (
<Row style={{width: "100%", marginBottom: "20px"}}>
<Input.Password addonBefore={i18next.t("user:Old Password")} placeholder={i18next.t("user:input password")} id="old-password"/>
</Row>
) : null}
<Row style={{width: "100%", marginBottom: "20px"}}>
<Input.Password addonBefore={i18next.t("user:New Password")} placeholder={i18next.t("user:input password")} id="new-password"/>
</Row>
<Row style={{width: "100%", marginBottom: "20px"}}>
<Input.Password addonBefore={i18next.t("user:Re-enter New")} placeholder={i18next.t("user:input password")} id="re-new-password"/>
</Row>
</Col>
</Modal>
</Row>
)
}
export default PasswordModal;

View File

@ -24,6 +24,7 @@ import * as AuthBackend from "./auth/AuthBackend";
import * as ApplicationBackend from "./backend/ApplicationBackend";
import * as ProviderBackend from "./backend/ProviderBackend";
import * as Provider from "./auth/Provider";
import PasswordModal from "./PasswordModal";
const { Option } = Select;
@ -266,11 +267,7 @@ class UserEditPage extends React.Component {
{i18next.t("general:Password")}:
</Col>
<Col span={22} >
<Row>
<Button type="default">
{this.state.user.password === "" ? i18next.t("user:Set password...") : i18next.t("user:Modify password...")}
</Button>
</Row>
<PasswordModal user={this.state.user} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >

View File

@ -79,3 +79,16 @@ export function uploadAvatar(avatar) {
});
});
}
export function setPassword(userOwner, userName, oldPassword, newPassword) {
let formData = new FormData();
formData.append("userOwner", userOwner);
formData.append("userName", userName);
formData.append("oldPassword", oldPassword);
formData.append("newPassword", newPassword);
return fetch(`${Setting.ServerUrl}/api/set-password`, {
method: "POST",
credentials: "include",
body: formData
}).then(res => res.json());
}

View File

@ -98,7 +98,24 @@
"Unlink": "Unlink",
"Is admin": "Is admin",
"Is global admin": "Is global admin",
"Is forbidden": "Is forbidden"
"Is forbidden": "Is forbidden",
"Empty input!": "Empty input!",
"Two passwords you typed do not match.": "Two passwords you typed do not match.",
"Password Set": "Password Set",
"Set Password": "Set Password",
"Old Password": "Old Password",
"New Password": "New Password",
"Re-enter New": "Re-enter New",
"Please login first.": "Please login first.",
"Session outdated. Please login again.": "Session outdated. Please login again.",
"Invalid user id.": "Invalid user id.",
"You don't have the permission to do this.": "You don't have the permission to do this.",
"Old password wrong.": "Old password wrong.",
"New password contains blank space.": "New password contains blank space.",
"Invalid new password": "Invalid new password",
"Password": "Password",
"Cancel": "Cancel",
"input password": "input password"
},
"application":
{

View File

@ -100,7 +100,24 @@
"Unlink": "解绑",
"Is admin": "是管理员",
"Is global admin": "是全局管理员",
"Is forbidden": "被禁用"
"Is forbidden": "被禁用",
"Empty input!": "输入为空!",
"Two passwords you typed do not match.": "两次输入的密码不匹配。",
"Password Set": "密码已设置",
"Set Password": "设置密码",
"Old Password": "旧密码",
"New Password": "新密码",
"Re-enter New": "重复新密码",
"Please login first.": "请先登录。",
"Session outdated. Please login again.": "会话已过期,请重新登陆。",
"Invalid user id.": "用户 ID 错误",
"You don't have the permission to do this.": "你没有权限这么做。",
"Old password wrong.": "旧密码错误!",
"New password contains blank space.": "新密码包含空格。",
"Invalid new password": "非法的新密码。",
"Password": "密码",
"Cancel": "取消",
"input password": "输入密码"
},
"application":
{