Fixed update user #70

This commit is contained in:
Sven Heidemann 2022-10-15 17:25:49 +02:00
parent 8e56ff6a8e
commit 9da95f4dfb
3 changed files with 30 additions and 22 deletions

View File

@ -16,12 +16,19 @@ class JSONProcessor:
continue continue
name = String.convert_to_camel_case(parameter.name) name = String.convert_to_camel_case(parameter.name)
name = name.replace('Dto', 'DTO')
name_first_lower = String.first_to_lower(name) name_first_lower = String.first_to_lower(name)
if name in values or name_first_lower in values: if name in values or name_first_lower in values:
value = ''
if name in values: if name in values:
args.append(values[name]) value = values[name]
else: else:
args.append(values[name_first_lower]) value = values[name_first_lower]
if isinstance(value, dict):
value = JSONProcessor.process(parameter.annotation, value)
args.append(value)
elif parameter.default != Parameter.empty: elif parameter.default != Parameter.empty:
args.append(parameter.default) args.append(parameter.default)

View File

@ -10,14 +10,14 @@ class UpdateAuthUserDTO(DtoABC):
def __init__( def __init__(
self, self,
auth_user: AuthUserDTO, auth_user_dto: AuthUserDTO,
new_auth_user: AuthUserDTO, new_auth_user_dto: AuthUserDTO,
change_password=False change_password=False
): ):
DtoABC.__init__(self) DtoABC.__init__(self)
self._auth_user = auth_user self._auth_user = auth_user_dto
self._new_auth_user = new_auth_user self._new_auth_user = new_auth_user_dto
self._change_password = change_password self._change_password = change_password
@property @property

View File

@ -191,12 +191,12 @@ class AuthService(AuthServiceABC):
# update first name # update first name
if update_user_dto.new_auth_user.first_name is not None and update_user_dto.auth_user.first_name != update_user_dto.new_auth_user.first_name: if update_user_dto.new_auth_user.first_name is not None and update_user_dto.auth_user.first_name != update_user_dto.new_auth_user.first_name:
user.FirstName = update_user_dto.new_auth_user.first_name user.first_name = update_user_dto.new_auth_user.first_name
# update last name # update last name
if update_user_dto.new_auth_user.last_name is not None and update_user_dto.new_auth_user.last_name != '' and \ if update_user_dto.new_auth_user.last_name is not None and update_user_dto.new_auth_user.last_name != '' and \
update_user_dto.auth_user.last_name != update_user_dto.new_auth_user.last_name: update_user_dto.auth_user.last_name != update_user_dto.new_auth_user.last_name:
user.LastName = update_user_dto.new_auth_user.last_name user.last_name = update_user_dto.new_auth_user.last_name
# update E-Mail # update E-Mail
if update_user_dto.new_auth_user.email is not None and update_user_dto.new_auth_user.email != '' and update_user_dto.auth_user.email != update_user_dto.new_auth_user.email: if update_user_dto.new_auth_user.email is not None and update_user_dto.new_auth_user.email != '' and update_user_dto.auth_user.email != update_user_dto.new_auth_user.email:
@ -208,21 +208,22 @@ class AuthService(AuthServiceABC):
is_existing_password_set = False is_existing_password_set = False
is_new_password_set = False is_new_password_set = False
# hash passwords in DTOs # hash passwords in DTOs
if update_user_dto.auth_user.Password is not None and update_user_dto.auth_user.Password != '': if update_user_dto.auth_user.password is not None and update_user_dto.auth_user.password != '':
is_existing_password_set = True is_existing_password_set = True
update_user_dto.auth_user.Password = self._hash_sha256(update_user_dto.auth_user.Password) update_user_dto.auth_user.password = self._hash_sha256(update_user_dto.auth_user.password)
if update_user_dto.auth_user.Password != user.Password: if update_user_dto.auth_user.password != user.password:
raise ServiceException(ServiceErrorCode.InvalidUser, 'Wrong password') raise ServiceException(ServiceErrorCode.InvalidUser, 'Wrong password')
if update_user_dto.new_auth_user.Password is not None and update_user_dto.new_auth_user.Password != '': if update_user_dto.new_auth_user.password is not None and update_user_dto.new_auth_user.password != '':
is_new_password_set = True is_new_password_set = True
update_user_dto.new_auth_user.Password = self._hash_sha256(update_user_dto.new_auth_user.Password) update_user_dto.new_auth_user.password = self._hash_sha256(update_user_dto.new_auth_user.password)
# update password # update password
if is_existing_password_set and is_new_password_set and update_user_dto.auth_user.Password != update_user_dto.new_auth_user.Password: if is_existing_password_set and is_new_password_set and update_user_dto.auth_user.password != update_user_dto.new_auth_user.password:
user.Password = update_user_dto.new_auth_user.Password user.password = update_user_dto.new_auth_user.password
self._auth_users.update_auth_user(user)
self._db.save_changes() self._db.save_changes()
async def update_user_as_admin_async(self, update_user_dto: UpdateAuthUserDTO): async def update_user_as_admin_async(self, update_user_dto: UpdateAuthUserDTO):
@ -242,31 +243,31 @@ class AuthService(AuthServiceABC):
if user is None: if user is None:
raise ServiceException(ServiceErrorCode.InvalidUser, 'User not found') raise ServiceException(ServiceErrorCode.InvalidUser, 'User not found')
if user.ConfirmationId is not None and update_user_dto.new_auth_user.is_confirmed: if user.confirmation_id is not None and update_user_dto.new_auth_user.is_confirmed:
user.ConfirmationId = None user.confirmation_id = None
elif user.ConfirmationId is None and not update_user_dto.new_auth_user.is_confirmed: elif user.confirmation_id is None and not update_user_dto.new_auth_user.is_confirmed:
user.confirmation_id = uuid.uuid4() user.confirmation_id = uuid.uuid4()
# else # else
# raise ServiceException(ServiceErrorCode.InvalidUser, 'E-Mail not confirmed') # raise ServiceException(ServiceErrorCode.InvalidUser, 'E-Mail not confirmed')
# update first name # update first name
if update_user_dto.new_auth_user.first_name is not None and update_user_dto.auth_user.first_name != update_user_dto.new_auth_user.first_name: if update_user_dto.new_auth_user.first_name is not None and update_user_dto.auth_user.first_name != update_user_dto.new_auth_user.first_name:
user.FirstName = update_user_dto.new_auth_user.first_name user.first_name = update_user_dto.new_auth_user.first_name
# update last name # update last name
if update_user_dto.new_auth_user.last_name is not None and update_user_dto.new_auth_user.last_name != '' and update_user_dto.auth_user.last_name != update_user_dto.new_auth_user.last_name: if update_user_dto.new_auth_user.last_name is not None and update_user_dto.new_auth_user.last_name != '' and update_user_dto.auth_user.last_name != update_user_dto.new_auth_user.last_name:
user.LastName = update_user_dto.new_auth_user.last_name user.last_name = update_user_dto.new_auth_user.last_name
# update E-Mail # update E-Mail
if update_user_dto.new_auth_user.email is not None and update_user_dto.new_auth_user.email != '' and update_user_dto.auth_user.email != update_user_dto.new_auth_user.email: if update_user_dto.new_auth_user.email is not None and update_user_dto.new_auth_user.email != '' and update_user_dto.auth_user.email != update_user_dto.new_auth_user.email:
user_by_new_e_mail = self._auth_users.find_auth_user_by_email(update_user_dto.new_auth_user.email) user_by_new_e_mail = self._auth_users.find_auth_user_by_email(update_user_dto.new_auth_user.email)
if user_by_new_e_mail is not None: if user_by_new_e_mail is not None:
raise ServiceException(ServiceErrorCode.InvalidUser, 'User already exists') raise ServiceException(ServiceErrorCode.InvalidUser, 'User already exists')
user.EMail = update_user_dto.new_auth_user.email user.email = update_user_dto.new_auth_user.email
# update password # update password
if update_user_dto.change_password and update_user_dto.auth_user.password != update_user_dto.new_auth_user.password: if update_user_dto.change_password and update_user_dto.auth_user.password != update_user_dto.new_auth_user.password:
user.Password = self._hash_sha256(update_user_dto.new_auth_user.password) user.password = self._hash_sha256(update_user_dto.new_auth_user.password)
# update role # update role
if user.auth_role == update_user_dto.auth_user.auth_role and user.auth_role != update_user_dto.new_auth_user.auth_role: if user.auth_role == update_user_dto.auth_user.auth_role and user.auth_role != update_user_dto.new_auth_user.auth_role: