butler_ddochi/custom_auth/views.py
icurfer ea11832a53
Some checks failed
Build And Test / build-and-push (push) Failing after 53s
init
2024-12-13 17:12:03 +09:00

94 lines
3.4 KiB
Python

from django.shortcuts import render, redirect
from .forms import CustomUserCreationForm, CustomUserChangeForm
from django.contrib.auth import login, authenticate
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth import update_session_auth_hash
from django.shortcuts import render, redirect
from .forms import SSHKeyForm
@login_required
def edit_profile_view(request):
if request.method == "POST":
form = CustomUserChangeForm(request.POST, instance=request.user)
if form.is_valid():
user = form.save()
# 세션 인증 유지
update_session_auth_hash(request, user)
messages.success(request, "프로필이 성공적으로 업데이트되었습니다.")
return redirect("/") # 리다이렉트할 URL
else:
form = CustomUserChangeForm(instance=request.user)
return render(request, "custom_auth/edit_profile.html", {"form": form})
def signup_view(request):
if request.method == "POST":
form = CustomUserCreationForm(request.POST)
if form.is_valid():
user = form.save()
# 로그인 처리 (선택적)
# login(request, user)
return redirect("/")
# return redirect('custom_auth:login')
else:
form = CustomUserCreationForm()
return render(request, "custom_auth/signup.html", {"form": form})
def login_view(request):
if request.method == "POST":
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect("/") # 원하는 URL로 리다이렉트 (여기서는 'home'으로 설정)
else:
messages.error(request, "Invalid username or password.")
return redirect("/") # 로그인 실패 시 루트 URL로 리다이렉트
return render(request, "custom_auth/login.html")
@login_required
# def upload_ssh_key(request):
# """SSH Private Key 업로드 뷰"""
# user = request.user
# if request.method == 'POST':
# form = SSHKeyForm(request.POST, instance=user)
# if form.is_valid():
# form.save()
# return redirect('custom_auth:upload_ssh_key') # 성공 시 동일 페이지 리다이렉트
# else:
# form = SSHKeyForm(instance=user)
# return render(request, 'custom_auth/upload_ssh_key.html', {'form': form})
# @login_required
def upload_ssh_key(request):
"""SSH Private Key 업로드 및 복호화 표시 뷰"""
user = request.user
decrypted_key = "" # 기본 값
# POST 요청 처리
if request.method == 'POST':
form = SSHKeyForm(request.POST, instance=user)
if form.is_valid():
form.save()
return redirect('custom_auth:upload_ssh_key') # 성공 시 리다이렉트
else:
form = SSHKeyForm(instance=user)
# 복호화된 SSH Key 가져오기
if user.encrypted_private_key:
try:
decrypted_key = user.decrypt_private_key()
except Exception as e:
decrypted_key = f"복호화 오류: {str(e)}"
# 템플릿으로 데이터 전달
return render(request, 'custom_auth/upload_ssh_key.html', {
'form': form,
'decrypted_key': decrypted_key
})