-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ronald Krist
committed
Oct 24, 2023
1 parent
1ce6780
commit c8d153b
Showing
28 changed files
with
450 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
black oarepo_requests tests --target-version py310 | ||
autoflake --in-place --remove-all-unused-imports --recursive oarepo_requests tests | ||
isort oarepo_requests tests --profile black | ||
black oarepo_communities tests --target-version py310 | ||
autoflake --in-place --remove-all-unused-imports --recursive oarepo_communities tests | ||
isort oarepo_communities tests --profile black |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from cachetools import TTLCache, cached | ||
from invenio_access.permissions import system_identity | ||
from invenio_communities import current_communities | ||
|
||
|
||
@cached(cache=TTLCache(maxsize=1028, ttl=600)) | ||
def permissions_cache(community_id): | ||
return current_communities.service.read(system_identity, community_id).data[ | ||
"custom_fields" | ||
]["permissions"] | ||
|
||
|
||
def usermap(community_id): | ||
return current_communities.service.read(system_identity, community_id).data[ | ||
"custom_fields" | ||
]["usermap"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
from flask import current_app | ||
|
||
|
||
def get_field(record_class): | ||
if str(record_class).find("invenio_communities.communities.records.api.Community") > 0: | ||
return "custom_fields", current_app.config["COMMUNITIES_CUSTOM_FIELDS"] | ||
return None | ||
if ( | ||
str(record_class).find("invenio_communities.communities.records.api.Community") | ||
> 0 | ||
and "COMMUNITIES_CUSTOM_FIELDS" in current_app.config | ||
): | ||
return current_app.config["COMMUNITIES_CUSTOM_FIELDS"] | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from .cache import permissions_cache | ||
|
||
|
||
class OARepoCommunities(object): | ||
"""OARepo extension of Invenio-Vocabularies.""" | ||
|
||
def __init__(self, app=None): | ||
"""Extension initialization.""" | ||
if app: | ||
self.init_app(app) | ||
|
||
def init_app(self, app): | ||
"""Flask application initialization.""" | ||
self.init_config(app) | ||
app.extensions["oarepo-communities"] = self | ||
|
||
def init_config(self, app): | ||
"""Initialize configuration.""" | ||
from . import ext_config | ||
|
||
if "OAREPO_PERMISSIONS_PRESETS" not in app.config: | ||
app.config["OAREPO_PERMISSIONS_PRESETS"] = {} | ||
|
||
for k in ext_config.OAREPO_PERMISSIONS_PRESETS: | ||
if k not in app.config["OAREPO_PERMISSIONS_PRESETS"]: | ||
app.config["OAREPO_PERMISSIONS_PRESETS"][ | ||
k | ||
] = ext_config.OAREPO_PERMISSIONS_PRESETS[k] | ||
|
||
self.permissions_cache = permissions_cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from oarepo_communities.permissions.presets import CommunityPermissionPolicy | ||
|
||
OAREPO_PERMISSIONS_PRESETS = { | ||
"community": CommunityPermissionPolicy, | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from invenio_records_permissions import RecordPermissionPolicy | ||
from invenio_records_permissions.generators import ( | ||
AnyUser, | ||
AuthenticatedUser, | ||
SystemProcess, | ||
) | ||
|
||
from .record import RecordCommunitiesGenerator | ||
|
||
|
||
class CommunityPermissionPolicy(RecordPermissionPolicy): | ||
can_search = [SystemProcess(), AnyUser()] | ||
can_read = [SystemProcess(), RecordCommunitiesGenerator("can_read")] | ||
can_create = [SystemProcess(), AuthenticatedUser()] | ||
can_update = [SystemProcess(), RecordCommunitiesGenerator("can_update")] | ||
can_delete = [SystemProcess(), RecordCommunitiesGenerator("can_delete")] | ||
can_manage = [SystemProcess()] | ||
|
||
can_create_files = [SystemProcess()] | ||
can_set_content_files = [SystemProcess()] | ||
can_get_content_files = [AnyUser(), SystemProcess()] | ||
can_commit_files = [SystemProcess()] | ||
can_read_files = [AnyUser(), SystemProcess()] | ||
can_update_files = [SystemProcess()] | ||
can_delete_files = [SystemProcess()] | ||
|
||
can_edit = [SystemProcess()] | ||
can_new_version = [SystemProcess()] | ||
can_search_drafts = [SystemProcess()] | ||
can_read_draft = [SystemProcess()] | ||
can_update_draft = [SystemProcess()] | ||
can_delete_draft = [SystemProcess()] | ||
can_publish = [SystemProcess(), RecordCommunitiesGenerator("can_publish")] | ||
can_draft_create_files = [SystemProcess()] | ||
can_draft_set_content_files = [SystemProcess()] | ||
can_draft_get_content_files = [SystemProcess()] | ||
can_draft_commit_files = [SystemProcess()] | ||
can_draft_read_files = [SystemProcess()] | ||
can_draft_update_files = [SystemProcess()] | ||
|
||
can_add_community = [SystemProcess(), AuthenticatedUser()] | ||
can_remove_community = [SystemProcess(), AuthenticatedUser()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from collections import defaultdict | ||
|
||
from cachetools import TTLCache, cached | ||
from invenio_communities.generators import CommunityRoleNeed | ||
from invenio_records_permissions.generators import Generator | ||
|
||
from ..proxies import current_communities_permissions | ||
|
||
|
||
class RecordCommunitiesGenerator(Generator): | ||
"""Allows system_process role.""" | ||
|
||
def __init__(self, action): | ||
self.action = action | ||
|
||
def needs(self, **kwargs): | ||
_needs = set() | ||
if "record" in kwargs and hasattr(kwargs["record"], "parent"): | ||
record = kwargs["record"] | ||
try: | ||
community_ids = record.parent["communities"]["ids"] | ||
except KeyError: | ||
return [] | ||
by_actions = record_community_permissions(frozenset(community_ids)) | ||
if self.action in by_actions: | ||
community2role_list = by_actions[self.action] | ||
for community_id, roles in community2role_list.items(): | ||
for role in roles: | ||
_needs.add(CommunityRoleNeed(community_id, role)) | ||
return _needs | ||
return [] | ||
|
||
""" | ||
def communities(self, identity): | ||
roles = self.roles() | ||
community_ids = set() | ||
for n in identity.provides: | ||
if n.method == "community" and n.role in roles: | ||
community_ids.add(n.value) | ||
return list(community_ids) | ||
def query_filter(self, identity=None, **kwargs): | ||
return dsl.Q("terms", **{"parent.communities.ids": self.communities(identity)}) | ||
""" | ||
|
||
|
||
@cached(cache=TTLCache(maxsize=1028, ttl=600)) | ||
def record_community_permissions(record_communities): | ||
communities_permissions = {} | ||
|
||
for record_community_id in record_communities: | ||
communities_permissions[record_community_id] = current_communities_permissions( | ||
record_community_id | ||
) | ||
|
||
by_actions = defaultdict(lambda: defaultdict(list)) | ||
for community_id, role_permissions_dct in communities_permissions.items(): | ||
for role, role_permissions in role_permissions_dct.items(): | ||
for action, val in role_permissions.items(): | ||
if val: | ||
by_actions[action][community_id].append(role) | ||
return by_actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from flask import current_app | ||
from werkzeug.local import LocalProxy | ||
|
||
current_communities_permissions = LocalProxy( | ||
lambda: current_app.extensions["oarepo-communities"].permissions_cache | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
from invenio_drafts_resources.resources import RecordResourceConfig | ||
|
||
from invenio_records_resources.services.base.config import ConfiguratorMixin, FromConfig | ||
from invenio_records_resources.services.base.config import ConfiguratorMixin | ||
|
||
|
||
class CommunityRecordsResourceConfig(RecordResourceConfig, ConfiguratorMixin): | ||
"""Community's records resource config.""" | ||
|
||
#blueprint_name = "community-records" | ||
#url_prefix = "/communities" | ||
routes = {"list": "/<pid_value>/records"} | ||
# blueprint_name = "community-records" | ||
# url_prefix = "/communities" | ||
routes = {"list": "/<pid_value>/records"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.