fix(task4): enforce unified password validation rule

This commit is contained in:
Atharv1705
2026-11-17 17:58:20 +05:30
parent c086833163
commit 5f649e3e4c
8 changed files with 264 additions and 29 deletions
+8 -15
View File
@@ -6,7 +6,7 @@ from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session, joinedload
from ..core.db import get_db
from ..core.deps import get_current_user, require_roles
from ..core.security import hash_password
from ..core.security import hash_password, validate_password
from ..models.masters import Department, User
from ..schemas.masters import (
PaginatedOut,
@@ -21,7 +21,7 @@ router = APIRouter(prefix="/users", tags=["users"])
class ResetPasswordRequest(BaseModel):
new_password: str = Field(..., min_length=6)
new_password: str = Field(..., max_length=128)
def _user_list_out(user: User) -> UserListOut:
@@ -93,6 +93,8 @@ def create_user(
if dept is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Department not found.")
validate_password(payload.password, email=payload.email)
user = User(
name=payload.name.strip(),
email=payload.email.strip().lower(),
@@ -175,12 +177,8 @@ def update_user(
data["email"] = data["email"].strip().lower()
password = data.pop("password", None)
if password is not None and password.strip():
if len(password) < 4:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Password must be at least 4 characters.",
)
if password is not None and password != "":
validate_password(password, email=data.get("email", user.email))
user.password_hash = hash_password(password)
for k, v in data.items():
@@ -243,12 +241,7 @@ def reset_user_password(
user = db.query(User).filter(User.id == user_id).first()
if user is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found.")
new_pass = payload.new_password.strip()
if len(new_pass) < 6:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Password must be at least 6 characters.",
)
user.password_hash = hash_password(new_pass)
validate_password(payload.new_password, email=user.email)
user.password_hash = hash_password(payload.new_password)
db.commit()
return {"message": f"Password for {user.name} has been reset successfully."}