add option to prompt for a validation password when initializing admin user (#2302)
parent
07dd8b94ed
commit
09acf215f0
@ -0,0 +1,47 @@
|
|||||||
|
import os
|
||||||
|
from flask import current_app, session
|
||||||
|
from flask_restful import Resource, reqparse
|
||||||
|
from libs.helper import str_len
|
||||||
|
from models.model import DifySetup
|
||||||
|
from services.account_service import TenantService
|
||||||
|
|
||||||
|
from . import api
|
||||||
|
from .error import AlreadySetupError, InitValidateFailedError
|
||||||
|
from .wraps import only_edition_self_hosted
|
||||||
|
|
||||||
|
|
||||||
|
class InitValidateAPI(Resource):
|
||||||
|
|
||||||
|
def get(self):
|
||||||
|
init_status = get_init_validate_status()
|
||||||
|
if init_status:
|
||||||
|
return { 'status': 'finished' }
|
||||||
|
return {'status': 'not_started' }
|
||||||
|
|
||||||
|
@only_edition_self_hosted
|
||||||
|
def post(self):
|
||||||
|
# is tenant created
|
||||||
|
tenant_count = TenantService.get_tenant_count()
|
||||||
|
if tenant_count > 0:
|
||||||
|
raise AlreadySetupError()
|
||||||
|
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('password', type=str_len(30),
|
||||||
|
required=True, location='json')
|
||||||
|
input_password = parser.parse_args()['password']
|
||||||
|
|
||||||
|
if input_password != os.environ.get('INIT_PASSWORD'):
|
||||||
|
session['is_init_validated'] = False
|
||||||
|
raise InitValidateFailedError()
|
||||||
|
|
||||||
|
session['is_init_validated'] = True
|
||||||
|
return {'result': 'success'}, 201
|
||||||
|
|
||||||
|
def get_init_validate_status():
|
||||||
|
if current_app.config['EDITION'] == 'SELF_HOSTED':
|
||||||
|
if os.environ.get('INIT_PASSWORD'):
|
||||||
|
return session.get('is_init_validated') or DifySetup.query.first()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
api.add_resource(InitValidateAPI, '/init')
|
||||||
@ -0,0 +1,82 @@
|
|||||||
|
'use client'
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
import { useTranslation } from 'react-i18next'
|
||||||
|
import { useRouter } from 'next/navigation'
|
||||||
|
import Toast from '../components/base/toast'
|
||||||
|
import Loading from '../components/base/loading'
|
||||||
|
import Button from '@/app/components/base/button'
|
||||||
|
import { fetchInitValidateStatus, initValidate } from '@/service/common'
|
||||||
|
import type { InitValidateStatusResponse } from '@/models/common'
|
||||||
|
|
||||||
|
const InitPasswordPopup = () => {
|
||||||
|
const [password, setPassword] = useState('')
|
||||||
|
const [loading, setLoading] = useState(true)
|
||||||
|
const [validated, setValidated] = useState(false)
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
|
const { t } = useTranslation()
|
||||||
|
|
||||||
|
const handleValidation = async () => {
|
||||||
|
setLoading(true)
|
||||||
|
try {
|
||||||
|
const response = await initValidate({ body: { password } })
|
||||||
|
if (response.result === 'success') {
|
||||||
|
setValidated(true)
|
||||||
|
router.push('/install') // or render setup form
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new Error('Validation failed')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (e: any) {
|
||||||
|
Toast.notify({
|
||||||
|
type: 'error',
|
||||||
|
message: e.message,
|
||||||
|
duration: 5000,
|
||||||
|
})
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchInitValidateStatus().then((res: InitValidateStatusResponse) => {
|
||||||
|
if (res.status === 'finished')
|
||||||
|
window.location.href = '/install'
|
||||||
|
else
|
||||||
|
setLoading(false)
|
||||||
|
})
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
loading
|
||||||
|
? <Loading />
|
||||||
|
: <div>
|
||||||
|
{!validated && (
|
||||||
|
<div className="block mx-12 min-w-28">
|
||||||
|
<div className="mb-4">
|
||||||
|
<label htmlFor="password" className="block text-sm font-medium text-gray-700">
|
||||||
|
{t('login.adminInitPassword')}
|
||||||
|
|
||||||
|
</label>
|
||||||
|
<div className="mt-1 relative rounded-md shadow-sm">
|
||||||
|
<input
|
||||||
|
id="password"
|
||||||
|
type="password"
|
||||||
|
value={password}
|
||||||
|
onChange={e => setPassword(e.target.value)}
|
||||||
|
className="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-row flex-wrap justify-stretch p-0">
|
||||||
|
<Button type="primary" onClick={handleValidation} className="basis-full min-w-28">
|
||||||
|
{t('login.validate')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default InitPasswordPopup
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import classNames from 'classnames'
|
||||||
|
import style from '../signin/page.module.css'
|
||||||
|
import InitPasswordPopup from './InitPasswordPopup'
|
||||||
|
|
||||||
|
const Install = () => {
|
||||||
|
return (
|
||||||
|
<div className={classNames(
|
||||||
|
style.background,
|
||||||
|
'flex w-full min-h-screen',
|
||||||
|
'p-4 lg:p-8',
|
||||||
|
'gap-x-20',
|
||||||
|
'justify-center lg:justify-start',
|
||||||
|
)}>
|
||||||
|
<div className="block m-auto w-96">
|
||||||
|
<InitPasswordPopup />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Install
|
||||||
Loading…
Reference in New Issue