From 34deef3e8140d7322b696bd3c5afa6041f9ea633 Mon Sep 17 00:00:00 2001 From: aji47 Date: Sat, 26 Sep 2020 11:44:53 +0800 Subject: [PATCH] global password --- src/client/AdminPage.js | 2 +- src/client/App.js | 34 ++++++++++++++++++++++++++++++++++ src/client/clientUtil.js | 11 +++++++++++ src/client/style/App.scss | 11 +++++++++++ src/config/user-config.js | 8 +++++++- 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/client/AdminPage.js b/src/client/AdminPage.js index 88d33337..4c855fb7 100644 --- a/src/client/AdminPage.js +++ b/src/client/AdminPage.js @@ -148,7 +148,7 @@ export default class AdminPage extends Component { content = (
You are authorized to move/delete files.
) }else{ content = ( -
Type password to move/delete file
+
Enter password to move/delete file
this.passwordInputRef = pathInput} placeholder="...type here" onChange={this.setPasswordCookie.bind(this)}/> diff --git a/src/client/App.js b/src/client/App.js index 1c184f1f..f0b0eab4 100644 --- a/src/client/App.js +++ b/src/client/App.js @@ -13,6 +13,8 @@ import screenfull from 'screenfull'; const clientUtil = require("./clientUtil"); const { getSearchInputText } = clientUtil; import { ToastContainer } from 'react-toastify'; +import ReactDOM from 'react-dom'; +import Cookie from "js-cookie"; import 'react-toastify/dist/ReactToastify.css'; // http://localhost:3000/ @@ -102,8 +104,40 @@ class App extends Component { // You can also log the error to an error reporting service console.error(error, info); } + + getPasswordInput(){ + const pathInput = ReactDOM.findDOMNode(this.passwordInputRef); + const text = (pathInput && pathInput.value) || ""; + return text; + } + + setPasswordCookie(){ + const text = this.getPasswordInput(); + Cookie.set("home-password", text, { expires: 3 }); + this.forceUpdate(); + } + + renderPasswordInput(){ + let content = ( +
Enter password to use Shigureader
+
+ this.passwordInputRef = pathInput} + placeholder="...type here" onChange={this.setPasswordCookie.bind(this)}/> +
+
); + + return ( +
+ {content} +
+ ) + } render() { + if(!clientUtil.isAllowedToEnter()){ + return this.renderPasswordInput(); + } + // document.title = this.getWebTitle(); if(this.searchText){ const path = clientUtil.getSearhLink(this.searchText); diff --git a/src/client/clientUtil.js b/src/client/clientUtil.js index 5cd3aa34..09ceba60 100644 --- a/src/client/clientUtil.js +++ b/src/client/clientUtil.js @@ -80,6 +80,17 @@ module.exports.isAuthorized = function(){ } } +module.exports.isAllowedToEnter = function(){ + const userConfig = require('@config/user-config'); + if(!userConfig.home_password){ + return true; + } + + const Cookie = require("js-cookie"); + const password = Cookie.get('home-password'); + return userConfig.home_password === password; +} + // module.exports.cleanSearchStr = function(str){ // // search/ケマオ9% will break everything // // it is too troublesome to do everything in url encoding diff --git a/src/client/style/App.scss b/src/client/style/App.scss index 13090835..214f4498 100644 --- a/src/client/style/App.scss +++ b/src/client/style/App.scss @@ -129,4 +129,15 @@ a { } } } +} + +//https://stackoverflow.com/questions/31217268/center-div-on-the-middle-of-screen +.home-admin-section{ + position: absolute; + top: 50%; + left: 50%; + margin-top: -50px; + margin-left: -50px; + width: 300px; + height: 200px; } \ No newline at end of file diff --git a/src/config/user-config.js b/src/config/user-config.js index 704b9e3c..e1c206ba 100644 --- a/src/config/user-config.js +++ b/src/config/user-config.js @@ -101,4 +101,10 @@ module.exports.img_reduce_resolution_dimension = "2800x2800"; //But this may cause more Memory usage //可以阅读文件夹的图片,就不需要打包成zip //但可能用很多内存 -module.exports.view_img_folder = true; \ No newline at end of file +module.exports.view_img_folder = true; + + +//global password +//when set, user need to enter password to use ShiguReader +//全局密码,设置以后用户必须输入密码才能打开网站 +module.exports.home_password = "" \ No newline at end of file