Commit a608d1c6 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

add user entry points

parent a62dc9a5
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -1090,6 +1090,16 @@ class ObjectPermission(models.Model):
                return str(obj_instance)
        return f"{self.object_class}({self.object_pk})"

    def collection(self) -> Union[Collection, None]:
        if self.object_class == "collection" and self.object_pk:
            return Collection.objects.filter(pk=self.object_pk).first()
        return None

    def resource(self) -> Union[Resource, None]:
        if self.object_class == "resource" and self.object_pk:
            return Resource.objects.filter(pk=self.object_pk).first()
        return None

    def __str__(self):
        return f"ObjectPermission({self.pk}) {self.object_class}({self.object_pk}): create({self.object_create}), read({self.object_read}), update({self.object_update}), delete({self.object_delete})."

+30 −0
Original line number Diff line number Diff line
@@ -2564,6 +2564,7 @@ def project_stats(user: User, project_id: int) -> dict:
                deleted_at__isnull=True,
                collectionmembership__isnull=True,
            ).count(),
            "user_entry_points": project_entry_points(user, project_id),
        }
    except Project.DoesNotExist:
        raise ServiceException(NO_SUCH_PROJECT)
@@ -3461,3 +3462,32 @@ def list_projects(user: User) -> List[dict]:
@_require_superuser
def list_users(user: User) -> List[dict]:
    return [serializers.user(u) for u in User.objects.all()]


@_rpc_groups(["Collection"])
def project_entry_points(user: User, project_id: int) -> List[dict]:
    """
    Returns a list of collections as entry points for the current project and user.
    This should list the lower level collections that are accessible to the user for the given project.
    """
    project_instance = Project.objects.filter(pk=project_id).first()
    if not project_instance:
        raise ServiceException(NO_SUCH_PROJECT)
    acl = UserAccess(user, project_instance)
    endpoints = []
    dedup_pks = []
    for perm in acl.permissions:
        if perm.object_class == "collection" and perm.object_read and perm.object_pk:
            col = perm.collection()
            if col:
                higher_col = col
                for ancestor in col.ancestors(False):
                    if acl.can_read(ancestor):
                        higher_col = ancestor
                if higher_col.pk not in dedup_pks:
                    endpoints.append(serializers.collection(higher_col))
                    dedup_pks.append(higher_col.pk)
    if not endpoints:
        if acl.can_read(project_instance.root_collection):
            endpoints.append(serializers.collection(project_instance.root_collection))
    return endpoints