1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-05 17:02:47 +00:00

Webdav secret (#5357)

* not show webdav password on user profile setting page

* hash webdav password

* show webdav url and username on user profile setting page

* update test

* update

* update

Co-authored-by: lian <lian@seafile.com>
This commit is contained in:
lian
2023-01-18 09:59:53 +08:00
committed by GitHub
parent d2aa07da63
commit ff9a0e92f9
10 changed files with 305 additions and 66 deletions

View File

@@ -0,0 +1,53 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Modal, ModalHeader, ModalBody, ModalFooter, Alert, Button, Input, InputGroup, InputGroupAddon } from 'reactstrap';
import { gettext } from '../../utils/constants';
import { Utils } from '../../utils/utils';
const propTypes = {
removePassword: PropTypes.func.isRequired,
toggle: PropTypes.func.isRequired
};
class RemoveWebdavPassword extends Component {
constructor(props) {
super(props);
this.state = {
btnDisabled: false,
errMsg: ''
};
}
submit = () => {
this.setState({
btnDisabled: true
});
this.props.removePassword();
}
render() {
const { toggle } = this.props;
let dialogMsg = gettext('Are you sure you want to remove {placeholder} ?').replace('{placeholder}', 'WebDAV password');
return (
<Modal centered={true} isOpen={true} toggle={toggle}>
<ModalHeader toggle={toggle}>{gettext('Remove WebDAV Password')}</ModalHeader>
<ModalBody>
<p>{dialogMsg}</p>
</ModalBody>
{this.state.errMsg && <Alert color="danger" className="m-0 mt-2">{gettext(this.state.errMsg)}</Alert>}
<ModalFooter>
<Button color="secondary" onClick={toggle}>{gettext('Cancel')}</Button>
<Button color="primary" onClick={this.submit} disabled={this.state.btnDisabled}>{gettext('Submit')}</Button>
</ModalFooter>
</Modal>
);
}
}
RemoveWebdavPassword.propTypes = propTypes;
export default RemoveWebdavPassword;

View File

@@ -5,19 +5,18 @@ import { gettext } from '../../utils/constants';
import { Utils } from '../../utils/utils';
const propTypes = {
password: PropTypes.string.isRequired,
updatePassword: PropTypes.func.isRequired,
resetPassword: PropTypes.func.isRequired,
toggle: PropTypes.func.isRequired
};
const { webdavSecretMinLength, webdavSecretStrengthLevel } = window.app.pageOptions;
class UpdateWebdavPassword extends Component {
class ResetWebdavPassword extends Component {
constructor(props) {
super(props);
this.state = {
password: this.props.password,
password: '',
isPasswordVisible: false,
btnDisabled: false,
errMsg: ''
@@ -44,7 +43,7 @@ class UpdateWebdavPassword extends Component {
btnDisabled: true
});
this.props.updatePassword(this.state.password.trim());
this.props.resetPassword(this.state.password.trim());
}
handleInputChange = (e) => {
@@ -71,7 +70,7 @@ class UpdateWebdavPassword extends Component {
return (
<Modal centered={true} isOpen={true} toggle={toggle}>
<ModalHeader toggle={toggle}>{gettext('WebDav Password')}</ModalHeader>
<ModalHeader toggle={toggle}>{gettext('Reset WebDAV Password')}</ModalHeader>
<ModalBody>
<InputGroup>
<Input type={this.state.isPasswordVisible ? 'text' : 'password'} value={this.state.password} onChange={this.handleInputChange} autoComplete="new-password"/>
@@ -92,6 +91,6 @@ class UpdateWebdavPassword extends Component {
}
}
UpdateWebdavPassword.propTypes = propTypes;
ResetWebdavPassword.propTypes = propTypes;
export default UpdateWebdavPassword;
export default ResetWebdavPassword;

View File

@@ -0,0 +1,96 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Modal, ModalHeader, ModalBody, ModalFooter, Alert, Button, Input, InputGroup, InputGroupAddon } from 'reactstrap';
import { gettext } from '../../utils/constants';
import { Utils } from '../../utils/utils';
const propTypes = {
setPassword: PropTypes.func.isRequired,
toggle: PropTypes.func.isRequired
};
const { webdavSecretMinLength, webdavSecretStrengthLevel } = window.app.pageOptions;
class SetWebdavPassword extends Component {
constructor(props) {
super(props);
this.state = {
password: '',
isPasswordVisible: false,
btnDisabled: false,
errMsg: ''
};
}
submit = () => {
if (this.state.password.length === 0) {
this.setState({errMsg: gettext('Please enter a password.')});
return false;
}
if (this.state.password.length < webdavSecretMinLength) {
this.setState({errMsg: gettext('The password is too short.')});
return false;
}
if (Utils.getStrengthLevel(this.state.password) < webdavSecretStrengthLevel) {
this.setState({errMsg: gettext('The password is too weak. It should include at least {passwordStrengthLevel} of the following: number, upper letter, lower letter and other symbols.').replace('{passwordStrengthLevel}', webdavSecretStrengthLevel)});
return false;
}
this.setState({
btnDisabled: true
});
this.props.setPassword(this.state.password.trim());
}
handleInputChange = (e) => {
this.setState({password: e.target.value});
}
togglePasswordVisible = () => {
this.setState({
isPasswordVisible: !this.state.isPasswordVisible
});
}
generatePassword = () => {
let randomPassword = Utils.generatePassword(webdavSecretMinLength);
this.setState({
password: randomPassword,
isPasswordVisible: true
});
}
render() {
const { toggle } = this.props;
const passwordTip = gettext('(at least {passwordMinLength} characters and includes {passwordStrengthLevel} of the following: number, upper letter, lower letter and other symbols)').replace('{passwordMinLength}', webdavSecretMinLength).replace('{passwordStrengthLevel}', webdavSecretStrengthLevel);
return (
<Modal centered={true} isOpen={true} toggle={toggle}>
<ModalHeader toggle={toggle}>{gettext('Set WebDAV Password')}</ModalHeader>
<ModalBody>
<InputGroup>
<Input type={this.state.isPasswordVisible ? 'text' : 'password'} value={this.state.password} onChange={this.handleInputChange} autoComplete="new-password"/>
<InputGroupAddon addonType="append">
<Button onClick={this.togglePasswordVisible}><i className={`fas ${this.state.isPasswordVisible ? 'fa-eye': 'fa-eye-slash'}`}></i></Button>
<Button onClick={this.generatePassword}><i className="fas fa-magic"></i></Button>
</InputGroupAddon>
</InputGroup>
<p className="form-text text-muted m-0">{passwordTip}</p>
{this.state.errMsg && <Alert color="danger" className="m-0 mt-2">{gettext(this.state.errMsg)}</Alert>}
</ModalBody>
<ModalFooter>
<Button color="secondary" onClick={toggle}>{gettext('Cancel')}</Button>
<Button color="primary" onClick={this.submit} disabled={this.state.btnDisabled}>{gettext('Submit')}</Button>
</ModalFooter>
</Modal>
);
}
}
SetWebdavPassword.propTypes = propTypes;
export default SetWebdavPassword;

View File

@@ -4,44 +4,78 @@ import { gettext } from '../../utils/constants';
import { seafileAPI } from '../../utils/seafile-api';
import { Utils } from '../../utils/utils';
import toaster from '../toast';
import UpdateWebdavPassword from '../dialog/update-webdav-password';
import SetWebdavPassword from '../dialog/set-webdav-password';
import ResetWebdavPassword from '../dialog/reset-webdav-password';
import RemoveWebdavPassword from '../dialog/remove-webdav-password';
const { webdavPasswd } = window.app.pageOptions;
const { username, webdavUrl, webdavPasswordSetted } = window.app.pageOptions;
class WebdavPassword extends React.Component {
constructor(props) {
super(props);
this.state = {
password: webdavPasswd,
isPasswordVisible: false,
isDialogOpen: false
isWebdavPasswordSetted: webdavPasswordSetted,
isSetPasserdDialogOpen: false,
isResetPasserdDialogOpen: false,
isRemovePasserdDialogOpen: false,
};
}
togglePasswordVisible = () => {
toggleSetPasswordDialog = () => {
this.setState({
isPasswordVisible: !this.state.isPasswordVisible
isSetPasserdDialogOpen: !this.state.isSetPasserdDialogOpen,
});
}
updatePassword = (password) => {
setPassword = (password) => {
seafileAPI.updateWebdavSecret(password).then((res) => {
this.toggleDialog();
this.toggleSetPasswordDialog();
this.setState({
password: password
isWebdavPasswordSetted: !this.state.isWebdavPasswordSetted,
});
toaster.success(gettext('Success'));
}).catch((error) => {
let errorMsg = Utils.getErrorMsg(error);
this.toggleDialog();
this.toggleSetPasswordDialog();
toaster.danger(errorMsg);
});
}
toggleDialog = () => {
toggleResetPasswordDialog = () => {
this.setState({
isDialogOpen: !this.state.isDialogOpen
isResetPasswordDialogOpen: !this.state.isResetPasswordDialogOpen,
});
}
resetPassword = (password) => {
seafileAPI.updateWebdavSecret(password).then((res) => {
this.toggleResetPasswordDialog();
toaster.success(gettext('Success'));
}).catch((error) => {
let errorMsg = Utils.getErrorMsg(error);
this.toggleResetPasswordDialog();
toaster.danger(errorMsg);
});
}
toggleRemovePasswordDialog = () => {
this.setState({
isRemovePasswordDialogOpen: !this.state.isRemovePasswordDialogOpen,
});
}
removePassword = () => {
seafileAPI.updateWebdavSecret().then((res) => {
this.toggleRemovePasswordDialog();
this.setState({
isWebdavPasswordSetted: !this.state.isWebdavPasswordSetted,
});
toaster.success(gettext('Success'));
}).catch((error) => {
let errorMsg = Utils.getErrorMsg(error);
this.toggleRemovePasswordDialog();
toaster.danger(errorMsg);
});
}
@@ -52,30 +86,47 @@ class WebdavPassword extends React.Component {
}
render() {
const { password, isPasswordVisible } = this.state;
const { isWebdavPasswordSetted } = this.state;
return (
<React.Fragment>
<div id="update-webdav-passwd" className="setting-item">
<h3 className="setting-item-heading">{gettext('WebDav Password')}</h3>
{password ? (
<React.Fragment>
<div className="d-flex align-items-center">
<label className="m-0 mr-2" htmlFor="passwd">{gettext('Password:')}</label>
<input id="passwd" className="border-0 mr-1" type="text" value={isPasswordVisible ? password : '**********'} readOnly={true} size={Math.max(password.length, 10)} />
<span tabIndex="0" role="button" aria-label={isPasswordVisible? gettext('Hide') : gettext('Show')} onClick={this.togglePasswordVisible} onKeyDown={this.onIconKeyDown} className={`eye-icon fas ${this.state.isPasswordVisible ? 'fa-eye': 'fa-eye-slash'}`}></span>
</div>
<button className="btn btn-outline-primary mt-2" onClick={this.toggleDialog}>{gettext('Update')}</button>
</React.Fragment>
) : (
<button className="btn btn-outline-primary" onClick={this.toggleDialog}>{gettext('Set Password')}</button>
)}
<h3 className="setting-item-heading">{gettext('WebDAV Password')}</h3>
<p>{gettext('WebDAV URL:')}<a href={webdavUrl}> {webdavUrl}</a></p>
<p>{gettext('WebDAV username:')} {username}</p>
{!isWebdavPasswordSetted ?
<React.Fragment>
<p>{gettext('WebDAV password:')} {gettext('not set')}</p>
<button className="btn btn-outline-primary" onClick={this.toggleSetPasswordDialog}>{gettext('Set Password')}</button>
</React.Fragment>
:
<React.Fragment>
<p>{gettext('WebDAV password:')} ***</p>
<button className="btn btn-outline-primary mr-2" onClick={this.toggleResetPasswordDialog}>{gettext('Reset Password')}</button>
<button className="btn btn-outline-primary" onClick={this.toggleRemovePasswordDialog}>{gettext('Remove Password')}</button>
</React.Fragment>
}
</div>
{this.state.isDialogOpen && (
{this.state.isSetPasserdDialogOpen && (
<ModalPortal>
<UpdateWebdavPassword
password={this.state.password}
updatePassword={this.updatePassword}
toggle={this.toggleDialog}
<SetWebdavPassword
setPassword={this.setPassword}
toggle={this.toggleSetPasswordDialog}
/>
</ModalPortal>
)}
{this.state.isResetPasswordDialogOpen && (
<ModalPortal>
<ResetWebdavPassword
resetPassword={this.resetPassword}
toggle={this.toggleResetPasswordDialog}
/>
</ModalPortal>
)}
{this.state.isRemovePasswordDialogOpen && (
<ModalPortal>
<RemoveWebdavPassword
removePassword={this.removePassword}
toggle={this.toggleRemovePasswordDialog}
/>
</ModalPortal>
)}