# Path: zenodo/modules/records/models.py
# class AccessRight(object):
#     """Class defining access right status."""
# 
#     OPEN = 'open'
# 
#     EMBARGOED = 'embargoed'
# 
#     RESTRICTED = 'restricted'
# 
#     CLOSED = 'closed'
# 
#     _all = (
#         (OPEN, _('Open Access')),
#         (EMBARGOED, _('Embargoed Access')),
#         (RESTRICTED, _('Restricted Access')),
#         (CLOSED, _('Closed Access')),
#     )
# 
#     _icon = {
#         OPEN: 'fa-unlock',
#         EMBARGOED: 'fa-ban',
#         RESTRICTED: 'fa-key',
#         CLOSED: 'fa-lock',
#     }
# 
#     _description = {
#         OPEN: _('Files are publicly accessible.'),
#         EMBARGOED: _('Files are currently under embargo but will be publicly '
#                      'accessible after {date}.'),
#         RESTRICTED: _('You may request access to the files in this upload, '
#                       'provided that you fulfil the conditions below. The '
#                       'decision whether to grant/deny access is solely under '
#                       'the responsibility of the record owner.'),
#         CLOSED: _('Files are not publicly accessible.'),
#     }
# 
#     _category = {
#         OPEN: 'success',
#         EMBARGOED: 'warning',
#         RESTRICTED: 'danger',
#         CLOSED: 'danger',
#     }
# 
#     @staticmethod
#     def is_embargoed(embargo_date):
#         """Test if date is still under embargo."""
#         return arrow.get(embargo_date).date() > datetime.utcnow().date()
# 
#     @classmethod
#     def is_valid(cls, value):
#         """Test if access right is valid."""
#         return bool([key for key, title in cls._all if key == value])
# 
#     @classmethod
#     def get(cls, value, embargo_date=None):
#         """Get access right."""
#         if embargo_date is not None and cls.EMBARGOED == value and \
#            not cls.is_embargoed(embargo_date):
#             return cls.OPEN
# 
#         return value
# 
#     @classmethod
#     def as_icon(cls, value):
#         """Get icon for a specific status."""
#         return cls._icon[value]
# 
#     @classmethod
#     def as_title(cls, value):
#         """Get title for a specific status."""
#         return dict(cls._all)[value]
# 
#     @classmethod
#     def as_description(cls, value, embargo_date=None):
#         """Get description for a specific status."""
#         return cls._description[value].format(
#             date=format_date(embargo_date, 'long'))
# 
#     @classmethod
#     def as_category(cls, value, **kwargs):
#         """Get title for a specific status."""
#         cat = cls._category[value]
#         return kwargs[cat] if cat in kwargs else cat
# 
#     @classmethod
#     def as_options(cls):
#         """Return list of access rights as options."""
#         return cls._all
# 
#     @classmethod
#     def get_expired_embargos(cls):
#         """Get records for which the embargo period have expired."""
#         endpoint = current_app.config['RECORDS_REST_ENDPOINTS']['recid']
# 
#         s = RecordsSearch(
#             using=current_search_client,
#             index=endpoint['search_index']
#         ).query(
#             'query_string',
#             query='access_right:{0} AND embargo_date:{{* TO {1}}}'.format(
#                 cls.EMBARGOED,
#                 # Uses timestamp instead of date on purpose.
#                 datetime.utcnow().isoformat()
#             ),
#             allow_leading_wildcard=False
#         ).source(False)
# 
#         return [hit.meta.id for hit in s.scan()]


from flask import current_app, request, session
from flask_principal import ActionNeed
from flask_security import current_user
from invenio_access import Permission
from invenio_files_rest.models import Bucket, MultipartObject, ObjectVersion
from invenio_pidrelations.contrib.versioning import PIDVersioning
from invenio_pidstore.models import PersistentIdentifier
from invenio_records.api import Record
from invenio_records_files.api import FileObject
from invenio_records_files.models import RecordsBuckets
from invenio_rest.errors import RESTException
from werkzeug.exceptions import HTTPException
from zenodo_accessrequests.models import SecretLink
from zenodo.modules.tokens import decode_rat
from zenodo.modules.utils import obj_or_import_string
from .api import ZenodoRecord
from .models import AccessRight
from .utils import is_deposit, is_record
    """

    @classmethod
    def create(cls, record, action, user=None):
        """Create a deposit permission."""
        if action in cls.read_actions:
            return cls(record, has_update_permission, user)
        elif action in cls.delete_actions:
            return cls(record, has_update_permission, user)
        return super(DepositPermission, cls).create(record, action, user=user)


#
# Utility functions
#
def deny(user, record):
    """Deny access."""
    return False


def allow(user, record):
    """Allow access."""
    return True


def has_read_files_permission(user, record):
    """Check if user has read access to the record."""
    # Allow if record is open access
    if AccessRight.get(
            record.get('access_right', 'closed'),

# Path: zenodo/modules/records/models.py
# class AccessRight(object):
#     """Class defining access right status."""
# 
#     OPEN = 'open'
# 
#     EMBARGOED = 'embargoed'
# 
#     RESTRICTED = 'restricted'
# 
#     CLOSED = 'closed'
# 
#     _all = (
#         (OPEN, _('Open Access')),
#         (EMBARGOED, _('Embargoed Access')),
#         (RESTRICTED, _('Restricted Access')),
#         (CLOSED, _('Closed Access')),
#     )
# 
#     _icon = {
#         OPEN: 'fa-unlock',
#         EMBARGOED: 'fa-ban',
#         RESTRICTED: 'fa-key',
#         CLOSED: 'fa-lock',
#     }
# 
#     _description = {
#         OPEN: _('Files are publicly accessible.'),
#         EMBARGOED: _('Files are currently under embargo but will be publicly '
#                      'accessible after {date}.'),
#         RESTRICTED: _('You may request access to the files in this upload, '
#                       'provided that you fulfil the conditions below. The '
#                       'decision whether to grant/deny access is solely under '
#                       'the responsibility of the record owner.'),
#         CLOSED: _('Files are not publicly accessible.'),
#     }
# 
#     _category = {
#         OPEN: 'success',
#         EMBARGOED: 'warning',
#         RESTRICTED: 'danger',
#         CLOSED: 'danger',
#     }
# 
#     @staticmethod
#     def is_embargoed(embargo_date):
#         """Test if date is still under embargo."""
#         return arrow.get(embargo_date).date() > datetime.utcnow().date()
# 
#     @classmethod
#     def is_valid(cls, value):
#         """Test if access right is valid."""
#         return bool([key for key, title in cls._all if key == value])
# 
#     @classmethod
#     def get(cls, value, embargo_date=None):
#         """Get access right."""
#         if embargo_date is not None and cls.EMBARGOED == value and \
#            not cls.is_embargoed(embargo_date):
#             return cls.OPEN
# 
#         return value
# 
#     @classmethod
#     def as_icon(cls, value):
#         """Get icon for a specific status."""
#         return cls._icon[value]
# 
#     @classmethod
#     def as_title(cls, value):
#         """Get title for a specific status."""
#         return dict(cls._all)[value]
# 
#     @classmethod
#     def as_description(cls, value, embargo_date=None):
#         """Get description for a specific status."""
#         return cls._description[value].format(
#             date=format_date(embargo_date, 'long'))
# 
#     @classmethod
#     def as_category(cls, value, **kwargs):
#         """Get title for a specific status."""
#         cat = cls._category[value]
#         return kwargs[cat] if cat in kwargs else cat
# 
#     @classmethod
#     def as_options(cls):
#         """Return list of access rights as options."""
#         return cls._all
# 
#     @classmethod
#     def get_expired_embargos(cls):
#         """Get records for which the embargo period have expired."""
#         endpoint = current_app.config['RECORDS_REST_ENDPOINTS']['recid']
# 
#         s = RecordsSearch(
#             using=current_search_client,
#             index=endpoint['search_index']
#         ).query(
#             'query_string',
#             query='access_right:{0} AND embargo_date:{{* TO {1}}}'.format(
#                 cls.EMBARGOED,
#                 # Uses timestamp instead of date on purpose.
#                 datetime.utcnow().isoformat()
#             ),
#             allow_leading_wildcard=False
#         ).source(False)
# 
#         return [hit.meta.id for hit in s.scan()]


# Path: zenodo/modules/records/permissions.py
from flask import current_app, request, session
from flask_principal import ActionNeed
from flask_security import current_user
from invenio_access import Permission
from invenio_files_rest.models import Bucket, MultipartObject, ObjectVersion
from invenio_pidrelations.contrib.versioning import PIDVersioning
from invenio_pidstore.models import PersistentIdentifier
from invenio_records.api import Record
from invenio_records_files.api import FileObject
from invenio_records_files.models import RecordsBuckets
from invenio_rest.errors import RESTException
from werkzeug.exceptions import HTTPException
from zenodo_accessrequests.models import SecretLink
from zenodo.modules.tokens import decode_rat
from zenodo.modules.utils import obj_or_import_string
from .api import ZenodoRecord
from .models import AccessRight
from .utils import is_deposit, is_record

    """

    @classmethod
    def create(cls, record, action, user=None):
        """Create a deposit permission."""
        if action in cls.read_actions:
            return cls(record, has_update_permission, user)
        elif action in cls.delete_actions:
            return cls(record, has_update_permission, user)
        return super(DepositPermission, cls).create(record, action, user=user)


#
# Utility functions
#
def deny(user, record):
    """Deny access."""
    return False


def allow(user, record):
    """Allow access."""
    return True


def has_read_files_permission(user, record):
    """Check if user has read access to the record."""
    # Allow if record is open access
    if AccessRight.get(
            record.get('access_right', 'closed'),

            record.get('embargo_date')) == AccessRight.OPEN: