Skip to content

Commit

Permalink
Merge pull request #656 from nearbeach/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
robotichead authored Sep 23, 2024
2 parents 7dcaac6 + c92aff8 commit e61af6c
Show file tree
Hide file tree
Showing 45 changed files with 242 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def inner(request, *args, **kwargs):

# User the FUNCTION_DICT to determine which partial permissions we need
# to reference
passes, user_level = FUNCTION_DICT[destination](request, kwargs)
passes, user_level, _ = FUNCTION_DICT[destination](request, kwargs)

if not passes:
raise PermissionDenied
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}


def check_user_generic_permissions(min_permission_level):
def check_user_generic_permissions(min_permission_level, extra_permissions=""):
"""
Checks the user's generic permissions. It will gather both the
- destination
Expand Down Expand Up @@ -68,26 +68,26 @@ def inner(request, *args, **kwargs):
if destination == "kanban_card":
# Setup kwargs to have kanban_card_id
kwargs["kanban_card_id"] = kwargs["location_id"]
passes, user_level = kanban_card_permissions(request, kwargs)
passes, user_level, extra_level = kanban_card_permissions(request, kwargs, extra_permissions)
elif destination == "requirement_item":
# Setup kwargs to have requirement item id
kwargs["requirement_item_id"] = kwargs["location_id"]
passes, user_level = requirement_item_permissions(request, kwargs)
passes, user_level, extra_level = requirement_item_permissions(request, kwargs, extra_permissions)
elif destination == "change_task":
# Setup kwargs to have change task id
kwargs["change_task_id"] = kwargs["location_id"]
passes, user_level = change_task_permissions(request, kwargs)
passes, user_level, extra_level = change_task_permissions(request, kwargs, extra_permissions)
elif destination == "organisation":
passes, user_level = organisation_permissions(request, kwargs)
passes, user_level, extra_level = organisation_permissions(request, kwargs, extra_permissions)
elif destination == "customer":
passes, user_level = customer_permissions(request, kwargs)
passes, user_level, extra_level = customer_permissions(request, kwargs)
else:
passes, user_level = generic_permissions(request, destination, kwargs)
passes, user_level, extra_level = generic_permissions(request, destination, kwargs, extra_permissions)

if not passes:
raise PermissionDenied

if user_level >= min_permission_level:
if user_level >= min_permission_level or extra_level:
# Everything is fine - continue on
return func(request, *args, **kwargs, user_level=user_level)

Expand Down Expand Up @@ -119,7 +119,7 @@ def inner(request, *args, **kwargs):

# Use the FUNCTION_DICT to determine which partial permissions we need to
# reference
passes, user_level = FUNCTION_DICT[object_lookup](request, kwargs)
passes, user_level, _ = FUNCTION_DICT[object_lookup](request, kwargs, "")

if not passes:
raise PermissionDenied
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db.models import Max, Q


def change_task_permissions(request, kwargs):
def change_task_permissions(request, kwargs, extra_permissions=""):
# Default user level is 0
user_group_results = UserGroup.objects.filter(
is_deleted=False,
Expand Down Expand Up @@ -33,12 +33,24 @@ def change_task_permissions(request, kwargs):
# Check to make sure the user groups intersect
if len(group_results) == 0:
# There are no matching groups - i.e. the user does not have any permission
return False, 0
return False, 0, False

# Get the max permission value from user_group_results
user_level = user_group_results.aggregate(
Max("permission_set__request_for_change")
)["permission_set__request_for_change__max"]

# Check all variations of the extra permissions
extra_level = False
if extra_permissions == "document":
extra_level = user_group_results.filter(
permission_set__document=1,
).count() > 0

# if extra_permissions == "history":
# extra_level = user_group_results.filter(
# permission_set__rfc_history=1,
# ).count() > 0

# Return
return True, user_level
return True, user_level, extra_level
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def customer_permissions(request, kwargs):
Max("permission_set__customer")
)["permission_set__customer__max"]

return True, user_level
return True, user_level, False
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from NearBeach.models import Group, ObjectAssignment, UserGroup


def generic_permissions(request, object_lookup, kwargs):
def generic_permissions(request, object_lookup, kwargs, extra_permissions):
# Default user level is 0
user_group_results = UserGroup.objects.filter(
is_deleted=False,
Expand All @@ -30,11 +30,24 @@ def generic_permissions(request, object_lookup, kwargs):
# Check to make sure the user groups intersect
if len(group_results) == 0:
# There are no matching groups - i.e. the user does not have any permission
return False, 0
return False, 0, False

# Get the max permission value from user_group_results
user_level = user_group_results.aggregate(
Max(f"permission_set__{object_lookup.replace('_id', '')}")
)[f"permission_set__{object_lookup.replace('_id', '')}__max"]

return True, user_level
extra_level = False
if extra_permissions == "document":
extra_level = user_group_results.filter(
permission_set__document=1,
).count() > 0

# TODO: Implement a more generic version, so we can include other objects like requirements, organisations, customers etc.
if object_lookup in ["project", "task"]:
if extra_permissions == "history":
extra_level = user_group_results.filter(
**{F"permission_set__{object_lookup}_history": 1}
).count() > 0

return True, user_level, extra_level
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


# Internal Function
def kanban_board_permissions(request, kwargs):
def kanban_board_permissions(request, kwargs, extra_permissions=""):
# Default user level is 0
user_group_results = UserGroup.objects.filter(
is_deleted=False,
Expand All @@ -27,12 +27,12 @@ def kanban_board_permissions(request, kwargs):
# Check to make sure the user groups intersect
if len(group_results) == 0:
# There are no matching groups - i.e. the user does not have any permission
return False, 0
return False, 0, False

# Get the max permission value from user_group_results
user_level = user_group_results.aggregate(
Max("permission_set__kanban_board")
)["permission_set__kanban_board__max"]

return True, user_level
return True, user_level, False

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


# Internal Function
def kanban_card_permissions(request, kwargs):
def kanban_card_permissions(request, kwargs, extra_permissions):
# Default user level is 0
user_group_results = UserGroup.objects.filter(
is_deleted=False,
Expand Down Expand Up @@ -36,5 +36,17 @@ def kanban_card_permissions(request, kwargs):
Max("permission_set__kanban_board")
)["permission_set__kanban_board__max"]

return True, user_level
# Check all variations of the extra permissions
extra_level = False
if extra_permissions == "document":
extra_level = user_group_results.filter(
permission_set__document=1,
).count() > 0

if extra_permissions == "history":
extra_level = user_group_results.filter(
permission_set__kanban_comment=1,
).count() > 0

return True, user_level, extra_level

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


# Internal Function
def kanban_column_permissions(request, kwargs):
def kanban_column_permissions(request, kwargs, extra_permissions=""):
# Default user level is 0
user_group_results = UserGroup.objects.filter(
is_deleted=False,
Expand All @@ -29,12 +29,12 @@ def kanban_column_permissions(request, kwargs):
# Check to make sure the user groups intersect
if len(group_results) == 0:
# There are no matching groups - i.e. the user does not have any permission
return False, 0
return False, 0, False

# Get the max permission value from user_group_results
user_level = user_group_results.aggregate(
Max("permission_set__kanban_board")
)["permission_set__kanban_board__max"]

return True, user_level
return True, user_level, False

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


# Internal Function
def kanban_level_permissions(request, kwargs):
def kanban_level_permissions(request, kwargs, extra_permissions=""):
# Default user level is 0
user_group_results = UserGroup.objects.filter(
is_deleted=False,
Expand All @@ -29,12 +29,12 @@ def kanban_level_permissions(request, kwargs):
# Check to make sure the user groups intersect
if len(group_results) == 0:
# There are no matching groups - i.e. the user does not have any permission
return False, 0
return False, 0, False

# Get the max permission value from user_group_results
user_level = user_group_results.aggregate(
Max("permission_set__kanban_board")
)["permission_set__kanban_board__max"]

return True, user_level
return True, user_level, False

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db.models import Max, Q


def object_note_permissions(request, kwargs):
def object_note_permissions(request, kwargs, extra_permissions=""):
"""
Checks the user's permission to determine if they have permission to delete this note.
Currently only;
Expand All @@ -17,6 +17,6 @@ def object_note_permissions(request, kwargs):
change_user=request.user,
).count()

return True, object_results
return True, object_results, False

return False, 0
return False, 0, False
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from NearBeach.models import UserGroup


def organisation_permissions(request, kwargs):
def organisation_permissions(request, kwargs, extra_permissions=""):
# Default user level is 0
user_group_results = UserGroup.objects.filter(
is_deleted=False,
Expand All @@ -14,4 +14,16 @@ def organisation_permissions(request, kwargs):
Max("permission_set__organisation")
)["permission_set__organisation__max"]

return True, user_level
# Check all variations of the extra permissions
extra_level = False
if extra_permissions == "document":
extra_level = user_group_results.filter(
permission_set__document=1,
).count() > 0

# if extra_permissions == "history":
# extra_level = user_group_results.filter(
# permission_set__organisation_history=1,
# ).count() > 0

return True, user_level, extra_level
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db.models import Max, Q


def project_permissions(request, kwargs):
def project_permissions(request, kwargs, extra_permissions=""):
# Default user level is 0
user_group_results = UserGroup.objects.filter(
is_deleted=False,
Expand All @@ -28,11 +28,11 @@ def project_permissions(request, kwargs):
# Check to see if there are any groups associated
if len(user_group_results) == 0:
# No groups - meaning no permissions
return False, 0
return False, 0, False

# Get the max permission value from user_group_results
user_level = user_group_results.aggregate(
Max("permission_set__project")
)["permission_set__project__max"]

return True, user_level
return True, user_level, False
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


# Internal Function
def request_for_change_permissions(request, kwargs):
def request_for_change_permissions(request, kwargs, extra_permissions=""):
# Default user level is 0
user_group_results = UserGroup.objects.filter(
is_deleted=False,
Expand All @@ -28,11 +28,11 @@ def request_for_change_permissions(request, kwargs):

# Check to make sure the user groups intersect
if len(group_results) == 0:
return False, 0
return False, 0, False

# Get the max permission value from user_group_results
user_level = user_group_results.aggregate(
Max("permission_set__request_for_change")
)["permission_set__request_for_change__max"]

return True, user_level
return True, user_level, False
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db.models import Max, Q


def requirement_item_permissions(request, kwargs):
def requirement_item_permissions(request, kwargs, extra_permissions=""):
# Default user level is 0
user_group_results = UserGroup.objects.filter(
is_deleted=False,
Expand Down Expand Up @@ -38,11 +38,23 @@ def requirement_item_permissions(request, kwargs):
# Check to see if there are any groups associated
if len(user_group_results) == 0:
# No groups - meaning no permissions
return False, 0
return False, 0, False

# Get the max permission value from user_group_results
user_level = user_group_results.aggregate(
Max("permission_set__requirement")
)["permission_set__requirement__max"]

return True, user_level
# Check all variation of the extra permissions
extra_level = False
if extra_permissions == "document":
extra_level = user_group_results.filter(
permission_set__document=1,
).count() > 0

# if extra_permissions == "history":
# extra_level = user_group_results.filter(
# permission_set__requirement_item_history=1
# ).count() > 0

return True, user_level, extra_level
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db.models import Max, Q


def requirement_permissions(request, kwargs):
def requirement_permissions(request, kwargs, extra_permissions=""):
# Default user level is 0
user_group_results = UserGroup.objects.filter(
is_deleted=False,
Expand All @@ -23,11 +23,11 @@ def requirement_permissions(request, kwargs):
# Check to see if there are any groups associated
if len(user_group_results) == 0:
# No groups - meaning no permissions
return False, 0
return False, 0, False

# Get the max permission value from user_group_results
user_level = user_group_results.aggregate(
Max("permission_set__requirement")
)["permission_set__requirement__max"]

return True, user_level
return True, user_level, False
Loading

0 comments on commit e61af6c

Please sign in to comment.