-
-
Notifications
You must be signed in to change notification settings - Fork 304
Feature organization model #6080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: unstable
Are you sure you want to change the base?
Changes from all commits
32edb66
f788782
24138b0
c509082
7bea2e6
05260f0
0a942d2
6a488b5
3639f87
bfe04de
e6e4847
838a8a5
0e7f272
82f0990
86d6528
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,6 +79,10 @@ | |
| from contentcuration.constants import feedback | ||
| from contentcuration.constants import user_history | ||
| from contentcuration.constants.contentnode import kind_activity_map | ||
| from contentcuration.constants.organization_roles import ORGANIZATION_ADMIN | ||
| from contentcuration.constants.organization_roles import ORGANIZATION_EDITOR | ||
| from contentcuration.constants.organization_roles import ORGANIZATION_ROLE_STATUS_ACTIVE | ||
| from contentcuration.constants.organization_roles import ORGANIZATION_VIEWER | ||
| from contentcuration.constants.organization_roles import organization_role_choices | ||
| from contentcuration.constants.organization_roles import ( | ||
| organization_role_status_choices, | ||
|
|
@@ -818,7 +822,7 @@ def file_on_disk_name(instance, filename): | |
|
|
||
|
|
||
| def generate_file_on_disk_name(checksum, filename): | ||
| """ Separated from file_on_disk_name to allow for simple way to check if has already exists """ | ||
| """Separated from file_on_disk_name to allow for simple way to check if has already exists""" | ||
| h = checksum | ||
| basename, ext = os.path.splitext(filename) | ||
| directory = os.path.join(settings.STORAGE_ROOT, h[0], h[1]) | ||
|
|
@@ -845,7 +849,7 @@ def object_storage_name(instance, filename): | |
|
|
||
|
|
||
| def generate_object_storage_name(checksum, filename, default_ext=""): | ||
| """ Separated from file_on_disk_name to allow for simple way to check if has already exists """ | ||
| """Separated from file_on_disk_name to allow for simple way to check if has already exists""" | ||
| h = checksum | ||
| basename, actual_ext = os.path.splitext(filename) | ||
| ext = actual_ext if actual_ext else default_ext | ||
|
|
@@ -1061,7 +1065,7 @@ class ChannelModelManager(models.Manager.from_queryset(ChannelModelQuerySet)): | |
|
|
||
|
|
||
| class Channel(models.Model): | ||
| """ Permissions come from association with organizations """ | ||
| """Permissions come from association with organizations""" | ||
|
|
||
| id = UUIDField(primary_key=True, default=uuid.uuid4) | ||
| name = models.CharField(max_length=200, blank=True) | ||
|
|
@@ -1225,11 +1229,55 @@ def filter_edit_queryset(cls, queryset, user): | |
| user_id=user_id, channel_id=OuterRef("id") | ||
| ) | ||
| ) | ||
| queryset = queryset.annotate(edit=edit) | ||
| organization_edit = Exists( | ||
| OrganizationRole.objects.filter( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: filters |
||
| user_id=user_id, | ||
| organization_id=OuterRef("organization_id"), | ||
| status=ORGANIZATION_ROLE_STATUS_ACTIVE, | ||
| role__in=( | ||
| ORGANIZATION_ADMIN, | ||
| ORGANIZATION_EDITOR, | ||
| ), | ||
| ) | ||
| ) | ||
| queryset = queryset.annotate( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: |
||
| edit=edit, | ||
| organization_edit=organization_edit, | ||
| ) | ||
| if user.is_admin: | ||
| return queryset | ||
|
|
||
| return queryset.filter(edit=True) | ||
| return queryset.filter(Q(edit=True) | Q(organization_edit=True)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ Resolved — addressed in the current code. blocking: this grant is the one I asked for last round, but it is one role too wide on the delete path. Split edit from delete: annotate the role alongside the boolean and check it in |
||
|
|
||
| @classmethod | ||
| def filter_delete_queryset(cls, queryset, user): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ Resolved — addressed in the current code. blocking: no callers; grep finds only this definition. Delete goes through My last-round ask, unwired. Add |
||
| user_id = not user.is_anonymous and user.id | ||
|
|
||
| if not user_id: | ||
| return queryset.none() | ||
|
|
||
| edit = Exists( | ||
| User.editable_channels.through.objects.filter( | ||
| user_id=user_id, channel_id=OuterRef("id") | ||
| ) | ||
| ) | ||
| organization_delete = Exists( | ||
| OrganizationRole.objects.filter( | ||
| user_id=user_id, | ||
| organization_id=OuterRef("organization_id"), | ||
| status=ORGANIZATION_ROLE_STATUS_ACTIVE, | ||
| role=ORGANIZATION_ADMIN, | ||
| ) | ||
| ) | ||
| queryset = queryset.annotate( | ||
| edit=edit, | ||
| organization_delete=organization_delete, | ||
| ) | ||
|
|
||
| if user.is_admin: | ||
| return queryset | ||
|
|
||
| return queryset.filter(Q(edit=True) | Q(organization_delete=True)) | ||
|
|
||
| @classmethod | ||
| def filter_view_queryset(cls, queryset, user): | ||
|
|
@@ -1238,35 +1286,60 @@ def filter_view_queryset(cls, queryset, user): | |
|
|
||
| if user_id: | ||
| filters = dict(user_id=user_id, channel_id=OuterRef("id")) | ||
|
|
||
| edit = Exists( | ||
| User.editable_channels.through.objects.filter(**filters).values( | ||
| "user_id" | ||
| ) | ||
| ) | ||
|
|
||
| view = Exists( | ||
| User.view_only_channels.through.objects.filter(**filters).values( | ||
| "user_id" | ||
| ) | ||
| ) | ||
|
|
||
| organization_view = Exists( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ Resolved — addressed in the current code. blocking: org roles now grant channel view but never channel edit. Either mirror this annotation into The new view grant is also untested — |
||
| OrganizationRole.objects.filter( | ||
| user_id=user_id, | ||
| organization_id=OuterRef("organization_id"), | ||
| status=ORGANIZATION_ROLE_STATUS_ACTIVE, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: this filters |
||
| role__in=( | ||
| ORGANIZATION_ADMIN, | ||
| ORGANIZATION_EDITOR, | ||
| ORGANIZATION_VIEWER, | ||
| ), | ||
| ) | ||
| ) | ||
| else: | ||
| edit = boolean_val(False) | ||
| view = boolean_val(False) | ||
| organization_view = boolean_val(False) | ||
|
|
||
| queryset = queryset.annotate( | ||
| edit=edit, | ||
| view=view, | ||
| organization_view=organization_view, | ||
| ) | ||
|
|
||
| if user_id and user.is_admin: | ||
| return queryset | ||
|
|
||
| permission_filter = Q() | ||
|
|
||
| if user_id: | ||
| pending_channels = Invitation.objects.filter( | ||
| email=user_email, revoked=False, declined=False, accepted=False | ||
| email=user_email, | ||
| revoked=False, | ||
| declined=False, | ||
| accepted=False, | ||
| ).values_list("channel_id", flat=True) | ||
|
|
||
| permission_filter = ( | ||
| Q(view=True) | Q(edit=True) | Q(deleted=False, id__in=pending_channels) | ||
| Q(view=True) | ||
| | Q(edit=True) | ||
| | Q(organization_view=True) | ||
| | Q(deleted=False, id__in=pending_channels) | ||
| ) | ||
|
|
||
| return queryset.filter(permission_filter | Q(deleted=False, public=True)) | ||
|
|
@@ -1881,6 +1954,40 @@ class Organization(models.Model): | |
|
|
||
| objects = CustomManager() | ||
|
|
||
| @classmethod | ||
| def filter_view_queryset(cls, queryset, user): | ||
| queryset = queryset.filter(deleted=False) | ||
|
|
||
| if user.is_anonymous: | ||
| return queryset.filter(public=True) | ||
|
|
||
| if user.is_admin: | ||
| return queryset | ||
|
|
||
| return queryset.filter( | ||
| Q(public=True) | ||
| | Q( | ||
| user_roles__user=user, | ||
| user_roles__status=ORGANIZATION_ROLE_STATUS_ACTIVE, | ||
| ) | ||
| ).distinct() | ||
|
|
||
| @classmethod | ||
| def filter_edit_queryset(cls, queryset, user): | ||
| queryset = queryset.filter(deleted=False) | ||
|
|
||
| if user.is_anonymous: | ||
| return queryset.none() | ||
|
|
||
| if user.is_admin: | ||
| return queryset | ||
|
|
||
| return queryset.filter( | ||
| user_roles__user=user, | ||
| user_roles__role=ORGANIZATION_ADMIN, | ||
| user_roles__status=ORGANIZATION_ROLE_STATUS_ACTIVE, | ||
| ).distinct() | ||
|
|
||
| class Meta: | ||
| verbose_name = "Organization" | ||
| verbose_name_plural = "Organizations" | ||
|
|
@@ -1936,6 +2043,47 @@ class OrganizationRole(models.Model): | |
| ) | ||
| updated_at = models.DateTimeField(auto_now=True, help_text="Last update timestamp") | ||
|
|
||
| @classmethod | ||
| def filter_view_queryset(cls, queryset, user): | ||
| queryset = queryset.filter( | ||
| organization__deleted=False, | ||
| ).select_related( | ||
| "organization", | ||
| "user", | ||
| ) | ||
|
|
||
| if user.is_anonymous: | ||
| return queryset.none() | ||
|
|
||
| if user.is_admin: | ||
| return queryset | ||
|
|
||
| return queryset.filter( | ||
| organization__user_roles__user=user, | ||
| organization__user_roles__status=ORGANIZATION_ROLE_STATUS_ACTIVE, | ||
| ).distinct() | ||
|
|
||
| @classmethod | ||
| def filter_edit_queryset(cls, queryset, user): | ||
| queryset = queryset.filter( | ||
| organization__deleted=False, | ||
| ).select_related( | ||
| "organization", | ||
| "user", | ||
| ) | ||
|
|
||
| if user.is_anonymous: | ||
| return queryset.none() | ||
|
|
||
| if user.is_admin: | ||
| return queryset | ||
|
|
||
| return queryset.filter( | ||
| organization__user_roles__user=user, | ||
| organization__user_roles__role=ORGANIZATION_ADMIN, | ||
| organization__user_roles__status=ORGANIZATION_ROLE_STATUS_ACTIVE, | ||
| ).distinct() | ||
|
|
||
| class Meta: | ||
| unique_together = ("user", "organization") | ||
| verbose_name = "Organization Role" | ||
|
|
@@ -3705,7 +3853,7 @@ def save(self, *args, **kwargs): | |
|
|
||
|
|
||
| class Invitation(models.Model): | ||
| """ Invitation to edit channel """ | ||
| """Invitation to edit channel""" | ||
|
|
||
| id = UUIDField(primary_key=True, default=uuid.uuid4) | ||
| accepted = models.BooleanField(default=False) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blocking: no coverage for this grant —
tests/viewsets/test_organization.pynever referencesChannel, andtests/viewsets/test_channel.pynever references organizations. #5967 requires automated tests verifying permission enforcement, and lists channel access for all three roles.Minimum cases: admin and editor can update an org-owned channel they have no m2m share on; viewer cannot; a non-member cannot; an
ORGANIZATION_ROLE_STATUS_PENDINGrole grants nothing.