新增“数据编辑”

This commit is contained in:
2025-11-10 09:31:54 +08:00
parent aba94c074a
commit 61b1d93718
3 changed files with 34 additions and 22 deletions

View File

@@ -39,6 +39,8 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'accounts', 'accounts',
'main', 'main',
'elastic',
'django_elasticsearch_dsl',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@@ -132,3 +134,17 @@ X_FRAME_OPTIONS = 'DENY'
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Elasticsearch configuration
ELASTICSEARCH_DSL = {
'default': {
'hosts': 'localhost:9200'
},
}
# Elasticsearch index settings
ELASTICSEARCH_INDEX_NAMES = {
'elastic.documents.AchievementDocument': 'wordsearch266666',
'elastic.documents.UserDocument': 'users',
'elastic.documents.NewsDocument': 'elastic_news',
}

View File

@@ -22,5 +22,6 @@ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls', namespace='accounts')), path('accounts/', include('accounts.urls', namespace='accounts')),
path('main/', include('main.urls', namespace='main')), path('main/', include('main.urls', namespace='main')),
path('elastic/', include('elastic.urls', namespace='elastic')),
path('', main_home, name='root_home'), path('', main_home, name='root_home'),
] ]

View File

@@ -1,5 +1,6 @@
import base64 import base64
import hashlib import hashlib
from elastic.es_connect import get_user_by_username as es_get_user_by_username
def _salt_for_username(username: str) -> bytes: def _salt_for_username(username: str) -> bytes:
@@ -12,27 +13,21 @@ def _derive_password(password_plain: str, salt: bytes) -> bytes:
def get_user_by_username(username: str): def get_user_by_username(username: str):
""" """
Placeholder for ES lookup. Returns fixed JSON for a demo user. 从Elasticsearch获取用户数据
In production this should query ES with the given mapping.
Demo user:
- username: admin
- password: Password123! (stored as PBKDF2-derived secret only)
- user_id: 1
- premission: 0 (admin)
""" """
if username != 'admin': # 首先尝试从ES获取用户数据
return None es_user = es_get_user_by_username(username)
salt = _salt_for_username(username) salt = _salt_for_username(username)
# Demo: derive and store secret from a known password for the placeholder derived = _derive_password(es_user.get('password', ''), salt)
derived = _derive_password('Password123!', salt) if es_user:
return { # 如果ES中有用户数据使用ES中的密码
'user_id': 1, return {
'username': 'admin', 'user_id': es_user.get('user_id', 0),
# Store only the derived secret, not the plaintext password 'username': es_user.get('username', ''),
'password': base64.b64encode(derived).decode('ascii'), 'password': base64.b64encode(derived).decode('ascii'),
'premission': 0, 'premission': es_user.get('permission', 1),
# Expose salt to the client during challenge so both sides derive consistently 'salt': base64.b64encode(salt).decode('ascii'),
'salt': base64.b64encode(salt).decode('ascii'), }
}
return None