Beispiel #1
0
def activate_deactivate_user(
        curr_user: dict, email: str, is_admin_action: bool, action) -> list:
    print(
        'INFO: activation deactivation request received at {} for'
        ' \nuser: {},\nemail: {},\nadmin_action: {},\npermission: {}]'.format(
            str(datetime.datetime.now()), curr_user, email, is_admin_action, action
        ))
    email_length_invalid = UserUtils.verify_email_length(email, email)
    if email_length_invalid:
        return email_length_invalid
    elif curr_user.get('email') != email and not is_admin_action:
        return [{'error': 'Please provide your own valid email id address.'}, 404]
    UserDatabaseService.activate_deactivate_user(curr_user, email, is_admin_action, action)
    if action == AdminPermissionEnums.DEACTIVATE.name:
        deleted_user = dataStateAccess.UserDTO.user_dto(UserDatabaseService.get_active_inactive_single_user_by_email(email))
        if deleted_user and not deleted_user.get('is_active'):
            # # Active tokens for current user should be revoked as soon as the user marks himself as inactive.
            user_session_history = SessionHistoryDatabaseService()
            session_bucket = user_session_history.get_active_sessions_for_user(deleted_user)
            if session_bucket:
                session_service = SessionService()
                session_service.revoke_session_token(session_bucket[0].get('access_token_jti'))
            return [{'response': 'User successfully deleted.'}, 200]
        return [{'error': 'No user with email {} found.'.format(email)}, 400]
    elif action == AdminPermissionEnums.ACTIVATE.name:
        activated_user = dataStateAccess.UserDTO.user_dto(UserDatabaseService.get_active_inactive_single_user_by_email(email))
        if activated_user and activated_user.get('is_active'):
            return [{'response': 'User successfully restored.'}, 200]
        return [{'error': 'No user with email {} found .'.format(email)}, 400]
    print("Some error encountered, {}.".format(str(datetime.datetime.now())))
    return [{'error': 'There was some error, please contact developer.'}, 500]
Beispiel #2
0
def update_user_email(user, old_em, new_em):
    is_length_invalid = UserUtils.verify_email_length(old_em, new_em)
    if is_length_invalid:
        return is_length_invalid
    u = get_existing_user_by_id(user['id'])
    if 'error' in u.keys():
        return [u, 500]
    if u['email'] != old_em:
        return [{'error': '{} does not match your current email address. Please check and try again.'.format(old_em)}, 404]
    elif old_em == new_em:
        return [{'error': 'Email is already up to date for the user.'}, 200]
    email_exists_already = verify_if_email_already_exists(new_em)
    if email_exists_already:
        return [{'error': 'Cannot update email as the user with email id - {} already exists.'.format(new_em)}, 409]
    updated_user = UserDatabaseService.update_email(user, new_em)
    return updated_user