Skip to content
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

Filter folders' queryset to domains #147

Merged
merged 7 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .base_models import *
from .validators import validate_file_size, validate_file_name
from .utils import camel_case
from iam.models import FolderMixin
from iam.models import FolderMixin, PublishInRootFolderMixin
from django.core import serializers

import os
Expand Down Expand Up @@ -162,7 +162,7 @@ def delete(self, *args, **kwargs):
super(Library, self).delete(*args, **kwargs)


class Threat(ReferentialObjectMixin):
class Threat(ReferentialObjectMixin, PublishInRootFolderMixin):
library = models.ForeignKey(
Library, on_delete=models.CASCADE, null=True, blank=True, related_name="threats"
)
Expand Down Expand Up @@ -484,7 +484,7 @@ def __str__(self):
return self.name


class Asset(NameDescriptionMixin, FolderMixin):
class Asset(NameDescriptionMixin, FolderMixin, PublishInRootFolderMixin):
class Type(models.TextChoices):
"""
The type of the asset.
Expand Down Expand Up @@ -535,7 +535,7 @@ def ancestors_plus_self(self) -> list[Self]:
return list(result)


class Evidence(NameDescriptionMixin, FolderMixin):
class Evidence(NameDescriptionMixin, FolderMixin, PublishInRootFolderMixin):
# TODO: Manage file upload to S3/MiniO
attachment = models.FileField(
# upload_to=settings.LOCAL_STORAGE_DIRECTORY,
Expand Down Expand Up @@ -570,7 +570,7 @@ def filename(self):
return os.path.basename(self.attachment.name)


class AppliedControl(NameDescriptionMixin, FolderMixin):
class AppliedControl(NameDescriptionMixin, FolderMixin, PublishInRootFolderMixin):
class Status(models.TextChoices):
PLANNED = "planned", _("Planned")
ACTIVE = "active", _("Active")
Expand Down Expand Up @@ -757,7 +757,7 @@ class Status(models.TextChoices):
default=Status.PLANNED,
verbose_name=_("Status"),
blank=True,
null=True
null=True,
)
authors = models.ManyToManyField(
User,
Expand Down Expand Up @@ -1284,7 +1284,6 @@ class Result(models.TextChoices):
verbose_name=_("Result"),
)


class Meta:
verbose_name = _("Compliance assessment")
verbose_name_plural = _("Compliance assessments")
Expand Down Expand Up @@ -1513,7 +1512,7 @@ class Meta:
########################### RiskAcesptance is a domain object relying on secondary objects #########################


class RiskAcceptance(NameDescriptionMixin, FolderMixin):
class RiskAcceptance(NameDescriptionMixin, FolderMixin, PublishInRootFolderMixin):
ACCEPTANCE_STATE = [
("created", _("Created")),
("submitted", _("Submitted")),
Expand Down
8 changes: 4 additions & 4 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,31 +887,31 @@ def perform_create(self, serializer):
role=Role.objects.get(name=RoleCodename.AUDITOR),
builtin=True,
folder=Folder.get_root_folder(),
is_recursive=True
is_recursive=True,
)
ra1.perimeter_folders.add(folder)
ra2 = RoleAssignment.objects.create(
user_group=approvers,
role=Role.objects.get(name=RoleCodename.APPROVER),
builtin=True,
folder=Folder.get_root_folder(),
is_recursive=True
is_recursive=True,
)
ra2.perimeter_folders.add(folder)
ra3 = RoleAssignment.objects.create(
user_group=analysts,
role=Role.objects.get(name=RoleCodename.ANALYST),
builtin=True,
folder=Folder.get_root_folder(),
is_recursive=True
is_recursive=True,
)
ra3.perimeter_folders.add(folder)
ra4 = RoleAssignment.objects.create(
user_group=managers,
role=Role.objects.get(name=RoleCodename.DOMAIN_MANAGER),
builtin=True,
folder=Folder.get_root_folder(),
is_recursive=True
is_recursive=True,
)
ra4.perimeter_folders.add(folder)

Expand Down
25 changes: 23 additions & 2 deletions backend/iam/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ class Meta:
abstract = True


class PublishInRootFolderMixin(models.Model):
"""
Set is_published to True if object is attached to the root folder
"""

class Meta:
abstract = True

def save(self, *args, **kwargs):
if (
getattr(self, "folder") == Folder.get_root_folder()
and hasattr(self, "is_published")
and not self.is_published
):
self.is_published = True
super().save(*args, **kwargs)


class UserGroup(NameDescriptionMixin, FolderMixin):
"""UserGroup objects contain users and can be used as principals in role assignments"""

Expand All @@ -201,7 +219,7 @@ def __str__(self) -> str:

def get_name_display(self) -> str:
return self.name

def get_localization_dict(self) -> dict:
return {
"folder": self.folder.name,
Expand Down Expand Up @@ -509,7 +527,10 @@ def is_access_allowed(
for ra in RoleAssignment.get_role_assignments(user):
f = folder
while f is not None:
if f in ra.perimeter_folders.all() and perm in ra.role.permissions.all():
if (
f in ra.perimeter_folders.all()
and perm in ra.role.permissions.all()
):
return True
f = f.parent_folder
return False
Expand Down
31 changes: 16 additions & 15 deletions documentation/architecture/data-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@ erDiagram
```mermaid
erDiagram

ROOT_FOLDER ||--o{ DOMAIN : contains
DOMAIN ||--o{ PROJECT : contains
DOMAIN ||--o{ APPLIED_CONTROL : contains
ROOT_FOLDER ||--o{ THREAT : contains
DOMAIN ||--o{ RISK_ACCEPTANCE : contains
DOMAIN ||--o{ RISK_ASSESSMENT_REVIEW : contains
DOMAIN ||--o{ COMPLIANCE_ASSESSMENT_REVIEW: contains
DOMAIN ||--o{ EVIDENCE : contains
ROOT_FOLDER ||--o{ FRAMEWORK : contains
ROOT_FOLDER ||--o{ REFERENCE_CONTROL : contains
ROOT_FOLDER ||--o{ LIBRARY : contains
ROOT_FOLDER ||--o{ USER : contains
ROOT_FOLDER ||--o{ USER_GROUP : contains
ROOT_FOLDER ||--o{ ROLE : contains
ROOT_FOLDER ||--o{ ROLE_ASSIGNMENT : contains
ROOT_FOLDER ||--o{ DOMAIN : contains
DOMAIN ||--o{ PROJECT : contains
DOMAIN ||--o{ RISK_ASSESSMENT_REVIEW : contains
DOMAIN ||--o{ COMPLIANCE_ASSESSMENT_REVIEW: contains
ROOT_FOLDER ||--o{ FRAMEWORK : contains
ROOT_FOLDER ||--o{ REFERENCE_CONTROL : contains
ROOT_FOLDER ||--o{ LIBRARY : contains
ROOT_FOLDER ||--o{ USER : contains
ROOT_FOLDER ||--o{ USER_GROUP : contains
ROOT_FOLDER ||--o{ ROLE : contains
ROOT_FOLDER ||--o{ ROLE_ASSIGNMENT : contains
ROOT_FOLDER_OR_DOMAIN ||--o{ EVIDENCE : contains
ROOT_FOLDER_OR_DOMAIN ||--o{ APPLIED_CONTROL : contains
ROOT_FOLDER_OR_DOMAIN ||--o{ RISK_ACCEPTANCE : contains
ROOT_FOLDER_OR_DOMAIN ||--o{ ASSET : contains
ROOT_FOLDER_OR_DOMAIN ||--o{ THREAT : contains

DOMAIN {
string name
Expand Down
Loading