Merge pull request #63 from valueonag/int
2nd perimeter to ensure mandate id for user in CRUD
This commit is contained in:
commit
9399748b5f
1 changed files with 33 additions and 1 deletions
|
|
@ -290,6 +290,18 @@ class AppObjects:
|
||||||
"""Returns the initial ID for a table."""
|
"""Returns the initial ID for a table."""
|
||||||
return self.db.getInitialId(model_class)
|
return self.db.getInitialId(model_class)
|
||||||
|
|
||||||
|
def _getDefaultMandateId(self) -> str:
|
||||||
|
"""Get the default mandate ID, creating it if necessary."""
|
||||||
|
defaultMandateId = self.getInitialId(Mandate)
|
||||||
|
if not defaultMandateId:
|
||||||
|
# If no default mandate exists, create one
|
||||||
|
logger.warning("No default mandate found, creating Root mandate")
|
||||||
|
self._initRootMandate()
|
||||||
|
defaultMandateId = self.getInitialId(Mandate)
|
||||||
|
if not defaultMandateId:
|
||||||
|
raise ValueError("Failed to get or create default mandate")
|
||||||
|
return defaultMandateId
|
||||||
|
|
||||||
def _getPasswordHash(self, password: str) -> str:
|
def _getPasswordHash(self, password: str) -> str:
|
||||||
"""Creates a hash for a password."""
|
"""Creates a hash for a password."""
|
||||||
return pwdContext.hash(password)
|
return pwdContext.hash(password)
|
||||||
|
|
@ -451,13 +463,19 @@ class AppObjects:
|
||||||
if not password.strip():
|
if not password.strip():
|
||||||
raise ValueError("Password cannot be empty")
|
raise ValueError("Password cannot be empty")
|
||||||
|
|
||||||
|
# Ensure mandateId is set - use self.mandateId or default mandate
|
||||||
|
mandateId = self.mandateId
|
||||||
|
if not mandateId:
|
||||||
|
mandateId = self._getDefaultMandateId()
|
||||||
|
logger.warning(f"Using default mandate ID {mandateId} for new user {username}")
|
||||||
|
|
||||||
# Create user data using UserInDB model
|
# Create user data using UserInDB model
|
||||||
userData = UserInDB(
|
userData = UserInDB(
|
||||||
username=username,
|
username=username,
|
||||||
email=email,
|
email=email,
|
||||||
fullName=fullName,
|
fullName=fullName,
|
||||||
language=language,
|
language=language,
|
||||||
mandateId=self.mandateId,
|
mandateId=mandateId,
|
||||||
enabled=enabled,
|
enabled=enabled,
|
||||||
privilege=privilege,
|
privilege=privilege,
|
||||||
authenticationAuthority=authenticationAuthority,
|
authenticationAuthority=authenticationAuthority,
|
||||||
|
|
@ -515,11 +533,25 @@ class AppObjects:
|
||||||
# Remove id field from updateDict if present - we'll use userId from parameter
|
# Remove id field from updateDict if present - we'll use userId from parameter
|
||||||
updateDict.pop("id", None)
|
updateDict.pop("id", None)
|
||||||
|
|
||||||
|
# Ensure mandateId is set - if missing or None, use default mandate
|
||||||
|
if "mandateId" not in updateDict or not updateDict.get("mandateId"):
|
||||||
|
if not user.mandateId:
|
||||||
|
# User has no mandateId, set to default
|
||||||
|
defaultMandateId = self._getDefaultMandateId()
|
||||||
|
updateDict["mandateId"] = defaultMandateId
|
||||||
|
logger.warning(f"Setting default mandate ID {defaultMandateId} for user {userId}")
|
||||||
|
else:
|
||||||
|
# Keep existing mandateId if update doesn't provide one
|
||||||
|
updateDict["mandateId"] = user.mandateId
|
||||||
|
|
||||||
# Update user data using model
|
# Update user data using model
|
||||||
updatedData = user.model_dump()
|
updatedData = user.model_dump()
|
||||||
updatedData.update(updateDict)
|
updatedData.update(updateDict)
|
||||||
# Ensure ID matches userId parameter
|
# Ensure ID matches userId parameter
|
||||||
updatedData["id"] = userId
|
updatedData["id"] = userId
|
||||||
|
# Ensure mandateId is set in final data
|
||||||
|
if not updatedData.get("mandateId"):
|
||||||
|
updatedData["mandateId"] = self._getDefaultMandateId()
|
||||||
updatedUser = User(**updatedData)
|
updatedUser = User(**updatedData)
|
||||||
|
|
||||||
# Update user record
|
# Update user record
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue