권한 관리 시스템 개선

 새로운 기능:
- 사용자별 세분화된 권한 체크 (can_manage_books, can_manage_notes, can_manage_novels)
- 페이지별 권한 가드 시스템 추가 (permission-guard.js)
- 헤더 메뉴 권한별 표시/숨김 기능

🔧 백엔드 개선:
- 모든 문서 관련 API에서 can_manage_books 권한 체크 추가
- documents.py: 개별 문서 조회, PDF 조회 권한 로직 수정
- highlights.py: 하이라이트 생성/조회 권한 체크 개선
- bookmarks.py: 북마크 생성/조회 권한 체크 개선
- document_links.py: 문서 링크 관련 권한 체크 개선

🎨 프론트엔드 개선:
- header-loader.js: updateMenuPermissions 함수 추가로 권한별 메뉴 제어
- permission-guard.js: 페이지 접근 권한 체크 및 리다이렉트 처리
- 권한 없는 페이지 접근 시 메인 페이지로 안전한 리다이렉트
- 헤더 사용자 정보 상태 보존 로직 추가

🛡️ 보안 강화:
- 403 Forbidden 에러 해결
- 권한 없는 사용자의 무단 페이지 접근 차단
- 문서 관리 권한이 있는 사용자는 모든 문서 공유 가능

📱 사용자 경험 개선:
- 권한에 따른 메뉴 자동 표시/숨김
- 로그인 상태 유지 개선
- 권한 없는 기능 접근 시 친화적인 알림 및 리다이렉트
This commit is contained in:
hyungi
2025-09-05 12:36:05 +09:00
parent 6a537008db
commit aebce091a9
12 changed files with 426 additions and 38 deletions

View File

@@ -75,8 +75,8 @@ async def create_bookmark(
detail="Document not found"
)
# 문서 접근 권한 확인
if not document.is_public and document.uploaded_by != current_user.id and not current_user.is_admin:
# 문서 접근 권한 확인 (관리자 또는 문서 관리 권한이 있으면 모든 문서 접근 가능)
if not (current_user.is_admin or current_user.can_manage_books) and not document.is_public and document.uploaded_by != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions to access this document"
@@ -155,8 +155,8 @@ async def get_document_bookmarks(
detail="Document not found"
)
# 문서 접근 권한 확인
if not document.is_public and document.uploaded_by != current_user.id and not current_user.is_admin:
# 문서 접근 권한 확인 (관리자 또는 문서 관리 권한이 있으면 모든 문서 접근 가능)
if not (current_user.is_admin or current_user.can_manage_books) and not document.is_public and document.uploaded_by != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions to access this document"

View File

@@ -109,8 +109,8 @@ async def create_document_link(
detail="Source document not found"
)
# 권한 확인
if not source_doc.is_public and source_doc.uploaded_by != current_user.id and not current_user.is_admin:
# 권한 확인 (관리자 또는 문서 관리 권한이 있으면 모든 문서 접근 가능)
if not (current_user.is_admin or current_user.can_manage_books) and not source_doc.is_public and source_doc.uploaded_by != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied to source document"
@@ -146,7 +146,7 @@ async def create_document_link(
# 대상 문서/노트 권한 확인
if target_doc:
if not target_doc.is_public and target_doc.uploaded_by != current_user.id and not current_user.is_admin:
if not (current_user.is_admin or current_user.can_manage_books) and not target_doc.is_public and target_doc.uploaded_by != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied to target document"
@@ -235,8 +235,8 @@ async def get_document_links(
detail="Document not found"
)
# 권한 확인
if not document.is_public and document.uploaded_by != current_user.id and not current_user.is_admin:
# 권한 확인 (관리자 또는 문서 관리 권한이 있으면 모든 문서 접근 가능)
if not (current_user.is_admin or current_user.can_manage_books) and not document.is_public and document.uploaded_by != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied"
@@ -345,8 +345,8 @@ async def get_linkable_documents(
and_(
Document.html_path.isnot(None), # HTML 문서만
Document.id != document_id, # 자기 자신 제외
# 권한 확인: 공개 문서이거나 본인이 업로드한 문서
(Document.is_public == True) | (Document.uploaded_by == current_user.id) | (current_user.is_admin == True)
# 권한 확인: 공개 문서이거나 본인이 업로드한 문서이거나 문서 관리 권한이 있음
(Document.is_public == True) | (Document.uploaded_by == current_user.id) | (current_user.is_admin == True) | (current_user.can_manage_books == True)
)
).order_by(
# 같은 서적 우선, 그 다음 정렬 순서
@@ -522,8 +522,8 @@ async def get_document_backlinks(
print(f"✅ 문서 찾음: {document.title}")
# 권한 확인
if not document.is_public and document.uploaded_by != current_user.id and not current_user.is_admin:
# 권한 확인 (관리자 또는 문서 관리 권한이 있으면 모든 문서 접근 가능)
if not (current_user.is_admin or current_user.can_manage_books) and not document.is_public and document.uploaded_by != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied"
@@ -541,8 +541,8 @@ async def get_document_backlinks(
).outerjoin(Book, Document.book_id == Book.id).where(
and_(
DocumentLink.target_document_id == document_id,
# 권한 확인: 공개 문서이거나 본인이 업로드한 문서
(Document.is_public == True) | (Document.uploaded_by == current_user.id) | (current_user.is_admin == True)
# 권한 확인: 공개 문서이거나 본인이 업로드한 문서이거나 문서 관리 권한이 있음
(Document.is_public == True) | (Document.uploaded_by == current_user.id) | (current_user.is_admin == True) | (current_user.can_manage_books == True)
)
).order_by(DocumentLink.created_at.desc())
@@ -650,8 +650,8 @@ async def get_document_link_fragments(
detail="Document not found"
)
# 권한 확인
if not document.is_public and document.uploaded_by != current_user.id and not current_user.is_admin:
# 권한 확인 (관리자 또는 문서 관리 권한이 있으면 모든 문서 접근 가능)
if not (current_user.is_admin or current_user.can_manage_books) and not document.is_public and document.uploaded_by != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied"
@@ -663,8 +663,8 @@ async def get_document_link_fragments(
).where(
and_(
DocumentLink.source_document_id == document_id,
# 권한 확인: 공개 문서이거나 본인이 업로드한 문서
(Document.is_public == True) | (Document.uploaded_by == current_user.id) | (current_user.is_admin == True)
# 권한 확인: 공개 문서이거나 본인이 업로드한 문서이거나 문서 관리 권한이 있음
(Document.is_public == True) | (Document.uploaded_by == current_user.id) | (current_user.is_admin == True) | (current_user.can_manage_books == True)
)
).order_by(DocumentLink.start_offset.asc())

View File

@@ -98,8 +98,8 @@ async def list_documents(
selectinload(Document.category) # 소분류 정보 추가
)
# 권한 필터링 (관리자 아니면 공개 문서 + 자신이 업로드한 문서만)
if not current_user.is_admin:
# 권한 필터링 (관리자 또는 문서 관리 권한이 있으면 모든 문서, 아니면 공개 문서 + 자신이 업로드한 문서만)
if not (current_user.is_admin or current_user.can_manage_books):
query = query.where(
or_(
Document.is_public == True,
@@ -175,8 +175,8 @@ async def list_all_documents(
selectinload(Document.category) # 소분류 정보 추가
)
# 권한 필터링 (관리자 아니면 공개 문서 + 자신이 업로드한 문서만)
if not current_user.is_admin:
# 권한 필터링 (관리자 또는 문서 관리 권한이 있으면 모든 문서, 아니면 공개 문서 + 자신이 업로드한 문서만)
if not (current_user.is_admin or current_user.can_manage_books):
query = query.where(
or_(
Document.is_public == True,
@@ -514,8 +514,8 @@ async def get_document(
detail="Document not found"
)
# 권한 확인
if not document.is_public and document.uploaded_by != current_user.id and not current_user.is_admin:
# 권한 확인 (관리자 또는 문서 관리 권한이 있으면 모든 문서 접근 가능)
if not (current_user.is_admin or current_user.can_manage_books) and not document.is_public and document.uploaded_by != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions"
@@ -616,9 +616,9 @@ async def get_document_pdf(
print(f"🔐 문서 권한: is_public={document.is_public}, uploaded_by={document.uploaded_by}")
print(f"👤 사용자 권한: is_admin={current_user.is_admin}, user_id={current_user.id}")
# 권한 확인
if not current_user.is_admin and not document.is_public and document.uploaded_by != current_user.id:
print(f"❌ 접근 권한 없음 - 관리자: {current_user.is_admin}, 공개: {document.is_public}, 소유자: {document.uploaded_by == current_user.id}")
# 권한 확인 (관리자 또는 문서 관리 권한이 있으면 모든 문서 접근 가능)
if not (current_user.is_admin or current_user.can_manage_books) and not document.is_public and document.uploaded_by != current_user.id:
print(f"❌ 접근 권한 없음 - 관리자: {current_user.is_admin}, 문서권한: {current_user.can_manage_books}, 공개: {document.is_public}, 소유자: {document.uploaded_by == current_user.id}")
raise HTTPException(status_code=403, detail="Access denied")
# PDF 파일 확인

View File

@@ -84,8 +84,8 @@ async def create_highlight(
detail="Document not found"
)
# 문서 접근 권한 확인
if not document.is_public and document.uploaded_by != current_user.id and not current_user.is_admin:
# 문서 접근 권한 확인 (관리자 또는 문서 관리 권한이 있으면 모든 문서 접근 가능)
if not (current_user.is_admin or current_user.can_manage_books) and not document.is_public and document.uploaded_by != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions to access this document"