fix user manage
This commit is contained in:
parent
3d5fea971a
commit
9d9fde387c
11
Dockerfile
11
Dockerfile
@ -4,21 +4,14 @@ ENV LANG="C.UTF-8" \
|
|||||||
PUID=0 \
|
PUID=0 \
|
||||||
PGID=0 \
|
PGID=0 \
|
||||||
UMASK=000 \
|
UMASK=000 \
|
||||||
WORKDIR="/app" \
|
|
||||||
CONFIG_DIR="/config" \
|
CONFIG_DIR="/config" \
|
||||||
API_TOKEN="moviepilot" \
|
API_TOKEN="moviepilot" \
|
||||||
SUPERUSER="admin" \
|
|
||||||
SUPERUSER_PASSWORD="password" \
|
|
||||||
AUTH_SITE="iyuu" \
|
AUTH_SITE="iyuu" \
|
||||||
LIBRARY_PATH="" \
|
LIBRARY_PATH="" \
|
||||||
DOWNLOAD_PATH="/downloads" \
|
DOWNLOAD_PATH="/downloads" \
|
||||||
TORRENT_TAG="MOVIEPILOT" \
|
|
||||||
SEARCH_SOURCE="themoviedb" \
|
|
||||||
SCRAP_SOURCE="themoviedb" \
|
|
||||||
COOKIECLOUD_HOST="https://nastool.org/cookiecloud" \
|
COOKIECLOUD_HOST="https://nastool.org/cookiecloud" \
|
||||||
COOKIECLOUD_KEY="" \
|
COOKIECLOUD_KEY="" \
|
||||||
COOKIECLOUD_PASSWORD="" \
|
COOKIECLOUD_PASSWORD="" \
|
||||||
USER_AGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57" \
|
|
||||||
MESSAGER="telegram" \
|
MESSAGER="telegram" \
|
||||||
TELEGRAM_TOKEN="" \
|
TELEGRAM_TOKEN="" \
|
||||||
TELEGRAM_CHAT_ID="" \
|
TELEGRAM_CHAT_ID="" \
|
||||||
@ -32,7 +25,7 @@ ENV LANG="C.UTF-8" \
|
|||||||
FILTER_RULE="!BLU & 4K & CN > !BLU & 1080P & CN > !BLU & 4K > !BLU & 1080P" \
|
FILTER_RULE="!BLU & 4K & CN > !BLU & 1080P & CN > !BLU & 4K > !BLU & 1080P" \
|
||||||
TRANSFER_TYPE="copy" \
|
TRANSFER_TYPE="copy" \
|
||||||
DOUBAN_USER_IDS=""
|
DOUBAN_USER_IDS=""
|
||||||
WORKDIR ${WORKDIR}
|
WORKDIR "/app"
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
&& apt-get -y install musl-dev nginx \
|
&& apt-get -y install musl-dev nginx \
|
||||||
@ -40,7 +33,7 @@ RUN apt-get update \
|
|||||||
&& cp -f nginx.conf /etc/nginx/nginx.conf \
|
&& cp -f nginx.conf /etc/nginx/nginx.conf \
|
||||||
&& pip install -r requirements.txt \
|
&& pip install -r requirements.txt \
|
||||||
&& python_ver=$(python3 -V | awk '{print $2}') \
|
&& python_ver=$(python3 -V | awk '{print $2}') \
|
||||||
&& echo "${WORKDIR}/" > /usr/local/lib/python${python_ver%.*}/site-packages/app.pth \
|
&& echo "/app/" > /usr/local/lib/python${python_ver%.*}/site-packages/app.pth \
|
||||||
&& echo 'fs.inotify.max_user_watches=5242880' >> /etc/sysctl.conf \
|
&& echo 'fs.inotify.max_user_watches=5242880' >> /etc/sysctl.conf \
|
||||||
&& echo 'fs.inotify.max_user_instances=5242880' >> /etc/sysctl.conf \
|
&& echo 'fs.inotify.max_user_instances=5242880' >> /etc/sysctl.conf \
|
||||||
&& playwright install-deps chromium \
|
&& playwright install-deps chromium \
|
||||||
|
@ -9,6 +9,7 @@ from app import schemas
|
|||||||
from app.chain.user import UserChain
|
from app.chain.user import UserChain
|
||||||
from app.core import security
|
from app.core import security
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
|
from app.core.security import get_password_hash
|
||||||
from app.db import get_db
|
from app.db import get_db
|
||||||
from app.db.models.user import User
|
from app.db.models.user import User
|
||||||
from app.log import logger
|
from app.log import logger
|
||||||
@ -42,7 +43,8 @@ async def login_access_token(
|
|||||||
user = User.get_by_name(db=db, name=form_data.username)
|
user = User.get_by_name(db=db, name=form_data.username)
|
||||||
if not user:
|
if not user:
|
||||||
logger.info(f"用户不存在,创建用户: {form_data.username}")
|
logger.info(f"用户不存在,创建用户: {form_data.username}")
|
||||||
user = User(name=form_data.username, is_active=True, is_superuser=False)
|
user = User(name=form_data.username, is_active=True,
|
||||||
|
is_superuser=False, hashed_password=get_password_hash(token))
|
||||||
user.create(db)
|
user.create(db)
|
||||||
elif not user.is_active:
|
elif not user.is_active:
|
||||||
raise HTTPException(status_code=403, detail="用户未启用")
|
raise HTTPException(status_code=403, detail="用户未启用")
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
import json
|
import json
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException, Depends
|
||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import StreamingResponse
|
||||||
|
|
||||||
|
from app import schemas
|
||||||
from app.core.security import verify_token
|
from app.core.security import verify_token
|
||||||
|
from app.db.systemconfig_oper import SystemConfigOper
|
||||||
from app.helper.message import MessageHelper
|
from app.helper.message import MessageHelper
|
||||||
from app.helper.progress import ProgressHelper
|
from app.helper.progress import ProgressHelper
|
||||||
|
|
||||||
@ -34,6 +37,23 @@ def get_progress(process_type: str, token: str):
|
|||||||
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/setting", summary="查询系统设置")
|
||||||
|
def get_setting(key: str, _: schemas.TokenPayload = Depends(verify_token)):
|
||||||
|
"""
|
||||||
|
查询系统设置
|
||||||
|
"""
|
||||||
|
return schemas.Response(success=True, data=SystemConfigOper().get(key))
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("/setting", summary="更新系统设置")
|
||||||
|
def set_setting(key: str, value: Any, _: schemas.TokenPayload = Depends(verify_token)):
|
||||||
|
"""
|
||||||
|
更新系统设置
|
||||||
|
"""
|
||||||
|
SystemConfigOper().set(key, value)
|
||||||
|
return schemas.Response(success=True)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/message", summary="实时消息")
|
@router.get("/message", summary="实时消息")
|
||||||
def get_progress(token: str):
|
def get_progress(token: str):
|
||||||
"""
|
"""
|
||||||
|
@ -24,7 +24,7 @@ def read_users(
|
|||||||
return users
|
return users
|
||||||
|
|
||||||
|
|
||||||
@router.post("/", summary="新增用户", response_model=schemas.User)
|
@router.post("/", summary="新增用户", response_model=schemas.Response)
|
||||||
def create_user(
|
def create_user(
|
||||||
*,
|
*,
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
@ -36,63 +36,64 @@ def create_user(
|
|||||||
"""
|
"""
|
||||||
user = current_user.get_by_name(db, name=user_in.name)
|
user = current_user.get_by_name(db, name=user_in.name)
|
||||||
if user:
|
if user:
|
||||||
raise HTTPException(
|
return schemas.Response(success=False, message="用户已存在")
|
||||||
status_code=400,
|
|
||||||
detail="用户已存在",
|
|
||||||
)
|
|
||||||
user_info = user_in.dict()
|
user_info = user_in.dict()
|
||||||
if user_info.get("password"):
|
if user_info.get("password"):
|
||||||
user_info["hashed_password"] = get_password_hash(user_info["password"])
|
user_info["hashed_password"] = get_password_hash(user_info["password"])
|
||||||
user_info.pop("password")
|
user_info.pop("password")
|
||||||
user = User(**user_info)
|
user = User(**user_info)
|
||||||
user = user.create(db)
|
user = user.create(db)
|
||||||
return user
|
return schemas.Response(success=True)
|
||||||
|
|
||||||
|
|
||||||
@router.put("/", summary="更新用户", response_model=schemas.User)
|
@router.put("/", summary="更新用户", response_model=schemas.Response)
|
||||||
def update_user(
|
def update_user(
|
||||||
*,
|
*,
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
user_in: schemas.UserCreate,
|
user_in: schemas.UserCreate,
|
||||||
current_user: User = Depends(get_current_active_superuser),
|
_: User = Depends(get_current_active_superuser),
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""
|
"""
|
||||||
更新用户
|
更新用户
|
||||||
"""
|
"""
|
||||||
user = current_user.get_by_name(db, name=user_in.name)
|
|
||||||
if not user:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=404,
|
|
||||||
detail="用户不存在",
|
|
||||||
)
|
|
||||||
user_info = user_in.dict()
|
user_info = user_in.dict()
|
||||||
if user_info.get("password"):
|
if user_info.get("password"):
|
||||||
user_info["hashed_password"] = get_password_hash(user_info["password"])
|
user_info["hashed_password"] = get_password_hash(user_info["password"])
|
||||||
user_info.pop("password")
|
user_info.pop("password")
|
||||||
user.update(db, **user_info)
|
user = User.get_by_name(db, name=user_info["name"])
|
||||||
return user
|
if not user:
|
||||||
|
return schemas.Response(success=False, message="用户不存在")
|
||||||
|
user.update(db, user_info)
|
||||||
|
return schemas.Response(success=True)
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/", summary="删除用户", response_model=schemas.Response)
|
@router.delete("/{user_name}", summary="删除用户", response_model=schemas.Response)
|
||||||
def delete_user(
|
def delete_user(
|
||||||
*,
|
*,
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
user_in: schemas.UserCreate,
|
user_name: str,
|
||||||
current_user: User = Depends(get_current_active_superuser),
|
current_user: User = Depends(get_current_active_superuser),
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""
|
"""
|
||||||
删除用户
|
删除用户
|
||||||
"""
|
"""
|
||||||
user = current_user.get_by_name(db, name=user_in.name)
|
user = current_user.get_by_name(db, name=user_name)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(
|
return schemas.Response(success=False, message="用户不存在")
|
||||||
status_code=404,
|
user.delete_by_name(db, user_name)
|
||||||
detail="用户不存在",
|
|
||||||
)
|
|
||||||
user.delete_by_name(db, user_in.name)
|
|
||||||
return schemas.Response(success=True)
|
return schemas.Response(success=True)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/current", summary="当前登录用户信息", response_model=schemas.User)
|
||||||
|
def read_current_user(
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
) -> Any:
|
||||||
|
"""
|
||||||
|
当前登录用户信息
|
||||||
|
"""
|
||||||
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{user_id}", summary="用户详情", response_model=schemas.User)
|
@router.get("/{user_id}", summary="用户详情", response_model=schemas.User)
|
||||||
def read_user_by_id(
|
def read_user_by_id(
|
||||||
user_id: int,
|
user_id: int,
|
||||||
|
@ -27,7 +27,7 @@ class Settings(BaseSettings):
|
|||||||
CONFIG_DIR: str = None
|
CONFIG_DIR: str = None
|
||||||
# 超级管理员
|
# 超级管理员
|
||||||
SUPERUSER: str = "admin"
|
SUPERUSER: str = "admin"
|
||||||
# 超级管理员密码
|
# 超级管理员初始密码
|
||||||
SUPERUSER_PASSWORD: str = "password"
|
SUPERUSER_PASSWORD: str = "password"
|
||||||
# API密钥,需要更换
|
# API密钥,需要更换
|
||||||
API_TOKEN: str = "moviepilot"
|
API_TOKEN: str = "moviepilot"
|
||||||
|
@ -14,9 +14,9 @@ class User(Base):
|
|||||||
# 用户名
|
# 用户名
|
||||||
name = Column(String, index=True, nullable=False)
|
name = Column(String, index=True, nullable=False)
|
||||||
# 邮箱,未启用
|
# 邮箱,未启用
|
||||||
email = Column(String, unique=True, index=True)
|
email = Column(String)
|
||||||
# 加密后密码
|
# 加密后密码
|
||||||
hashed_password = Column(String, nullable=False)
|
hashed_password = Column(String)
|
||||||
# 是否启用
|
# 是否启用
|
||||||
is_active = Column(Boolean(), default=True)
|
is_active = Column(Boolean(), default=True)
|
||||||
# 是否管理员
|
# 是否管理员
|
||||||
|
@ -21,7 +21,7 @@ class UserBase(BaseModel):
|
|||||||
class UserCreate(UserBase):
|
class UserCreate(UserBase):
|
||||||
name: str
|
name: str
|
||||||
email: Optional[str] = None
|
email: Optional[str] = None
|
||||||
password: str
|
password: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
# Properties to receive via API on update
|
# Properties to receive via API on update
|
||||||
|
Loading…
x
Reference in New Issue
Block a user