Skip to content

Commit

Permalink
Merge pull request #958 from opengisch/QF-3822-limit-project-edit-to-…
Browse files Browse the repository at this point in the history
…admin

Limit QGIS project modifications to managers and admins
  • Loading branch information
suricactus authored Jun 20, 2024
2 parents 89d4622 + 0ea0f74 commit 61288ce
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions docker-app/qfieldcloud/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ class ProjectAdmin(QFieldCloudModelAdmin):
"status",
"status_code",
"project_filename",
"has_restricted_projectfiles",
"file_storage_bytes",
"storage_keep_versions",
"packaging_offliner",
Expand Down
9 changes: 9 additions & 0 deletions docker-app/qfieldcloud/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ class MultipleProjectsError(QFieldCloudException):
status_code = status.HTTP_400_BAD_REQUEST


class RestrictedProjectModificationError(QFieldCloudException):
"""Raised when a user with insufficient role is trying to modify QGIS/QField projectfiles
of a project that has the 'has_restricted_projectfiles' flag set"""

code = "restricted_project_modification"
message = "Restricted project modification"
status_code = status.HTTP_400_BAD_REQUEST


class DeltafileValidationError(QFieldCloudException):
"""Raised when a deltafile validation fails"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2.25 on 2024-05-25 10:20

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("core", "0075_auto_20240323_1419"),
]

operations = [
migrations.AddField(
model_name="project",
name="has_restricted_projectfiles",
field=models.BooleanField(
default=False,
help_text="Restrict modifications of QGIS/QField projectfiles to managers and administrators.",
),
),
]
8 changes: 8 additions & 0 deletions docker-app/qfieldcloud/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,14 @@ class Meta:
"If enabled, QFieldCloud will automatically overwrite conflicts in this project. Disabling this will force the project manager to manually resolve all the conflicts."
),
)

has_restricted_projectfiles = models.BooleanField(
default=False,
help_text=_(
"Restrict modifications of QGIS/QField projectfiles to managers and administrators."
),
)

thumbnail_uri = models.CharField(
_("Thumbnail Picture URI"), max_length=255, blank=True
)
Expand Down
23 changes: 23 additions & 0 deletions docker-app/qfieldcloud/core/permissions_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,29 @@ def can_create_files(user: QfcUser, project: Project) -> bool:
)


def can_modify_qgis_projectfile(user: QfcUser, project: Project) -> bool:
if project.has_restricted_projectfiles:
return user_has_project_roles(
user,
project,
[
ProjectCollaborator.Roles.ADMIN,
ProjectCollaborator.Roles.MANAGER,
],
)
else:
return user_has_project_roles(
user,
project,
[
ProjectCollaborator.Roles.ADMIN,
ProjectCollaborator.Roles.MANAGER,
ProjectCollaborator.Roles.EDITOR,
ProjectCollaborator.Roles.REPORTER,
],
)


def can_read_projects(user: QfcUser, _account: QfcUser) -> bool:
return user.is_authenticated

Expand Down
8 changes: 8 additions & 0 deletions docker-app/qfieldcloud/core/views/files_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ def post(self, request, projectid, filename, format=None):
project = Project.objects.get(id=projectid)
is_qgis_project_file = utils.is_qgis_project_file(filename)

# check if the project restricts qgs/qgz file modification to admins
if is_qgis_project_file and not permissions_utils.can_modify_qgis_projectfile(
request.user, project
):
raise exceptions.RestrictedProjectModificationError(
"The project restricts modification of the QGIS project file to managers and administrators."
)

# check only one qgs/qgz file per project
if (
is_qgis_project_file
Expand Down

0 comments on commit 61288ce

Please sign in to comment.