global: add function

* this function was moved from invenio-alma
This commit is contained in:
Christoph Ladurner
2022-10-18 22:10:52 +02:00
parent 8669f5dcda
commit 14e9e0557a
2 changed files with 39 additions and 1 deletions

View File

@@ -10,7 +10,13 @@
from .ext import InvenioConfigTugraz
from .generators import RecordIp
from .utils import get_identity_from_user_by_email
__version__ = "0.10.0"
__all__ = ("__version__", "InvenioConfigTugraz", "RecordIp")
__all__ = (
"__version__",
"InvenioConfigTugraz",
"RecordIp",
"get_identity_from_user_by_email",
)

View File

@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022 Graz University of Technology.
#
# invenio-config-tugraz is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
# details.
"""Utils file."""
from flask_principal import Identity
from invenio_access import any_user
from invenio_access.utils import get_identity
from invenio_accounts import current_accounts
def get_identity_from_user_by_email(email: str = None) -> Identity:
"""Get the user specified via email or ID."""
if email is None:
raise ValueError("the email has to be set to get a identity")
user = current_accounts.datastore.get_user(email)
if user is None:
raise LookupError(f"user with {email} not found")
identity = get_identity(user)
# TODO: this is a temporary solution. this should be done with data from the db
identity.provides.add(any_user)
return identity