Example #1
0
    def find_by_jwt_token(cls, token: dict = None):
        """Find user from database by user token."""
        if not token:
            return None

        user_model = UserModel.find_by_jwt_token(token)

        if not user_model:
            raise BusinessException(Error.DATA_NOT_FOUND, None)

        is_anonymous_user = token.get('accessType',
                                      None) == AccessType.ANONYMOUS.value
        # If terms accepted , double check if there is a new TOS in place. If so, update the flag to false.
        if user_model.is_terms_of_use_accepted:
            document_type = DocumentType.TERMS_OF_USE_DIRECTOR_SEARCH.value if is_anonymous_user \
                else DocumentType.TERMS_OF_USE.value
            # get the digit version of the terms of service..ie d1 gives 1 ; d2 gives 2..for proper comparison
            latest_version = util.digitify(
                DocumentService.find_latest_version_by_type(document_type))
            current_version = util.digitify(
                user_model.terms_of_use_accepted_version)
            if latest_version > current_version:
                user_model.is_terms_of_use_accepted = False

        return User(user_model)
Example #2
0
    def find_by_jwt_token(cls, **kwargs):
        """Find user from database by user token."""
        user_from_context: UserContext = kwargs['user_context']
        if not user_from_context.token_info:
            return None

        user_model = UserModel.find_by_jwt_token()

        if not user_model:
            if kwargs.get('silent_mode', False):
                return None
            raise BusinessException(Error.DATA_NOT_FOUND, None)

        is_anonymous_user = user_from_context.token_info.get('accessType', None) == AccessType.ANONYMOUS.value
        is_govm_user = user_from_context.login_source == LoginSource.STAFF.value
        # If terms accepted , double check if there is a new TOS in place. If so, update the flag to false.
        if user_model.is_terms_of_use_accepted:
            if is_anonymous_user:
                document_type = DocumentType.TERMS_OF_USE_DIRECTOR_SEARCH.value
            elif is_govm_user:
                document_type = DocumentType.TERMS_OF_USE_GOVM.value
            else:
                document_type = DocumentType.TERMS_OF_USE.value
            # get the digit version of the terms of service..ie d1 gives 1 ; d2 gives 2..for proper comparison
            latest_version = util.digitify(DocumentService.find_latest_version_by_type(document_type))
            current_version = util.digitify(user_model.terms_of_use_accepted_version)
            if latest_version > current_version:
                user_model.is_terms_of_use_accepted = False

        return User(user_model)
Example #3
0
    def save_from_jwt_token(cls, token: dict, request_json: Dict = None):
        """Save user to database (create/update)."""
        current_app.logger.debug('save_from_jwt_token')
        if not token:
            return None
        request_json = {} if not request_json else request_json

        is_anonymous_user = token.get('accessType',
                                      None) == AccessType.ANONYMOUS.value
        if not is_anonymous_user:
            existing_user = UserModel.find_by_jwt_token(token)
        else:
            existing_user = UserModel.find_by_username(
                token.get('preferred_username'))

        first_name, last_name = User._get_names(existing_user, request_json,
                                                token)

        if existing_user is None:
            user_model = UserModel.create_from_jwt_token(
                token, first_name, last_name)
        else:
            user_model = UserModel.update_from_jwt_token(
                existing_user,
                token,
                first_name,
                last_name,
                is_login=request_json.get('isLogin', False))

        if not user_model:
            return None

        # if accepted , double check if there is a new TOS in place .IF so , update the flag to false
        if user_model.is_terms_of_use_accepted:
            document_type = DocumentType.TERMS_OF_USE_DIRECTOR_SEARCH.value if is_anonymous_user \
                else DocumentType.TERMS_OF_USE.value
            # get the digit version of the terms of service..ie d1 gives 1 ; d2 gives 2..for proper comparison
            latest_version = util.digitify(
                DocumentService.find_latest_version_by_type(document_type))
            current_version = util.digitify(
                user_model.terms_of_use_accepted_version)
            if latest_version > current_version:
                user_model.is_terms_of_use_accepted = False

        user = User(user_model)
        return user