ilyass yani commited on
Commit ·
1e4744e
1
Parent(s): 565c7dd
feat: maj auth, main.py et schemas user
Browse files- app/api/auth.py +1 -1
- app/main.py +36 -0
- app/schemas/user.py +8 -2
app/api/auth.py
CHANGED
|
@@ -59,7 +59,7 @@ async def register(user_create: UserCreate, db: Session = Depends(get_db)) -> To
|
|
| 59 |
email=user_create.email,
|
| 60 |
hashed_password=hashed_password,
|
| 61 |
full_name=user_create.full_name,
|
| 62 |
-
role=DBUserRole(user_create.role.value),
|
| 63 |
)
|
| 64 |
db.add(db_user)
|
| 65 |
db.commit()
|
|
|
|
| 59 |
email=user_create.email,
|
| 60 |
hashed_password=hashed_password,
|
| 61 |
full_name=user_create.full_name,
|
| 62 |
+
role=DBUserRole(user_create.role.value), # recruiter or candidate only
|
| 63 |
)
|
| 64 |
db.add(db_user)
|
| 65 |
db.commit()
|
app/main.py
CHANGED
|
@@ -106,6 +106,42 @@ def on_startup():
|
|
| 106 |
except Exception as e:
|
| 107 |
logging.warning("Could not preload pipeline config: %s", e)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
# Conditionally include API routers. If a router import fails (e.g. heavy
|
| 110 |
# ML dependencies missing), the app still starts and exposes /health.
|
| 111 |
include_optional_router("app.api.auth")
|
|
|
|
| 106 |
except Exception as e:
|
| 107 |
logging.warning("Could not preload pipeline config: %s", e)
|
| 108 |
|
| 109 |
+
# Seed the admin account from environment variables.
|
| 110 |
+
# If ADMIN_EMAIL and ADMIN_PASSWORD are set and no account with that email
|
| 111 |
+
# exists yet, create it automatically with the admin role.
|
| 112 |
+
admin_email = os.getenv("ADMIN_EMAIL", "").strip()
|
| 113 |
+
admin_password = os.getenv("ADMIN_PASSWORD", "").strip()
|
| 114 |
+
admin_name = os.getenv("ADMIN_FULL_NAME", "Admin").strip()
|
| 115 |
+
if admin_email and admin_password:
|
| 116 |
+
try:
|
| 117 |
+
from app.core.database import SessionLocal
|
| 118 |
+
from app.core.security import get_password_hash
|
| 119 |
+
from app.models.models import User as UserModel, UserRole as DBUserRole
|
| 120 |
+
_db = SessionLocal()
|
| 121 |
+
try:
|
| 122 |
+
existing = _db.query(UserModel).filter(UserModel.email == admin_email).first()
|
| 123 |
+
if existing:
|
| 124 |
+
if existing.role != DBUserRole.admin:
|
| 125 |
+
existing.role = DBUserRole.admin
|
| 126 |
+
_db.commit()
|
| 127 |
+
logging.info("Admin role enforced for: %s", admin_email)
|
| 128 |
+
else:
|
| 129 |
+
admin_user = UserModel(
|
| 130 |
+
email=admin_email,
|
| 131 |
+
hashed_password=get_password_hash(admin_password),
|
| 132 |
+
full_name=admin_name,
|
| 133 |
+
role=DBUserRole.admin,
|
| 134 |
+
)
|
| 135 |
+
_db.add(admin_user)
|
| 136 |
+
_db.commit()
|
| 137 |
+
logging.info("Admin account created: %s", admin_email)
|
| 138 |
+
finally:
|
| 139 |
+
_db.close()
|
| 140 |
+
except Exception as e:
|
| 141 |
+
logging.warning("Could not seed admin account: %s", e)
|
| 142 |
+
else:
|
| 143 |
+
logging.warning("ADMIN_EMAIL or ADMIN_PASSWORD not set — no admin account seeded.")
|
| 144 |
+
|
| 145 |
# Conditionally include API routers. If a router import fails (e.g. heavy
|
| 146 |
# ML dependencies missing), the app still starts and exposes /health.
|
| 147 |
include_optional_router("app.api.auth")
|
app/schemas/user.py
CHANGED
|
@@ -9,12 +9,18 @@ class UserRole(str, Enum):
|
|
| 9 |
candidate = "candidate"
|
| 10 |
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
class UserCreate(BaseModel):
|
| 13 |
-
"""Schema for user registration"""
|
| 14 |
email: EmailStr
|
| 15 |
password: str = Field(..., min_length=6)
|
| 16 |
full_name: str = Field(..., min_length=2)
|
| 17 |
-
role:
|
| 18 |
|
| 19 |
|
| 20 |
class UserLogin(BaseModel):
|
|
|
|
| 9 |
candidate = "candidate"
|
| 10 |
|
| 11 |
|
| 12 |
+
class UserRolePublic(str, Enum):
|
| 13 |
+
"""Roles available for public self-registration — admin is excluded."""
|
| 14 |
+
recruiter = "recruiter"
|
| 15 |
+
candidate = "candidate"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
class UserCreate(BaseModel):
|
| 19 |
+
"""Schema for user registration — admin role is not allowed."""
|
| 20 |
email: EmailStr
|
| 21 |
password: str = Field(..., min_length=6)
|
| 22 |
full_name: str = Field(..., min_length=2)
|
| 23 |
+
role: UserRolePublic = UserRolePublic.recruiter
|
| 24 |
|
| 25 |
|
| 26 |
class UserLogin(BaseModel):
|